[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Bacula-devel] DBI, added sqlite and sqlite3 support


Hello,

In Bacula trunk:

 * Added sqlite and sqlite3 support

Thanks.

Index: autoconf/bacula-macros/db.m4
===================================================================
--- autoconf/bacula-macros/db.m4	(revision 7470)
+++ autoconf/bacula-macros/db.m4	(working copy)
@@ -222,6 +222,64 @@
            fi
            DB_PROG_LIB=$SQL_LIBDIR/libpq.a
         ;;
+        "sqlite")
+           db_prog="sqlite"
+           if test -f /usr/local/bin/sqlite; then
+              SQL_BINDIR=/usr/local/bin
+              if test -d /usr/local/lib64; then
+                 SQL_LIBDIR=/usr/local/lib64
+              else
+                 SQL_LIBDIR=/usr/local/lib
+              fi
+           elif test -f /usr/bin/sqlite; then
+              SQL_BINDIR=/usr/bin
+              if test -d /usr/lib64; then
+                 SQL_LIBDIR=/usr/lib64
+              else
+                 SQL_LIBDIR=/usr/lib
+              fi
+           elif test -f $withval/bin/sqlite; then
+              SQL_BINDIR=$withval/bin
+              if test -d $withval/lib64; then
+                 SQL_LIBDIR=$withval/lib64
+              else
+                 SQL_LIBDIR=$withval/lib
+              fi
+           else
+              AC_MSG_RESULT(no)
+              AC_MSG_ERROR(Unable to find sqlite in standard locations)
+           fi
+           DB_PROG_LIB=$SQL_LIBDIR/libsqlite.a
+        ;;
+        "sqlite3")
+           db_prog="sqlite3"
+           if test -f /usr/local/bin/sqlite3; then
+              SQL_BINDIR=/usr/local/bin
+              if test -d /usr/local/lib64; then
+                 SQL_LIBDIR=/usr/local/lib64
+              else
+                 SQL_LIBDIR=/usr/local/lib
+              fi
+           elif test -f /usr/bin/sqlite3; then
+              SQL_BINDIR=/usr/bin
+              if test -d /usr/lib64; then
+                 SQL_LIBDIR=/usr/lib64
+              else
+                 SQL_LIBDIR=/usr/lib
+              fi
+           elif test -f $withval/bin/sqlite3; then
+              SQL_BINDIR=$withval/bin
+              if test -d $withval/lib64; then
+                 SQL_LIBDIR=$withval/lib64
+              else
+                 SQL_LIBDIR=$withval/lib
+              fi
+           else
+              AC_MSG_RESULT(no)
+              AC_MSG_ERROR(Unable to find sqlite in standard locations)
+           fi
+           DB_PROG_LIB=$SQL_LIBDIR/libsqlite3.a
+        ;;
         *)
            AC_MSG_RESULT(no)
            AC_MSG_ERROR(Unable to set DBI driver. $withval is not supported)
Index: src/cats/dbi.c
===================================================================
--- src/cats/dbi.c	(revision 7553)
+++ src/cats/dbi.c	(working copy)
@@ -101,10 +101,13 @@
       bstrncpy(db_driver,"pgsql", sizeof(db_driver));
       break;
    case SQL_TYPE_SQLITE:
-      bstrncpy(db_driver,"pgsql", sizeof(db_driver));
+      bstrncpy(db_driver,"sqlite", sizeof(db_driver));
+      break;
+   case SQL_TYPE_SQLITE3:
+      bstrncpy(db_driver,"sqlite3", sizeof(db_driver));
       break;
    }
-
+
    /* Set db_driverdir whereis is the libdbi drivers */
    bstrncpy(db_driverdir, DBI_DRIVER_DIR, 255);

@@ -178,10 +181,13 @@
 {
    int errstat;
    int dbstat;
+   uint8_t len;
    const char *errmsg;
    char buf[10], *port;
-   int numdrivers;
-
+   int numdrivers;
+   char *db_name = NULL;
+   char *db_dir = NULL;
+
    P(mutex);
    if (mdb->connected) {
       V(mutex);
@@ -213,12 +219,50 @@
       return 0;
    }
    mdb->db = (void **)dbi_conn_new_r(mdb->db_driver, mdb->instance);
-   dbi_conn_set_option(mdb->db, "host", mdb->db_address); /* default
= localhost */
-   dbi_conn_set_option(mdb->db, "port", port);            /* default port */
-   dbi_conn_set_option(mdb->db, "username", mdb->db_user);     /* login name */
-   dbi_conn_set_option(mdb->db, "password", mdb->db_password); /* password */
-   dbi_conn_set_option(mdb->db, "dbname", mdb->db_name);       /*
database name */
-
+   /* Can be many types of databases */
+   switch (mdb->db_type) {
+      case SQL_TYPE_MYSQL:
+      dbi_conn_set_option(mdb->db, "host", mdb->db_address); /*
default = localhost */
+      dbi_conn_set_option(mdb->db, "port", port);            /* default port */
+      dbi_conn_set_option(mdb->db, "username", mdb->db_user);     /*
login name */
+      dbi_conn_set_option(mdb->db, "password", mdb->db_password); /*
password */
+      dbi_conn_set_option(mdb->db, "dbname", mdb->db_name);       /*
database name */
+      break;
+      case SQL_TYPE_POSTGRESQL:
+      dbi_conn_set_option(mdb->db, "host", mdb->db_address);
+      dbi_conn_set_option(mdb->db, "port", port);
+      dbi_conn_set_option(mdb->db, "username", mdb->db_user);
+      dbi_conn_set_option(mdb->db, "password", mdb->db_password);
+      dbi_conn_set_option(mdb->db, "dbname", mdb->db_name);
+      break;
+   /* TODO: refactory, one db_name set */
+      case SQL_TYPE_SQLITE:
+      len = strlen(working_directory) + 5;
+      db_dir = (char *)malloc(len);
+      strcpy(db_dir, working_directory);
+      strcat(db_dir, "/");
+      len = strlen(mdb->db_name) + 5;
+      db_name = (char *)malloc(len);
+      strcpy(db_name, mdb->db_name);
+      strcat(db_name, ".db");
+      dbi_conn_set_option(mdb->db, "sqlite_dbdir", db_dir);
+      dbi_conn_set_option(mdb->db, "dbname", db_name);
+      break;
+   case SQL_TYPE_SQLITE3:
+      len = strlen(working_directory) + 5;
+      db_dir = (char *)malloc(len);
+      strcpy(db_dir, working_directory);
+      strcat(db_dir, "/");
+      len = strlen(mdb->db_name) + 5;
+      db_name = (char *)malloc(len);
+      strcpy(db_name, mdb->db_name);
+      strcat(db_name, ".db");
+      dbi_conn_set_option(mdb->db, "sqlite3_dbdir", db_dir);
+      dbi_conn_set_option(mdb->db, "dbname", db_name);
+      Dmsg2(500, "SQLITE: %s %s\n", db_dir, db_name);
+      break;
+   }
+
    /* If connection fails, try at 5 sec intervals for 30 seconds. */
    for (int retry=0; retry < 6; retry++) {

@@ -269,10 +313,15 @@
       sql_query(mdb, "SET datestyle TO 'ISO, YMD'");
       sql_query(mdb, "set standard_conforming_strings=on");
       break;
-   case SQL_TYPE_SQLITE:
-      break;
    }
-
+
+   if(db_dir) {
+      free(db_dir);
+   }
+   if(db_name) {
+      free(db_name);
+   }
+
    V(mutex);
    return 1;
 }
@@ -430,9 +479,7 @@
    DBI_ROW row = NULL; // by default, return NULL

    Dmsg0(500, "my_dbi_fetch_row start\n");
-
-
-   if (!mdb->row || mdb->row_size < mdb->num_fields) {
+   if ((!mdb->row || mdb->row_size < mdb->num_fields) && mdb->num_rows > 0) {
       int num_fields = mdb->num_fields;
       Dmsg1(500, "we have need space of %d bytes\n", sizeof(char *) *
mdb->num_fields);

@@ -458,7 +505,7 @@
    }

    // if still within the result set
-   if (mdb->row_number <= mdb->num_rows) {
+   if (mdb->row_number <= mdb->num_rows && mdb->row_number !=
DBI_ERROR_BADPTR) {
       Dmsg2(500, "my_dbi_fetch_row row number '%d' is acceptable
(1..%d)\n", mdb->row_number, mdb->num_rows);
       // get each value from this row
       for (j = 0; j < mdb->num_fields; j++) {
@@ -627,8 +674,7 @@
 {

    DBI_FIELD_GET *f;
-   db_lock(mdb);
-   int i = 0;
+   db_lock(mdb);
    if (mdb->result) {
       Dmsg1(500, "my_dbi_free_result result '%p'\n", mdb->result);
       dbi_result_free(mdb->result);
Index: src/cats/sql_cmds.h
===================================================================
--- src/cats/sql_cmds.h	(revision 7553)
+++ src/cats/sql_cmds.h	(working copy)
@@ -70,9 +70,9 @@
 extern const char CATS_IMP_EXP *uar_jobid_fileindex_from_table;
 extern const char CATS_IMP_EXP *uar_sel_jobid_temp;

-extern const char CATS_IMP_EXP *create_deltabs[3];
+extern const char CATS_IMP_EXP *create_deltabs[4];

-extern const char CATS_IMP_EXP *uar_file[3];
-extern const char CATS_IMP_EXP *uar_create_temp[3];
-extern const char CATS_IMP_EXP *uar_create_temp1[3];
-extern const char CATS_IMP_EXP *uar_jobid_fileindex_from_dir[3];
+extern const char CATS_IMP_EXP *uar_file[4];
+extern const char CATS_IMP_EXP *uar_create_temp[4];
+extern const char CATS_IMP_EXP *uar_create_temp1[4];
+extern const char CATS_IMP_EXP *uar_jobid_fileindex_from_dir[4];
Index: src/cats/cats.h
===================================================================
--- src/cats/cats.h	(revision 7586)
+++ src/cats/cats.h	(working copy)
@@ -72,7 +72,8 @@
 enum {
    SQL_TYPE_MYSQL      = 0,
    SQL_TYPE_POSTGRESQL = 1,
-   SQL_TYPE_SQLITE     = 2
+   SQL_TYPE_SQLITE     = 2,
+   SQL_TYPE_SQLITE3
 };


Index: src/cats/sql.c
===================================================================
--- src/cats/sql.c	(revision 7553)
+++ src/cats/sql.c	(working copy)
@@ -75,6 +75,8 @@
       db_type = SQL_TYPE_POSTGRESQL;
    } else if (strcasecmp(p, "sqlite") == 0) {
       db_type = SQL_TYPE_SQLITE;
+   } else if (strcasecmp(p, "sqlite3") == 0) {
+      db_type = SQL_TYPE_SQLITE3;
    } else {
       Jmsg1(jcr, M_ABORT, 0, _("Unknown database type: %s\n"), p);
    }
Index: src/cats/sql_cmds.c
===================================================================
--- src/cats/sql_cmds.c	(revision 7553)
+++ src/cats/sql_cmds.c	(working copy)
@@ -421,7 +421,7 @@
 /* ====== ua_prune.c */

 /* List of SQL commands to create temp table and indicies  */
-const char *create_deltabs[3] = {
+const char *create_deltabs[4] = {
    /* MySQL */
    "CREATE TEMPORARY TABLE DelCandidates ("
    "JobId INTEGER UNSIGNED NOT NULL, "
@@ -442,6 +442,13 @@
    "PurgedFiles TINYINT, "
    "FileSetId INTEGER UNSIGNED, "
    "JobFiles INTEGER UNSIGNED, "
+   "JobStatus CHAR)",
+   /* SQLite3 */
+   "CREATE TEMPORARY TABLE DelCandidates ("
+   "JobId INTEGER UNSIGNED NOT NULL, "
+   "PurgedFiles TINYINT, "
+   "FileSetId INTEGER UNSIGNED, "
+   "JobFiles INTEGER UNSIGNED, "
    "JobStatus CHAR)"};

 /* ======= ua_restore.c */
@@ -447,7 +454,7 @@
 /* ======= ua_restore.c */

 /* List Jobs where a particular file is saved */
-const char *uar_file[3] = {
+const char *uar_file[4] = {
    /* Mysql */
    "SELECT Job.JobId as JobId,"
    "CONCAT(Path.Path,Filename.Name) as Name, "
@@ -474,9 +481,18 @@
    "AND Client.ClientId=Job.ClientId "
    "AND Job.JobId=File.JobId "
    "AND Path.PathId=File.PathId AND Filename.FilenameId=File.FilenameId "
+   "AND Filename.Name='%s' ORDER BY StartTime DESC LIMIT 20",
+   /* SQLite3 */
+   "SELECT Job.JobId as JobId,"
+   "Path.Path||Filename.Name as Name, "
+   "StartTime,Type as JobType,JobStatus,JobFiles,JobBytes "
+   "FROM Client,Job,File,Filename,Path WHERE Client.Name='%s' "
+   "AND Client.ClientId=Job.ClientId "
+   "AND Job.JobId=File.JobId "
+   "AND Path.PathId=File.PathId AND Filename.FilenameId=File.FilenameId "
    "AND Filename.Name='%s' ORDER BY StartTime DESC LIMIT 20"};

-const char *uar_create_temp[3] = {
+const char *uar_create_temp[4] = {
    /* Mysql */
    "CREATE TEMPORARY TABLE temp ("
    "JobId INTEGER UNSIGNED NOT NULL,"
@@ -515,9 +531,22 @@
    "VolumeName TEXT,"
    "StartFile INTEGER UNSIGNED,"
    "VolSessionId INTEGER UNSIGNED,"
+   "VolSessionTime INTEGER UNSIGNED)",
+   /* SQLite3 */
+   "CREATE TEMPORARY TABLE temp ("
+   "JobId INTEGER UNSIGNED NOT NULL,"
+   "JobTDate BIGINT UNSIGNED,"
+   "ClientId INTEGER UNSIGNED,"
+   "Level CHAR,"
+   "JobFiles INTEGER UNSIGNED,"
+   "JobBytes BIGINT UNSIGNED,"
+   "StartTime TEXT,"
+   "VolumeName TEXT,"
+   "StartFile INTEGER UNSIGNED,"
+   "VolSessionId INTEGER UNSIGNED,"
    "VolSessionTime INTEGER UNSIGNED)"};

-const char *uar_create_temp1[3] = {
+const char *uar_create_temp1[4] = {
    /* Mysql */
    "CREATE TEMPORARY TABLE temp1 ("
    "JobId INTEGER UNSIGNED NOT NULL,"
@@ -529,6 +558,10 @@
    /* SQLite */
    "CREATE TEMPORARY TABLE temp1 ("
    "JobId INTEGER UNSIGNED NOT NULL,"
+   "JobTDate BIGINT UNSIGNED)",
+   /* SQLite3 */
+   "CREATE TEMPORARY TABLE temp1 ("
+   "JobId INTEGER UNSIGNED NOT NULL,"
    "JobTDate BIGINT UNSIGNED)"};

 /* Query to get all files in a directory -- no recursing
@@ -538,7 +571,7 @@
  *  for each time it was backed up.
  */

-const char *uar_jobid_fileindex_from_dir[3] = {
+const char *uar_jobid_fileindex_from_dir[4] = {
    /* Mysql */
    "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
    "WHERE Job.JobId IN (%s) "
@@ -567,4 +600,14 @@
    "AND Job.ClientId=Client.ClientId "
    "AND Path.PathId=File.Pathid "
    "AND Filename.FilenameId=File.FilenameId "
+   "GROUP BY File.FileIndex ",
+   /* SQLite3 */
+   "SELECT Job.JobId,File.FileIndex FROM Job,File,Path,Filename,Client "
+   "WHERE Job.JobId IN (%s) "
+   "AND Job.JobId=File.JobId "
+   "AND Path.Path='%s' "
+   "AND Client.Name='%s' "
+   "AND Job.ClientId=Client.ClientId "
+   "AND Path.PathId=File.Pathid "
+   "AND Filename.FilenameId=File.FilenameId "
    "GROUP BY File.FileIndex "};


-- 
-----------------------------------------------------------
João Henrique Freitas - joaohf_at_gmail.com
Campinas-SP-Brasil
BSD051283
LPI 1
http://joaohf.pbwiki.com
http://www.livejournal.com/users/joaohf/
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Bacula-devel mailing list
Bacula-devel@xxxxxxxxxxxxxxxxxxxxx
https://lists.sourceforge.net/lists/listinfo/bacula-devel


This mailing list archive is a service of Copilot Consulting.