/home/bes3soft/bes3soft/Boss/7.0.2/dist/7.0.2/Database/DatabaseSvc/DatabaseSvc-00-00-24/src/SqliteInterface.cxx

Go to the documentation of this file.
00001 #include "DatabaseSvc/SqliteInterface.h"
00002 
00003 #include <iostream>
00004 #include <cstring>
00005 
00006 #include <sqlite3.h>
00007 
00008 using namespace std;
00009 /*
00010 static int callback(void *result, int argc, char **argv, char **azColName){
00011   int i;
00012   DatabaseRecordVector* recv = static_cast<DatabaseRecordVector*>(result);
00013   DatabaseRecord dbrec;
00014   for(i=0; i<argc; i++){
00015     //  printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
00016     unsigned long field_len = 1;
00017     char* new_record = new char[];
00018     memcpy(new_record,argv[i],field_len);
00019     dbrec[azColName[i]]=new_record;
00020   }
00021   recv->push_back(dbrec);
00022 
00023   return 0;
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   //std::cout << "Open database " << fname << std::endl;
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) // need to change db
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  // char ***pazResult;
00085  // int pnRow;
00086  // int pnColumn;
00087 
00088   int status = select_db(dbname);
00089   if(status<0)
00090     return -1;
00091 
00092   records.clear();
00093  // status = sqlite3_exec(m_conn, sql.c_str(), callback, &records, &zErrMsg);
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       //loop over columns
00110       int ncolumns = sqlite3_column_count(ppStmt);
00111       int i;
00112       for(i=0; i<ncolumns; i++)
00113       {
00114         // create new record
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    // anything else means that error happened
00146    cerr << "SQLITE query error: " << zErrMsg << endl;   
00147    return -1;
00148   }
00149 
00150   sqlite3_free(zErrMsg);
00151   sqlite3_finalize(ppStmt);
00152 //  if(! m_reuseConnection)
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 }

Generated on Tue Nov 29 22:58:00 2016 for BOSS_7.0.2 by  doxygen 1.4.7