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