Commit 74f0786a authored by Pavel Vainerman's avatar Pavel Vainerman

(SQLite): работа над SQLiteInterface-ом

parent 1e332ce8
...@@ -14,6 +14,10 @@ bin_PROGRAMS = uniset-sqlite-dbserver ...@@ -14,6 +14,10 @@ bin_PROGRAMS = uniset-sqlite-dbserver
uniset_sqlite_dbserver_LDADD = libUniSet-sqlite.la $(top_builddir)/lib/libUniSet.la uniset_sqlite_dbserver_LDADD = libUniSet-sqlite.la $(top_builddir)/lib/libUniSet.la
uniset_sqlite_dbserver_SOURCES = main.cc uniset_sqlite_dbserver_SOURCES = main.cc
noinst_PROGRAMS = sqlite-test
sqlite_test_LDADD = libUniSet-sqlite.la $(top_builddir)/lib/libUniSet.la
sqlite_test_SOURCES = test.cc
include $(top_builddir)/conf/setting.mk include $(top_builddir)/conf/setting.mk
# install # install
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
*/ */
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
#include <sstream> #include <sstream>
#include <cstdio>
#include "UniSetTypes.h" #include "UniSetTypes.h"
#include "SQLiteInterface.h" #include "SQLiteInterface.h"
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
...@@ -31,6 +32,7 @@ using namespace UniSetTypes; ...@@ -31,6 +32,7 @@ using namespace UniSetTypes;
SQLiteInterface::SQLiteInterface(): SQLiteInterface::SQLiteInterface():
db(0), db(0),
curStmt(0),
lastQ(""), lastQ(""),
queryok(false), queryok(false),
connected(false), connected(false),
...@@ -42,40 +44,35 @@ opCheckPause(50) ...@@ -42,40 +44,35 @@ opCheckPause(50)
SQLiteInterface::~SQLiteInterface() SQLiteInterface::~SQLiteInterface()
{ {
close(); close();
delete db; if( db )
delete db;
} }
// ----------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------
bool SQLiteInterface::connect( const string dbfile ) bool SQLiteInterface::connect( const string dbfile )
{ {
int rc = sqlite3_open(dbfile.c_str(), &db); int rc = sqlite3_open(dbfile.c_str(), &db);
if( !rc )
{
cerr << sqlite3_errmsg(db) << endl;
sqlite3_close(db);
db = 0;
connected = false;
return false;
}
if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED || rc==SQLITE_INTERRUPT || rc==SQLITE_IOERR ) if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED || rc==SQLITE_INTERRUPT || rc==SQLITE_IOERR )
{ {
cerr << sqlite3_errmsg(db) << endl; cerr << "SQLiteInterface::connect): rc=" << rc << " error: " << sqlite3_errmsg(db) << endl;
sqlite3_close(db); sqlite3_close(db);
db = 0; db = 0;
connected = false; connected = false;
return false; return false;
} }
connected = true; connected = true;
return true; return true;
} }
// ----------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------
bool SQLiteInterface::close() bool SQLiteInterface::close()
{ {
if(db) if( db )
{
sqlite3_close(db); sqlite3_close(db);
db = 0;
}
return true; return true;
} }
...@@ -129,7 +126,7 @@ bool SQLiteInterface::query( const string q ) ...@@ -129,7 +126,7 @@ bool SQLiteInterface::query( const string q )
} }
lastQ = q; lastQ = q;
curStmt = pStmt;
// int cnum = sqlite3_column_count(pStmt); // int cnum = sqlite3_column_count(pStmt);
/* /*
...@@ -143,7 +140,7 @@ bool SQLiteInterface::query( const string q ) ...@@ -143,7 +140,7 @@ bool SQLiteInterface::query( const string q )
} }
*/ */
sqlite3_finalize(pStmt); // sqlite3_finalize(pStmt);
queryok=true; queryok=true;
return true; return true;
} }
...@@ -177,6 +174,12 @@ const string SQLiteInterface::lastQuery() ...@@ -177,6 +174,12 @@ const string SQLiteInterface::lastQuery()
return lastQ; return lastQ;
} }
// ----------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------
void SQLiteInterface::freeResult()
{
sqlite3_finalize(curStmt);
curStmt = 0;
}
// -----------------------------------------------------------------------------------------
int SQLiteInterface::insert_id() int SQLiteInterface::insert_id()
{ {
if( !db ) if( !db )
...@@ -190,3 +193,75 @@ bool SQLiteInterface::isConnection() ...@@ -190,3 +193,75 @@ bool SQLiteInterface::isConnection()
return connected; return connected;
} }
// ----------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------
SQLiteInterface::iterator SQLiteInterface::begin()
{
return SQLiteIterator(curStmt);
}
// -----------------------------------------------------------------------------------------
SQLiteInterface::iterator SQLiteInterface::end()
{
return SQLiteIterator();
}
// -----------------------------------------------------------------------------------------
SQLiteInterface::SQLiteIterator SQLiteInterface::SQLiteIterator::operator ++(int c)
{
if( row == -1 || c<0 )
return *this;
if( c==0 )
c = 1;
for( int i=0; i<c; i++ )
{
int rc = sqlite3_step(stmt);
// cerr << "**** ++: rc=" << rc << " err: " << sqlite3_errmsg( sqlite3_db_handle(stmt) ) << endl;
if( rc != SQLITE_ROW )
{
row = -1;
break;
}
row++;
}
return *this;
}
// -----------------------------------------------------------------------------------------
#if 0
SQLiteInterface::SQLiteIterator SQLiteInterface::SQLiteIterator::operator --()
{
}
#endif
// -----------------------------------------------------------------------------------------
std::string SQLiteInterface::SQLiteIterator::get_text( int col )
{
return string( (char*)sqlite3_column_text(stmt,col) );
}
// -----------------------------------------------------------------------------------------
int SQLiteInterface::SQLiteIterator::get_int( int col )
{
return sqlite3_column_int(stmt,col);
}
// -----------------------------------------------------------------------------------------
double SQLiteInterface::SQLiteIterator::get_double( int col )
{
return sqlite3_column_double(stmt,col);
}
// -----------------------------------------------------------------------------------------
int SQLiteInterface::SQLiteIterator::get_num_cols()
{
return sqlite3_data_count(stmt);
}
// -----------------------------------------------------------------------------------------
bool SQLiteInterface::SQLiteIterator::is_end()
{
return ( row == -1 );
}
// -----------------------------------------------------------------------------------------
void SQLiteInterface::SQLiteIterator::free_result()
{
sqlite3_finalize(stmt);
stmt = 0;
}
// -----------------------------------------------------------------------------------------
...@@ -55,6 +55,53 @@ class SQLiteInterface ...@@ -55,6 +55,53 @@ class SQLiteInterface
const std::string error(); const std::string error();
void freeResult();
class SQLiteIterator
{
public:
SQLiteIterator( sqlite3_stmt* s ):stmt(s),row(0){}
SQLiteIterator():stmt(0),row(-1){}
~SQLiteIterator(){};
SQLiteIterator operator ++(int);
// SQLiteIterator operator --();
inline bool operator==(const SQLiteIterator& other) const
{
if( row == -1 && other.stmt==0 && other.row == -1 )
return true;
return ( stmt == other.stmt && row == other.row );
}
inline bool operator!=(const SQLiteIterator& other) const
{
return !operator==(other);
}
inline int row_num(){ return row; }
std::string get_text( int col );
int get_int( int col );
double get_double( int col );
int get_num_cols();
void free_result();
bool is_end();
protected:
sqlite3_stmt* stmt;
int row;
};
typedef SQLiteIterator iterator;
iterator begin();
iterator end();
protected: protected:
bool wait( sqlite3_stmt* stmt, int result ); bool wait( sqlite3_stmt* stmt, int result );
...@@ -62,6 +109,7 @@ class SQLiteInterface ...@@ -62,6 +109,7 @@ class SQLiteInterface
private: private:
sqlite3* db; sqlite3* db;
sqlite3_stmt* curStmt;
std::string lastQ; std::string lastQ;
bool queryok; // успешность текущего запроса bool queryok; // успешность текущего запроса
......
#!/bin/sh
dbname=test.db
[ -n "$1" ] && dbname="$1"
sqlite3 $dbname <<"_EOF_"
DROP TABLE IF EXISTS main_history;
CREATE TABLE main_history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
date date NOT NULL,
time time NOT NULL,
time_usec INTEGER NOT NULL,
sensor_id INTEGER NOT NULL,
value DOUBLE NOT NULL,
node INTEGER NOT NULL,
confirm INTEGER DEFAULT NULL
);
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);
_EOF_
# KEY main_history_sensor_id (sensor_id)
#include <iostream>
#include <sstream>
#include "Exceptions.h"
#include "SQLiteInterface.h"
// --------------------------------------------------------------------------
using namespace UniSetTypes;
using namespace std;
// --------------------------------------------------------------------------
int main(int argc, char** argv)
{
try
{
SQLiteInterface db;
if( !db.connect("test.db") )
{
cerr << "db connect error: " << db.error() << endl;
return 1;
}
stringstream q;
q << "SELECT * from main_history";
if( !db.query(q.str()) )
{
cerr << "db connect error: " << db.error() << endl;
return 1;
}
SQLiteInterface::iterator it = db.begin();
for( ; it!=db.end(); it++ )
{
cout << "get result: row=" << it.row_num() << " coln=" << it.get_num_cols() << endl;
for( int i=0; i<it.get_num_cols(); i++ )
cout << it.get_text(i) << "(" << it.get_double(i) << ") | ";
cout << endl;
}
db.freeResult();
db.close();
}
catch(Exception& ex)
{
cerr << "(test): " << ex << endl;
}
catch(...)
{
cerr << "(test): catch ..." << endl;
}
return 0;
}
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