BuildingWithVolumeRecordMap BuildingAccessorPostgresql::getRecords( ITransactionShrPtr a_transaction, IDHolder const & a_id_holder ) const { TransactionPostgresqlShrPtr transaction = shared_dynamic_cast<TransactionPostgresql>(a_transaction); pqxx::transaction<> & backbone_transaction = transaction->getBackboneTransaction(); string query = "SELECT * FROM " + getTableName(a_id_holder) + " WHERE holder_name = " + backbone_transaction.quote(a_id_holder.getValue2()); pqxx::result result = backbone_transaction.exec(query); BuildingWithVolumeRecordMap records; string key; Volume volume; for (pqxx::result::const_iterator it = result.begin(); it != result.end(); ++it) { it["building_key"].to(key); it["volume"].to(volume); BuildingWithVolumeRecordShrPtr record = make_shared<BuildingWithVolumeRecord>(a_id_holder, key, volume); BuildingWithVolumeRecordPair pair(key, record); records.insert(pair); } return records; }
//取得 Magic* MagicManager::getById(int id) { std::string sql = "SELECT * FROM " + getTableName() + " WHERE id = ?"; const char *pzTest; sqlite3_prepare_v2(_db, sql.c_str(), strlen(sql.c_str()), &_stmt, &pzTest); sqlite3_bind_int(_stmt, 1, id); sqlite3_step(_stmt); auto magic = Magic::getInstance(); if(sqlite3_data_count(_stmt)){ magic->setId(sqlite3_column_int(_stmt, 0)); magic->setRank(sqlite3_column_int(_stmt, 1)); magic->setName((char*)sqlite3_column_text(_stmt, 2)); magic->setParamAttack(sqlite3_column_int(_stmt, 3)); magic->setParamCure(sqlite3_column_int(_stmt, 4)); magic->setParamStan(sqlite3_column_int(_stmt, 5)); } sqlite3_reset(_stmt); sqlite3_clear_bindings(_stmt); sqlite3_finalize(_stmt); return magic; }
void LatexDocVisitor::visitPre(DocHtmlTable *t) { m_rowSpans.clear(); m_insideTable = true; if (m_hide) { return; } if (t->hasCaption()) { m_t << "\\begin{table}[h]"; } m_t << "\\begin{" << getTableName(t->parent()) << "}{" << t->numColumns() << "}\n"; m_numCols = t->numColumns(); m_t << "\\hline\n"; // check if first row is a heading and then render the row already here // and end it with \endfirsthead (triggered via m_firstRow == TRUE) // then repeat the row as normal and end it with \endhead (m_firstRow == FALSE) DocHtmlRow *firstRow = t->firstRow(); if (firstRow && firstRow->isHeading()) { m_firstRow = true; firstRow->accept(this); m_firstRow = false; } }
QueryBuilder Repository::deleteByIdQb(int id) { QueryBuilder qb; qb.deleteFrom(getTableName()) ->where("id = " + QString::number(id)); return qb; }
//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ SessionDataMap::pFutureDataCollection_type SessionDataMap::getAll() { pFutureDataCollection_type pFutureCollection(new FutureDataCollection_type); pDataCollection_type pCollection(new SessionDataCollection, &destroyCollection); // Start a transaction Zen::Database::I_DatabaseConnection::pDatabaseTransaction_type pTransaction = getDatabaseConnection()->beginTransaction(); // Create a query Zen::Database::I_DatabaseTransaction::pStaticQuery_type pQuery = pTransaction->createStaticQuery(); // Get all of the records from the Session. std::stringstream query; query << "SELECT * FROM " << getTableName(); pQuery->setQuery(query.str()); // Create a result handler to handle the execution results Zen::Database::I_DatabaseTransaction::pQueryResultHandler_type pResultHandler(new CollectionResultHandler(pTransaction, pFutureCollection, pCollection)); // Execute the query. pTransaction->executeQuery(pQuery, pResultHandler); return pFutureCollection; }
//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ SessionDataMap::pFutureDomainObject_type SessionDataMap::getByKey(boost::uint64_t _primaryKeyValue) { // Start a transaction Zen::Database::I_DatabaseConnection::pDatabaseTransaction_type pTransaction = getDatabaseConnection()->beginTransaction(); // Create a query Zen::Database::I_DatabaseTransaction::pStaticQuery_type pQuery = pTransaction->createStaticQuery(); const boost::uint64_t primaryKeyValue(_primaryKeyValue); // Get a record from the Session. std::stringstream query; query << "SELECT * FROM " << getTableName() << " WHERE sessionId = " << primaryKeyValue; pQuery->setQuery(query.str()); pFutureDomainObject_type pReturnValue(new FutureDomainObject_type); pDomainObject_type pValue(new SessionDomainObject, &destroyObject); // Create a result handler to handle the execution results Zen::Database::I_DatabaseTransaction::pQueryResultHandler_type pResultHandler(new ResultHandler(pTransaction, pReturnValue, pValue)); // Execute the query. pTransaction->executeQuery(pQuery, pResultHandler); return pReturnValue; }
//-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ void SessionDataMap::deleteByKey(boost::uint64_t _primaryKeyValue) { // Don't do anything if the key value is 0. But should this be an error? if (_primaryKeyValue == 0) { // TODO Throw an exception or return an error? return; } // Start a transaction Zen::Database::I_DatabaseConnection::pDatabaseTransaction_type pTransaction = getDatabaseConnection()->beginTransaction(); // Create a query Zen::Database::I_DatabaseTransaction::pStaticQuery_type pQuery = pTransaction->createStaticQuery(); const boost::uint64_t primaryKeyValue(_primaryKeyValue); // Delete a record in the Session. std::stringstream query; query << "DELETE FROM " << getTableName() << " WHERE sessionId = " << primaryKeyValue; pQuery->setQuery(query.str()); // Execute the query. pTransaction->executeQuery(pQuery); }
std::string Mapper::getFilename(std::string bulkDirectory, std::string columnName) { std::string filename = bulkDirectory + "/" + getTableName(); filename += "_" + columnName; filename += ".dat"; return filename; }
/** * Save model (insert/update record) */ bool save() { try { if(mIsNew) { mAdapter->insert(getTableName(), mAttributes); mIsNew = false; } else { std::map<std::string, std::string> where = { {getPrimaryKey(), mAttributes[getPrimaryKey()]} }; mAdapter->update(getTableName(), mAttributes, where); } } catch (const std::exception& e) { return false; } return true; }
HumanWithVolumeRecordShrPtr HumanAccessorPostgresql::getRecord( ITransactionShrPtr a_transaction, IDHolder const & a_id_holder, IKey const & a_key ) const { TransactionPostgresqlShrPtr transaction = shared_dynamic_cast<TransactionPostgresql>(a_transaction); pqxx::transaction<> & backbone_transaction = transaction->getBackboneTransaction(); string query = "SELECT volume FROM " + getTableName(a_id_holder) + " WHERE holder_name = " + backbone_transaction.quote(a_id_holder.getValue2()) + " AND human_key = " + backbone_transaction.quote(a_key.c_str()); pqxx::result result = backbone_transaction.exec(query); if (result.size() > 0) { Volume volume; result[0]["volume"].to(volume); return make_shared<HumanWithVolumeRecord>(a_id_holder, a_key, volume); } else { return HumanWithVolumeRecordShrPtr(); } }
/** * @brief Repository::updateByIdQB Constructs a query to update a row identified by its ID in the * child's Repository associated table. * @param id Id of the row to update. * @param cols Columns to update. */ QueryBuilder Repository::updateByIdQB(int id, QList<QString> cols) { QueryBuilder qb; qb.update(getTableName(), QStringList(cols)); qb.where("id = " + QString::number(id)); return qb; }
void LatexDocVisitor::visitPre(DocHtmlCaption *c) { if (m_hide) { return; } m_t << "\\end{" << getTableName(c->parent()->parent()) << "}\n\\centering\n\\caption{"; }
/** * Remove record from table */ void destroy() { std::map<std::string, std::string> where = { {getPrimaryKey(), mAttributes[getPrimaryKey()]} }; mAdapter->destroy(getTableName(), where); if (mCommitFlag) commit(); mIsNew = true; }
/** * @brief Repository::findByIdQB Constructs a query to find one row by its id from the child's * repository associated table. * @param id Id of the row to find. * @return QueryBuilder */ QueryBuilder Repository::findByIdQB(int id) { QueryBuilder qb; qb.select("*") ->from(getTableName()) ->where("id = " + QString::number(id)); return qb; }
/** * @brief Repository::isEmpty Verifies if the table is empty. * @return Return whether the table associated with the inherithing Repository is empty or not. */ bool Repository::isEmpty() { QueryBuilder qb; qb.select("count(*) AS rows") ->from(getTableName()); DBItem dbItem = Database::instance()->pullFirst(qb.getQuery()); int rowCount = dbItem.getHashMap().value("rows").toInt(); return (rowCount == 0); }
bool logDB::set( const logInfo& log) { char sql[1024] =""; char* errMsg; sprintf(sql, "insert or replace into %s\ (date, isUploaded)\ values(\"%s\" , %d);", getTableName().c_str(), log.date.c_str(), log.isUploaded ? 1 : -1 ); if(SQLITE_OK == sqlite3_exec(s_db, sql, NULL, NULL, &errMsg)) return true; printf("Insert or replace failed in table %s: %s\n", getTableName().c_str(), errMsg); return false; }
int Repository::insertIntoDefault(DBItem *item) { QueryBuilder qb; qb.insertIntoDefault(getTableName()); qb.withAsSelect(qb, "id"); QSqlQuery query = qb.getQuery(); int id = Database::instance()->pushWithId(query); item->setId(id); return id; }
bool logDB::get(const string& date1, const string& date2, vector<logInfo> & logArray) { char sql[1024] = {0}; sqlite3_stmt *stmt; int rc; string curDate = getStrDate(); logArray.clear(); sprintf(sql, "select * from %s where date >= '%s' and date <= '%s';", getTableName().c_str(), date1.c_str(), date2.c_str()); #if 1 { int nrow = 0, ncolumn = 0; char **azResult; char * zErrMsg; char s[] = "SELECT * FROM logTable"; sqlite3_get_table(s_db , s , &azResult , &nrow , &ncolumn , &zErrMsg ); int i = 0 ; printf( "row:%d column=%d \n" , nrow , ncolumn ); printf( "\nThe result of querying is : \n" ); for( i=0 ; i<( nrow + 1 ) * ncolumn ; i++ ) printf( "azResult[%d] = %s\n", i , azResult[i] ); } #endif rc= sqlite3_prepare(s_db,sql, strlen(sql), &stmt,0); if( rc ){ fprintf(stderr, "Can't open statement: %s\n", sqlite3_errmsg(s_db)); sqlite3_close(s_db); return false; } string str; while(sqlite3_step(stmt)==SQLITE_ROW ) { logInfo logI; logI.date = string( (const char*)sqlite3_column_text(stmt,0)); logI.isUploaded = sqlite3_column_int(stmt,1) == 1 ? true : false; if (logI.date == curDate) continue; logArray.push_back(logI); } sqlite3_finalize(stmt); return true; }
void LatexDocVisitor::visitPost(DocHtmlTable *t) { m_insideTable=FALSE; if (m_hide) return; if (t->hasCaption()) { m_t << "\\end{table}\n"; } else { m_t << "\\end{" << getTableName(t->parent()) << "}\n"; } }
void LatexDocVisitor::visitPre(DocHtmlTable *t) { m_rowSpans.clear(); m_insideTable=TRUE; if (m_hide) return; if (t->hasCaption()) { m_t << "\\begin{table}[h]"; } m_t << "\\begin{" << getTableName(t->parent()) << "}{" << t->numColumns() << "}\n"; m_numCols = t->numColumns(); m_t << "\\hline\n"; }
template <class WriteSet> SQLiteObjectStorage::Error SQLiteObjectStorage::applyWriteSet(const SQLiteDBPtr& db, const WriteSet& ws, int retries) { int num_writes=ws.writes_size(); for (int ws_it=0;ws_it<num_writes;++ws_it) { String object_hex = getTableName(ws.writes(ws_it)); String key_name = getKeyName(ws.writes(ws_it)); int rc; char* remain; // Insert or replace the value String value_insert = "INSERT OR REPLACE INTO "; if (!ws.writes(ws_it).has_data()) { value_insert = "DELETE FROM "; } value_insert += "\"" TABLE_NAME "\""; if (!ws.writes(ws_it).has_data()) { value_insert+= " WHERE object = x\'" + object_hex + "\' AND key = ?"; }else { //if (ws.writes(ws_it).has_data()) { value_insert += " (object, key, value) VALUES(x\'" + object_hex + "\', ?, ?)"; //} } sqlite3_stmt* value_insert_stmt; rc = sqlite3_prepare_v2(db->db(), value_insert.c_str(), -1, &value_insert_stmt, (const char**)&remain); SQLite::check_sql_error(db->db(), rc, NULL, "Error preparing value insert statement"); rc = sqlite3_bind_text(value_insert_stmt, 1, key_name.data(), (int)key_name.size(), SQLITE_TRANSIENT); SQLite::check_sql_error(db->db(), rc, NULL, "Error binding key name to value insert statement"); if (rc==SQLITE_OK) { if (ws.writes(ws_it).has_data()) { rc = sqlite3_bind_blob(value_insert_stmt, 2, ws.writes(ws_it).data().data(), (int)ws.writes(ws_it).data().size(), SQLITE_TRANSIENT); SQLite::check_sql_error(db->db(), rc, NULL, "Error binding value to value insert statement"); } } int step_rc = sqlite3_step(value_insert_stmt); if (step_rc != SQLITE_OK && step_rc != SQLITE_DONE) sqlite3_reset(value_insert_stmt); // allow this to be cleaned up rc = sqlite3_finalize(value_insert_stmt); SQLite::check_sql_error(db->db(), rc, NULL, "Error finalizing value insert statement"); if (step_rc == SQLITE_BUSY || step_rc == SQLITE_LOCKED) return DatabaseLocked; } return None; }
mblDB::mblDB() { // 1. should open the db, but the db is opened // 2. name the divec talbe setTableName("mobileTable"); // 3. create the table char sql[1024] =""; if(!tableExist()) { char* errMsg; // creat the table sprintf(sql, "CREATE TABLE %s ( key integer PRIMARY KEY, pkgID int);", getTableName().c_str()); int rc = sqlite3_exec(s_db, sql, NULL, NULL, &errMsg); if( rc ){ fprintf(stderr, "Can't create table %s: %s\n", getTableName().c_str(), errMsg); sqlite3_close(s_db); exit(1); } } }
HumanWithVolumeRecordMap HumanAccessorPostgresql::getRecords( ITransactionShrPtr a_transaction, IDHolder const & a_id_holder ) const { TransactionPostgresqlShrPtr transaction = shared_dynamic_cast<TransactionPostgresql>(a_transaction); pqxx::transaction<> & backbone_transaction = transaction->getBackboneTransaction(); string query = "SELECT * FROM " + getTableName(a_id_holder) + " WHERE holder_name = " + backbone_transaction.quote(a_id_holder.getValue2()); return prepareResultGetRecords(backbone_transaction.exec(query), a_id_holder); }
/** * Update many attributes * @param attributes_I map attributes hash * @return bool */ bool update(std::map<std::string, std::string> attributes_I) { try { if (!mIsNew) { std::map<std::string, std::string> where = { {getPrimaryKey(), mAttributes[getPrimaryKey()]} }; mAdapter->update(getTableName(), attributes_I, where); if (mCommitFlag) commit(); return true; } } catch (const std::exception& e) { return false; } return false; }
void HumanAccessorPostgresql::deleteRecord( ITransactionShrPtr a_transaction, IDHolder const & a_id_holder, IKey const & a_key ) const { TransactionPostgresqlShrPtr transaction = shared_dynamic_cast<TransactionPostgresql>(a_transaction); pqxx::transaction<> & backbone_transaction = transaction->getBackboneTransaction(); string query = "DELETE FROM " + getTableName(a_id_holder) + " WHERE holder_name = " + backbone_transaction.quote(a_id_holder.getValue2()) + " AND human_key = " + backbone_transaction.quote(a_key.c_str()); pqxx::result result = backbone_transaction.exec(query); }
string Table::printSelf(){ ostringstream stringForConversion; ostringstream stringForConversion2; string s = getTableName() + ", "; stringForConversion << columnCount(); s += stringForConversion.str() + ", "; stringForConversion2 << rowCount(); s += stringForConversion2.str() + "; "; s += printColumns(); return s; }
// Process update columns of the delta to populate data members // containsIndirectUpdateColumn_, containsDirectUpdateColumn_ and // containsUpdateColUsedByMv_ void DeltaDefinition::processUpdateColumns(MVInfoForDML *mvInfo) { if (updateColumnsProcessed_) return; mvInfo->initUsedObjectsHash(); const MVUsedObjectInfo *usedObject = mvInfo-> findUsedInfoForTable(*getTableName()); const IntegerList *updatedCols = getUpdatedColumnList(); if (updatedCols==NULL) { // We have no statistics on updated column - we have to assume the worst case. containsIndirectUpdateColumn_ = TRUE; containsDirectUpdateColumn_ = TRUE; containsUpdateColUsedByMv_ = TRUE; } else { for (CollIndex i = 0; i < updatedCols->entries(); i++ ) { Lng32 updatedCol = updatedCols->at(i); if (usedObject->isIndirectUpdateCol(updatedCol)) { containsUpdateColUsedByMv_ = TRUE; containsIndirectUpdateColumn_ = TRUE; } else if (usedObject->isUsedColumn(updatedCol)) { containsUpdateColUsedByMv_ = TRUE; containsDirectUpdateColumn_ = TRUE; } // if already seen one DIRECT update column, one INDIRECT update column // and one update column that is used by the MV if (containsIndirectUpdateColumn_ && containsDirectUpdateColumn_ && containsUpdateColUsedByMv_) break; } } updateColumnsProcessed_ = TRUE; }
bool mblDB::set( const mblInfo& mbl) { char sql[1024] =""; char* errMsg; sprintf(sql, "insert or replace into %s\ (key, pkgID)\ values( %d, %d);", getTableName().c_str(), mbl.mblID, mbl.pkgID ); if(SQLITE_OK == sqlite3_exec(s_db, sql, NULL, NULL, &errMsg)) return true; return false; }
void HumanAccessorPostgresql::decreaseVolume( ITransactionShrPtr a_transaction, IDHolder const & a_id_holder, IKey const & a_key, Volume const & a_volume ) const { TransactionPostgresqlShrPtr transaction = shared_dynamic_cast<TransactionPostgresql>(a_transaction); pqxx::transaction<> & backbone_transaction = transaction->getBackboneTransaction(); string query = "UPDATE " + getTableName(a_id_holder) + " SET volume = volume - " + backbone_transaction.quote(a_volume) + " WHERE holder_name = " + backbone_transaction.quote(a_id_holder.getValue2()) + " AND human_key = " + backbone_transaction.quote(a_key.c_str()); pqxx::result result = backbone_transaction.exec(query); }
void HumanAccessorPostgresql::insertRecord( ITransactionShrPtr a_transaction, IDHolder const & a_id_holder, IKey const & a_key, Volume const & a_volume ) const { TransactionPostgresqlShrPtr transaction = shared_dynamic_cast<TransactionPostgresql>(a_transaction); pqxx::transaction<> & backbone_transaction = transaction->getBackboneTransaction(); string query = "INSERT INTO " + getTableName(a_id_holder) + "(holder_name, human_key, volume) VALUES(" + backbone_transaction.quote(a_id_holder.getValue2()) + ", " + backbone_transaction.quote(a_key.c_str()) + ", " + backbone_transaction.quote(a_volume) + ")"; pqxx::result result = backbone_transaction.exec(query); }