00001 00002 #ifndef __BN_SQLITE 00003 #define __BN_SQLITE 00004 00005 #include "sqlite3.h" 00006 00007 #include "../../Interfaces/BufferHolder.h" 00008 #include "../../Interfaces/DcAdapter.h" 00009 00010 #include <string> 00011 #include <iostream> 00012 #include <vector> 00013 #include <map> 00014 using namespace std; 00015 00016 typedef vector<string> t_query_fields; 00017 typedef vector<t_query_fields> t_query; 00018 typedef t_query::iterator t_query_itr; 00019 typedef map<string, int> t_fields_names; 00020 typedef t_fields_names::iterator t_fields_names_itr; 00021 00022 /** 00023 * This class is a thin wrapper for the SQLite database. It uses mostly the 00024 * <b>sqlite3_exec</b> function to execute SQL commands. When executing a SELECT 00025 * command, it saves the values, and the user can access this data. 00026 * <p> 00027 * <b>Read</b>: Reads the value of a filed in the current record. <b>address</b> 00028 * can be a field name or a field number. 00029 * if address is <b>_LastError</b> it returns a string of the last operation 00030 * error (or "OK"). 00031 * if address is <b>_EOF</b> it returns "True" if the current record is in the 00032 * end of the records set, or "False".<br> 00033 * <b>Write</b>: <b>address</b> can be <b>MoveFirst</b>, <b>MoveNext</b>, 00034 * <b>Clear</b> or <b>Exec</b>. Clear clears the previously saved data. Exec 00035 * executes the buffer <b>value</b>. 00036 * <P> 00037 * Note: You don't have to call Clear before running a new SELECT command. 00038 * But if you don't, and the new SELECT has different fields then the previous 00039 * one, you will be able to access the new data just by the field numbers. The 00040 * fields names will still be of the first SELECT statement. Every SELECT adds 00041 * the data to the end of the record set. 00042 * <p> 00043 * Configuration:<br> 00044 * <b>DB</b> - path and file name of the database. If this file does not exist, 00045 * SQLite creates a new database. 00046 */ 00047 class BnSQLite : public IdcAdapter 00048 { 00049 private: 00050 /** Pointer to a function of Beesnest to get parameters from the 00051 configuration files. */ 00052 bool (*pfGetParam)(const char*, char*, int); 00053 00054 /** Stores the name of this adapter object. */ 00055 string sName; 00056 /** Pointer to the database. */ 00057 sqlite3 *db; 00058 /** Flag for an initialize error */ 00059 bool isInitErr; 00060 /** Last operation error code (0 = OK) */ 00061 int nLastErrCode; 00062 /** Pointer to a "C" string of error message SQLLite allocates. */ 00063 char* csErrMsg; 00064 /** Holds the string this class returns to the user. */ 00065 string sRetMsg; 00066 00067 /** Vector that hold the record set returned from select command. */ 00068 t_query vLastQuery; 00069 /** Iterator to the record set vector. */ 00070 t_query_itr vLastQueryItr; 00071 /** Map of field names to field index. */ 00072 t_fields_names mLastQueryNames; 00073 00074 public: 00075 BnSQLite (const char* name, bool (*pGetParam)(const char*, char*, int)); 00076 00077 ~BnSQLite (); 00078 00079 virtual BufferHolder Read (BufferHolder address) throw (BufferHolder); 00080 00081 virtual bool Write (BufferHolder address, BufferHolder value) throw (BufferHolder); 00082 00083 virtual bool IsMultiUse () 00084 { return true; } 00085 00086 /** This function saves the query data to the record set vector. */ 00087 friend int callback(void* p_this, int argc, char** argv, char** azColName); 00088 }; 00089 00090 #endif /* __BN_SQLITE */ 00091