00001 /* 00002 * Copyright (C) 2007-2008 Patrick Ohly 00003 * 00004 * This program is free software; you can redistribute it and/or modify 00005 * it under the terms of the GNU General Public License as published by 00006 * the Free Software Foundation; either version 2 of the License, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 */ 00018 00019 #ifndef INCL_SQLITESYNCSOURCE 00020 #define INCL_SQLITESYNCSOURCE 00021 00022 #ifdef ENABLE_SQLITE 00023 00024 #include <sqlite3.h> 00025 #include "EvolutionSmartPtr.h" 00026 00027 #include <string> 00028 using namespace std; 00029 00030 namespace vocl { 00031 class VObject; 00032 } 00033 00034 class SQLiteUnref { 00035 public: 00036 static void unref(sqlite3 *db) { sqlite3_close(db); } 00037 static void unref(sqlite3_stmt *stmt) { sqlite3_finalize(stmt); } 00038 }; 00039 00040 typedef eptr<sqlite3_stmt, sqlite3_stmt, SQLiteUnref> sqliteptr; 00041 00042 /** 00043 * This class implements access to SQLite database files: 00044 * - opening the database file 00045 * - error reporting 00046 * - creating a database file 00047 * - converting to and from a VObject via a simple property<->column name mapping 00048 */ 00049 class SQLiteUtil 00050 { 00051 public: 00052 /** information about the database mapping */ 00053 struct Mapping { 00054 const char *colname; /**< column name in SQL table */ 00055 const char *tablename; /**< name of the SQL table which has this column */ 00056 const char *propname; /**< optional: vcard/vcalendar property which corresponds to this */ 00057 int colindex; /**< determined dynamically in open(): index of the column, -1 if not present */ 00058 }; 00059 00060 const Mapping &getMapping(int i) { return m_mapping[i]; } 00061 00062 /** 00063 * @param name a name for the data source, used for error messages 00064 * @param fileid a descriptor which identifies the file to be opened: 00065 * currently valid syntax is file:// followed by path 00066 * @param mapping array with database mapping, terminated by NULL colname 00067 * @param schema database schema to use when creating new databases, may be NULL 00068 */ 00069 void open(const string &name, 00070 const string &fileid, 00071 const Mapping *mapping, 00072 const char *schema); 00073 00074 void close(); 00075 00076 /** 00077 * throw error for a specific sqlite3 operation on m_db 00078 * @param operation a description of the operation which failed 00079 */ 00080 void throwError(const string &operation); 00081 00082 /** 00083 * wrapper around sqlite3_prepare() which operates on the current 00084 * database and throws an error if the call fails 00085 * 00086 * @param sqlfmt printf-style format string for query, followed by parameters for sprintf 00087 */ 00088 sqlite3_stmt *prepareSQL(const char *sqlfmt, ...); 00089 00090 /** 00091 * wrapper around sqlite3_prepare() which operates on the current 00092 * database and throws an error if the call fails 00093 * 00094 * @param sql preformatted SQL statement(s) 00095 * @param nextsql pointer to next statement in sql 00096 */ 00097 sqlite3_stmt *prepareSQLWrapper(const char *sql, const char **nextsql = NULL); 00098 00099 00100 /** checks the result of an sqlite3 call, throws an error if faulty, otherwise returns the result */ 00101 int checkSQL(int res, const char *operation = "SQLite call") { 00102 if (res != SQLITE_OK && res != SQLITE_ROW && res != SQLITE_DONE) { 00103 throwError(operation); 00104 } 00105 return res; 00106 } 00107 00108 /** type used for row keys */ 00109 typedef long long key_t; 00110 string toString(key_t key) { char buffer[32]; sprintf(buffer, "%lld", key); return buffer; } 00111 #define SQLITE3_COLUMN_KEY sqlite3_column_int64 00112 00113 /** return row ID for a certain row */ 00114 key_t findKey(const char *database, const char *keyname, const char *key); 00115 00116 /** return a specific column for a row identified by a certain key column as text, returns default text if not found */ 00117 string findColumn(const char *database, const char *keyname, const char *key, const char *column, const char *def); 00118 00119 /** a wrapper for sqlite3_column_test() which will check for NULL and returns default text instead */ 00120 string getTextColumn(sqlite3_stmt *stmt, int col, const char *def = ""); 00121 00122 typedef unsigned long syncml_time_t; 00123 /** transform column to same time base as used by SyncML libary (typically time()) */ 00124 syncml_time_t getTimeColumn(sqlite3_stmt *stmt, int col); 00125 00126 /** convert time to string */ 00127 static string time2str(syncml_time_t t); 00128 00129 /** copies all columns which directly map to a property into the vobj */ 00130 void rowToVObject(sqlite3_stmt *stmt, vocl::VObject &vobj); 00131 00132 /** 00133 * Creates a SQL INSERT INTO <tablename> ( <cols> ) VALUES ( <values> ) 00134 * statement and binds all rows/values that map directly from the vobj. 00135 * 00136 * @param numparams number of ? placeholders in values; the caller has 00137 * to bind those before executing the statement 00138 */ 00139 sqlite3_stmt *vObjectToRow(vocl::VObject &vobj, 00140 const string &tablename, 00141 int numparams, 00142 const string &cols, 00143 const string &values); 00144 00145 private: 00146 /* copy of open() parameters */ 00147 arrayptr<Mapping> m_mapping; 00148 string m_name; 00149 string m_fileid; 00150 00151 /** current database */ 00152 eptr<sqlite3, sqlite3, SQLiteUnref> m_db; 00153 }; 00154 00155 #endif // ENABLE_SQLITE 00156 #endif // INCL_SQLITESYNCSOURCE