Commit 5545e179 authored by Pavel Vainerman's avatar Pavel Vainerman

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

parent b6f3f03f
......@@ -226,18 +226,8 @@ bool DBServer_SQLite::writeToBase( const string& query )
flushBuffer();
// А теперь собственно запрос..
db->query(query);
// Дело в том что на INSERT И UPDATE запросы
// db->query() может возвращать false и надо самому
// отдельно проверять действительно ли произошла ошибка
// см. SQLiteInterface::query.
string err(db->error());
if( err.empty() )
{
// db->freeResult();
if( db->insert(query) )
return true;
}
return false;
}
......
......@@ -33,6 +33,7 @@ using namespace UniSetTypes;
SQLiteInterface::SQLiteInterface():
db(0),
lastQ(""),
lastE(""),
queryok(false),
connected(false),
opTimeout(300),
......@@ -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 )
{
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);
db = 0;
connected = false;
......@@ -87,7 +95,7 @@ bool SQLiteInterface::insert( const string q )
// Компилируем SQL запрос
if( sqlite3_prepare(db, q.c_str(), -1, &pStmt, NULL) != SQLITE_OK )
{
queryok=false;
queryok = false;
return false;
}
......@@ -96,7 +104,7 @@ bool SQLiteInterface::insert( const string q )
if( !checkResult(rc) && !wait(pStmt, SQLITE_DONE) )
{
sqlite3_finalize(pStmt);
queryok=false;
queryok = false;
return false;
}
......@@ -127,7 +135,7 @@ SQLiteResult SQLiteInterface::query( const string q )
if( !checkResult(rc) && !wait(pStmt, SQLITE_ROW) )
{
sqlite3_finalize(pStmt);
queryok=false;
queryok = false;
return SQLiteResult();
}
......@@ -152,12 +160,12 @@ bool SQLiteInterface::wait( sqlite3_stmt* stmt, int result )
return false;
}
// -----------------------------------------------------------------------------------------
const string SQLiteInterface::error()
string SQLiteInterface::error()
{
if( !db )
return "";
if( db )
lastE = sqlite3_errmsg(db);
return sqlite3_errmsg(db);
return lastE;
}
// -----------------------------------------------------------------------------------------
const string SQLiteInterface::lastQuery()
......@@ -249,3 +257,4 @@ SQLiteResult::SQLiteResult( sqlite3_stmt* s, bool finalize )
if( finalize )
sqlite3_finalize(s);
}
// -----------------------------------------------------------------------------------------
......@@ -40,8 +40,9 @@ class SQLiteInterface
SQLiteInterface();
~SQLiteInterface();
bool connect( const std::string dbfile );
bool connect( const std::string dbfile, bool create = false );
bool close();
bool isConnection();
inline void setOperationTimeout( timeout_t msec ){ opTimeout = msec; }
inline timeout_t getOperationTimeout(){ return opTimeout; }
......@@ -51,13 +52,11 @@ class SQLiteInterface
SQLiteResult query( const std::string q );
const std::string lastQuery();
bool insert( const std::string q );
bool isConnection();
bool insert( const std::string q );
int insert_id();
const std::string error();
std::string error();
protected:
......@@ -70,6 +69,7 @@ class SQLiteInterface
// sqlite3_stmt* curStmt;
std::string lastQ;
std::string lastE;
bool queryok; // успешность текущего запроса
bool connected;
......@@ -94,15 +94,21 @@ class SQLiteResult
inline operator bool(){ return !res.empty(); }
inline int size(){ return res.size(); }
inline bool empty(){ return res.empty(); }
protected:
ROW res;
};
// ----------------------------------------------------------------------------
int num_cols( SQLiteResult::iterator& );
// ROW
int as_int( SQLiteResult::iterator&, int col );
double as_double( SQLiteResult::iterator&, int col );
std::string as_text( SQLiteResult::iterator&, int col );
// ----------------------------------------------------------------------------
// COL
int as_int( SQLiteResult::COL::iterator& );
double as_double( 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);
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,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_
......
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