Main Page | Modules | Namespace List | Class Hierarchy | Data Structures | Directories | File List | Namespace Members | Data Fields | Globals

pm_sql.h

Go to the documentation of this file.
00001 /********************************************************************
00002 
00003 Copyright 2006, ACCESS Systems Americas, Inc. All rights reserved.
00004 
00005 The contents of this file are subject to the Mozilla Public License Version
00006 1.1 (the "License"); you may not use this file except in compliance with
00007 the License. You may obtain a copy of the License at
00008 http://www.mozilla.org/MPL/
00009 
00010 Software distributed under the License is distributed on an "AS IS" basis,
00011 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012 for the specific language governing rights and limitations under the
00013 License.
00014 
00015 The Original Code is the entire contents of this file.
00016 
00017 The Initial Developer of the Original Code is ACCESS Systems Americas, Inc.
00018 
00019 Portions created by ACCESS Systems Americas, Inc. are Copyright © 2006. All
00020 Rights Reserved.
00021 
00022 Contributor(s): none.
00023 
00024 ********************************************************************/
00025 #define _GNU_SOURCE // for strndup from string.h
00026 
00027 #include <sqlite3.h>
00028 
00029 /* Cheap SQL macros. Where's embedded SQL?! */
00030 
00031 /* I realize these are in similar territory to SAL -- I may well convert, if it
00032    makes sense. */
00033 
00034 /* Send message through traces, then trigger app death */
00035 #define ABORT(format, args...) ({ ALP_TMTL(format, args); ALP_FAIL_FATAL_IF(1, format "\n", args); })
00036 
00037 #define S_DB db
00038 #define S_STMT sqlite3_stmt *
00039 #define S_EXEC(cmd) \
00040 ({ ALP_FAIL_FATAL_IF(!S_DB, "Error: bundle manager not initialized\n"); \
00041    int _result = sqlite3_exec(S_DB, (cmd), NULL, NULL, NULL); \
00042    if (_result != SQLITE_OK) { \
00043      ABORT("Error executing command %s: %s\n", (cmd), sqlite3_errmsg(S_DB)); \
00044    } \
00045 })
00046 #define S_EXEC_W(cmd) \
00047 ({ ALP_FAIL_FATAL_IF(!S_DB, "Error: bundle manager not initialized\n"); \
00048    int _result = sqlite3_exec(S_DB, (cmd), NULL, NULL, NULL); \
00049    if (_result != SQLITE_OK) { \
00050      ALP_TMTL("Warning executing command %s: %s\n", (cmd), sqlite3_errmsg(S_DB)); \
00051    } \
00052 })
00053 #define S_PREPARE(stmt, cmd) \
00054 ({ ALP_FAIL_FATAL_IF(!S_DB, "Error: bundle manager not initialized\n"); \
00055    int _result = sqlite3_prepare(S_DB, (cmd), -1, &(stmt), NULL); \
00056    if (_result != SQLITE_OK) { \
00057      ABORT("Error preparing command: %s\n", sqlite3_errmsg(S_DB)); \
00058    } \
00059 })
00060 #define S_PREPARE_P(stmt, cmd) \
00061 ({ ALP_FAIL_FATAL_IF(!S_DB, "Error: bundle manager not initialized\n"); \
00062    int _result = sqlite3_prepare(S_DB, (cmd), -1, &(stmt), NULL);       \
00063    _result == SQLITE_OK;                                                \
00064 })
00065 #define S_RESET(stmt) \
00066 ({ sqlite3_reset((stmt)); })
00067 
00068 #define S_STEP_ROW(stmt) \
00069         ({ int _result = sqlite3_step(stmt); \
00070                 if (_result != SQLITE_DONE && _result != SQLITE_ROW) {  \
00071                         _result = sqlite3_reset(stmt); \
00072                         ALP_TMTL("Cannot step command: %s", sqlite3_errmsg(S_DB)); \
00073                 } \
00074            (_result == SQLITE_ROW) ? 1 : 0; \
00075          })
00076 #define S_FINALIZE(stmt)        \
00077         sqlite3_finalize((stmt));
00078 
00079 #define S_BEGIN_SELECT(stmt) \
00080 { int _result; \
00081   while ((_result = sqlite3_step((stmt))) == SQLITE_ROW) {
00082 
00083 #define S_BREAK_SELECT(stmt) \
00084         ({ _result = SQLITE_DONE; break; })
00085 
00086 #define S_END_SELECT(stmt) \
00087   } \
00088   if (_result != SQLITE_DONE) { \
00089      _result = sqlite3_reset(stmt); \
00090     ABORT("Can't step command: %s\n", sqlite3_errmsg(S_DB)); \
00091   } else { \
00092     sqlite3_finalize((stmt)); \
00093   } \
00094 }
00095 
00096 #define S_DO(stmt) \
00097 ({ int _result = sqlite3_step(stmt); \
00098    if (_result != SQLITE_DONE) { \
00099      _result = sqlite3_reset(stmt); \
00100      ABORT("Error executing prepared command: %s\n", sqlite3_errmsg(S_DB)); \
00101    } \
00102    sqlite3_finalize((stmt)); \
00103 })
00104 #define S_DO_OR_ABORT(stmt) \
00105 ({ int _result = sqlite3_step(stmt); \
00106    if (_result != SQLITE_DONE && _result != SQLITE_ERROR) { \
00107      _result = sqlite3_reset(stmt); \
00108      ABORT("Error executing prepared command: %s\n", sqlite3_errmsg(S_DB)); \
00109    } \
00110    sqlite3_finalize((stmt)); \
00111    (_result == SQLITE_DONE) ? 0 : 1; \
00112 })
00113 #define S_DO_W(stmt) \
00114 ({ int _result = sqlite3_step(stmt); \
00115    if (_result != SQLITE_DONE) { \
00116      _result = sqlite3_reset(stmt); \
00117      ALP_TMTL("Warning executing prepared command: %s\n", sqlite3_errmsg(S_DB)); \
00118    } \
00119    sqlite3_finalize((stmt)); \
00120 })
00121 
00122 #define S_BIND_INT(stmt, param, value) \
00123         sqlite3_bind_int((stmt), (param), (value))
00124 #define S_BIND_INT64(stmt, param, value) \
00125         sqlite3_bind_int64((stmt), (param), (value))
00126 #define S_BIND_UINT(stmt, param, value) \
00127         sqlite3_bind_int64((stmt), (param), (long long)((unsigned int)(value)))
00128 #define S_BIND_TEXT(stmt, param, value) \
00129         sqlite3_bind_text((stmt), (param), (value), -1, SQLITE_TRANSIENT)
00130 #define S_BIND_TEXTLEN(stmt, param, value, len) \
00131         sqlite3_bind_text((stmt), (param), (value), (len), SQLITE_TRANSIENT)
00132 #define S_BIND_BLOBLEN(stmt, param, value, len) \
00133         sqlite3_bind_blob(stmt, param, value, len, SQLITE_TRANSIENT)
00134 #define S_BIND_NULL(stmt, param) \
00135         sqlite3_bind_null(stmt, param)
00136 #define S_BIND_GSTRING(stmt, param, value) \
00137         sqlite3_bind_text((stmt), (param), (value)->str, (value)->len, SQLITE_TRANSIENT)
00138 #define S_COL_INT(stmt, param) \
00139         sqlite3_column_int((stmt),(param))
00140 #define S_COL_INT64(stmt, param) \
00141         sqlite3_column_int64((stmt),(param))
00142 #define S_COL_TEXT(stmt, param) \
00143         sqlite3_column_text((stmt),(param))
00144 #define S_COL_BLOB(stmt, param) \
00145         sqlite3_column_blob((stmt),(param))
00146 #define S_COL_BYTES(stmt, param) \
00147         sqlite3_column_bytes((stmt),(param))
00148 #define S_COL_ISNULL(stmt, param) \
00149         (sqlite3_column_type((stmt),(param)) == SQLITE_NULL)
00150 
00151 
00152 
00153 
00154 
00155 
00156 
00157 
00158 
00159 
00160 struct memory_sqlite3_stmt {
00161         const char * cmd_str; // for debugging
00162         sqlite3_stmt * sqlstmt;
00163         struct memory_sqlite3_stmt * next;
00164 };
00165 
00166 extern struct memory_sqlite3_stmt * gmemory_sqlite3_stmt;
00167 
00168 #define SK_STMT struct sqlite3_stmt *
00169 
00170 #define SK_PREPARE(stmt, cmd) \
00171 ({ ALP_FAIL_FATAL_IF(!S_DB, "Error: bundle manager not initialized\n"); \
00172    if (!__builtin_constant_p(cmd)) {                            \
00173         ALP_TMTL("Error preparing command: %s (line %d of %s) -- not constant!", (cmd), __LINE__, __FUNCTION__); \
00174   }                                                             \
00175    static struct memory_sqlite3_stmt _stmt_memory;              \
00176    if (_stmt_memory.sqlstmt != NULL && sqlite3_expired(_stmt_memory.sqlstmt)) {         \
00177         sqlite3_finalize(_stmt_memory.sqlstmt);                                         \
00178            int _result = sqlite3_prepare(S_DB, (cmd), -1, &(_stmt_memory.sqlstmt), NULL); \
00179            if (_result != SQLITE_OK) {                                   \
00180              ABORT("Error re-preparing command: %s\n", sqlite3_errmsg(S_DB)); \
00181            }                                                            \
00182    } else if (_stmt_memory.sqlstmt == NULL ) {                          \
00183            int _result = sqlite3_prepare(S_DB, (cmd), -1, &(_stmt_memory.sqlstmt), NULL); \
00184            if (_result != SQLITE_OK) {                                   \
00185              ABORT("Error preparing command: %s\n", sqlite3_errmsg(S_DB)); \
00186            }                                                            \
00187            _stmt_memory.cmd_str = (cmd);                                        \
00188            _stmt_memory.next = gmemory_sqlite3_stmt;                    \
00189            gmemory_sqlite3_stmt = &_stmt_memory;                        \
00190   }                                                                     \
00191   (stmt) = _stmt_memory.sqlstmt;                                                        \
00192 })
00193 #define SK_RESET(stmt) \
00194 ({ sqlite3_reset((stmt)); })
00195 
00196 #define SK_STEP_ROW(stmt) \
00197         ({ int _result = sqlite3_step((stmt)); \
00198                 if (_result != SQLITE_DONE && _result != SQLITE_ROW) {  \
00199                         _result = sqlite3_reset((stmt)); \
00200                         ALP_TMTL("Cannot step command: %s", sqlite3_errmsg(S_DB)); \
00201                 } \
00202            (_result == SQLITE_ROW) ? 1 : 0; \
00203          })
00204          
00205 #define SK_FINALIZE(stmt)                       \
00206         ({ sqlite3_reset((stmt));       \
00207            sqlite3_clear_bindings((stmt)); })
00208 
00209 #define SK_BEGIN_SELECT(stmt) \
00210 { int _result; \
00211   while ((_result = sqlite3_step((stmt))) == SQLITE_ROW) {
00212 
00213 #define SK_BREAK_SELECT(stmt) \
00214         ({ _result = SQLITE_DONE; break; })
00215 
00216 #define SK_END_SELECT(stmt) \
00217   } \
00218   if (_result != SQLITE_DONE) { \
00219     _result = sqlite3_reset((stmt)); \
00220     ABORT("Can't step command: %s\n", sqlite3_errmsg(S_DB)); \
00221   } else { \
00222     sqlite3_reset((stmt)); \
00223     sqlite3_clear_bindings((stmt)); \
00224   } \
00225 }
00226 
00227 #define SK_DO(stmt) \
00228 ({ int _result = sqlite3_step((stmt)); \
00229    if (_result != SQLITE_DONE) { \
00230      _result = sqlite3_reset((stmt)); \
00231      ABORT("Error executing prepared command: %s\n", sqlite3_errmsg(S_DB)); \
00232    } \
00233    sqlite3_reset((stmt)); \
00234    sqlite3_clear_bindings((stmt)); \
00235 })
00236 #define SK_DO_OR_ABORT(stmt) \
00237 ({ int _result = sqlite3_step(stmt); \
00238    if (_result != SQLITE_DONE && _result != SQLITE_ERROR) { \
00239      _result = sqlite3_reset((stmt)); \
00240      ABORT("Error executing prepared command: %s\n", sqlite3_errmsg(S_DB)); \
00241    } \
00242    sqlite3_reset((stmt)); \
00243    sqlite3_clear_bindings((stmt)); \
00244    (_result == SQLITE_DONE) ? 0 : 1; \
00245 })
00246 #define SK_DO_W(stmt) \
00247 ({ int _result = sqlite3_step(stmt); \
00248    if (_result != SQLITE_DONE) { \
00249      _result = sqlite3_reset((stmt)); \
00250      ALP_TMTL("Warning executing prepared command: %s\n", sqlite3_errmsg(S_DB)); \
00251    } \
00252    sqlite3_reset((stmt)); \
00253    sqlite3_clear_bindings((stmt)); \
00254 })
00255 extern struct memory_sqlite3_stmt gmemoize_sqlite3_begin;
00256 extern struct memory_sqlite3_stmt gmemoize_sqlite3_commit;
00257 #if 0
00258 
00259 #define SK_BEGIN()                              \
00260 ({                                              \
00261    const char * cmd = "begin";                  \
00262    if (gmemoize_sqlite3_begin.sqlstmt == NULL) {                                \
00263            int _result = sqlite3_prepare(S_DB, (cmd), -1, &(gmemoize_sqlite3_begin.sqlstmt), NULL); \
00264            if (_result != SQLITE_OK) {                                   \
00265              ABORT("Error preparing command: %s\n", sqlite3_errmsg(S_DB)); \
00266            }                                                            \
00267            ALP_TMTL("Prepared command %s to stmt %p without error", (cmd), gmemoize_sqlite3_begin.sqlstmt);     \
00268            gmemoize_sqlite3_begin.cmd_str = cmd;                                        \
00269            gmemoize_sqlite3_begin.next = gmemory_sqlite3_stmt;                  \
00270            gmemory_sqlite3_stmt = &gmemoize_sqlite3_begin;                      \
00271   }                                                                     \
00272    int _result = sqlite3_step((gmemoize_sqlite3_begin.sqlstmt)); \
00273    if (_result != SQLITE_OK && _result != SQLITE_DONE) { \
00274      ABORT("Error executing prepared command %p: %d, %s\n", gmemoize_sqlite3_begin.sqlstmt, _result, sqlite3_errmsg(S_DB)); \
00275    } \
00276    sqlite3_reset((gmemoize_sqlite3_begin.sqlstmt)); \
00277 })
00278 #define SK_COMMIT()                             \
00279 ({                                              \
00280    const char * cmd = "commit";                 \
00281    if (gmemoize_sqlite3_commit.sqlstmt == NULL) {                               \
00282            int _result = sqlite3_prepare(S_DB, (cmd), -1, &(gmemoize_sqlite3_commit.sqlstmt), NULL); \
00283            if (_result != SQLITE_OK) {                                   \
00284              ABORT("Error preparing command: %s\n", sqlite3_errmsg(S_DB)); \
00285            }                                                            \
00286            gmemoize_sqlite3_commit.cmd_str = cmd;                                       \
00287            gmemoize_sqlite3_commit.next = gmemory_sqlite3_stmt;                 \
00288            gmemory_sqlite3_stmt = &gmemoize_sqlite3_commit;                     \
00289   }                                                                     \
00290    int _result = sqlite3_step((gmemoize_sqlite3_commit.sqlstmt)); \
00291    if (_result != SQLITE_OK && _result != SQLITE_DONE) { \
00292      ABORT("Error executing prepared command: %d, %s\n", _result, sqlite3_errmsg(S_DB)); \
00293    } \
00294    sqlite3_reset((gmemoize_sqlite3_commit.sqlstmt)); \
00295 })
00296 #endif
00297 
00298 #define SK_BEGIN() S_EXEC("BEGIN")
00299 #define SK_COMMIT() S_EXEC("COMMIT")
00300 
00301 #define SK_BIND_INT(stmt, param, value) \
00302         sqlite3_bind_int((stmt), (param), (value))
00303 #define SK_BIND_TEXT(stmt, param, value) \
00304         sqlite3_bind_text((stmt), (param), (value), -1, SQLITE_TRANSIENT)
00305 #define SK_BIND_TEXTLEN(stmt, param, value, len) \
00306         sqlite3_bind_text((stmt), (param), (value), (len), SQLITE_TRANSIENT)
00307 #define SK_BIND_BLOBLEN(stmt, param, value, len) \
00308         sqlite3_bind_blob(stmt, param, value, len, SQLITE_TRANSIENT)
00309 #define SK_BIND_NULL(stmt, param) \
00310         sqlite3_bind_null(stmt, param)
00311 #define SK_BIND_GSTRING(stmt, param, value) \
00312         sqlite3_bind_text((stmt), (param), (value)->str, (value)->len, SQLITE_TRANSIENT)
00313 #define SK_COL_INT(stmt, param) \
00314         sqlite3_column_int((stmt),(param))
00315 #define SK_COL_TEXT(stmt, param) \
00316         sqlite3_column_text((stmt),(param))
00317 #define SK_COL_BLOB(stmt, param) \
00318         sqlite3_column_blob((stmt),(param))
00319 #define SK_COL_BYTES(stmt, param) \
00320         sqlite3_column_bytes((stmt),(param))
00321 #define SK_COL_ISNULL(stmt, param) \
00322         (sqlite3_column_type((stmt),(param)) == SQLITE_NULL)

Generated on Sat Dec 16 20:29:47 2006 for hiker-0.9 by  doxygen 1.4.4