void TestIteratorsAndRanges() { REMARK("testing iterators compliance\n"); TestIteratorTraits<MyTable::iterator,MyTable::value_type>(); TestIteratorTraits<MyTable::const_iterator,const MyTable::value_type>(); MyTable v; MyTable const &u = v; TestIteratorAssignment<MyTable::const_iterator>( u.begin() ); TestIteratorAssignment<MyTable::const_iterator>( v.begin() ); TestIteratorAssignment<MyTable::iterator>( v.begin() ); // doesn't compile as expected: TestIteratorAssignment<typename V::iterator>( u.begin() ); // check for non-existing ASSERT(v.equal_range(MyKey::make(-1)) == std::make_pair(v.end(), v.end()), NULL); ASSERT(u.equal_range(MyKey::make(-1)) == std::make_pair(u.end(), u.end()), NULL); REMARK("testing ranges compliance\n"); TestRangeAssignment<MyTable::const_range_type>( u.range() ); TestRangeAssignment<MyTable::const_range_type>( v.range() ); TestRangeAssignment<MyTable::range_type>( v.range() ); // doesn't compile as expected: TestRangeAssignment<typename V::range_type>( u.range() ); REMARK("testing construction and insertion from iterators range\n"); FillTable( v, 1000 ); MyTable2 t(v.begin(), v.end()); v.rehash(); CheckTable(t, 1000); t.insert(v.begin(), v.end()); // do nothing CheckTable(t, 1000); t.clear(); t.insert(v.begin(), v.end()); // restore CheckTable(t, 1000); REMARK("testing comparison\n"); typedef tbb::concurrent_hash_map<MyKey,MyData2,YourHashCompare,MyAllocator> YourTable1; typedef tbb::concurrent_hash_map<MyKey,MyData2,YourHashCompare> YourTable2; YourTable1 t1; FillTable( t1, 10 ); CheckTable(t1, 10 ); YourTable2 t2(t1.begin(), t1.end()); MyKey key( MyKey::make(-5) ); MyData2 data; ASSERT(t2.erase(key), NULL); YourTable2::accessor a; ASSERT(t2.insert(a, key), NULL); data.set_value(0); a->second = data; ASSERT( t1 != t2, NULL); data.set_value(5*5); a->second = data; ASSERT( t1 == t2, NULL); }
void CMQuestionDetail::DoClear() { m_mutex.Lock(); if (m_lstItem) m_lstItem->clear(); m_mutex.UnLock(); sqlite3* db = CheckTable(m_tablename); if (db != NULL) { CHAR sql[1024]; sql[0] = '\0'; sqlite3_stmt *stmt; snprintf(sql,sizeof(sql),"DELETE FROM %s WHERE questionid = ? ", m_tablename); if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK) { BindParam(stmt, 1, sQuestionID); if (sqlite3_step(stmt) != SQLITE_DONE) { CM_ERRP("exec %s failed.", sql); } } } }
void CMExerciseList::DoClear() { m_mutex.Lock(); if(m_lstItem) m_lstItem->clear(); m_mutex.UnLock(); CHAR sql[1024]; sql[0] = '\0'; char* errorMsg; sqlite3_stmt *stmt; sqlite3* db = CheckTable(m_tablename); snprintf(sql,sizeof(sql),"DELETE FROM %s", m_tablename); if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK) { if (sqlite3_step(stmt) != SQLITE_DONE) { CM_ERRP("exec %s failed.error:%s", sql, errorMsg); } sqlite3_finalize(stmt); } }
void CMBrowser::DoClear() { DoClearList(); sqlite3* db = CheckTable(); if(strlen(m_tablename)<=0) return; if(db != NULL) { CHAR sql[1024]; sql[0] = '\0'; sqlite3_stmt *stmt; snprintf(sql,1024,"DELETE FROM %s WHERE categoryid = ? ", m_tablename); if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK) { BindParam(stmt,1, sCategoryID); if (sqlite3_step(stmt) != SQLITE_DONE) { CM_ERRP("exec %s failed.", sql); } sqlite3_finalize(stmt); } } }
BOOL CMExerciseList::LoadFromDB() { BOOL ret = FALSE; if(m_lstItem) m_lstItem->clear(); else m_lstItem = new CMList<TExerciseListItem>; sqlite3* db = CheckTable(m_tablename); if (!db) return ret; char* errorMsg; if (sqlite3_exec(db, "BEGIN TRANSACTION;", 0,0, &errorMsg) != SQLITE_OK) { CM_ERRP("BEGIN TRANSACTION failed.error:%s", errorMsg); } ret = DoGetCacheItems(db); if (db != NULL && sqlite3_exec(db, "COMMIT TRANSACTION;", 0,0, &errorMsg) != SQLITE_OK) { CM_ERRP("COMMIT TRANSACTION failed.error:%s", errorMsg); } return ret; }
BOOL CMExerciseList::CheckExerciseListIsExit(const char *sID) { BOOL ret = FALSE; if(strlen(m_tablename)==0) return ret; sqlite3_stmt *stmt; CHAR sql[1024]; sql[0] = '\0'; sqlite3*db=CheckTable(m_tablename); if (db == NULL) return FALSE; int nCount = 0; snprintf(sql, 1024,"SELECT COUNT(*) FROM %s WHERE id=?", m_tablename); if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK) { BindParam(stmt, 1, sID); if (sqlite3_step(stmt) == SQLITE_ROW) { nCount = sqlite3_column_int(stmt, 0); if (nCount > 0) ret = TRUE; } sqlite3_finalize(stmt); } else { CM_ERRP("sqlite3_step %s failed.error:%s", sql, sqlite3_errmsg(db)); } return ret; }
bool NFCDataBaseModule::AfterInit() { //test or create table CheckTable(); // NFCValueList valueAccount; // QueryAccountProperty( "11", valueAccount ); // // for ( int i = 0; i < valueAccount.GetCount(); i++ ) // { // std::cout << valueAccount.StringVal( i ) << std::endl; // } // // NFCValueList valueProperty; // QueryRoleProperty( "qq", valueProperty ); // for ( int i = 0; i < valueProperty.GetCount(); i++ ) // { // std::cout << valueProperty.StringVal( i ) << std::endl; // } // // SetAccountProperty( "11", NFCValueList() << "111,33" << "222,444" ); return true; }
BOOL CMWrongQuestion::ClearAnswer() { BOOL ret = FALSE; sqlite3* db = CheckTable(); char* errorMsg; if (sqlite3_exec(db, "BEGIN TRANSACTION;", 0,0, &errorMsg) != SQLITE_OK) { CM_ERRP("BEGIN TRANSACTION failed.error:%s", errorMsg); } SetCurIndex(0); for (int i = 0; i < GetItemCount(); i++) { TQuestion& qitem = m_lstItem->at(i); qitem.item.isChecked = FALSE; qitem.item.isRight = FALSE; qitem.item.sMyAnswer = ""; qitem.item.sAnswer = ""; sLastUpdate = 0; DoClearAnswer(qitem); ret = TRUE; } if (db != NULL && sqlite3_exec(db, "COMMIT TRANSACTION;", 0,0, &errorMsg) != SQLITE_OK) { CM_ERRP("COMMIT TRANSACTION failed.error:%s", errorMsg); } return ret; }
BOOL CMMyInfo::DoUpdate(const TDummyItem& obj) { BOOL ret = FALSE; sqlite3* db = CheckTable(m_tablename); if (db) { m_mutex.Lock(); CHAR sql[1024]; sqlite3_stmt *stmt; snprintf(sql,1024, "UPDATE %s SET jobnumber=?, headimage=?, fullname=?, organization=?, department=?, position=?, title=?, rankurl=?, detailurl=?, level=?, value=?, credit=?, studyrecord=?, positioncoursecount=?, currentminvalue=?, nextvalue=?, undoexam=?, wrongqa=?,contributevalue=?", m_tablename); if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK) { BindParam(stmt, 1, sJobnumber.c_str()); BindParam(stmt, 2, sHeadimage.c_str()); BindParam(stmt, 3, sWorkname.c_str()); BindParam(stmt, 4, sOrg.c_str()); BindParam(stmt, 5, sDevelopment.c_str()); BindParam(stmt, 6, sPosition.c_str()); BindParam(stmt, 7, sTitle.c_str()); BindParam(stmt, 8, sRankurl.c_str()); BindParam(stmt, 9, sDetailurl.c_str()); BindParam(stmt, 10, nLevel); BindParam(stmt, 11, nValue); BindParam(stmt, 12, nCredit); BindParam(stmt, 13, nStudycount); BindParam(stmt, 14, nPositionCourseCount); BindParam(stmt, 15, nCurrentminvalue); BindParam(stmt, 16, nNextvalue); BindParam(stmt, 17, nUndoExam); BindParam(stmt, 18, nWrongQa); BindParam(stmt,19,nContributeValue); if (sqlite3_step(stmt) == SQLITE_DONE) { ret = TRUE; } else { CM_ERRP("exec %s failed.error:%s", sql, sqlite3_errmsg(db)); } } else { CM_ERRP("exec %s failed.error:%s", sql, sqlite3_errmsg(db)); } m_mutex.UnLock(); sqlite3_finalize(stmt); } return ret; }
void TableGet::Compile() { CheckTable(); SwithTag(); PerformGet(); SearchForTM(); SaveResult(); FinishGet(); }
void TestAssignment() { REMARK("testing assignment\n"); for( int i=0; i<1000; i=(i<30 ? i+1 : i*5) ) { for( int j=0; j<1000; j=(j<30 ? j+1 : j*7) ) { MyTable t1; MyTable t2; FillTable(t1,i); FillTable(t2,j); ASSERT( (t1 == t2) == (i == j), NULL ); CheckTable(t2,j); MyTable& tref = t2=t1; ASSERT( &tref==&t2, NULL ); ASSERT( t1 == t2, NULL ); CheckTable(t1,i); CheckTable(t2,i); t1.clear(); CheckTable(t1,0); CheckTable(t2,i); ASSERT( MyDataCount==i, "data leak?" ); t2.clear(); CheckTable(t1,0); CheckTable(t2,0); ASSERT( MyDataCount==0, "data leak?" ); } } }
static void TestCopy() { REMARK("testing copy\n"); MyTable t1; for( int i=0; i<10000; i=(i<100 ? i+1 : i*3) ) { MyDataCount = 0; FillTable(t1,i); // Do not call CheckTable(t1,i) before copying, it enforces rehashing MyTable t2(t1); // Check that copy constructor did not mangle source table. CheckTable(t1,i); swap(t1, t2); CheckTable(t1,i); ASSERT( !(t1 != t2), NULL ); // Clear original table t2.clear(); swap(t2, t1); CheckTable(t1,0); // Verify that copy of t1 is correct, even after t1 is cleared. CheckTable(t2,i); t2.clear(); t1.swap( t2 ); CheckTable(t1,0); CheckTable(t2,0); ASSERT( MyDataCount==0, "data leak?" ); } }
void CMSpecialTopic::DoClear() { m_mutex.Lock(); if(m_lstHeadItem) { for(int i = 0; i < m_lstHeadItem->size(); i++) { TBrowserItem* ptr = m_lstHeadItem->at(i); SAFEDELETE(ptr); } m_lstHeadItem->clear(); } m_mutex.UnLock(); m_mutex.Lock(); if(m_lstItem) { for(int i = 0; i < m_lstItem->size(); i++) { TBrowserItem* ptr = m_lstItem->at(i); SAFEDELETE(ptr); } m_lstItem->clear(); } m_mutex.UnLock(); sqlite3* db = CheckTable(); if(db != NULL) { CHAR sql[1024]; sql[0] = '\0'; sqlite3_stmt *stmt; snprintf(sql, sizeof(sql),"DELETE FROM %s WHERE parentspecid = ? ", m_tablename); if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK) { BindParam(stmt, 1, sParentSpecId); if (sqlite3_step(stmt) != SQLITE_DONE) { CM_ERRP("exec %s failed.", sql); } } } }
bool state_probability_queue::hashtable_lookup(state_and_probability_pair * &in) { unsigned long key = in->_state.hashkey(); unsigned long h1 = key % hashtable_size; unsigned long h2 = 1 + key % (hashtable_size - 1); unsigned long h = h1; unsigned long probe = 0; // no hash compaction, uses double hashing bool empty, equal = FALSE; while (!(empty = (Full->get(h) == 0)) && !(equal = (in->_state == table[h]._state)) && (probe < MAX_HT_CHAIN_LENGTH)) { h = (h1 + probe * h2) % hashtable_size; // double hashing probe++; } if (empty) { /* Go ahead and insert the element. */ table[h] = *in; in = &table[h]; Full->set(h); hashtable_num_elts++; return FALSE; } else if (equal) { in = &table[h]; return TRUE; } else { #if (DBG > 0) fprintf(stderr, "hashtable_lookup: table is full,clear it!\n"); #endif //table is full,clear it! CheckTable(); //the table now is empty: use the first slot available for this state h = h1 % hashtable_size; table[h] = *in; in = &table[h]; Full->set(h); hashtable_num_elts++; return FALSE; } return FALSE; }
void CInputTablePage::SetWizardButton (void) { ASSERT (NULL != m_pParams); if (CheckDatabase () && // Database-Einstellung OK m_pParams -> m_SourceSet.IsOpen () && m_pParams -> m_pTableInfo && CheckTable (m_pParams -> m_SourceSet, m_pParams -> m_pTableInfo -> InputTable (), m_pParams -> m_pTableInfo -> TableName ())) GetParent () -> SetWizardButtons (PSWIZB_NEXT); else GetParent () -> SetWizardButtons (0); m_lbTables.EnableWindow (m_lbTables.GetCount () > 0); }
bool CWizIndexBase::Open(const CString& strFileName) { m_strFileName = strFileName; //m_strDatabasePath = WizExtractFilePath(strFileName); try { m_db.open(strFileName); } catch (const CppSQLite3Exception& e) { return LogSQLException(e, _T("open database")); } for (int i = 0; i < TABLE_COUNT; i++) { if (!CheckTable(g_arrayTableName[i])) return false; } return true; }
bool state_probability_queue::enqueue(state * &e, double p) { if (hashtable_num_elts >= hashtable_size) CheckTable(); state_and_probability_pair pair(e, p); state_and_probability_pair *ppair = &pair; if (!hashtable_lookup(ppair)) { ppair->_probability = p; e = &(ppair->_state); return true; } else { ppair->_probability += p; e = &(ppair->_state); return false; } }
void Refresh(CBlock* block, CBlock* next){ int tiem=SDL_GetTicks(); if(!(done%15) && done!=0){SpeedUp();done++;} if(tiem-LastAction>1000/speed){ if(!warning){ LastAction=tiem; MoveBlockDown(block, 0); //block->tiletable[0][0] }else{ for(int i=0; i<4; i++){ for(int j=0; j<4; j++){ if(block->tiletable[i][j]){ GameTable[i+gameblockx][j+gameblocky]=new CTile; GameTable[i+gameblockx][j+gameblocky]->Set(250+30*(i+gameblockx), 30*(j+gameblocky)); } } } gameblockx=3; gameblocky=-1; *block=*next; if(MoveBlockDown(block, 0)){gamestatus=4;} srand(time(NULL)); switch(rand()%7){ case 0: next->Set(0, -1, 0, 0, 0, 1, 0, 0, -1,179,206,221);break; //t case 1: next->Set(0, -1,-1,0,-1,0,0,1,0, 198,255,126);break; //z case 2: next->Set(0, -1,0,0,0,0,-1,1,-1, 126,196,255);break; //s case 3: next->Set(1, 0,0,1,0,0,1,1,1, 100, 100, 0);break; //o case 4: next->Set(0, 0,-1,0,0,0,1,0,2, 255,159,126);break; //i case 5: next->Set(0, -1,1,0,1,0,0,0,-1, 139,77,156);break; //j case 6: next->Set(0, 0,-1,0,0,0,1,1,1, 255,229,126);break; //l } warning=0; CheckTable(); } } }
void TestRehash() { REMARK("testing rehashing\n"); MyTable w; w.insert( std::make_pair(MyKey::make(-5), MyData()) ); w.rehash(); // without this, assertion will fail MyTable::iterator it = w.begin(); int i = 0; // check for non-rehashed buckets for( ; it != w.end(); i++ ) w.count( (it++)->first ); ASSERT( i == 1, NULL ); for( i=0; i<1000; i=(i<29 ? i+1 : i*2) ) { for( int j=max(256+i, i*2); j<10000; j*=3 ) { MyTable v; FillTable( v, i ); ASSERT(int(v.size()) == i, NULL); ASSERT(int(v.bucket_count()) <= j, NULL); v.rehash( j ); ASSERT(int(v.bucket_count()) >= j, NULL); CheckTable( v, i ); } } }
void __fastcall TDBChartCrossTab::FormCreate(TObject *Sender) { CheckTable(Table1); }
BOOL CMExerciseList::DoRefresh(TExerciseListItem& obj) { BOOL ret = FALSE; if (strcmp(m_tablename, "") == 0) return FALSE; //根据obj的sID属性更新 sqlite3_stmt *stmt; CHAR sql[1024]; sql[0] = '\0'; sqlite3*db=CheckTable(m_tablename); if (db == NULL) return FALSE; snprintf(sql,sizeof(sql),"SELECT id,title,questioncount,uncompletecount,desc,category,usercompletecount,pubdate,curindex,nwrongcount,rightcount,trainid FROM %s WHERE id = ?", m_tablename); if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK) { BindParam(stmt, 1, obj.sID.c_str()); if (sqlite3_step(stmt) == SQLITE_ROW) { obj.sID = (const char*)sqlite3_column_text(stmt, 0); obj.sTitle = (const char*)sqlite3_column_text(stmt, 1); obj.nQuestioncount = sqlite3_column_int(stmt, 2); obj.nUncompletecount = sqlite3_column_int(stmt, 3); obj.sDesc = (const char*)sqlite3_column_text(stmt, 4); obj.sCategory = (const char*)sqlite3_column_text(stmt, 5); obj.nUsercompletecount = sqlite3_column_int(stmt, 6); obj.sPubdate = (const char*)sqlite3_column_text(stmt, 7); obj.nCurIndex = sqlite3_column_int(stmt, 8); obj.nWrongCount = sqlite3_column_int(stmt, 9); obj.nRightCount = sqlite3_column_int(stmt, 10); obj.sTrainID = (const char*)sqlite3_column_text(stmt,11); m_mutex.Lock(); for (int i = 0; i < m_lstItem->size(); i++) { TExerciseListItem& item = m_lstItem->at(i); if (strcmp(item.sID.c_str(), obj.sID.c_str()) == 0) { item = obj; ret = TRUE; break; } } m_mutex.UnLock(); } else { CM_ERRP("sqlite3_prepare_v2 %s failed.error:%s", sql, sqlite3_errmsg(db)); } sqlite3_finalize(stmt); } else { CM_ERRP("sqlite3_step %s failed.error:%s", sql, sqlite3_errmsg(db)); } return ret; }
//--------------------------------------------------------------------------- void __fastcall TDBChartSingleRecord::FormCreate(TObject *Sender) { CheckTable(Table1); }
// CMSOmniProvRowset::Execute // 1. Parse the SQL query, // 2. Execute the query, and // 3. Build the initial rowset HRESULT CMSOmniProvRowset::Execute(DBPARAMS * pParams, DBROWCOUNT* pcRowsAffected) { USES_CONVERSION; CMSOmniProvRowset* pT = (CMSOmniProvRowset*) this; CMSOmniProvRowset::ObjectLock cab((CMSOmniProvRowset*) this); HRESULT hr; _bstr_t m_bstrFileName; if ( FAILED(hr=pT->GetDataSource(m_bstrFileName)) ) return hr; // Check the property value whether read/ updatabiliy property is set or not... _variant_t varUpd; GetPropValue(&DBPROPSET_ROWSET,DBPROP_UPDATABILITY, &varUpd); if ( 0 != varUpd.iVal ) { // 1. a) Build the file's Schema m_prgColInfo from the '.sxt' file, // b) Open the file '.txt', and // c) Fill the m_DBFile.m_Rows structure // Open in exclusive mode if (!m_DBFile.Open((LPCTSTR) m_bstrFileName,true)) return DB_E_NOTABLE; if (!m_DBFile.FillRowArray()) return E_FAIL; } else // Open in non-exclusive mode { if (!m_DBFile.Open((LPCTSTR) m_bstrFileName,false)) return DB_E_NOTABLE; if (!m_DBFile.FillRowArray()) return E_FAIL; } // Validate Command // 2. PARSE the SQL Query here (Only SELECT * FROM <Table_Name> is supported) TCHAR sep[] = " "; _bstr_t bstrSQL(pT->m_strCommandText); LPTSTR pchNextToken = NULL; TCHAR * token = _tcstok_s((TCHAR*) bstrSQL, (TCHAR*) sep, &pchNextToken); if (!CheckTable((TCHAR*) token)) { // The Rowset was created using the ICommand::Execute( )... // Only "SELECT * FROM Table_Name" Queries are supported if(_tcsicmp(token,TEXT("select")) != 0) { ATLTRACE2(atlTraceDBProvider,0,(const TCHAR*) (_bstr_t("Query: '")+ bstrSQL + _bstr_t("' is not a valid Query\n"))); return DB_E_ERRORSINCOMMAND; } ATLTRACE2(atlTraceDBProvider,0,(const TCHAR*) (_bstr_t("\tIt is a valid '")+_bstr_t(token) + _bstr_t("' Query\n"))); TCHAR szTblNm[MAX_TABLE_NAME_SIZE]; while (token != NULL) { _tcscpy_s(szTblNm, _countof(szTblNm), token); token= _tcstok_s(NULL,(TCHAR*) sep, &pchNextToken); } if (!CheckTable((TCHAR*) szTblNm)) return DB_E_NOTABLE; } // Allocate proxy buffers based on the schema information // Each CRow contains proxy buffer that the data is trasnferred to in the native // format. This information then needs to be copied out to the file in character format // on SetData() calls. CreateColInfo(); AllocateProxyBuffers(); if (pcRowsAffected != NULL) *pcRowsAffected = m_DBFile.m_Rows.GetCount(); return S_OK; }
/* Called by a thread to initialize its database access. * After this call: * - database connection is established * - schema is created * - prepared statements are ready to be used */ static int init_database_access(db_thread_info_t *p_thr_info) { char db_file[MAXPATHLEN + 1]; int rc; char **result = NULL; int rows, cols; char *errmsg = NULL; const char *unparsed; /* first open the database file */ snprintf(db_file, MAXPATHLEN, "%s/%s.%u", dbmap_dir, DB_FILE_PREFIX, p_thr_info->thr_index); rc = sqlite3_open(db_file, &p_thr_info->db_conn); if (rc != 0) { if (p_thr_info->db_conn) { LogCrit(COMPONENT_FSAL, "ERROR: could not connect to SQLite3 database (file %s): %s", db_file, sqlite3_errmsg(p_thr_info->db_conn)); sqlite3_close(p_thr_info->db_conn); } else { LogCrit(COMPONENT_FSAL, "ERROR: could not connect to SQLite3 database (file %s): status=%d", db_file, rc); } return HANDLEMAP_DB_ERROR; } /* Now check, that the map table exists */ rc = sqlite3_get_table(p_thr_info->db_conn, "SELECT name FROM sqlite_master WHERE type = 'table' AND name = '" MAP_TABLE "'", &result, &rows, &cols, &errmsg); CheckTable(p_thr_info->db_conn, rc, errmsg, result); /* no need for the result, just the number of rows returned */ sqlite3_free_table(result); if (rows != 1) { /* table must be created */ rc = sqlite3_exec(p_thr_info->db_conn, "CREATE TABLE " MAP_TABLE " ( " OBJID_FIELD " BIGINT NOT NULL, " HASH_FIELD " INT NOT NULL, " HANDLE_FIELD " TEXT, " "PRIMARY KEY(" OBJID_FIELD ", " HASH_FIELD ") )", NULL, NULL, &errmsg); CheckCommand(p_thr_info->db_conn, rc, errmsg); } /* Now, create prepared statements */ rc = sqlite3_prepare_v2(p_thr_info->db_conn, "SELECT " OBJID_FIELD "," HASH_FIELD "," HANDLE_FIELD " FROM " MAP_TABLE, -1, &(p_thr_info->prep_stmt[LOAD_ALL_STATEMENT]), &unparsed); CheckPrepare(p_thr_info->db_conn, rc); rc = sqlite3_prepare_v2(p_thr_info->db_conn, "INSERT INTO " MAP_TABLE "(" OBJID_FIELD "," HASH_FIELD "," HANDLE_FIELD ") " "VALUES (?1, ?2, ?3 )", -1, &(p_thr_info->prep_stmt[INSERT_STATEMENT]), &unparsed); CheckPrepare(p_thr_info->db_conn, rc); rc = sqlite3_prepare_v2(p_thr_info->db_conn, "DELETE FROM " MAP_TABLE " WHERE " OBJID_FIELD "=?1 AND " HASH_FIELD "=?2", -1, &(p_thr_info->prep_stmt[DELETE_STATEMENT]), &unparsed); CheckPrepare(p_thr_info->db_conn, rc); /* Everything is OK now ! */ return HANDLEMAP_SUCCESS; } /* init_database_access */
BOOL CMExerciseList::DoUpdate(const TExerciseListItem& obj) { BOOL ret = FALSE; //更新对象 m_mutex.Lock(); for (int i = 0; i < m_lstItem->size(); i++) { TExerciseListItem& item = m_lstItem->at(i); if (strcmp(item.sID.c_str(), obj.sID.c_str()) == 0) { item = obj; ret = TRUE; break; } } m_mutex.UnLock(); if (strcmp(m_tablename, "") == 0) return FALSE; //保存入数据库 sqlite3*db=CheckTable(m_tablename); if(db) { sqlite3_stmt *stmt; CHAR sql[512]; snprintf(sql,sizeof(sql),"UPDATE %s SET id=?, title=?, questioncount=?, uncompletecount=?, desc=?, category=?, usercompletecount=?, pubdate=?,curindex=?,nwrongcount=?,rightcount=?,trainid=? WHERE id=? ", m_tablename); if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK) { BindParam(stmt, 1, obj.sID.c_str()); BindParam(stmt, 2, obj.sTitle.c_str()); BindParam(stmt, 3, obj.nQuestioncount); BindParam(stmt, 4, obj.nUncompletecount); BindParam(stmt, 5, obj.sDesc.c_str()); BindParam(stmt, 6, obj.sCategory.c_str()); BindParam(stmt, 7, obj.nUsercompletecount); BindParam(stmt, 8, obj.sPubdate.c_str()); BindParam(stmt, 9, obj.nCurIndex); BindParam(stmt, 10, obj.nWrongCount); BindParam(stmt, 11, obj.nRightCount); BindParam(stmt, 12, obj.sTrainID.c_str()); if(sqlite3_step(stmt)==SQLITE_DONE) { ret = TRUE; } else { CM_ERRP("sqlite3_step %s failed.error:%s", sql, sqlite3_errmsg(db)); } } else { CM_ERRP("prepare %s failed.error:%s", sql, sqlite3_errmsg(db)); } sqlite3_finalize(stmt); } return ret; }
BOOL CMQuestionDetail::DoUpdate(const TAnswerItem& obj) { BOOL ret = FALSE; //更新对象 m_mutex.Lock(); for (int i = 0; i < m_lstItem->size(); i++) { TAnswerItem& item = m_lstItem->at(i); if (strcmp(item.sID.c_str(), obj.sID.c_str()) == 0) { item = obj; //若设了最佳答案,踢出链表 if(item.nAnswerflag!=0) { if(m_lstItem) { m_hasFlagAnswerItem=item; m_lstItem->removeAt(i); } } ret = TRUE; break; } } m_mutex.UnLock(); if (strlen(sQuestionID) == 0) return FALSE; if (strcmp(m_tablename, "") == 0) { //表名为空表示不需同步数据库,直接返回成功。 return TRUE; } //保存入数据库 sqlite3*db = CheckTable(m_tablename); if (db) { sqlite3_stmt *stmt; CHAR sql[512]; snprintf(sql,sizeof(sql),"UPDATE %s SET id=?, answer=?, answer_username=?, answer_fullname=?, answer_icon=?, pubdate=?, answerflag=?, pv=?, questionid=?, isliked=?, pic_url=?, thumburl=? WHERE id=? ", m_tablename); if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK) { BindParam(stmt, 1, obj.sID.c_str()); BindParam(stmt, 2, obj.sAnswer.c_str()); BindParam(stmt, 3, obj.sAnswerer_username.c_str()); BindParam(stmt, 4, obj.sAnswerer_fullname.c_str()); BindParam(stmt, 5, obj.sAnswerer_icon.c_str()); BindParam(stmt, 6, obj.sPubdate.c_str()); BindParam(stmt, 7, obj.nAnswerflag); BindParam(stmt, 8, obj.nPV); BindParam(stmt, 9, sQuestionID); BindParam(stmt, 10, obj.nIsliked); BindParam(stmt, 11, obj.sPic_url.c_str()); BindParam(stmt, 12, obj.sThumburl.c_str()); BindParam(stmt, 13,obj.sID.c_str()); if (sqlite3_step(stmt) == SQLITE_DONE) { } else { CM_ERRP("sqlite3_step %s failed.error:%s", sql, sqlite3_errmsg(db)); } } else { CM_ERRP("prepare %s failed.error:%s", sql, sqlite3_errmsg(db)); } sqlite3_finalize(stmt); } return ret; }
BOOL CMQuestionDetail::DoRefresh(TAnswerItem& obj) { BOOL ret = FALSE; m_mutex.Lock(); for (int i = 0; i < m_lstItem->size(); i++) { TAnswerItem& item = m_lstItem->at(i); if (strcmp(item.sID.c_str(), obj.sID.c_str()) == 0) { obj=item; ret = TRUE; break; } } m_mutex.UnLock(); if (strcmp(m_tablename, "") == 0) return FALSE; //根据obj的sID属性更新 sqlite3_stmt *stmt; CHAR sql[1024]; sql[0] = '\0'; sqlite3*db = CheckTable(m_tablename); if (db == NULL) return FALSE; snprintf(sql,sizeof(sql),"SELECT * FROM %s WHERE id = ? ", m_tablename); if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK) { BindParam(stmt, 1, obj.sID.c_str()); if (sqlite3_step(stmt) == SQLITE_ROW) { TAnswerItem item; item.sID = (const char*) sqlite3_column_text(stmt, 1); item.sAnswer = (const char*) sqlite3_column_text(stmt, 2); item.sAnswerer_username =(const char*) sqlite3_column_text(stmt, 3); item.sAnswerer_fullname =(const char*) sqlite3_column_text(stmt, 4); item.sAnswerer_icon = (const char*) sqlite3_column_text(stmt, 5); item.sPubdate = (const char*) sqlite3_column_text(stmt, 6); item.nAnswerflag = sqlite3_column_int(stmt, 7); item.nPV = sqlite3_column_int(stmt, 8); //strcpy(sQuestionID, (const char*)sqlite3_column_text(stmt, 9)); utf8ncpy(sQuestionID, (const char*) sqlite3_column_text(stmt, 9), 63); item.nIsliked = sqlite3_column_int(stmt, 10); item.sPic_url = (const char*) sqlite3_column_text(stmt, 11); item.sThumburl =(const char*) sqlite3_column_text(stmt, 12); } else { CM_ERRP("sqlite3_prepare_v2 %s failed.error:%s", sql, sqlite3_errmsg(db)); } sqlite3_finalize(stmt); } else { CM_ERRP("sqlite3_step %s failed.error:%s", sql, sqlite3_errmsg(db)); } return ret; }
void CMExerciseList::OnSessionCmd(unsigned int nCmdID, unsigned int nCode, TiXmlDocument* pDoc) { INT32 result = TResult::EUnknownError; if (nCode == MER_OK) { //获得根元素res。 TiXmlElement *pItem = pDoc->RootElement(); if(pItem) { INT32 nCmdID = 0; pItem->QueryIntAttribute("no", &nCmdID); INT32 nErr = 0; if(pItem->QueryIntAttribute("errno", &nErr) == TIXML_SUCCESS) { if (nErr == 0) { if (nCmdID == SERVICE_GETEXERCISELIST || nCmdID == SERVICE_GETEXERCISEBYID||nCmdID == SERVICE_GETMYTRAINEXERCISELIST) { sqlite3* db = CheckTable(); if(db) { DoCreate(db); } pItem = pItem->FirstChildElement("item"); char* errorMsg; if (m_lstItem) { m_lstItem->clear(); } if (db != NULL && sqlite3_exec(db, "BEGIN TRANSACTION;", 0,0, &errorMsg) != SQLITE_OK) { CM_ERRP("BEGIN TRANSACTION failed.error:%s", errorMsg); } while(pItem) { TExerciseListItem a; if (DoPutItem(pItem, db, a)) { m_lstItem->push_back(a); } pItem = pItem->NextSiblingElement("item"); } if (db != NULL && sqlite3_exec(db, "COMMIT TRANSACTION;", 0,0, &errorMsg) != SQLITE_OK) { CM_ERRP("COMMIT TRANSACTION failed.error:%s", errorMsg); } if(GetItemCount() > 0) result = TResult::ESuccess; else result = TResult::ENothing; } } else { result = TResult::ENetDisconnect; LoadFromDB(); } } } } else { LoadFromDB(); result = TResult::ENetDisconnect; } if(m_pListener) m_pListener->OnUpdateDataFinish(m_UserData, result); }
void CMMyInfo::OnSessionCmd(unsigned int nCmdID, unsigned int nCode, TiXmlDocument* pDoc) { int result = TResult::ENothing; if (SERVICE_PERSONALINFORMATION == nCmdID) { if (nCode == MER_OK) { ASSERT(pDoc); TiXmlElement *pItem = pDoc->RootElement(); INT32 nErr = 0; if (pItem && pItem->QueryIntAttribute("errno", &nErr) == TIXML_SUCCESS) { pItem = pItem->FirstChildElement(); if (nErr == 0 && pItem) { Clear(); m_isFetched = TRUE; sqlite3* db = CheckTable(m_tablename); sqlite3* tagsaliasdb = CheckRootTable(); if(tagsaliasdb) { CreateTagsAlias(tagsaliasdb); } TDummyItem a; DoPutItem(pItem, db, a); GetTagsAliasItem(pItem, tagsaliasdb, a); pItem = pItem->NextSiblingElement(); result = TResult::ESuccess; } } else { CM_ERR("CMMyInfo OnSessionCmd not ok"); } } else if(nCode == MERN_INITIALIZE) { result = TResult::ENetDisconnect; } if (m_pListener) { m_pListener->OnUpdateDataFinish(m_UserData, result); } } else if (SERVICE_UPHEADPHTHO == nCmdID) { if (nCode == MER_OK) { TiXmlElement *pItem = pDoc->RootElement(); int nErr = TResult::EUnknownError; pItem->QueryIntAttribute("errno", &nErr); if (nErr == TResult::ESuccess) { // const CHAR* urlStr = NULL; // // if(!pItem->NoChildren()) // { // // TiXmlElement* psonItem = pItem->FirstChildElement("item"); // // urlStr = psonItem->Attribute("url"); // if(urlStr){ // sHeadimage = urlStr; // } // } result = TResult::ESuccess; } else result = TResult::EUnknownError; } else result = ResultHandle(nCode); m_pListener2->OnRequestFinish(this->m_UserData, result); } }
void TestExceptions() { typedef local_counting_allocator<tbb::tbb_allocator<MyData2> > allocator_t; typedef tbb::concurrent_hash_map<MyKey,MyData2,MyHashCompare,allocator_t> ThrowingTable; enum methods { zero_method = 0, ctor_copy, op_assign, op_insert, all_methods }; REMARK("testing exception-safety guarantees\n"); ThrowingTable src; FillTable( src, 1000 ); ASSERT( MyDataCount==1000, NULL ); try { for(int t = 0; t < 2; t++) // exception type for(int m = zero_method+1; m < all_methods; m++) { allocator_t a; if(t) MyDataCountLimit = 101; else a.set_limits(101); ThrowingTable victim(a); MyDataCount = 0; try { switch(m) { case ctor_copy: { ThrowingTable acopy(src, a); } break; case op_assign: { victim = src; } break; case op_insert: { FillTable( victim, 1000 ); } break; default:; } ASSERT(false, "should throw an exception"); } catch(std::bad_alloc &e) { MyDataCountLimit = 0; size_t size = victim.size(); switch(m) { case op_assign: ASSERT( MyDataCount==100, "data leak?" ); ASSERT( size>=100, NULL ); CheckAllocator(victim, 100+t, t); case ctor_copy: CheckTable(src, 1000); break; case op_insert: ASSERT( size==size_t(100-t), NULL ); ASSERT( MyDataCount==100-t, "data leak?" ); CheckTable(victim, 100-t); CheckAllocator(victim, 100, t); break; default:; // nothing to check here } REMARK("Exception %d: %s\t- ok ()\n", m, e.what()); } catch ( ... ) { ASSERT ( __TBB_EXCEPTION_TYPE_INFO_BROKEN, "Unrecognized exception" ); } } } catch(...) { ASSERT(false, "unexpected exception"); } src.clear(); MyDataCount = 0; }