Skip to content

Commit

Permalink
Adding in some code to create a database all nice like
Browse files Browse the repository at this point in the history
  • Loading branch information
ted-gould committed Jan 8, 2014
1 parent 9cd7f18 commit 6c7e0b5
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ include_directories(${JSONGLIB_INCLUDE_DIRS})
pkg_check_modules(DBUSTEST REQUIRED dbustest-1>=14.04.0)
include_directories(${DBUSTEST_INCLUDE_DIRS})

pkg_check_modules(SQLITE REQUIRED sqlite3)
include_directories(${SQLITE_INCLUDE_DIRS})

if(${LOCAL_INSTALL})
set(DBUSSERVICEDIR "${CMAKE_INSTALL_DATADIR}/dbus-1/services/")
else()
Expand Down
12 changes: 12 additions & 0 deletions service/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@

include(UseConstantBuilder)

###########################
# Generated Lib
###########################


set(SERVICE_GENERATED_HEADERS
create-db-sql.h
service-iface.h
)

set(SERVICE_GENERATED_SOURCES
service-iface.c
)

add_constant_template(SERVICE_GENERATED_SOURCES
create-db-sql
create_db_sql
"${CMAKE_CURRENT_SOURCE_DIR}/create-db.sql"
)

add_gdbus_codegen(
OUTFILES SERVICE_GENERATED_SOURCES
NAME service-iface
Expand All @@ -33,6 +43,7 @@ ${JSONGLIB_LIBRARIES}
###########################

add_library(dispatcher-lib STATIC
create-db.c
dispatcher.h
dispatcher.c)

Expand All @@ -41,6 +52,7 @@ target_link_libraries(dispatcher-lib
${GLIB2_LIBRARIES}
${GOBJECT2_LIBRARIES}
${GIO2_LIBRARIES}
${SQLITE_LIBRARIES}
)

###########################
Expand Down
3 changes: 3 additions & 0 deletions service/create-db-sql.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

extern const char * create_db_sql;

60 changes: 60 additions & 0 deletions service/create-db.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@

#include <sqlite3.h>
#include <glib.h>
#include "create-db-sql.h"

sqlite3 *
create_database (void)
{
const gchar * cachedir = g_getenv("URL_DISPATCHER_CACHE_DIR"); /* Mostly for testing */

if (G_LIKELY(cachedir == NULL)) {
cachedir = g_get_user_cache_dir();
}

gchar * urldispatchercachedir = g_build_filename(cachedir, "url-dispatcher", NULL);
if (!g_file_test(urldispatchercachedir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
gint cachedirokay = g_mkdir_with_parents(urldispatchercachedir, 1 << 6 | 1 << 7 | 1 << 8); // 700

if (cachedirokay != 0) {
g_warning("Unable to make or find cache directory '%s'", urldispatchercachedir);
g_free(urldispatchercachedir);
return NULL;
}
}

gchar * dbfilename = g_build_filename(urldispatchercachedir, "urls.db", NULL);
g_free(urldispatchercachedir);

gboolean dbexists = g_file_test(dbfilename, G_FILE_TEST_EXISTS);
int open_status = SQLITE_ERROR;
sqlite3 * db = NULL;

open_status = sqlite3_open(dbfilename, &db);
if (open_status != SQLITE_OK) {
g_warning("Unable to open URL database");
g_free(dbfilename);
if (db != NULL) {
sqlite3_close(db);
}
return NULL;
}

g_free(dbfilename);

if (!dbexists) { /* First usage */
int exec_status = SQLITE_ERROR;
char * failstring = NULL;

exec_status = sqlite3_exec(db, create_db_sql, NULL, NULL, &failstring);

if (exec_status != SQLITE_OK) {
g_warning("Unable to create tables: %s", failstring);
sqlite3_close(db);
return NULL;
}

}

return db;
}

0 comments on commit 6c7e0b5

Please sign in to comment.