24 #include <config/sqlite.h> 25 #include <core/threading/mutex.h> 26 #include <core/exceptions/system.h> 27 #include <core/exceptions/software.h> 45 #define TABLE_HOST_CONFIG "config" 46 #define TABLE_DEFAULT_CONFIG "defaults.config" 48 #define SQL_CREATE_TABLE_HOST_CONFIG \ 49 "CREATE TABLE IF NOT EXISTS config (\n" \ 50 " path TEXT NOT NULL,\n" \ 51 " type TEXT NOT NULL,\n" \ 52 " value NOT NULL,\n" \ 54 " PRIMARY KEY (path)\n" \ 57 #define SQL_CREATE_TABLE_DEFAULT_CONFIG \ 58 "CREATE TABLE IF NOT EXISTS defaults.config (\n" \ 59 " path TEXT NOT NULL,\n" \ 60 " type TEXT NOT NULL,\n" \ 61 " value NOT NULL,\n" \ 63 " PRIMARY KEY (path)\n" \ 66 #define SQL_CREATE_TABLE_MODIFIED_CONFIG \ 67 "CREATE TABLE IF NOT EXISTS modified.config (\n" \ 68 " path TEXT NOT NULL,\n" \ 69 " type TEXT NOT NULL,\n" \ 70 " value NOT NULL,\n" \ 72 " modtype TEXT NOT NULL,\n" \ 73 " oldvalue NOT NULL,\n" \ 74 " PRIMARY KEY (path)\n" \ 77 #define SQL_ATTACH_DEFAULTS \ 78 "ATTACH DATABASE '%s' AS defaults" 80 #define SQL_ATTACH_MODIFIED \ 81 "ATTACH DATABASE ':memory:' AS modified" 83 #define SQL_ATTACH_DUMPED \ 84 "ATTACH DATABASE '%s' AS dumped" 86 #define SQL_DETACH_DUMPED \ 87 "DETACH DATABASE dumped" 89 #define SQL_SELECT_VALUE_TYPE \ 90 "SELECT type, value, 0 AS is_default FROM config WHERE path=? UNION " \ 91 "SELECT type, value, 1 AS is_default FROM defaults.config AS dc " \ 92 "WHERE path=? AND NOT EXISTS " \ 93 "(SELECT path FROM config WHERE dc.path=path)" 95 #define SQL_SELECT_COMPLETE \ 96 "SELECT *, 0 AS is_default FROM config WHERE path LIKE ? UNION " \ 97 "SELECT *, 1 AS is_default FROM defaults.config AS dc " \ 98 "WHERE path LIKE ? AND NOT EXISTS " \ 99 "(SELECT path FROM config WHERE dc.path = path) " \ 102 #define SQL_SELECT_TYPE \ 103 "SELECT type, 0 AS is_default FROM config WHERE path=? UNION " \ 104 "SELECT type, 1 AS is_default FROM defaults.config AS dc " \ 105 "WHERE path=? AND NOT EXISTS " \ 106 "(SELECT path FROM config WHERE dc.path = path)" 108 #define SQL_SELECT_COMMENT \ 109 "SELECT comment, 0 AS is_default FROM config WHERE path=?" 111 #define SQL_SELECT_DEFAULT_COMMENT \ 112 "SELECT comment, 1 AS is_default FROM defaults.config AS dc " \ 115 #define SQL_UPDATE_VALUE \ 116 "UPDATE config SET value=? WHERE path=?" 118 #define SQL_UPDATE_DEFAULT_VALUE \ 119 "UPDATE defaults.config SET value=? WHERE path=?" 121 #define SQL_UPDATE_COMMENT \ 122 "UPDATE config SET comment=? WHERE path=?" 124 #define SQL_UPDATE_DEFAULT_COMMENT \ 125 "UPDATE defaults.config SET comment=? WHERE path=?" 127 #define SQL_INSERT_VALUE \ 128 "INSERT INTO config (path, type, value) VALUES (?, ?, ?)" 130 #define SQL_INSERT_DEFAULT_VALUE \ 131 "INSERT INTO defaults.config (path, type, value) VALUES (?, ?, ?)" 133 #define SQL_SELECT_ALL \ 134 "SELECT *, 0 AS is_default FROM config UNION " \ 135 "SELECT *, 1 AS is_default FROM defaults.config AS dc " \ 136 "WHERE NOT EXISTS " \ 137 "(SELECT path FROM config WHERE dc.path = path) " \ 140 #define SQL_SELECT_ALL_DEFAULT \ 141 "SELECT *, 1 AS is_default FROM defaults.config" 143 #define SQL_SELECT_ALL_HOSTSPECIFIC \ 144 "SELECT *, 0 AS is_default FROM config" 146 #define SQL_DELETE_VALUE \ 147 "DELETE FROM config WHERE path=?" 149 #define SQL_DELETE_DEFAULT_VALUE \ 150 "DELETE FROM defaults.config WHERE path=?" 152 #define SQL_UPDATE_DEFAULT_DB \ 153 "INSERT INTO config SELECT * FROM defaults.config AS dc " \ 154 "WHERE NOT EXISTS (SELECT path from config WHERE path = dc.path)" 156 #define SQL_UPDATE_MODIFIED_DB_ADDED \ 157 "INSERT INTO modified.config " \ 158 " SELECT duc.*,'added' AS modtype, duc.value " \ 159 " FROM dumped.config AS duc " \ 160 " WHERE NOT EXISTS (SELECT dc.path FROM defaults.config AS dc " \ 161 " WHERE dc.path=duc.path) " \ 164 #define SQL_UPDATE_MODIFIED_DB_ERASED \ 165 "INSERT INTO modified.config " \ 166 " SELECT dc.*,'erased' AS modtype, dc.value " \ 167 " FROM defaults.config AS dc " \ 168 " WHERE NOT EXISTS (SELECT duc.path FROM dumped.config AS duc " \ 169 " WHERE duc.path=dc.path) " \ 172 #define SQL_UPDATE_MODIFIED_DB_CHANGED \ 173 "INSERT INTO modified.config " \ 174 " SELECT duc.*,'changed' AS modtype, dc.value " \ 175 " FROM dumped.config AS duc, defaults.config AS dc " \ 176 " WHERE duc.path = dc.path " \ 177 " AND (dc.type != duc.type OR dc.value != duc.value) " \ 180 #define SQL_COPY_DUMP \ 181 "DELETE FROM defaults.config; " \ 182 "INSERT INTO defaults.config SELECT * FROM dumped.config" 184 #define SQL_SELECT_MODIFIED_ALL \ 185 "SELECT * FROM modified.config" 187 #define MEMORY_DUMP_DB_NAME "file:tmp_dump_db?mode=memory&cache=shared" 211 __userconfdir = NULL;
212 __default_file = NULL;
213 __default_sql = NULL;
227 const char *userconfdir)
232 __sysconfdir = strdup(sysconfdir);
233 __default_file = NULL;
234 __default_sql = NULL;
236 if (userconfdir != NULL) {
237 __userconfdir = strdup(userconfdir);
239 const char *homedir = getenv(
"HOME");
240 if (homedir == NULL) {
241 __userconfdir = strdup(sysconfdir);
243 if (asprintf(&__userconfdir,
"%s/%s", homedir, USERDIR) == -1) {
244 __userconfdir = strdup(sysconfdir);
255 if ( sqlite3_close(db) == SQLITE_BUSY ) {
256 printf(
"Boom, we are dead, database cannot be closed " 257 "because there are open handles\n");
261 if (__host_file) free(__host_file);
262 if (__default_file) free(__default_file);
263 if (__default_sql) free(__default_sql);
264 if (__sysconfdir) free(__sysconfdir);
265 if (__userconfdir) free(__userconfdir);
305 SQLiteConfiguration::init_dbs()
308 if ( (sqlite3_exec(db, SQL_CREATE_TABLE_HOST_CONFIG, NULL, NULL, &errmsg) != SQLITE_OK) ||
309 (sqlite3_exec(db, SQL_CREATE_TABLE_DEFAULT_CONFIG, NULL, NULL, &errmsg) != SQLITE_OK) ) {
326 std::string tisql =
"PRAGMA table_info(\"";
331 if ( sqlite3_prepare(tdb, tisql.c_str(), -1, &stmt, 0) != SQLITE_OK ) {
334 std::string value_query =
"SELECT 'INSERT INTO ' || '\"";
335 value_query += table_name;
336 value_query +=
"\"' || ' VALUES(' || ";
337 int rv = sqlite3_step(stmt);
338 while ( rv == SQLITE_ROW ) {
339 value_query +=
"quote(\"";
340 value_query += (
const char *)sqlite3_column_text(stmt, 1);
341 value_query +=
"\") || ";
342 rv = sqlite3_step(stmt);
343 if ( rv == SQLITE_ROW ) {
344 value_query +=
" ',' || ";
347 value_query +=
"')' FROM ";
348 value_query += table_name;
349 sqlite3_finalize(stmt);
352 if ( sqlite3_prepare(tdb, value_query.c_str(), -1, &vstmt, 0) != SQLITE_OK ) {
355 while ( sqlite3_step(vstmt) == SQLITE_ROW ) {
356 fprintf(f,
"%s;\n", sqlite3_column_text(vstmt, 0));
358 sqlite3_finalize(vstmt);
362 SQLiteConfiguration::dump(::sqlite3 *tdb,
const char *dumpfile)
364 FILE *f = fopen(dumpfile,
"w");
369 fprintf(f,
"BEGIN TRANSACTION;\n");
371 const char *sql =
"SELECT name, sql FROM sqlite_master " 372 "WHERE sql NOT NULL AND type=='table'";
374 if ( (sqlite3_prepare(tdb, sql, -1, &stmt, 0) != SQLITE_OK) || ! stmt ) {
377 while ( sqlite3_step(stmt) == SQLITE_ROW ) {
378 fprintf(f,
"%s;\n", sqlite3_column_text(stmt, 1));
379 dump_table(f, tdb, (
const char *)sqlite3_column_text(stmt, 0));
381 sqlite3_finalize(stmt);
383 fprintf(f,
"COMMIT;\n");
396 if ( __default_sql ) {
398 if ( sqlite3_open(__default_file, &tdb) == SQLITE_OK ) {
400 dump(tdb, __default_sql);
411 SQLiteConfiguration::import(::sqlite3 *tdb,
const char *dumpfile)
413 FILE *f = fopen(dumpfile,
"r");
424 while (! feof(f) && (i <
sizeof(line) - 1)) {
425 if (fread(&(line[i]), 1, 1, f) == 1) {
427 if ( (i > 2) && (line[i-1] ==
'\n') && (line[i-2] ==
';') ) {
435 if ( line[0] != 0 ) {
436 if ( sqlite3_exec(tdb, line, 0, 0, &errmsg) != SQLITE_OK ) {
438 sqlite3_free(errmsg);
449 SQLiteConfiguration::import_default(
const char *default_sql)
453 if ( sqlite3_open(MEMORY_DUMP_DB_NAME, &dump_db) == SQLITE_OK ) {
454 import(dump_db, default_sql);
455 sqlite3_close(dump_db);
463 if ( asprintf(&attach_sql, SQL_ATTACH_DUMPED, MEMORY_DUMP_DB_NAME) == -1 ) {
466 if ( sqlite3_exec(db, attach_sql, NULL, NULL, &errmsg) != SQLITE_OK ) {
469 sqlite3_free(errmsg);
475 if ( (sqlite3_exec(db, SQL_ATTACH_MODIFIED, NULL, NULL, &errmsg) != SQLITE_OK) ||
476 (sqlite3_exec(db, SQL_CREATE_TABLE_MODIFIED_CONFIG, NULL, NULL, &errmsg) != SQLITE_OK) ) {
478 sqlite3_free(errmsg);
483 if ( (sqlite3_exec(db, SQL_UPDATE_MODIFIED_DB_ADDED, NULL, NULL, &errmsg) != SQLITE_OK) ||
484 (sqlite3_exec(db, SQL_UPDATE_MODIFIED_DB_ERASED, NULL, NULL, &errmsg) != SQLITE_OK) ||
485 (sqlite3_exec(db, SQL_UPDATE_MODIFIED_DB_CHANGED, NULL, NULL, &errmsg) != SQLITE_OK) ) {
487 sqlite3_free(errmsg);
492 if ( (sqlite3_exec(db, SQL_COPY_DUMP, NULL, NULL, &errmsg) != SQLITE_OK) ) {
494 sqlite3_free(errmsg);
499 if ( sqlite3_exec(db, SQL_DETACH_DUMPED, NULL, NULL, &errmsg) != SQLITE_OK ) {
501 sqlite3_free(errmsg);
513 const char *sql =
"BEGIN DEFERRED TRANSACTION;";
515 sql =
"BEGIN IMMEDIATE TRANSACTION;";
517 sql =
"BEGIN EXCLUSIVE TRANSACTION;";
521 if ( (sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK) ) {
530 const char *sql =
"COMMIT TRANSACTION;";
533 if ( (sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK) ) {
543 const char *sql =
"ROLLBACK TRANSACTION;";
546 if ( (sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK) ) {
552 SQLiteConfiguration::attach_default(
const char *db_file)
556 if ( asprintf(&attach_sql, SQL_ATTACH_DEFAULTS, db_file) == -1 ) {
559 if (sqlite3_exec(db, attach_sql, NULL, NULL, &errmsg) != SQLITE_OK) {
561 ce.
append(
"Failed to attach default file (%s)", db_file);
574 if (__default_file) free(__default_file);
575 if (__default_sql) free(__default_sql);
576 __default_file = NULL;
577 __default_sql = NULL;
579 const char *try_paths[] = {__sysconfdir, __userconfdir};
580 int try_paths_len = 2;
582 char *host_name = NULL;
584 if (strcmp(file_path,
":memory:") == 0) {
585 __host_file = strdup(
":memory:");
587 if (sqlite3_open(file_path, &db) != SQLITE_OK) {
589 ce.
append(
"Failed to open memory database");
594 if ( asprintf(&host_name,
"%s.db", hostinfo.
short_name()) == -1 ) {
600 for (
int i = 0; i < try_paths_len; ++i) {
602 if (asprintf(&path,
"%s/%s", try_paths[i], host_name) != -1) {
603 if (sqlite3_open(path, &db) == SQLITE_OK) {
613 if (__host_file == NULL) {
615 ce.
append(
"Failed to open host db (paths)");
616 if (host_name) free(host_name);
620 if (file_path == NULL) {
621 file_path =
"default.sql";
625 if (strcmp(file_path,
":memory:") == 0) {
627 attach_default(
":memory:");
629 if (host_name) free(host_name);
632 __default_file = strdup(
":memory:");
634 if (file_path[0] ==
'/') {
636 __default_sql = strdup(file_path);
639 for (
int i = 0; i < try_paths_len; ++i) {
641 if (asprintf(&path,
"%s/%s", try_paths[i], file_path) != -1) {
642 if (access(path, F_OK | R_OK) == 0) {
643 __default_sql = path;
656 size_t len = strlen(file_path);
657 if (fnmatch(
"*.sql", file_path, FNM_PATHNAME) == 0) {
658 defaults_db = (
char *)calloc(1, len);
659 strncpy(defaults_db, file_path, len - 3);
660 strcat(defaults_db,
"db");
662 defaults_db = (
char *)calloc(1, len + 4);
663 strcpy(defaults_db, file_path);
664 strcat(defaults_db,
".db");
667 if (defaults_db[0] ==
'/') {
669 attach_default(defaults_db);
670 __default_file = defaults_db;
672 if (host_name) free(host_name);
678 for (
int i = 0; i < try_paths_len; ++i) {
680 if (asprintf(&path,
"%s/%s", try_paths[i], defaults_db) != -1) {
682 attach_default(path);
683 __default_file = path;
693 if (__default_file == NULL) {
694 if (host_name) free(host_name);
701 if ( __default_sql ) import_default(__default_sql);
702 if (host_name) free(host_name);
723 while ( i->
next() ) {
726 }
else if ( i->
is_int() ) {
751 if ( sqlite3_prepare(db, SQL_SELECT_TYPE, -1, &stmt, &tail) != SQLITE_OK ) {
755 if ( sqlite3_bind_text(stmt, 1, path, -1, NULL) != SQLITE_OK ) {
759 if ( sqlite3_bind_text(stmt, 2, path, -1, NULL) != SQLITE_OK ) {
763 e = ( sqlite3_step(stmt) == SQLITE_ROW );
764 sqlite3_finalize(stmt);
780 if ( sqlite3_prepare(db, SQL_SELECT_TYPE, -1, &stmt, &tail) != SQLITE_OK ) {
784 if ( sqlite3_bind_text(stmt, 1, path, -1, NULL) != SQLITE_OK ) {
788 if ( sqlite3_bind_text(stmt, 2, path, -1, NULL) != SQLITE_OK ) {
792 if ( sqlite3_step(stmt) == SQLITE_ROW ) {
793 s = (
char *)sqlite3_column_text(stmt, 0);
794 sqlite3_finalize(stmt);
798 sqlite3_finalize(stmt);
814 if ( sqlite3_prepare(db, SQL_SELECT_COMMENT, -1, &stmt, &tail) != SQLITE_OK ) {
818 if ( sqlite3_bind_text(stmt, 1, path, -1, NULL) != SQLITE_OK ) {
822 if ( sqlite3_step(stmt) == SQLITE_ROW ) {
823 s = (
char *)sqlite3_column_text(stmt, 0);
824 sqlite3_finalize(stmt);
828 sqlite3_finalize(stmt);
844 if ( sqlite3_prepare(db, SQL_SELECT_DEFAULT_COMMENT, -1, &stmt, &tail) != SQLITE_OK ) {
848 if ( sqlite3_bind_text(stmt, 1, path, -1, NULL) != SQLITE_OK ) {
852 if ( sqlite3_step(stmt) == SQLITE_ROW ) {
853 s = (
char *)sqlite3_column_text(stmt, 0);
854 sqlite3_finalize(stmt);
858 sqlite3_finalize(stmt);
875 return (
get_type(path) ==
"unsigned int");
896 return (
get_type(path) ==
"string");
915 if ( sqlite3_prepare(db, SQL_SELECT_TYPE, -1, &stmt, &tail) != SQLITE_OK ) {
919 if ( sqlite3_bind_text(stmt, 1, path, -1, NULL) != SQLITE_OK ) {
923 if ( sqlite3_bind_text(stmt, 2, path, -1, NULL) != SQLITE_OK ) {
927 e = ( (sqlite3_step(stmt) == SQLITE_ROW) && (sqlite3_column_int(stmt, 1) == 1 ));
928 sqlite3_finalize(stmt);
940 SQLiteConfiguration::get_typed_value(
const char *path,
946 if ( sqlite3_prepare(db, SQL_SELECT_VALUE_TYPE, -1, &stmt, &tail) != SQLITE_OK ) {
949 if ( sqlite3_bind_text(stmt, 1, path, -1, NULL) != SQLITE_OK ) {
952 if ( sqlite3_bind_text(stmt, 2, path, -1, NULL) != SQLITE_OK ) {
956 if ( sqlite3_step(stmt) == SQLITE_ROW ) {
957 if ( type == NULL ) {
961 if (strcmp((
char *)sqlite3_column_text(stmt, 0), type) != 0) {
963 sqlite3_finalize(stmt);
970 sqlite3_finalize(stmt);
982 stmt = get_typed_value(path,
"float");
983 float f = (float)sqlite3_column_double(stmt, 1);
984 sqlite3_finalize(stmt);
1001 stmt = get_typed_value(path,
"unsigned int");
1002 int i = sqlite3_column_int(stmt, 1);
1003 sqlite3_finalize(stmt);
1024 stmt = get_typed_value(path,
"int");
1025 int i = sqlite3_column_int(stmt, 1);
1026 sqlite3_finalize(stmt);
1043 stmt = get_typed_value(path,
"bool");
1044 int i = sqlite3_column_int(stmt, 1);
1045 sqlite3_finalize(stmt);
1061 stmt = get_typed_value(path,
"string");
1062 const char *c = (
char *)sqlite3_column_text(stmt, 1);
1064 sqlite3_finalize(stmt);
1069 e.
append(
"SQLiteConfiguration::get_string: Fetching %s failed.", path);
1084 std::vector<unsigned int>
1103 std::vector<std::string>
1116 if ( sqlite3_prepare(db, SQL_SELECT_COMPLETE, -1, &stmt, &tail) != SQLITE_OK ) {
1119 if ( sqlite3_bind_text(stmt, 1, path, -1, NULL) != SQLITE_OK ) {
1122 if ( sqlite3_bind_text(stmt, 2, path, -1, NULL) != SQLITE_OK ) {
1131 SQLiteConfiguration::prepare_update(
const char *sql,
1137 if ( sqlite3_prepare(db, sql, -1, &stmt, &tail) != SQLITE_OK ) {
1140 if ( sqlite3_bind_text(stmt, 2, path, -1, NULL) != SQLITE_OK ) {
1142 sqlite3_finalize(stmt);
1151 SQLiteConfiguration::prepare_insert_value(
const char *sql,
const char *type,
1157 if ( sqlite3_prepare(db, sql, -1, &stmt, &tail) != SQLITE_OK ) {
1160 if ( (sqlite3_bind_text(stmt, 1, path, -1, NULL) != SQLITE_OK) ||
1161 (sqlite3_bind_text(stmt, 2, type, -1, NULL) != SQLITE_OK) ) {
1163 sqlite3_finalize(stmt);
1172 SQLiteConfiguration::execute_insert_or_update(sqlite3_stmt *stmt)
1174 if ( sqlite3_step(stmt) != SQLITE_DONE ) {
1176 sqlite3_finalize(stmt);
1185 sqlite3_stmt *stmt = NULL;
1190 stmt = prepare_update(SQL_UPDATE_VALUE, path);
1191 if ( (sqlite3_bind_double(stmt, 1, f) != SQLITE_OK) ) {
1193 sqlite3_finalize(stmt);
1197 execute_insert_or_update(stmt);
1198 sqlite3_finalize(stmt);
1200 if ( stmt != NULL ) sqlite3_finalize(stmt);
1205 if ( sqlite3_changes(db) == 0 ) {
1209 stmt = prepare_insert_value(SQL_INSERT_VALUE,
"float", path);
1210 if ( (sqlite3_bind_double(stmt, 3, f) != SQLITE_OK) ) {
1212 sqlite3_finalize(stmt);
1216 execute_insert_or_update(stmt);
1217 sqlite3_finalize(stmt);
1219 if ( stmt != NULL ) sqlite3_finalize(stmt);
1234 sqlite3_stmt *stmt = NULL;
1239 stmt = prepare_update(SQL_UPDATE_VALUE, path);
1240 if ( (sqlite3_bind_int(stmt, 1, uint) != SQLITE_OK) ) {
1242 sqlite3_finalize(stmt);
1246 execute_insert_or_update(stmt);
1247 sqlite3_finalize(stmt);
1249 if ( stmt != NULL ) sqlite3_finalize(stmt);
1254 if ( sqlite3_changes(db) == 0 ) {
1258 stmt = prepare_insert_value(SQL_INSERT_VALUE,
"unsigned int", path);
1259 if ( (sqlite3_bind_int(stmt, 3, uint) != SQLITE_OK) ) {
1261 sqlite3_finalize(stmt);
1265 execute_insert_or_update(stmt);
1266 sqlite3_finalize(stmt);
1268 if ( stmt != NULL ) sqlite3_finalize(stmt);
1282 sqlite3_stmt *stmt = NULL;
1287 stmt = prepare_update(SQL_UPDATE_VALUE, path);
1288 if ( (sqlite3_bind_int(stmt, 1, i) != SQLITE_OK) ) {
1290 sqlite3_finalize(stmt);
1294 execute_insert_or_update(stmt);
1295 sqlite3_finalize(stmt);
1297 if ( stmt != NULL ) sqlite3_finalize(stmt);
1302 if ( sqlite3_changes(db) == 0 ) {
1306 stmt = prepare_insert_value(SQL_INSERT_VALUE,
"int", path);
1307 if ( (sqlite3_bind_int(stmt, 3, i) != SQLITE_OK) ) {
1309 sqlite3_finalize(stmt);
1313 execute_insert_or_update(stmt);
1314 sqlite3_finalize(stmt);
1316 if ( stmt != NULL ) sqlite3_finalize(stmt);
1331 sqlite3_stmt *stmt = NULL;
1336 stmt = prepare_update(SQL_UPDATE_VALUE, path);
1337 if ( (sqlite3_bind_int(stmt, 1, (b ? 1 : 0)) != SQLITE_OK) ) {
1339 sqlite3_finalize(stmt);
1343 execute_insert_or_update(stmt);
1344 sqlite3_finalize(stmt);
1346 if ( stmt != NULL ) sqlite3_finalize(stmt);
1351 if ( sqlite3_changes(db) == 0 ) {
1355 stmt = prepare_insert_value(SQL_INSERT_VALUE,
"bool", path);
1356 if ( (sqlite3_bind_int(stmt, 3, (b ? 1 : 0)) != SQLITE_OK) ) {
1358 sqlite3_finalize(stmt);
1362 execute_insert_or_update(stmt);
1363 sqlite3_finalize(stmt);
1365 if ( stmt != NULL ) sqlite3_finalize(stmt);
1381 sqlite3_stmt *stmt = NULL;
1385 size_t s_length = strlen(s);
1388 stmt = prepare_update(SQL_UPDATE_VALUE, path);
1389 if ( (sqlite3_bind_text(stmt, 1, s, s_length, SQLITE_STATIC) != SQLITE_OK) ) {
1391 sqlite3_finalize(stmt);
1395 execute_insert_or_update(stmt);
1396 sqlite3_finalize(stmt);
1398 if ( stmt != NULL ) sqlite3_finalize(stmt);
1403 if ( sqlite3_changes(db) == 0 ) {
1407 stmt = prepare_insert_value(SQL_INSERT_VALUE,
"string", path);
1408 if ( (sqlite3_bind_text(stmt, 3, s, s_length, SQLITE_STATIC) != SQLITE_OK) ) {
1410 sqlite3_finalize(stmt);
1414 execute_insert_or_update(stmt);
1415 sqlite3_finalize(stmt);
1417 if ( stmt != NULL ) sqlite3_finalize(stmt);
1475 sqlite3_stmt *stmt = NULL;
1479 size_t s_length = strlen(comment);
1482 stmt = prepare_update(SQL_UPDATE_COMMENT, path);
1483 if ( (sqlite3_bind_text(stmt, 1, comment, s_length, SQLITE_STATIC) != SQLITE_OK) ) {
1485 sqlite3_finalize(stmt);
1489 execute_insert_or_update(stmt);
1490 sqlite3_finalize(stmt);
1492 if ( stmt != NULL ) sqlite3_finalize(stmt);
1497 if ( sqlite3_changes(db) == 0 ) {
1522 if ( sqlite3_prepare(db, SQL_DELETE_VALUE, -1, &stmt, &tail) != SQLITE_OK ) {
1525 if ( sqlite3_bind_text(stmt, 1, path, -1, NULL) != SQLITE_OK ) {
1527 sqlite3_finalize(stmt);
1531 if ( sqlite3_step(stmt) != SQLITE_DONE ) {
1533 sqlite3_finalize(stmt);
1537 sqlite3_finalize(stmt);
1546 sqlite3_stmt *stmt = NULL;
1551 stmt = prepare_update(SQL_UPDATE_DEFAULT_VALUE, path);
1552 if ( (sqlite3_bind_double(stmt, 1, f) != SQLITE_OK) ) {
1554 sqlite3_finalize(stmt);
1558 execute_insert_or_update(stmt);
1559 sqlite3_finalize(stmt);
1561 if ( stmt != NULL ) sqlite3_finalize(stmt);
1566 if ( sqlite3_changes(db) == 0 ) {
1570 stmt = prepare_insert_value(SQL_INSERT_DEFAULT_VALUE,
"float", path);
1571 if ( (sqlite3_bind_double(stmt, 3, f) != SQLITE_OK) ) {
1573 sqlite3_finalize(stmt);
1577 execute_insert_or_update(stmt);
1578 sqlite3_finalize(stmt);
1580 if ( stmt != NULL ) sqlite3_finalize(stmt);
1595 sqlite3_stmt *stmt = NULL;
1600 stmt = prepare_update(SQL_UPDATE_DEFAULT_VALUE, path);
1601 if ( (sqlite3_bind_int(stmt, 1, uint) != SQLITE_OK) ) {
1603 sqlite3_finalize(stmt);
1607 execute_insert_or_update(stmt);
1608 sqlite3_finalize(stmt);
1610 if ( stmt != NULL ) sqlite3_finalize(stmt);
1615 if ( sqlite3_changes(db) == 0 ) {
1619 stmt = prepare_insert_value(SQL_INSERT_DEFAULT_VALUE,
"unsigned int", path);
1620 if ( (sqlite3_bind_int(stmt, 3, uint) != SQLITE_OK) ) {
1622 sqlite3_finalize(stmt);
1626 execute_insert_or_update(stmt);
1627 sqlite3_finalize(stmt);
1629 if ( stmt != NULL ) sqlite3_finalize(stmt);
1643 sqlite3_stmt *stmt = NULL;
1647 stmt = prepare_update(SQL_UPDATE_DEFAULT_VALUE, path);
1648 if ( (sqlite3_bind_int(stmt, 1, i) != SQLITE_OK) ) {
1650 sqlite3_finalize(stmt);
1654 execute_insert_or_update(stmt);
1655 sqlite3_finalize(stmt);
1657 if ( stmt != NULL ) sqlite3_finalize(stmt);
1662 if ( sqlite3_changes(db) == 0 ) {
1665 stmt = prepare_insert_value(SQL_INSERT_DEFAULT_VALUE,
"int", path);
1666 if ( (sqlite3_bind_int(stmt, 3, i) != SQLITE_OK) ) {
1668 sqlite3_finalize(stmt);
1672 execute_insert_or_update(stmt);
1673 sqlite3_finalize(stmt);
1675 if ( stmt != NULL ) sqlite3_finalize(stmt);
1690 sqlite3_stmt *stmt = NULL;
1695 stmt = prepare_update(SQL_UPDATE_DEFAULT_VALUE, path);
1696 if ( (sqlite3_bind_int(stmt, 1, (b ? 1 : 0)) != SQLITE_OK) ) {
1698 sqlite3_finalize(stmt);
1702 execute_insert_or_update(stmt);
1703 sqlite3_finalize(stmt);
1705 if ( stmt != NULL ) sqlite3_finalize(stmt);
1710 if ( sqlite3_changes(db) == 0 ) {
1714 stmt = prepare_insert_value(SQL_INSERT_DEFAULT_VALUE,
"bool", path);
1715 if ( (sqlite3_bind_int(stmt, 3, (b ? 1 : 0)) != SQLITE_OK) ) {
1717 sqlite3_finalize(stmt);
1721 execute_insert_or_update(stmt);
1722 sqlite3_finalize(stmt);
1724 if ( stmt != NULL ) sqlite3_finalize(stmt);
1740 sqlite3_stmt *stmt = NULL;
1743 size_t s_length = strlen(s);
1746 stmt = prepare_update(SQL_UPDATE_DEFAULT_VALUE, path);
1747 if ( (sqlite3_bind_text(stmt, 1, s, s_length, SQLITE_STATIC) != SQLITE_OK) ) {
1749 sqlite3_finalize(stmt);
1753 execute_insert_or_update(stmt);
1754 sqlite3_finalize(stmt);
1756 if ( stmt != NULL ) sqlite3_finalize(stmt);
1761 if ( sqlite3_changes(db) == 0 ) {
1765 stmt = prepare_insert_value(SQL_INSERT_DEFAULT_VALUE,
"string", path);
1766 if ( (sqlite3_bind_text(stmt, 3, s, s_length, SQLITE_STATIC) != SQLITE_OK) ) {
1768 sqlite3_finalize(stmt);
1772 execute_insert_or_update(stmt);
1773 sqlite3_finalize(stmt);
1775 if ( stmt != NULL ) sqlite3_finalize(stmt);
1797 sqlite3_stmt *stmt = NULL;
1800 size_t s_length = strlen(comment);
1803 stmt = prepare_update(SQL_UPDATE_DEFAULT_COMMENT, path);
1804 if ( (sqlite3_bind_text(stmt, 1, comment, s_length, SQLITE_STATIC) != SQLITE_OK) ) {
1806 sqlite3_finalize(stmt);
1810 execute_insert_or_update(stmt);
1811 sqlite3_finalize(stmt);
1813 if ( stmt != NULL ) sqlite3_finalize(stmt);
1818 if ( sqlite3_changes(db) == 0 ) {
1843 if ( sqlite3_prepare(db, SQL_DELETE_DEFAULT_VALUE, -1, &stmt, &tail) != SQLITE_OK ) {
1846 if ( sqlite3_bind_text(stmt, 1, path, -1, NULL) != SQLITE_OK ) {
1848 sqlite3_finalize(stmt);
1852 if ( sqlite3_step(stmt) != SQLITE_DONE ) {
1854 sqlite3_finalize(stmt);
1858 sqlite3_finalize(stmt);
1901 if ( sqlite3_prepare(db, SQL_SELECT_ALL, -1, &stmt, &tail) != SQLITE_OK ) {
1922 if ( sqlite3_prepare(db, SQL_SELECT_ALL_DEFAULT, -1, &stmt, &tail) != SQLITE_OK ) {
1942 if ( sqlite3_prepare(db, SQL_SELECT_ALL_HOSTSPECIFIC, -1, &stmt, &tail) != SQLITE_OK ) {
1960 if ( sqlite3_prepare(db, SQL_SELECT_MODIFIED_ALL, -1, &stmt, &tail) != SQLITE_OK ) {
1986 if ( asprintf(&p,
"%s%%", path) == -1 ) {
1990 if ( sqlite3_prepare(db, SQL_SELECT_COMPLETE, -1, &stmt, &tail) != SQLITE_OK ) {
1994 if ( sqlite3_bind_text(stmt, 1, p, -1, NULL) != SQLITE_OK ) {
1998 if ( sqlite3_bind_text(stmt, 2, p, -1, NULL) != SQLITE_OK ) {
2026 if ( __stmt != NULL ) {
2027 sqlite3_finalize(__stmt);
2030 if ( __p != NULL ) {
2043 if ( __stmt == NULL)
return false;
2045 if (sqlite3_step(__stmt) == SQLITE_ROW ) {
2048 sqlite3_finalize(__stmt);
2062 return ( __stmt != NULL);
2072 return (
const char *)sqlite3_column_text(__stmt, 0);
2082 return (
const char *)sqlite3_column_text(__stmt, 1);
2089 return (strcmp(
"float", (
const char *)sqlite3_column_text(__stmt, 1)) == 0);
2096 return (strcmp(
"unsigned int", (
const char *)sqlite3_column_text(__stmt, 1)) == 0);
2102 return (strcmp(
"int", (
const char *)sqlite3_column_text(__stmt, 1)) == 0);
2109 return (strcmp(
"bool", (
const char *)sqlite3_column_text(__stmt, 1)) == 0);
2116 return (strcmp(
"string", (
const char *)sqlite3_column_text(__stmt, 1)) == 0);
2135 return (sqlite3_column_int(__stmt, 4) == 1);
2145 return (
float)sqlite3_column_double(__stmt, 2);
2155 int i = sqlite3_column_int(__stmt, 2);
2170 return sqlite3_column_int(__stmt, 2);
2179 return (sqlite3_column_int(__stmt, 2) != 0);
2188 return (
const char *)sqlite3_column_text(__stmt, 2);
2198 std::vector<unsigned int>
2216 std::vector<std::string>
2228 return (
const char *)sqlite3_column_text(__stmt, 2);
2237 const char *c = (
const char *)sqlite3_column_text(__stmt, 3);
2250 const char *c = (
const char *)sqlite3_column_text(__stmt, 4);
2266 const char *c = (
const char *)sqlite3_column_text(__stmt, 5);
const char * short_name()
Get short hostname (up to first dot).
void lock()
Lock the config.
virtual ValueIterator * iterator()=0
Iterator for all values.
File could not be opened.
virtual void erase(const char *path)
Erase the given value from the configuration.
virtual std::string get_string(const char *path)
Get value from configuration which is of type string.
virtual bool is_default(const char *path)
Check if a value was read from the default config.
ValueIterator * iterator()
Iterator for all values.
virtual bool is_list(const char *path)
Check if a value is a list.
virtual int get_int(const char *path)
Get value from configuration which is of type int.
virtual int get_int() const
Get int value.
virtual bool is_bool() const
Check if current value is a bool.
virtual unsigned int get_uint() const
Get unsigned int value.
SQLite configuration value iterator.
SQLiteConfiguration()
Constructor.
virtual bool get_bool(const char *path)
Get value from configuration which is of type bool.
virtual std::vector< std::string > get_strings() const
Get list of values from configuration which is of type string.
virtual std::vector< float > get_floats(const char *path)
Get list of values from configuration which is of type float.
virtual bool is_bool() const =0
Check if current value is a bool.
Fawkes library namespace.
void unlock()
Unlock the mutex.
Called method has not been implemented.
virtual std::vector< int > get_ints() const
Get list of values from configuration which is of type int.
virtual void set_bools(const char *path, std::vector< bool > &b)
Set new value in configuration of type bool.
Thrown if config could not be opened.
virtual std::string get_default_comment(const char *path)
Get comment of value at given path.
ValueIterator * search(const char *path)
Iterator with search results.
virtual unsigned int get_uint(const char *path)
Get value from configuration which is of type unsigned int.
Thrown if a config entry could not be found.
virtual void set_default_bool(const char *path, bool b)
Set new default value in configuration of type bool.
virtual bool next()=0
Check if there is another element and advance to this if possible.
virtual std::vector< float > get_floats() const
Get list of values from configuration which is of type float.
virtual float get_float() const =0
Get float value.
virtual unsigned int get_uint() const =0
Get unsigned int value.
virtual std::string get_as_string() const
Get value as string.
bool try_lock()
Try to lock the config.
virtual bool is_float() const =0
Check if current value is a float.
virtual size_t get_list_size() const
Get number of elements in list value.
virtual std::string get_comment(const char *path)
Get comment of value at given path.
virtual bool is_string() const
Check if current value is a string.
virtual bool is_int() const =0
Check if current value is a int.
void transaction_begin(transaction_type_t ttype=TRANSACTION_DEFERRED)
Begin SQL Transaction.
virtual void set_default_float(const char *path, float f)
Set new default value in configuration of type float.
virtual void copy(Configuration *copyconf)
Copy all values from the given configuration.
virtual bool get_bool() const =0
Get bool value.
virtual void set_strings(const char *path, std::vector< std::string > &s)
Set new value in configuration of type string.
void try_dump()
Try to dump default configuration.
virtual void load(const char *filename)
Load configuration.
virtual std::string get_string() const
Get string value.
virtual int get_int() const =0
Get int value.
virtual void set_default_string(const char *path, std::string &s)
Set new default value in configuration of type string.
virtual bool is_int(const char *path)
Check if a value is of type int.
virtual ~SQLiteConfiguration()
Destructor.
virtual std::vector< unsigned int > get_uints(const char *path)
Get list of values from configuration which is of type unsigned int.
virtual std::vector< bool > get_bools(const char *path)
Get list of values from configuration which is of type bool.
virtual bool is_string() const =0
Check if current value is a string.
virtual float get_float(const char *path)
Get value from configuration which is of type float.
virtual std::string get_type(const char *path)
Get type of value at given path.
virtual bool is_uint(const char *path)
Check if a value is of type unsigned int.
virtual bool is_uint() const
Check if current value is a unsigned int.
Base class for exceptions in Fawkes.
virtual void set_float(const char *path, float f)
Set new value in configuration of type float.
virtual bool is_string(const char *path)
Check if a value is of type string.
Immediately acquire lock, no more reading or writing possible.
std::string get_oldvalue() const
Get old value (as string).
virtual bool next()
Check if there is another element and advance to this if possible.
virtual void set_default_comment(const char *path, const char *comment)
Set new default comment for existing default configuration value.
Thrown if there a type problem was detected for example if you tried to query a float with get_int()...
virtual void set_int(const char *path, int i)
Set new value in configuration of type int.
virtual bool is_uint() const =0
Check if current value is a unsigned int.
virtual bool is_list() const
Check if a value is a list.
virtual std::vector< int > get_ints(const char *path)
Get list of values from configuration which is of type int.
virtual void set_bool(const char *path, bool b)
Set new value in configuration of type bool.
virtual std::string get_string() const =0
Get string value.
void transaction_rollback()
Rollback SQL Transaction.
virtual ~SQLiteValueIterator()
Destructor.
virtual bool is_int() const
Check if current value is a int.
virtual bool exists(const char *path)
Check if a given value exists.
Generic configuration exception.
virtual const char * path() const =0
Path of value.
bool try_lock()
Tries to lock the mutex.
virtual const char * type() const
Type of value.
virtual std::vector< bool > get_bools() const
Get list of values from configuration which is of type bool.
virtual void unlock()=0
Unlock the config.
virtual bool is_bool(const char *path)
Check if a value is of type bool.
virtual bool is_float(const char *path)
Check if a value is of type float.
virtual ValueIterator * get_value(const char *path)
Get value from configuration.
ValueIterator * iterator_default()
Iterator for all default values.
virtual std::vector< unsigned int > get_uints() const
Get list of values from configuration which is of type unsigned int.
void notify_handlers(const char *path, bool comment_changed=false)
Notify handlers for given path.
virtual bool valid() const
Check if the current element is valid.
std::string get_modtype() const
Get modification type.
virtual bool is_default() const
Check if current value was read from the default config.
ValueIterator * iterator_hostspecific()
Iterator for all host-specific values.
virtual void set_floats(const char *path, std::vector< float > &f)
Set new value in configuration of type float.
Iterator interface to iterate over config values.
void unlock()
Unlock the config.
virtual void set_uint(const char *path, unsigned int uint)
Set new value in configuration of type unsigned int.
virtual void set_default_uint(const char *path, unsigned int uint)
Set new default value in configuration of type unsigned int.
virtual void set_string(const char *path, std::string &s)
Set new value in configuration of type string.
static void dump_table(FILE *f, ::sqlite3 *tdb, const char *table_name)
Dump table.
virtual void set_ints(const char *path, std::vector< int > &i)
Set new value in configuration of type int.
void lock()
Lock this mutex.
virtual void erase_default(const char *path)
Erase the given default value from the configuration.
void transaction_commit()
Commit SQL Transaction.
virtual std::vector< std::string > get_strings(const char *path)
Get list of values from configuration which is of type string.
virtual void set_uints(const char *path, std::vector< unsigned int > &uint)
Set new value in configuration of type unsigned int.
virtual bool get_bool() const
Get bool value.
Mutex mutual exclusion lock.
virtual bool is_float() const
Check if current value is a float.
virtual const char * path() const
Path of value.
virtual float get_float() const
Get float value.
Interface for configuration handling.
virtual void set_comment(const char *path, std::string &comment)
Set new comment for existing value.
virtual std::string get_comment() const
Get comment.
SQLiteValueIterator(::sqlite3_stmt *stmt, void *p=NULL)
Constructor.
void append(const char *format,...)
Append messages to the message list.
Immediately acquire lock, reading remains possible.
virtual void lock()=0
Lock the config.
transaction_type_t
Transaction type.
SQLiteValueIterator * modified_iterator()
Iterator for modified values.
virtual void set_default_int(const char *path, int i)
Set new default value in configuration of type int.