[PATCH] sqlite3 DLR storage

Stipe Tolj <[email protected]>
Newsgroups gmane.comp.mobile.kannel.devel
Organization tolj.org system architecture
Message-ID <[email protected]>
Hi list,

there has been a patch pending for a long time, kindly contributed by David
Butler, originally posted 2009-11-23 to the list, with Msg-ID
<22DE7AAC95BA4B5A9EEF3FEC90222162@VAIO>.

I have picked up the code from David, and added the parts for the user's guide,
along with some fixing for the destination number prefix handling, (as it's in
the DLR mysql code), and resolving Alex critics regarding the config directive,
which he is of course right. ;)

Please find the diff against svn trunk attached.

Tested it locally with high-load regression, and worked pretty fine. Please drop
a vote.

Stipe

-- 
-------------------------------------------------------------------
Kölner Landstrasse 419
40589 Düsseldorf, NRW, Germany

tolj.org system architecture      Kannel Software Foundation (KSF)
http://www.tolj.org/              http://www.kannel.org/

mailto:st_{at}_tolj.org           mailto:stolj_{at}_kannel.org
-------------------------------------------------------------------
dlr-sqlite3.diff (text/plain, 23 KB)
Index: gwlib/dbpool_sqlite3.c
===================================================================
--- gwlib/dbpool_sqlite3.c	(revision 4879)
+++ gwlib/dbpool_sqlite3.c	(working copy)
@@ -58,6 +58,7 @@
  * dbpool_sqlite3.c - implement SQLite3 operations for generic database connection pool
  *
  * Stipe Tolj <[email protected]>
+ * David Butler <[email protected]> - modeled select and update from dbpool_oracle.c 
  */
 
 #ifdef HAVE_SQLITE3
@@ -132,12 +133,123 @@
     gw_free(db_conf);
 }
 
+static int sqlite3_select(void *theconn, const Octstr *sql, List *binds, List **res)
+{
+    sqlite3 *db = theconn;
+    sqlite3_stmt *stmt;
+    const char *rem;
+    List *row;
+    int status;
+    int columns;
+    int i;
+    int binds_len = (binds ? gwlist_len(binds) : 0);
 
+    *res = NULL;
+
+    /* prepare statement */
+    status = sqlite3_prepare_v2(db, octstr_get_cstr(sql), octstr_len(sql) + 1, &stmt, &rem);
+    if (SQLITE_OK != status) {
+        error(0, "SQLite3: %s", sqlite3_errmsg(db));
+        return -1;
+    }
+
+    /* bind variables */
+    for (i = 0; i < binds_len; i++) {
+        Octstr *bind = gwlist_get(binds, i);
+        status = sqlite3_bind_text(stmt, i + 1, octstr_get_cstr(bind), octstr_len(bind), SQLITE_STATIC);
+        if (SQLITE_OK != status) {
+            error(0, "SQLite3: %s", sqlite3_errmsg(db));
+            sqlite3_finalize(stmt);
+            return -1;
+        }
+    }
+
+    /* execute our statement */
+    *res = gwlist_create();
+    while ((status = sqlite3_step(stmt)) == SQLITE_ROW) {
+        columns = sqlite3_data_count(stmt);
+        debug("dbpool.sqlite3",0,"SQL has %d columns", columns);
+        row = gwlist_create();
+        for (i = 0; i < columns; i++) {
+            if (sqlite3_column_type(stmt, i) == SQLITE_NULL) {
+                gwlist_insert(row, i, octstr_create(""));
+            } else {
+                gwlist_insert(row, i, octstr_create(sqlite3_column_text(stmt, i)));
+            }
+            /* debug("dbpool.sqlite3",0,"inserted value = '%s'", 
+                     octstr_get_cstr(gwlist_get(row,i))); */
+        }
+        gwlist_append(*res, row);
+    }
+
+    if (SQLITE_DONE != status) {
+        error(0, "SQLite3: %s", sqlite3_errmsg(db));
+        while ((row = gwlist_extract_first(*res)) != NULL)
+            gwlist_destroy(row, octstr_destroy_item);
+        gwlist_destroy(*res, NULL);
+        *res = NULL;
+        sqlite3_finalize(stmt);
+        return -1;
+    }
+
+    sqlite3_finalize(stmt);
+
+    return 0;
+}
+
+
+static int sqlite3_update(void *theconn, const Octstr *sql, List *binds)
+{
+    sqlite3 *db = theconn;
+    sqlite3_stmt *stmt;
+    const char *rem;
+    int status;
+    int rows;
+    int i;
+    int binds_len = (binds ? gwlist_len(binds) : 0);
+
+    /* prepare statement */
+    status = sqlite3_prepare_v2(db, octstr_get_cstr(sql), octstr_len(sql) + 1, &stmt, &rem);
+    if (SQLITE_OK != status) {
+        error(0, "SQLite3: %s", sqlite3_errmsg(db));
+        return -1;
+    }
+    debug("dbpool.sqlite3",0,"sqlite3_prepare done");
+
+    /* bind variables */
+    for (i = 0; i < binds_len; i++) {
+        Octstr *bind = gwlist_get(binds, i);
+        status = sqlite3_bind_text(stmt, i + 1, octstr_get_cstr(bind), octstr_len(bind), SQLITE_STATIC);
+        if (SQLITE_OK != status) {
+            error(0, "SQLite3: %s", sqlite3_errmsg(db));
+            sqlite3_finalize(stmt);
+            return -1;
+        }
+    }
+
+    /* execute our statement */
+    if ((status = sqlite3_step(stmt)) != SQLITE_DONE) {
+        error(0, "SQLite3: %s", sqlite3_errmsg(db));
+        sqlite3_finalize(stmt);
+        return -1;
+    }
+    debug("dbpool.sqlite3",0,"sqlite3_step done");
+
+    rows = sqlite3_changes(db);
+    debug("dbpool.sqlite3",0,"rows processed = %d", rows);
+
+    sqlite3_finalize(stmt);
+
+    return rows;
+}
+
 static struct db_ops sqlite3_ops = {
     .open = sqlite3_open_conn,
     .close = sqlite3_close_conn,
     .check = sqlite3_check_conn,
-    .conf_destroy = sqlite3_conf_destroy
+    .conf_destroy = sqlite3_conf_destroy,
+    .select = sqlite3_select,
+    .update = sqlite3_update
 };
 
 #endif /* HAVE_SQLITE3 */
Index: doc/userguide/userguide.xml
===================================================================
--- doc/userguide/userguide.xml	(revision 4879)
+++ doc/userguide/userguide.xml	(working copy)
@@ -1491,9 +1491,15 @@
      <entry valign="bottom">
         Defines the way DLRs are stored. If you have build-in external 
           DLR storage support, i.e. using MySQL you may define here the
-          alternative storage type like 'mysql'. Supported types are:
-          internal, mysql, pgsql, sdb, mssql and oracle. By default this
-          is set to 'internal'.
+          alternative storage type like <literal>mysql</literal>. 
+          Supported types are:
+          <literal>internal</literal>, 
+          <literal>mysql</literal>,
+          <literal>pgsql</literal>,
+          <literal>sdb</literal>,
+          <literal>mssql</literal>,
+          <literal>sqlite3</literal> and <literal>oracle</literal>.
+          By default this is set to <literal>internal</literal>.
      </entry></row>
 
      <row><entry><literal>maximum-queue-length</literal></entry>
@@ -5459,6 +5465,43 @@
 
     </sect2>
 
+	<sect2>
+	<title>SQLite3 DLR storage</title>
+	<para>To store DLR information into a SQLite3 database you may use the
+	<literal>dlr-storage = sqlite3</literal> configuration directive in the
+	<literal>core</literal> group.
+	</para>
+	<para>In addition to that you must have a <literal>dlr-db</literal>
+	group defined that specifies the table field names that are used to the
+	DLR attributes and a <literal>sqlite3-connection</literal> group that
+	defines the connection to the SQLite3 database itself.
+	</para>
+	<para>Here is the example configuration:
+
+<programlisting>
+group = sqlite3-connection
+id = mydlr
+database = /path/to/file
+max-connections = 1
+
+group = dlr-db
+id = mydlr
+table = dlr
+field-smsc = smsc
+field-timestamp = ts
+field-destination = destination
+field-source = source
+field-service = service
+field-url = url
+field-mask = mask
+field-status = status
+field-boxc-id = boxc
+</programlisting>
+
+	</para>
+
+	</sect2>
+
         <sect2>
 	<title>DLR database field configuration</title>
 	<para>For external database storage of DLR information in relational
Index: doc/examples/dlr-sqlite3.conf
===================================================================
--- doc/examples/dlr-sqlite3.conf	(revision 0)
+++ doc/examples/dlr-sqlite3.conf	(revision 0)
@@ -0,0 +1,43 @@
+#
+# DLR with SQLite3 support configuration
+#
+# Example defining a SQLite3 database connection resource and 
+# the required table and field values.
+#
+
+group = sqlite3-connection
+id = mydlr
+database = /path/to/file
+# max count of connections that will be opened for dbpool
+# default is 1
+max-connections = 1
+
+# 
+# Create the required table for the DLR support in the database 
+# with something similar like this: 
+# 
+#   CREATE TABLE dlr (
+#     smsc varchar(40),
+#     ts varchar(40),
+#     destination varchar(40),
+#     source varchar(40),
+#     service varchar(40),
+#     url varchar(255),
+#     mask int(10),
+#     status int(10),
+#     boxc varchar(40)
+#   )
+#
+
+group = dlr-db
+id = mydlr
+table = dlr
+field-smsc = smsc
+field-timestamp = ts
+field-destination = destination
+field-source = source
+field-service = service
+field-url = url
+field-mask = mask
+field-status = status
+field-boxc-id = boxc
Index: gw/dlr_p.h
===================================================================
--- gw/dlr_p.h	(revision 4879)
+++ gw/dlr_p.h	(working copy)
@@ -171,6 +171,7 @@
 struct dlr_storage *dlr_init_oracle(Cfg *cfg);
 struct dlr_storage *dlr_init_pgsql(Cfg *cfg);
 struct dlr_storage *dlr_init_mssql(Cfg *cfg);
+struct dlr_storage *dlr_init_sqlite3(Cfg *cfg);
 
 
 #endif /* DLR_P_H */
Index: gw/dlr.c
===================================================================
--- gw/dlr.c	(revision 4879)
+++ gw/dlr.c	(working copy)
@@ -260,6 +260,8 @@
         handles = dlr_init_pgsql(cfg);
     } else if (octstr_compare(dlr_type, octstr_imm("mssql")) == 0) {
         handles = dlr_init_mssql(cfg);
+    } else if (octstr_compare(dlr_type, octstr_imm("sqlite3")) == 0) {
+        handles = dlr_init_sqlite3(cfg);
     }
 
     /*
Index: gw/dlr_sqlite3.c
===================================================================
--- gw/dlr_sqlite3.c	(revision 0)
+++ gw/dlr_sqlite3.c	(revision 0)
@@ -0,0 +1,425 @@
+/* ==================================================================== 
+ * The Kannel Software License, Version 1.0 
+ * 
+ * Copyright (c) 2001-2011 Kannel Group  
+ * Copyright (c) 1998-2001 WapIT Ltd.   
+ * All rights reserved. 
+ * 
+ * Redistribution and use in source and binary forms, with or without 
+ * modification, are permitted provided that the following conditions 
+ * are met: 
+ * 
+ * 1. Redistributions of source code must retain the above copyright 
+ *    notice, this list of conditions and the following disclaimer. 
+ * 
+ * 2. Redistributions in binary form must reproduce the above copyright 
+ *    notice, this list of conditions and the following disclaimer in 
+ *    the documentation and/or other materials provided with the 
+ *    distribution. 
+ * 
+ * 3. The end-user documentation included with the redistribution, 
+ *    if any, must include the following acknowledgment: 
+ *       "This product includes software developed by the 
+ *        Kannel Group (http://www.kannel.org/)." 
+ *    Alternately, this acknowledgment may appear in the software itself, 
+ *    if and wherever such third-party acknowledgments normally appear. 
+ * 
+ * 4. The names "Kannel" and "Kannel Group" must not be used to 
+ *    endorse or promote products derived from this software without 
+ *    prior written permission. For written permission, please  
+ *    contact [email protected]. 
+ * 
+ * 5. Products derived from this software may not be called "Kannel", 
+ *    nor may "Kannel" appear in their name, without prior written 
+ *    permission of the Kannel Group. 
+ * 
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
+ * DISCLAIMED.  IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS 
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,  
+ * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT  
+ * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR  
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE  
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,  
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
+ * ==================================================================== 
+ * 
+ * This software consists of voluntary contributions made by many 
+ * individuals on behalf of the Kannel Group.  For more information on  
+ * the Kannel Group, please see <http://www.kannel.org/>. 
+ * 
+ * Portions of this software are based upon software originally written at  
+ * WapIT Ltd., Helsinki, Finland for the Kannel project.  
+ */ 
+
+/*
+ * dlr_sqlite3.c - sqlite3 dlr storage implementation.
+ *
+ * Author: David Butler <[email protected]>
+ * 
+ * Based on dlr_oracle.c
+ *
+ * Copyright: See COPYING file that comes with this distribution
+ */
+
+#include "gwlib/gwlib.h"
+#include "gwlib/dbpool.h"
+#include "dlr_p.h"
+
+
+#ifdef HAVE_SQLITE3
+
+/*
+ * Our connection pool to sqlite3.
+ */
+static DBPool *pool = NULL;
+
+/*
+ * Database fields, which we are use.
+ */
+static struct dlr_db_fields *fields = NULL;
+
+
+static long dlr_messages_sqlite3()
+{
+    List *result, *row;
+    Octstr *sql;
+    DBPoolConn *conn;
+    long msgs = -1;
+
+    conn = dbpool_conn_consume(pool);
+    if (conn == NULL)
+        return -1;
+
+    sql = octstr_format("SELECT count(*) FROM %S", fields->table);
+#if defined(DLR_TRACE)
+    debug("dlr.sqlite3", 0, "sql: %s", octstr_get_cstr(sql));
+#endif
+
+    if (dbpool_conn_select(conn, sql, NULL, &result) != 0) {
+        octstr_destroy(sql);
+        dbpool_conn_produce(conn);
+        return -1;
+    }
+    dbpool_conn_produce(conn);
+    octstr_destroy(sql);
+
+    if (gwlist_len(result) > 0) {
+        row = gwlist_extract_first(result);
+        msgs = strtol(octstr_get_cstr(gwlist_get(row, 0)), NULL, 10);
+        gwlist_destroy(row, octstr_destroy_item);
+    }
+    gwlist_destroy(result, NULL);
+
+    return msgs;
+}
+
+static void dlr_shutdown_sqlite3()
+{
+    dbpool_destroy(pool);
+    dlr_db_fields_destroy(fields);
+}
+
+static void dlr_add_sqlite3(struct dlr_entry *entry)
+{
+    Octstr *sql, *os_mask;
+    DBPoolConn *pconn;
+    List *binds = gwlist_create();
+    int res;
+
+    debug("dlr.sqlite3", 0, "adding DLR entry into database");
+
+    pconn = dbpool_conn_consume(pool);
+    /* just for sure */
+    if (pconn == NULL) {
+        dlr_entry_destroy(entry);
+        return;
+    }
+
+    sql = octstr_format("INSERT INTO %S (%S, %S, %S, %S, %S, %S, %S, %S, %S) VALUES "
+                        "(?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, 0)",
+                        fields->table, fields->field_smsc, fields->field_ts,
+                        fields->field_src, fields->field_dst, fields->field_serv, 
+                        fields->field_url, fields->field_mask, fields->field_boxc,
+                        fields->field_status);
+    os_mask = octstr_format("%d", entry->mask);
+    
+    gwlist_append(binds, entry->smsc);         /* ?1 */
+    gwlist_append(binds, entry->timestamp);    /* ?2 */
+    gwlist_append(binds, entry->source);       /* ?3 */
+    gwlist_append(binds, entry->destination);  /* ?4 */
+    gwlist_append(binds, entry->service);      /* ?5 */
+    gwlist_append(binds, entry->url);          /* ?6 */
+    gwlist_append(binds, os_mask);             /* ?7 */
+    gwlist_append(binds, entry->boxc_id);      /* ?8 */
+#if defined(DLR_TRACE)
+    debug("dlr.sqlite3", 0, "sql: %s", octstr_get_cstr(sql));
+#endif
+    if ((res = dbpool_conn_update(pconn, sql, binds)) == -1)
+        error(0, "DLR: SQLite3: Error while adding dlr entry for DST<%s>", octstr_get_cstr(entry->destination));
+    else if (!res)
+        warning(0, "DLR: SQLite3: No dlr inserted for DST<%s>", octstr_get_cstr(entry->destination));
+
+    dbpool_conn_produce(pconn);
+    octstr_destroy(sql);
+    gwlist_destroy(binds, NULL);
+    octstr_destroy(os_mask);
+    dlr_entry_destroy(entry);
+}
+
+static void dlr_remove_sqlite3(const Octstr *smsc, const Octstr *ts, const Octstr *dst)
+{
+    Octstr *sql, *like;
+    DBPoolConn *pconn;
+    List *binds = gwlist_create();
+    int res;
+    debug("dlr.sqlite3", 0, "removing DLR from database");
+
+    pconn = dbpool_conn_consume(pool);
+    /* just for sure */
+    if (pconn == NULL)
+        return;
+    
+    if (dst)
+        like = octstr_format("AND %S LIKE '%?3'", fields->field_dst);
+    else
+        like = octstr_imm("");
+
+    sql = octstr_format("DELETE FROM %S WHERE ROWID IN (SELECT ROWID FROM %S WHERE %S=?1 AND %S=?2 %S LIMIT 1)",
+                        fields->table, fields->table,
+                        fields->field_smsc, fields->field_ts, like);
+
+    gwlist_append(binds, (Octstr *)smsc);      /* ?1 */
+    gwlist_append(binds, (Octstr *)ts);        /* ?2 */
+    if (dst)
+        gwlist_append(binds, (Octstr *)dst);   /* ?3 */
+
+#if defined(DLR_TRACE)
+    debug("dlr.sqlite3", 0, "sql: %s", octstr_get_cstr(sql));
+#endif
+
+    if ((res = dbpool_conn_update(pconn, sql, binds)) == -1)
+        error(0, "DLR: SQLite3: Error while removing dlr entry for DST<%s>", octstr_get_cstr(dst));
+    else if (!res)
+        warning(0, "DLR: SQLite3: No dlr deleted for DST<%s>", octstr_get_cstr(dst));
+
+    dbpool_conn_produce(pconn);
+    gwlist_destroy(binds, NULL);
+    octstr_destroy(sql);
+    octstr_destroy(like);
+}
+
+static struct dlr_entry* dlr_get_sqlite3(const Octstr *smsc, const Octstr *ts, const Octstr *dst)
+{
+    Octstr *sql, *like;
+    DBPoolConn *pconn;
+    List *result = NULL, *row;
+    struct dlr_entry *res = NULL;
+    List *binds = gwlist_create();
+
+    pconn = dbpool_conn_consume(pool);
+    if (pconn == NULL) /* should not happens, but sure is sure */
+        return NULL;
+
+    if (dst)
+        like = octstr_format("AND %S LIKE '%?3'", fields->field_dst);
+    else
+        like = octstr_imm("");
+
+    sql = octstr_format("SELECT %S, %S, %S, %S, %S, %S FROM %S WHERE %S=?1 AND %S=?2 %S LIMIT 1",
+                        fields->field_mask, fields->field_serv,
+                        fields->field_url, fields->field_src,
+                        fields->field_dst, fields->field_boxc,
+                        fields->table, fields->field_smsc,
+                        fields->field_ts, like);
+
+    gwlist_append(binds, (Octstr *)smsc);      /* ?1 */
+    gwlist_append(binds, (Octstr *)ts);        /* ?2 */
+    if (dst)
+        gwlist_append(binds, (Octstr *)dst);   /* ?3 */
+
+#if defined(DLR_TRACE)
+    debug("dlr.sqlite3", 0, "sql: %s", octstr_get_cstr(sql));
+#endif
+    if (dbpool_conn_select(pconn, sql, binds, &result) != 0) {
+        octstr_destroy(sql);
+        dbpool_conn_produce(pconn);
+        return NULL;
+    }
+    octstr_destroy(sql);
+    octstr_destroy(like);
+    gwlist_destroy(binds, NULL);
+    dbpool_conn_produce(pconn);
+
+#define LO2CSTR(r, i) octstr_get_cstr(gwlist_get(r, i))
+
+    if (gwlist_len(result) > 0) {
+        row = gwlist_extract_first(result);
+        res = dlr_entry_create();
+        gw_assert(res != NULL);
+        res->mask = atoi(LO2CSTR(row,0));
+        res->service = octstr_create(LO2CSTR(row, 1));
+        res->url = octstr_create(LO2CSTR(row,2));
+        res->source = octstr_create(LO2CSTR(row, 3));
+        res->destination = octstr_create(LO2CSTR(row, 4));
+        res->boxc_id = octstr_create(LO2CSTR(row, 5));
+        gwlist_destroy(row, octstr_destroy_item);
+        res->smsc = octstr_duplicate(smsc);
+    }
+    gwlist_destroy(result, NULL);
+
+#undef LO2CSTR
+
+    return res;
+}
+
+static void dlr_update_sqlite3(const Octstr *smsc, const Octstr *ts, const Octstr *dst, int status)
+{
+    Octstr *sql, *os_status, *like;
+    DBPoolConn *pconn;
+    List *binds = gwlist_create();
+    int res;
+
+    debug("dlr.sqlite3", 0, "updating DLR status in database");
+
+    pconn = dbpool_conn_consume(pool);
+    /* just for sure */
+    if (pconn == NULL)
+        return;
+
+    if (dst)
+        like = octstr_format("AND %S LIKE '%?4'", fields->field_dst);
+    else
+        like = octstr_imm("");
+
+    sql = octstr_format("UPDATE %S SET %S=?1 WHERE ROWID IN (SELECT ROWID FROM %S WHERE %S=?2 AND %S=?3 %S LIMIT 1)",
+                        fields->table, fields->field_status, fields->table,
+                        fields->field_smsc, fields->field_ts, fields->field_dst);
+
+    os_status = octstr_format("%d", status);
+    gwlist_append(binds, (Octstr *)os_status); /* ?1 */
+    gwlist_append(binds, (Octstr *)smsc);      /* ?2 */
+    gwlist_append(binds, (Octstr *)ts);        /* ?3 */
+    if (dst)
+        gwlist_append(binds, (Octstr *)dst);   /* ?4 */
+    
+#if defined(DLR_TRACE)
+    debug("dlr.sqlite3", 0, "sql: %s", octstr_get_cstr(sql));
+#endif
+    if ((res = dbpool_conn_update(pconn, sql, binds)) == -1)
+        error(0, "DLR: SQLite3: Error while updating dlr entry for DST<%s>", octstr_get_cstr(dst));
+    else if (!res)
+        warning(0, "DLR: SQLite3: No dlr found to update for DST<%s> (status: %d)", octstr_get_cstr(dst), status);
+
+    dbpool_conn_produce(pconn);
+    gwlist_destroy(binds, NULL);
+    octstr_destroy(os_status);
+    octstr_destroy(sql);
+    octstr_destroy(like);
+}
+
+static void dlr_flush_sqlite3 (void)
+{
+    Octstr *sql;
+    DBPoolConn *pconn;
+    int rows;
+
+    pconn = dbpool_conn_consume(pool);
+    /* just for sure */
+    if (pconn == NULL)
+        return;
+
+    sql = octstr_format("DELETE FROM %S", fields->table);
+#if defined(DLR_TRACE)
+    debug("dlr.sqlite3", 0, "sql: %s", octstr_get_cstr(sql));
+#endif
+    rows = dbpool_conn_update(pconn, sql, NULL);
+    if (rows == -1)
+        error(0, "DLR: SQLite3: Error while flushing dlr entries from database");
+    else
+        debug("dlr.sqlite3", 0, "Flushing %d DLR entries from database", rows);
+    dbpool_conn_produce(pconn);
+    octstr_destroy(sql);
+}
+
+static struct dlr_storage handles = {
+    .type = "sqlite3",
+    .dlr_messages = dlr_messages_sqlite3,
+    .dlr_shutdown = dlr_shutdown_sqlite3,
+    .dlr_add = dlr_add_sqlite3,
+    .dlr_get = dlr_get_sqlite3,
+    .dlr_remove = dlr_remove_sqlite3,
+    .dlr_update = dlr_update_sqlite3,
+    .dlr_flush = dlr_flush_sqlite3
+};
+
+struct dlr_storage *dlr_init_sqlite3(Cfg *cfg)
+{
+    CfgGroup *grp;
+    List *grplist;
+    long pool_size;
+    DBConf *db_conf = NULL;
+    Octstr *id, *file;
+    int found;
+
+    if ((grp = cfg_get_single_group(cfg, octstr_imm("dlr-db"))) == NULL)
+        panic(0, "DLR: SQLite3: group 'dlr-db' is not specified!");
+
+    if (!(id = cfg_get(grp, octstr_imm("id"))))
+       panic(0, "DLR: SQLite3: directive 'id' is not specified!");
+
+    /* initialize database fields */
+    fields = dlr_db_fields_create(grp);
+    gw_assert(fields != NULL);
+
+    grplist = cfg_get_multi_group(cfg, octstr_imm("sqlite3-connection"));
+    found = 0;
+    while (grplist && (grp = gwlist_extract_first(grplist)) != NULL) {
+        Octstr *p = cfg_get(grp, octstr_imm("id"));
+        if (p != NULL && octstr_compare(p, id) == 0) {
+            found = 1;
+        }
+        if (p != NULL) 
+            octstr_destroy(p);
+        if (found == 1) 
+            break;
+    }
+    gwlist_destroy(grplist, NULL);
+
+    if (found == 0)
+        panic(0, "DLR: SQLite3: connection settings for id '%s' are not specified!",
+              octstr_get_cstr(id));
+
+    file = cfg_get(grp, octstr_imm("database"));
+    if (cfg_get_integer(&pool_size, grp, octstr_imm("max-connections")) == -1)
+        pool_size = 1;
+
+    if (file == NULL)
+        panic(0, "DLR: SQLite3: connection settings missing for id '%s', please"
+                 " check you configuration.",octstr_get_cstr(id));
+
+    /* ok we are ready to create dbpool */
+    db_conf = gw_malloc(sizeof(*db_conf));
+    db_conf->sqlite3 = gw_malloc(sizeof(SQLite3Conf));
+
+    db_conf->sqlite3->file = file;
+
+    pool = dbpool_create(DBPOOL_SQLITE3, db_conf, pool_size);
+    gw_assert(pool != NULL);
+
+    if (dbpool_conn_count(pool) == 0)
+        panic(0, "DLR: SQLite3: Could not establish sqlite3 connection(s).");
+
+    octstr_destroy(id);
+
+    return &handles;
+}
+#else
+/* no sqlite3 support build in */
+struct dlr_storage *dlr_init_sqlite3(Cfg *cfg)
+{
+    return NULL;
+}
+#endif /* HAVE_SQLITE3 */
smime.p7s (application/pkcs7-signature, 5.8 KB) - not displayed
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.