067d946b7bdbf4fd3a21e606498303aafbfb0bbc
[platform/upstream/syncevolution.git] / src / backends / sqlite / SQLiteUtil.h
1 /*
2  * Copyright (C) 2007 Patrick Ohly
3  * Copyright (C) 2007 Funambol
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #ifndef INCL_SQLITESYNCSOURCE
21 #define INCL_SQLITESYNCSOURCE
22
23 #ifdef ENABLE_SQLITE
24
25 #include <sqlite3.h>
26 #include "EvolutionSmartPtr.h"
27
28 #include <string>
29 using namespace std;
30
31 namespace vocl {
32     class VObject;
33 }
34
35 class SQLiteUnref {
36  public:
37     static void unref(sqlite3 *db) { sqlite3_close(db); }
38     static void unref(sqlite3_stmt *stmt) { sqlite3_finalize(stmt); }
39 };
40
41 typedef eptr<sqlite3_stmt, sqlite3_stmt, SQLiteUnref> sqliteptr;
42
43 /**
44  * This class implements access to SQLite database files:
45  * - opening the database file
46  * - error reporting
47  * - creating a database file
48  * - converting to and from a VObject via a simple property<->column name mapping
49  */
50 class SQLiteUtil
51 {
52   public:
53     /** information about the database mapping */
54     struct 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 */
59     };
60
61     const Mapping &getMapping(int i) { return m_mapping[i]; }
62
63     /**
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
69      */
70     void open(const string &name,
71               const string &fileid,
72               const Mapping *mapping,
73               const char *schema);
74
75     void close();
76
77     /**
78      * throw error for a specific sqlite3 operation on m_db
79      * @param operation   a description of the operation which failed
80      */
81     void throwError(const string &operation);
82
83     /**
84      * wrapper around sqlite3_prepare() which operates on the current
85      * database and throws an error if the call fails
86      *
87      * @param sqlfmt         printf-style format string for query, followed by parameters for sprintf
88      */
89     sqlite3_stmt *prepareSQL(const char *sqlfmt, ...);
90
91     /**
92      * wrapper around sqlite3_prepare() which operates on the current
93      * database and throws an error if the call fails
94      *
95      * @param sql       preformatted SQL statement(s)
96      * @param nextsql   pointer to next statement in sql
97      */
98     sqlite3_stmt *prepareSQLWrapper(const char *sql, const char **nextsql = NULL);
99
100
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);
105         }
106         return res;
107     }
108
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
113
114     /** return row ID for a certain row */
115     key_t findKey(const char *database, const char *keyname, const char *key);
116
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);
119
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 = "");
122
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);
126
127     /** convert time to string */
128     static string time2str(syncml_time_t t);
129
130     /** copies all columns which directly map to a property into the vobj */
131     void rowToVObject(sqlite3_stmt *stmt, vocl::VObject &vobj);
132
133     /**
134      * Creates a SQL INSERT INTO <tablename> ( <cols> ) VALUES ( <values> )
135      * statement and binds all rows/values that map directly from the vobj.
136      *
137      * @param numparams      number of ? placeholders in values; the caller has
138      *                       to bind those before executing the statement
139      */
140     sqlite3_stmt *vObjectToRow(vocl::VObject &vobj,
141                                const string &tablename,
142                                int numparams,
143                                const string &cols,
144                                const string &values);
145
146  private:
147     /* copy of open() parameters */
148     arrayptr<Mapping> m_mapping;
149     string m_name;
150     string m_fileid;
151
152     /** current database */
153     eptr<sqlite3, sqlite3, SQLiteUnref> m_db;
154 };
155
156 #endif // ENABLE_SQLITE
157 #endif // INCL_SQLITESYNCSOURCE