Exemple #1
0
void Database::renew_value(const std::string &table, const std::string &key, const std::string &val,
                           const std::string &cond) {

    std::string query(utils::sql::prepare_update_start(table));
    query+= utils::sql::prepare_update_vals(key, val);
    query+= cond;

    QSqlQuery q = db->exec(QString::fromStdString(query));
    if (q.lastError().isValid()) throw InvalidQuery(QString::fromStdString(query), q.lastError());
}
Exemple #2
0
size_t Database::select_sum(const std::string &table, const std::string &key, const std::string &cond)
{
    std::string query = utils::sql::prepare_single_sum(table, key);
    query += cond;

    QSqlQuery q = db->exec(QString::fromStdString(query));
    if (q.lastError().isValid()) throw InvalidQuery(QString::fromStdString(query), q.lastError());
    q.next();
    return q.value(0).toUInt();
}
Exemple #3
0
std::string Database::select_one(const std::string &table, const std::string &row, int row_id,
                                 const std::string &condition) {
    std::string query = utils::sql::prepare_select(table, row);
    if (!condition.empty()) query += condition;

    QSqlQuery q = db->exec(QString::fromStdString(query));
    if (q.lastError().isValid()) throw InvalidQuery(QString::fromStdString(query), q.lastError());
    q.next();
    return q.value(row_id).toString().toStdString();
}
Exemple #4
0
size_t Database::insert_and_get(const std::string &table, const std::string &key_name, const std::string &key)
{
    std::string query = utils::sql::prepare_insert_start<std::string>(table, key_name);
    query+=utils::sql::prepare_insert_end(utils::str::make_dquote(key));

    QSqlQuery q = db->exec(QString::fromStdString(query));
    if (q.lastError().isValid()) throw InvalidQuery(QString::fromStdString(query), q.lastError());

    return select_id(table, key_name, key);
}
Exemple #5
0
size_t Database::select_id(const std::string &table, const std::string &key_name, const std::string &key)
{
    QString query = utils::sql::prepare_select<QString>(QString::fromStdString(table), cfg::db::row::id);
    query += utils::sql::prepare_where<std::string>(key_name, utils::sql::chars::equal, utils::str::make_dquote(key)).c_str();

    QSqlQuery q = db->exec(query);
    if (q.lastError().isValid()) throw InvalidQuery(query, q.lastError());
    if (!q.next()) throw NotExist(table, key_name);

    return q.value(0).toUInt();
}
Exemple #6
0
void Graph::init_from_file( const std::string& input_graph )
{
	clear_graph();

	std::fstream in(input_graph.c_str(), std::fstream::in );
	if( !in.good() ){
		throw InvalidQuery("Check input file path!");
	}

	in >> *this;
	in.close();
}
Exemple #7
0
/*!
 * \brief Database::AddCustomer Add a customer to the database
 * \param name Customer's name
 * \param address Customer's address
 * \param interest 0 = not interested, 1 = somewhat interested, 2 = very interested
 * \param key true if is key customer
 * \return true if successful
 */
bool Database::AddCustomer(QString name, QString address, QString interest, QString key)
{
  if(key == "true") { key = "1"; }
  else if(key == "false") { key = "0"; }
  if(query.exec("insert into customers values(NULL, \"" + name +
                "\", \"" + address + "\", " + interest + ", " + key +");"))
    return true;
  else
  {
    qDebug() << query.lastError().text();
    throw InvalidQuery();
  }
}
Exemple #8
0
/*!
 * \brief Database::Contains
 * Check if a certain value exists in a certain field in a table
 * \param tableName
 * \param fieldName
 * \param value
 * \return true if value exists in field of table
 */
bool Database::Contains(QString tableName, QString fieldName, QString value)
{
  if(query.exec("select * from \"" + tableName +
                "\" where \"" + fieldName + "\" = \"" + value + "\";"))
  {
    return query.next();
  }
  else
  {
    qDebug() << query.lastError().text();
    throw InvalidQuery();
  }
}
Exemple #9
0
std::vector<std::string> Database::select_row(const std::string &query, int reserve_size)
{
    QSqlQuery q = db->exec(QString::fromStdString(query));
    if (q.lastError().isValid()) throw InvalidQuery(QString::fromStdString(query), q.lastError());
    if (!q.next()) throw NotExist(query);
    std::vector<std::string> res;
    //sqlquery size unknown?! Reseve?
    if (reserve_size != -1) res.reserve(reserve_size);
    int ind = 0;
    while(q.value(ind).isValid())
    {
        res.emplace_back(q.value(ind).toString().toStdString());
        ++ind;
    }
    return res;
}
Exemple #10
0
Database::ReplyFormat Database::get_table_names(const std::string &table, const std::string &name)
{

    QString query(QString::fromStdString(utils::sql::prepare_select(table, name)));
    QSqlQuery q = db->exec(query);

    if (q.lastError().isValid()) throw InvalidQuery(query, q.lastError());
    if (!q.next()) throw NotExist(table, name);

    ReplyFormat result;
    do
    {
        result.push_back(q.value(0).toString().toStdString());
    }while(q.next());

    return result;
}
Exemple #11
0
std::deque<std::string> Database::select_column(const std::string &table, const std::string &row,
                                                const std::string &cond)
{
    std::string query = utils::sql::prepare_select(table, row);
    query+=cond;

    QSqlQuery q = db->exec(QString::fromStdString(query));
    if (q.lastError().isValid()) throw InvalidQuery(QString::fromStdString(query), q.lastError());
    if (!q.next()) throw NotExist(query);

    std::deque<std::string> res;
    do
    {
        res.emplace_back(q.value(0).toString().toStdString());
    }
    while(q.next());

    return res;
}
Exemple #12
0
/*!
 * \brief Database::IsKey Check if a customer is a key customer or not
 * \param name
 * \return true if customer is a key customer
 */
bool Database::IsKey(QString name)
{
  //execute query
  if(this->query.exec("select * from customers where name = \""
                + name + "\";"))
    //if there is data in the query
    if(query.next())
    {
      //get info from "key" field in this record
      return (query.record().field("key").value().toBool());
    }
    else
    {
      qDebug() << query.lastError().text();
      throw EmptyQuery();
    }
  else
  {
    qDebug() << query.lastError().text();
    throw InvalidQuery();
  }
}
Exemple #13
0
void Database::execute(const std::string &query)
{
    QSqlQuery q = db->exec(QString::fromStdString(query));
    if (q.lastError().isValid()) throw InvalidQuery(QString::fromStdString(query), q.lastError());
}