kurtis.heimerl | e766abb | 2012-11-14 03:51:51 +0000 | [diff] [blame] | 1 | /* |
kurtis.heimerl | f7afddd | 2013-02-15 02:57:54 +0000 | [diff] [blame] | 2 | * Copyright 2010 Kestrel Signal Processing, Inc. |
| 3 | * All rights reserved. |
kurtis.heimerl | e766abb | 2012-11-14 03:51:51 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | |
Max | 2dee3e9 | 2017-01-26 14:57:44 +0100 | [diff] [blame^] | 7 | #include <sqlite3.h> |
kurtis.heimerl | e766abb | 2012-11-14 03:51:51 +0000 | [diff] [blame] | 8 | #include "sqlite3util.h" |
| 9 | |
| 10 | #include <string.h> |
| 11 | #include <unistd.h> |
| 12 | #include <stdio.h> |
| 13 | |
| 14 | |
| 15 | // Wrappers to sqlite operations. |
| 16 | // These will eventually get moved to commonlibs. |
| 17 | |
| 18 | int sqlite3_prepare_statement(sqlite3* DB, sqlite3_stmt **stmt, const char* query) |
| 19 | { |
kurtis.heimerl | f7afddd | 2013-02-15 02:57:54 +0000 | [diff] [blame] | 20 | int src = SQLITE_BUSY; |
| 21 | while (src==SQLITE_BUSY) { |
| 22 | src = sqlite3_prepare_v2(DB,query,strlen(query),stmt,NULL); |
| 23 | if (src==SQLITE_BUSY) { |
| 24 | usleep(100000); |
| 25 | } |
| 26 | } |
| 27 | if (src) { |
| 28 | fprintf(stderr,"sqlite3_prepare_v2 failed for \"%s\": %s\n",query,sqlite3_errmsg(DB)); |
| 29 | sqlite3_finalize(*stmt); |
| 30 | } |
| 31 | return src; |
kurtis.heimerl | e766abb | 2012-11-14 03:51:51 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | int sqlite3_run_query(sqlite3* DB, sqlite3_stmt *stmt) |
| 35 | { |
| 36 | int src = SQLITE_BUSY; |
| 37 | while (src==SQLITE_BUSY) { |
| 38 | src = sqlite3_step(stmt); |
| 39 | if (src==SQLITE_BUSY) { |
| 40 | usleep(100000); |
| 41 | } |
| 42 | } |
| 43 | if ((src!=SQLITE_DONE) && (src!=SQLITE_ROW)) { |
| 44 | fprintf(stderr,"sqlite3_run_query failed: %s: %s\n", sqlite3_sql(stmt), sqlite3_errmsg(DB)); |
| 45 | } |
| 46 | return src; |
| 47 | } |
| 48 | |
| 49 | |
| 50 | bool sqlite3_exists(sqlite3* DB, const char *tableName, |
| 51 | const char* keyName, const char* keyData) |
| 52 | { |
| 53 | size_t stringSize = 100 + strlen(tableName) + strlen(keyName) + strlen(keyData); |
| 54 | char query[stringSize]; |
| 55 | sprintf(query,"SELECT * FROM %s WHERE %s == \"%s\"",tableName,keyName,keyData); |
| 56 | // Prepare the statement. |
| 57 | sqlite3_stmt *stmt; |
| 58 | if (sqlite3_prepare_statement(DB,&stmt,query)) return false; |
| 59 | // Read the result. |
| 60 | int src = sqlite3_run_query(DB,stmt); |
| 61 | sqlite3_finalize(stmt); |
| 62 | // Anything there? |
| 63 | return (src == SQLITE_ROW); |
| 64 | } |
| 65 | |
| 66 | |
| 67 | |
| 68 | bool sqlite3_single_lookup(sqlite3* DB, const char *tableName, |
| 69 | const char* keyName, const char* keyData, |
| 70 | const char* valueName, unsigned &valueData) |
| 71 | { |
| 72 | size_t stringSize = 100 + strlen(valueName) + strlen(tableName) + strlen(keyName) + strlen(keyData); |
| 73 | char query[stringSize]; |
| 74 | sprintf(query,"SELECT %s FROM %s WHERE %s == \"%s\"",valueName,tableName,keyName,keyData); |
| 75 | // Prepare the statement. |
| 76 | sqlite3_stmt *stmt; |
| 77 | if (sqlite3_prepare_statement(DB,&stmt,query)) return false; |
| 78 | // Read the result. |
| 79 | int src = sqlite3_run_query(DB,stmt); |
| 80 | bool retVal = false; |
| 81 | if (src == SQLITE_ROW) { |
| 82 | valueData = (unsigned)sqlite3_column_int64(stmt,0); |
| 83 | retVal = true; |
| 84 | } |
| 85 | sqlite3_finalize(stmt); |
| 86 | return retVal; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | // This function returns an allocated string that must be free'd by the caller. |
| 91 | bool sqlite3_single_lookup(sqlite3* DB, const char* tableName, |
| 92 | const char* keyName, const char* keyData, |
| 93 | const char* valueName, char* &valueData) |
| 94 | { |
| 95 | valueData=NULL; |
| 96 | size_t stringSize = 100 + strlen(valueName) + strlen(tableName) + strlen(keyName) + strlen(keyData); |
| 97 | char query[stringSize]; |
| 98 | sprintf(query,"SELECT %s FROM %s WHERE %s == \"%s\"",valueName,tableName,keyName,keyData); |
| 99 | // Prepare the statement. |
| 100 | sqlite3_stmt *stmt; |
| 101 | if (sqlite3_prepare_statement(DB,&stmt,query)) return false; |
| 102 | // Read the result. |
| 103 | int src = sqlite3_run_query(DB,stmt); |
| 104 | bool retVal = false; |
| 105 | if (src == SQLITE_ROW) { |
| 106 | const char* ptr = (const char*)sqlite3_column_text(stmt,0); |
| 107 | if (ptr) valueData = strdup(ptr); |
| 108 | retVal = true; |
| 109 | } |
| 110 | sqlite3_finalize(stmt); |
| 111 | return retVal; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | // This function returns an allocated string that must be free'd by tha caller. |
| 116 | bool sqlite3_single_lookup(sqlite3* DB, const char* tableName, |
| 117 | const char* keyName, unsigned keyData, |
| 118 | const char* valueName, char* &valueData) |
| 119 | { |
| 120 | valueData=NULL; |
| 121 | size_t stringSize = 100 + strlen(valueName) + strlen(tableName) + strlen(keyName) + 20; |
| 122 | char query[stringSize]; |
| 123 | sprintf(query,"SELECT %s FROM %s WHERE %s == %u",valueName,tableName,keyName,keyData); |
| 124 | // Prepare the statement. |
| 125 | sqlite3_stmt *stmt; |
| 126 | if (sqlite3_prepare_statement(DB,&stmt,query)) return false; |
| 127 | // Read the result. |
| 128 | int src = sqlite3_run_query(DB,stmt); |
| 129 | bool retVal = false; |
| 130 | if (src == SQLITE_ROW) { |
| 131 | const char* ptr = (const char*)sqlite3_column_text(stmt,0); |
| 132 | if (ptr) valueData = strdup(ptr); |
| 133 | retVal = true; |
| 134 | } |
| 135 | sqlite3_finalize(stmt); |
| 136 | return retVal; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | bool sqlite3_command(sqlite3* DB, const char* query) |
| 143 | { |
| 144 | // Prepare the statement. |
| 145 | sqlite3_stmt *stmt; |
| 146 | if (sqlite3_prepare_statement(DB,&stmt,query)) return false; |
| 147 | // Run the query. |
| 148 | int src = sqlite3_run_query(DB,stmt); |
kurtis.heimerl | f7afddd | 2013-02-15 02:57:54 +0000 | [diff] [blame] | 149 | sqlite3_finalize(stmt); |
kurtis.heimerl | e766abb | 2012-11-14 03:51:51 +0000 | [diff] [blame] | 150 | return src==SQLITE_DONE; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | |