📄 sqlitedataset.cpp
字号:
GB.Error(db->getErrorMsg()); } } // end of for if (db->in_transaction() && autocommit) db->commit_transaction(); active = true; ds_state = dsSelect; refresh(); } // end of try catch(...) { if (db->in_transaction()) db->rollback_transaction(); }}void SqliteDataset::make_insert() { make_query(insert_sql); last();}void SqliteDataset::make_edit() { make_query(update_sql);}void SqliteDataset::make_deletion() { make_query(delete_sql);}void SqliteDataset::fill_fields() { //cout <<"rr "<<result.records.size()<<"|" << frecno <<"\n"; if ((db == NULL) || (result.record_header.size() == 0) || (result.records.size() < (uint)frecno)) return; if (fields_object->size() == 0) // Filling columns name for (uint i = 0; i < result.record_header.size(); i++) { (*fields_object)[i].props = result.record_header[i]; (*edit_object)[i].props = result.record_header[i]; } //Filling result if (result.records.size() != 0) { for (uint i = 0; i < result.records[frecno].size(); i++){ (*fields_object)[i].val = result.records[frecno][i]; (*edit_object)[i].val = result.records[frecno][i]; } } else for (uint i = 0; i < result.record_header.size(); i++){ (*fields_object)[i].val = ""; (*edit_object)[i].val = ""; } }//------------- public functions implementation -----------------//// BM: should retry if error = SQLITE_SCHEMAint SqliteDataset::exec(const string &sql) { int res; int retry; if (!handle()) GB.Error("No Database Connection"); exec_res.record_header.clear(); exec_res.records.clear(); exec_res.conn = handle();//NG //if ((strncmp("select",sql.c_str(),6) == 0) || (strncmp("SELECT",sql.c_str(),6) == 0)) for (retry = 1; retry <= 2; retry++) { res = sqlite_exec(handle(), sql.c_str(), &callback, &exec_res, &errmsg); if (res != SQLITE_SCHEMA) break; } db->setErr(res); //if (res != SQLITE_OK) // GB.Error(db->getErrorMsg()); return (res == SQLITE_OK);}int SqliteDataset::exec() { return exec(sql);}const void* SqliteDataset::getExecRes() { return &exec_res;}bool SqliteDataset::query(const char *query) { int res; int retry; //try{ if (db == NULL) GB.Error("Database is not Defined"); if(dynamic_cast<SqliteDatabase*>(db)->getHandle() == NULL) GB.Error("No Database Connection"); if ((strncasecmp("select",query,6) != 0) /*&& (strncasecmp("PRAGMA table",query,12) !=0) && (strncasecmp("PRAGMA index",query,12) !=0)*/ ) GB.Error("MUST be select SQL or PRAGMA table or index!"); close(); //cout << "Curr size "<<num_rows()<<"\n\n"; result.conn = handle();//NG for (retry = 1; retry <= 2; retry++) { res = sqlite_exec(handle(), query, &callback, &result, &errmsg); if (res != SQLITE_SCHEMA) break; } db->setErr(res); if (res == SQLITE_OK) { active = true; ds_state = dsSelect; //cout << "Curr size2 "<<num_rows()<<"\n"; this->first(); //cout << "Curr size3 "<<num_rows()<<"\n"; //cout << "Curr fcount "<<field_count()<<"\n\n"; } return (res == SQLITE_OK);}bool SqliteDataset::query(const string &q){ return query(q.c_str());}void SqliteDataset::open(const string &sql) { set_select_sql(sql); open();}void SqliteDataset::open() { if (select_sql.size()) { query(select_sql.c_str()); } else { ds_state = dsInactive; }}void SqliteDataset::close() { Dataset::close(); result.record_header.clear(); result.records.clear(); edit_object->clear(); fields_object->clear(); ds_state = dsInactive; active = false;}void SqliteDataset::cancel() { if ((ds_state == dsInsert) || (ds_state==dsEdit)) if (result.record_header.size()) ds_state = dsSelect; else ds_state = dsInactive;}int SqliteDataset::num_rows() { return result.records.size();}bool SqliteDataset::eof() { return feof;}bool SqliteDataset::bof() { return fbof;}void SqliteDataset::first() { Dataset::first(); this->fill_fields(); //cout << "In first "<< fields_object->size()<<"\n";}void SqliteDataset::last() { Dataset::last(); fill_fields();}void SqliteDataset::prev(void) { Dataset::prev(); fill_fields();}void SqliteDataset::next(void) { Dataset::next(); if (!eof()) fill_fields();}//bool SqliteDataset::seek(int pos=0) {bool SqliteDataset::seek(int pos) { if (ds_state == dsSelect) { Dataset::seek(pos); fill_fields(); return true; } return false;}long SqliteDataset::nextid(const char *seq_name) { if (handle()) return db->nextid(seq_name); else return DB_UNEXPECTED_RESULT;}/* Helper function */void SetFieldType( result_set *r, Tables tables){ Tables::iterator it; sqlite_vm *vm; const char *tail; const char **vals; const char **names; int ncol; unsigned int len; // result_set res; char sqlcmd[512]; for ( it = tables.begin(); it != tables.end(); it++ ){ sprintf(sqlcmd,"PRAGMA table_info('%s')", (*it).data()); if ( sqlite_compile(r->conn,sqlcmd,&tail,&vm,NULL) != SQLITE_OK) { return; } while ( sqlite_step(vm, &ncol, &vals, &names) == SQLITE_ROW ){ /* Type is in 2 */ /* field name is 1 */ /* not null indicator is 3 */ /* dflt value is 4 */ for (uint e=0; e<r->record_header.size(); e++){ if ( r->record_header[e].name == vals[1] && r->record_header[e].field_table == (*it).data()) { r->record_header[e].type = GetFieldType((char *)vals[2], &len); r->record_header[e].field_len = len; /* Is not null */ r->record_header[e].notnull = vals[3][0]; } } /* For end */ } /* while end */ sqlite_finalize(vm, NULL); }}/* Return fType and length from String field*/fType GetFieldType( char *Type, unsigned int *length ){ char *upper; char *_left, *_right; fType rType; /* For return */ unsigned int rTypeLen; upper = Type; while (*upper) { *upper = (char) toupper((int)*upper); upper++; } if (strstr(Type, "BLOB") || strstr(Type, "CHAR(") /* note the opening bracket */ || strstr(Type, "CLOB") || strstr(Type, "TEXT") /* also catches TINYTEXT */ || strstr(Type, "VARCHAR") || strstr(Type, "ENUM") || strstr(Type, "SET") || strstr(Type, "YEAR")) { /* MySQL 2 or 4 digit year (string) */ rType = ft_String; } else if (strstr(Type, "CHAR") /* this is a 1-byte value */ || strstr(Type, "TINYINT") || strstr(Type, "INT1") || strstr(Type, "BOOL")) { rType = ft_Boolean; /* Length is for when value is used as a string */ rTypeLen = ft_Boolean_Length; } else if (strstr(Type, "SMALLINT") || strstr(Type, "INT2")) { rType = ft_Short; /* Length is for when value is used as a string */ rTypeLen = ft_Short_Length; } else if (strstr(Type, "MEDIUMINT")) { rType = ft_Short; /* Length is for when value is used as a string */ rTypeLen = ft_Short_Length; } else if (strstr(Type, "BIGINT") || strstr(Type, "INT8")) { rType = ft_LongDouble; /* Length is for when value is used as a string */ rTypeLen = ft_LongDouble_Length; } else if (strstr(Type, "INTEGER") || strstr(Type, "INT") || strstr(Type, "INT4")) { rType = ft_Long; /* Length is for when value is used as a string */ rTypeLen = ft_Long_Length; } else if (strstr(Type, "DECIMAL") || strstr(Type, "NUMERIC")) { rType = ft_Float; /* Length is for when value is used as a string */ rTypeLen = ft_Float_Length; } else if (strstr(Type, "TIMESTAMP") || strstr(Type, "DATETIME")) { rType = ft_Date; rTypeLen = ft_Date_Length; } else if (strstr(Type, "DATE")) { rType = ft_Date; /* Length is for when value is used as a string */ rTypeLen = ft_Date_Length; } else if (strstr(Type, "TIME")) { rType = ft_Date; /* Length is for when value is used as a string */ rTypeLen = ft_Date_Length; } else if (strstr(Type, "DOUBLE")) { rType = ft_Double; /* Length is for when value is used as a string */ rTypeLen = ft_Double_Length; } else if (strstr(Type, "REAL") /* this is PostgreSQL "real", not MySQL "real" which is a synonym of "double" */ || strstr(Type, "FLOAT") || strstr(Type, "FLOAT8") || strstr(Type, "FLOAT4")) { rType = ft_Float; /* Length is for when value is used as a string */ rTypeLen = ft_Float_Length; } else { rType = ft_String; /* most reasonable default */ } if ( rType == ft_String){ /* if a length has been defined it will be between ()*/ _right = rindex(Type, ')'); _left = index(Type, '('); if (_right){ _right = '\0'; rTypeLen = atoi(_left + 1); } else { /* set a default length */ rTypeLen = DEFAULT_STRING_LENGTH; } } if ( length != NULL ) *length = rTypeLen; return rType; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -