Commit 5b23e1a6 authored by Pavel Vainerman's avatar Pavel Vainerman

(SQLite): немного подправил интерфейс..

parent 88e9e2d3
...@@ -226,19 +226,9 @@ bool DBServer_SQLite::writeToBase( const string& query ) ...@@ -226,19 +226,9 @@ bool DBServer_SQLite::writeToBase( const string& query )
flushBuffer(); flushBuffer();
// А теперь собственно запрос.. // А теперь собственно запрос..
db->query(query); if( db->insert(query) )
// Дело в том что на INSERT И UPDATE запросы
// db->query() может возвращать false и надо самому
// отдельно проверять действительно ли произошла ошибка
// см. SQLiteInterface::query.
string err(db->error());
if( err.empty() )
{
// db->freeResult();
return true; return true;
}
return false; return false;
} }
//-------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------
......
...@@ -33,6 +33,7 @@ using namespace UniSetTypes; ...@@ -33,6 +33,7 @@ using namespace UniSetTypes;
SQLiteInterface::SQLiteInterface(): SQLiteInterface::SQLiteInterface():
db(0), db(0),
lastQ(""), lastQ(""),
lastE(""),
queryok(false), queryok(false),
connected(false), connected(false),
opTimeout(300), opTimeout(300),
...@@ -48,13 +49,20 @@ SQLiteInterface::~SQLiteInterface() ...@@ -48,13 +49,20 @@ SQLiteInterface::~SQLiteInterface()
} }
// ----------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------
bool SQLiteInterface::connect( const string dbfile ) bool SQLiteInterface::connect( const string dbfile, bool create )
{ {
int rc = sqlite3_open(dbfile.c_str(), &db); // т.к. sqlite3 по умолчанию, создаёт файл при открытии, то проверим "сами"
// if( !create && !UniSetTypes::file_exist(dbfile) )
// return false;
int flags = create ? 0 : SQLITE_OPEN_READWRITE;
int rc = sqlite3_open_v2(dbfile.c_str(), &db, flags, NULL);
if( rc != SQLITE_OK ) if( rc != SQLITE_OK )
{ {
cerr << "SQLiteInterface::connect): rc=" << rc << " error: " << sqlite3_errmsg(db) << endl; // cerr << "SQLiteInterface::connect): rc=" << rc << " error: " << sqlite3_errmsg(db) << endl;
lastE = "open '" + dbfile + "' error: " + string(sqlite3_errmsg(db));
sqlite3_close(db); sqlite3_close(db);
db = 0; db = 0;
connected = false; connected = false;
...@@ -87,7 +95,7 @@ bool SQLiteInterface::insert( const string q ) ...@@ -87,7 +95,7 @@ bool SQLiteInterface::insert( const string q )
// Компилируем SQL запрос // Компилируем SQL запрос
if( sqlite3_prepare(db, q.c_str(), -1, &pStmt, NULL) != SQLITE_OK ) if( sqlite3_prepare(db, q.c_str(), -1, &pStmt, NULL) != SQLITE_OK )
{ {
queryok=false; queryok = false;
return false; return false;
} }
...@@ -96,7 +104,7 @@ bool SQLiteInterface::insert( const string q ) ...@@ -96,7 +104,7 @@ bool SQLiteInterface::insert( const string q )
if( !checkResult(rc) && !wait(pStmt, SQLITE_DONE) ) if( !checkResult(rc) && !wait(pStmt, SQLITE_DONE) )
{ {
sqlite3_finalize(pStmt); sqlite3_finalize(pStmt);
queryok=false; queryok = false;
return false; return false;
} }
...@@ -127,7 +135,7 @@ SQLiteResult SQLiteInterface::query( const string q ) ...@@ -127,7 +135,7 @@ SQLiteResult SQLiteInterface::query( const string q )
if( !checkResult(rc) && !wait(pStmt, SQLITE_ROW) ) if( !checkResult(rc) && !wait(pStmt, SQLITE_ROW) )
{ {
sqlite3_finalize(pStmt); sqlite3_finalize(pStmt);
queryok=false; queryok = false;
return SQLiteResult(); return SQLiteResult();
} }
...@@ -152,12 +160,12 @@ bool SQLiteInterface::wait( sqlite3_stmt* stmt, int result ) ...@@ -152,12 +160,12 @@ bool SQLiteInterface::wait( sqlite3_stmt* stmt, int result )
return false; return false;
} }
// ----------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------
const string SQLiteInterface::error() string SQLiteInterface::error()
{ {
if( !db ) if( db )
return ""; lastE = sqlite3_errmsg(db);
return sqlite3_errmsg(db); return lastE;
} }
// ----------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------
const string SQLiteInterface::lastQuery() const string SQLiteInterface::lastQuery()
...@@ -249,3 +257,4 @@ SQLiteResult::SQLiteResult( sqlite3_stmt* s, bool finalize ) ...@@ -249,3 +257,4 @@ SQLiteResult::SQLiteResult( sqlite3_stmt* s, bool finalize )
if( finalize ) if( finalize )
sqlite3_finalize(s); sqlite3_finalize(s);
} }
// -----------------------------------------------------------------------------------------
...@@ -40,8 +40,9 @@ class SQLiteInterface ...@@ -40,8 +40,9 @@ class SQLiteInterface
SQLiteInterface(); SQLiteInterface();
~SQLiteInterface(); ~SQLiteInterface();
bool connect( const std::string dbfile ); bool connect( const std::string dbfile, bool create = false );
bool close(); bool close();
bool isConnection();
inline void setOperationTimeout( timeout_t msec ){ opTimeout = msec; } inline void setOperationTimeout( timeout_t msec ){ opTimeout = msec; }
inline timeout_t getOperationTimeout(){ return opTimeout; } inline timeout_t getOperationTimeout(){ return opTimeout; }
...@@ -51,13 +52,11 @@ class SQLiteInterface ...@@ -51,13 +52,11 @@ class SQLiteInterface
SQLiteResult query( const std::string q ); SQLiteResult query( const std::string q );
const std::string lastQuery(); const std::string lastQuery();
bool insert( const std::string q ); bool insert( const std::string q );
bool isConnection();
int insert_id(); int insert_id();
const std::string error(); std::string error();
protected: protected:
...@@ -70,6 +69,7 @@ class SQLiteInterface ...@@ -70,6 +69,7 @@ class SQLiteInterface
// sqlite3_stmt* curStmt; // sqlite3_stmt* curStmt;
std::string lastQ; std::string lastQ;
std::string lastE;
bool queryok; // успешность текущего запроса bool queryok; // успешность текущего запроса
bool connected; bool connected;
...@@ -94,15 +94,21 @@ class SQLiteResult ...@@ -94,15 +94,21 @@ class SQLiteResult
inline operator bool(){ return !res.empty(); } inline operator bool(){ return !res.empty(); }
inline int size(){ return res.size(); }
inline bool empty(){ return res.empty(); }
protected: protected:
ROW res; ROW res;
}; };
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
int num_cols( SQLiteResult::iterator& ); int num_cols( SQLiteResult::iterator& );
// ROW
int as_int( SQLiteResult::iterator&, int col ); int as_int( SQLiteResult::iterator&, int col );
double as_double( SQLiteResult::iterator&, int col ); double as_double( SQLiteResult::iterator&, int col );
std::string as_text( SQLiteResult::iterator&, int col ); std::string as_text( SQLiteResult::iterator&, int col );
// ----------------------------------------------------------------------------
// COL
int as_int( SQLiteResult::COL::iterator& ); int as_int( SQLiteResult::COL::iterator& );
double as_double( SQLiteResult::COL::iterator& ); double as_double( SQLiteResult::COL::iterator& );
std::string as_string( SQLiteResult::COL::iterator& ); std::string as_string( SQLiteResult::COL::iterator& );
......
...@@ -21,6 +21,10 @@ INSERT INTO main_history VALUES(NULL,0,0,0,100,20.3,1,0); ...@@ -21,6 +21,10 @@ INSERT INTO main_history VALUES(NULL,0,0,0,100,20.3,1,0);
INSERT INTO main_history VALUES(NULL,0,0,0,101,20.65,1,0); INSERT INTO main_history VALUES(NULL,0,0,0,101,20.65,1,0);
INSERT INTO main_history VALUES(NULL,0,0,0,102,20.7,1,0); INSERT INTO main_history VALUES(NULL,0,0,0,102,20.7,1,0);
INSERT INTO main_history VALUES(NULL,0,0,0,103,20.1,1,0); INSERT INTO main_history VALUES(NULL,0,0,0,103,20.1,1,0);
INSERT INTO main_history VALUES(NULL,0,0,0,105,20.3,1,0);
INSERT INTO main_history VALUES(NULL,0,0,0,106,20.65,1,0);
INSERT INTO main_history VALUES(NULL,0,0,0,107,20233.7,1,0);
INSERT INTO main_history VALUES(NULL,0,0,0,108,245560.67671,1,0);
_EOF_ _EOF_
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment