db_creation.c

Go to the documentation of this file.
00001 #include "db_creation.h"
00002 
00003 #include "application_config.h"
00004 #include "er_errors.h"
00005 #include "sa_sensor_list_log_db.h"
00006 #include "sqlite_cl2.h"
00007 #include "utils_cl2.h" // for mkdir_p
00008 
00009 #include <glib/gstdio.h>
00010 
00011 gboolean create_database(const char* db_dir,
00012        const char* db_file,
00013        const char* sql,
00014        GError** error)
00015 {
00016   sqlite3* db = NULL;
00017 
00018   if (!mkdir_p(db_dir, error)) {
00019     return FALSE;
00020   }
00021   //logt("log db directory existence ensured");
00022   
00023   // This still allocates a handle, except for those cases in which
00024   // the memory for the handle cannot be allocated. We can hence get
00025   // an error message if "db" is non-NULL.
00026   int errCode = sqlite3_open(db_file, &db);
00027   if (errCode) {
00028     if (error)
00029       *error = gx_error_new(domain_cl2app, code_database_open, "error opening database '%s': %s (%d)", db_file, sqlite_get_error_string(db), errCode);
00030     goto fail;
00031   }
00032   //logg("database opened %d", errCode);
00033 
00034   logt(sql);
00035   errCode = sqlite3_exec(db, sql, NULL, NULL, NULL);
00036   //logg("errCode is %d", errCode);
00037   if (errCode) {
00038     if (error)
00039       *error = gx_error_new(domain_cl2app, code_database_command, "error creating tables for database '%s': %s (%d)", db_file, sqlite_get_error_string(db), errCode);
00040     goto fail;
00041   }
00042   logt("database created");
00043 
00044   // We are not doing anything fancy here and would assume closing to
00045   // succeed. Or if it does not, it is likely not our fault, and not
00046   // much we can do about it.
00047   errCode = sqlite3_close(db);
00048   if (errCode) {
00049     if (error)
00050       *error = gx_error_new(domain_cl2app, code_database_close, "error closing database '%s': (%d)", db_file, errCode);
00051     goto fail;
00052   }
00053   //logt("database session closed");
00054 
00055   return TRUE;
00056 
00057  fail:
00058   // If the database creation should fail in any way, we shall delete
00059   // the potentially incompletely created file, so that at next launch
00060   // there will be a new attempt to create it, possibly under more
00061   // favorable conditions.
00062   rm_file(db_file, NULL);
00063   return FALSE;
00064 }
00065 
00066 gboolean ensure_database_created(const char* db_dir,
00067          const char* db_file,
00068          const char* sql,
00069          GError** error)
00070 {
00071   assert_error_unset(error);
00072 
00073   if (!g_file_test(db_file, G_FILE_TEST_EXISTS)) {
00074     return create_database(db_dir, db_file, sql, error);
00075   }
00076 
00077   return TRUE;
00078 }
00079 
00080 /**
00081 
00082 db_creation.c
00083 
00084 Copyright 2009 Helsinki Institute for Information Technology (HIIT)
00085 and the authors. All rights reserved.
00086 
00087 Authors: Tero Hasu <tero.hasu@hut.fi>
00088 
00089 Permission is hereby granted, free of charge, to any person
00090 obtaining a copy of this software and associated documentation files
00091 (the "Software"), to deal in the Software without restriction,
00092 including without limitation the rights to use, copy, modify, merge,
00093 publish, distribute, sublicense, and/or sell copies of the Software,
00094 and to permit persons to whom the Software is furnished to do so,
00095 subject to the following conditions:
00096 
00097 The above copyright notice and this permission notice shall be
00098 included in all copies or substantial portions of the Software.
00099 
00100 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00101 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00102 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00103 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
00104 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
00105 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00106 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00107 SOFTWARE.
00108 
00109  **/

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:52 2011 by Doxygen 1.6.1