00001 #include "DatabaseSvc/SqliteInterface.h"
00002
00003 #include <iostream>
00004 #include <cstring>
00005
00006 #include <sqlite3.h>
00007
00008 using namespace std;
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 SqliteInterface::SqliteInterface(){}
00027
00028 SqliteInterface::~SqliteInterface(){}
00029
00030 int SqliteInterface::connect()
00031 {
00032 return 0;
00033 }
00034
00035 int SqliteInterface::connect(std::string fname)
00036 {
00037 int status = sqlite3_open_v2(fname.c_str(), &m_conn, SQLITE_OPEN_READONLY,0);
00038 if( status ){
00039 cerr << "Can't open database " << fname << ": "
00040 << sqlite3_errmsg(m_conn) << endl;
00041 cerr.flush();
00042 sqlite3_close(m_conn);
00043 return -1;
00044 }
00045
00046
00047
00048 m_isConnected = true;
00049
00050 return 0;
00051 }
00052
00053 int SqliteInterface::select_db(std::string dbname)
00054 {
00055 if(m_isConnected)
00056 {
00057 if(m_dbName != dbname)
00058 disconnect();
00059 }
00060
00061 m_dbName = dbname;
00062 string fname = m_dbPath+"/"+m_dbName+".db";
00063
00064 if(! m_isConnected)
00065 {
00066 int status = connect(fname);
00067 if(status<0)
00068 return -1;
00069 }
00070
00071 return 0;
00072 }
00073
00074 int SqliteInterface::query(std::string dbname, std::string sql)
00075 {
00076 DatabaseRecordVector dummy;
00077 int status = query(dbname, sql, dummy);
00078 return status;
00079 }
00080
00081 int SqliteInterface::query(std::string dbname, std::string sql, DatabaseRecordVector& records)
00082 {
00083 char *zErrMsg = 0;
00084
00085
00086
00087
00088 int status = select_db(dbname);
00089 if(status<0)
00090 return -1;
00091
00092 records.clear();
00093
00094 sqlite3_stmt *ppStmt;
00095 sqlite3_prepare_v2(m_conn, sql.c_str(), -1, &ppStmt, 0);
00096
00097 while ( true )
00098 {
00099 status = sqlite3_step(ppStmt);
00100 if(status == SQLITE_DONE)
00101 {
00102 break;
00103 }
00104
00105 if(status == SQLITE_ROW)
00106 {
00107 DatabaseRecord* dbrec = new DatabaseRecord;
00108
00109
00110 int ncolumns = sqlite3_column_count(ppStmt);
00111 int i;
00112 for(i=0; i<ncolumns; i++)
00113 {
00114
00115 unsigned long field_len = sqlite3_column_bytes(ppStmt,i);
00116 char* new_record;
00117 const char* col_name = sqlite3_column_name(ppStmt,i);
00118 char column_name[255];
00119 strcpy(column_name,col_name);
00120 int type = sqlite3_column_type(ppStmt,i);
00121 if(type == SQLITE_BLOB)
00122 {
00123 new_record = new char[field_len];
00124 char* col_result = (char*)sqlite3_column_blob(ppStmt,i);
00125 memcpy(new_record,col_result,field_len);
00126 }
00127 else if (type != SQLITE_NULL)
00128 {
00129 new_record = new char[field_len+1];
00130 char* col_result = (char*)sqlite3_column_text(ppStmt,i);
00131 strcpy(new_record,col_result);
00132 }
00133 else
00134 {
00135 new_record = NULL;
00136 }
00137 (*dbrec)[column_name] = new_record;
00138 }
00139
00140 records.push_back(dbrec);
00141
00142 continue;
00143 }
00144
00145
00146 cerr << "SQLITE query error: " << zErrMsg << endl;
00147 return -1;
00148 }
00149
00150 sqlite3_free(zErrMsg);
00151 sqlite3_finalize(ppStmt);
00152
00153 disconnect();
00154
00155 return records.size();
00156 }
00157
00158 int SqliteInterface::disconnect()
00159 {
00160 int res = sqlite3_close(m_conn);
00161 if (res)
00162 cerr << "ERROR: Cannot close connection to " << m_dbName << endl;
00163 m_isConnected = false;
00164 return 0;
00165 }