2 * Copyright (C) 2007 Patrick Ohly
3 * Copyright (C) 2007 Funambol
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #ifndef INCL_SQLITESYNCSOURCE
21 #define INCL_SQLITESYNCSOURCE
26 #include "EvolutionSmartPtr.h"
37 static void unref(sqlite3 *db) { sqlite3_close(db); }
38 static void unref(sqlite3_stmt *stmt) { sqlite3_finalize(stmt); }
41 typedef eptr<sqlite3_stmt, sqlite3_stmt, SQLiteUnref> sqliteptr;
44 * This class implements access to SQLite database files:
45 * - opening the database file
47 * - creating a database file
48 * - converting to and from a VObject via a simple property<->column name mapping
53 /** information about the database mapping */
55 const char *colname; /**< column name in SQL table */
56 const char *tablename; /**< name of the SQL table which has this column */
57 const char *propname; /**< optional: vcard/vcalendar property which corresponds to this */
58 int colindex; /**< determined dynamically in open(): index of the column, -1 if not present */
61 const Mapping &getMapping(int i) { return m_mapping[i]; }
64 * @param name a name for the data source, used for error messages
65 * @param fileid a descriptor which identifies the file to be opened:
66 * currently valid syntax is file:// followed by path
67 * @param mapping array with database mapping, terminated by NULL colname
68 * @param schema database schema to use when creating new databases, may be NULL
70 void open(const string &name,
72 const Mapping *mapping,
78 * throw error for a specific sqlite3 operation on m_db
79 * @param operation a description of the operation which failed
81 void throwError(const string &operation);
84 * wrapper around sqlite3_prepare() which operates on the current
85 * database and throws an error if the call fails
87 * @param sqlfmt printf-style format string for query, followed by parameters for sprintf
89 sqlite3_stmt *prepareSQL(const char *sqlfmt, ...);
92 * wrapper around sqlite3_prepare() which operates on the current
93 * database and throws an error if the call fails
95 * @param sql preformatted SQL statement(s)
96 * @param nextsql pointer to next statement in sql
98 sqlite3_stmt *prepareSQLWrapper(const char *sql, const char **nextsql = NULL);
101 /** checks the result of an sqlite3 call, throws an error if faulty, otherwise returns the result */
102 int checkSQL(int res, const char *operation = "SQLite call") {
103 if (res != SQLITE_OK && res != SQLITE_ROW && res != SQLITE_DONE) {
104 throwError(operation);
109 /** type used for row keys */
110 typedef long long key_t;
111 string toString(key_t key) { char buffer[32]; sprintf(buffer, "%lld", key); return buffer; }
112 #define SQLITE3_COLUMN_KEY sqlite3_column_int64
114 /** return row ID for a certain row */
115 key_t findKey(const char *database, const char *keyname, const char *key);
117 /** return a specific column for a row identified by a certain key column as text, returns default text if not found */
118 string findColumn(const char *database, const char *keyname, const char *key, const char *column, const char *def);
120 /** a wrapper for sqlite3_column_test() which will check for NULL and returns default text instead */
121 string getTextColumn(sqlite3_stmt *stmt, int col, const char *def = "");
123 typedef unsigned long syncml_time_t;
124 /** transform column to same time base as used by SyncML libary (typically time()) */
125 syncml_time_t getTimeColumn(sqlite3_stmt *stmt, int col);
127 /** convert time to string */
128 static string time2str(syncml_time_t t);
130 /** copies all columns which directly map to a property into the vobj */
131 void rowToVObject(sqlite3_stmt *stmt, vocl::VObject &vobj);
134 * Creates a SQL INSERT INTO <tablename> ( <cols> ) VALUES ( <values> )
135 * statement and binds all rows/values that map directly from the vobj.
137 * @param numparams number of ? placeholders in values; the caller has
138 * to bind those before executing the statement
140 sqlite3_stmt *vObjectToRow(vocl::VObject &vobj,
141 const string &tablename,
144 const string &values);
147 /* copy of open() parameters */
148 arrayptr<Mapping> m_mapping;
152 /** current database */
153 eptr<sqlite3, sqlite3, SQLiteUnref> m_db;
156 #endif // ENABLE_SQLITE
157 #endif // INCL_SQLITESYNCSOURCE