Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include <stdarg.h>
00041 #include <string.h>
00042 #include <stdio.h>
00043 #include <time.h>
00044 #include <unistd.h>
00045
00046 #include <sqlite3.h>
00047
00048 #include "ksm/dbsdef.h"
00049 #include "ksm/database.h"
00050 #include "ksm/debug.h"
00051 #include "ksm/memory.h"
00052 #include "ksm/message.h"
00053 #include "ksm/string_util.h"
00054
00055 #define MIN(x, y) ((x) < (y) ? (x) : (y))
00056 #define MAX(x, y) ((x) > (y) ? (x) : (y))
00057
00058
00059 int sqlite3_my_step(sqlite3_stmt *pStmt)
00060 {
00061 int rc;
00062 struct timeval tv;
00063
00064 rc = sqlite3_step(pStmt);
00065
00066 while (rc == SQLITE_LOCKED || rc == SQLITE_BUSY) {
00067 tv.tv_sec = 1;
00068 tv.tv_usec = 0;
00069 select(0, NULL, NULL, NULL, &tv);
00070
00071 rc = sqlite3_step(pStmt);
00072 }
00073
00074 return rc;
00075
00076 }
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099 static int DbExecuteSqlStatement(DB_HANDLE handle, const char* stmt_str, DB_RESULT* result)
00100 {
00101 int rc;
00102 DbgOutput(DBG_M_SQL, "%s\n", stmt_str);
00103 rc = sqlite3_prepare_v2((sqlite3*) handle, stmt_str, -1, &((*result)->data), 0);
00104 if( rc != SQLITE_OK )
00105 {
00106 return rc;
00107 }
00108
00109 return sqlite3_step((*result)->data);
00110 }
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 int DbExecuteSql(DB_HANDLE handle, const char* stmt_str, DB_RESULT* result)
00139 {
00140 const char* errmsg = NULL;
00141 int status = 0;
00142
00143
00144
00145 if ((!handle) || (!stmt_str) || (*stmt_str == '\0') || (! result)) {
00146 status = MsgLog(DBS_INVARG, "DbExecuteSql");
00147 return status;
00148 }
00149
00150
00151
00152 *result = (DB_RESULT) MemCalloc(1, sizeof(struct db_result));
00153 (*result)->magic = DB_RESULT_MAGIC;
00154 (*result)->handle = handle;
00155 (*result)->first_row = 1;
00156
00157
00158
00159 status = DbExecuteSqlStatement(handle, stmt_str, result);
00160 if (status == SQLITE_ROW) {
00161
00162 status = 0;
00163
00164
00165 if ((*result)->data == NULL) {
00166
00167
00168
00169
00170
00171
00172
00173
00174 errmsg = DbErrmsg(handle);
00175 if (errmsg && *errmsg) {
00176
00177
00178
00179 status = MsgLog(DBS_SQLFAIL, errmsg);
00180 }
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193 MemFree(*result);
00194 *result = NULL;
00195 }
00196 else {
00197
00198
00199
00200
00201
00202
00203 (*result)->count = sqlite3_data_count((sqlite3_stmt*) (*result)->data);
00204 }
00205 }
00206 else if (status == SQLITE_DONE)
00207 {
00208
00209
00210 status = sqlite3_finalize((sqlite3_stmt*) (*result)->data);
00211
00212 MemFree(*result);
00213 *result = NULL;
00214 }
00215 else {
00216
00217
00218
00219 status = MsgLog(DBS_SQLFAIL, DbErrmsg(handle));
00220 MemFree(*result);
00221 *result = NULL;
00222 }
00223
00224 return status;
00225 }
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241 void DbFreeResult(DB_RESULT result)
00242 {
00243 if (result) {
00244 if (result->magic == DB_RESULT_MAGIC) {
00245
00246
00247
00248 sqlite3_finalize(result->data);
00249 MemFree(result);
00250 result = NULL;
00251 }
00252 else {
00253
00254
00255
00256 (void) MsgLog(DBS_INVARG, "DbFreeResult");
00257 }
00258 }
00259
00260 return;
00261 }
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286 int DbFetchRow(DB_RESULT result, DB_ROW* row)
00287 {
00288 int status = 0;
00289
00290 if (result && (result->magic == DB_RESULT_MAGIC) && row) {
00291
00292
00293
00294 if (result->first_row == 1)
00295 {
00296 result->first_row = 0;
00297 *row = (DB_ROW) MemCalloc(1, sizeof(struct db_row));
00298 (*row)->magic = DB_ROW_MAGIC;
00299 (*row)->result=result;
00300 }
00301 else
00302 {
00303 status = sqlite3_step(result->data);
00304 if (status == SQLITE_DONE) {
00305
00306
00307
00308
00309 status = -1;
00310 }
00311 else if (status == SQLITE_ROW)
00312 {
00313 *row = (DB_ROW) MemCalloc(1, sizeof(struct db_row));
00314 (*row)->magic = DB_ROW_MAGIC;
00315 (*row)->result=result;
00316 status = 0;
00317 }
00318 }
00319 }
00320 else if (row) {
00321 *row = NULL;
00322 status = -1;
00323 }
00324 else{
00325 status = MsgLog(DBS_INVARG, "DbFetchRow");
00326
00327 }
00328
00329 return status;
00330 }
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346 void DbFreeRow(DB_ROW row)
00347 {
00348 if (row) {
00349 if (row->magic == DB_ROW_MAGIC) {
00350 MemFree(row);
00351 }
00352 else {
00353
00354
00355
00356 (void) MsgLog(DBS_INVARG, "DbFreeRow");
00357 }
00358 }
00359
00360 return;
00361 }
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389 int DbString(DB_ROW row, int field_index, char** result)
00390 {
00391 int status = 0;
00392 unsigned long width;
00393
00394
00395
00396 if (row && (row->magic == DB_ROW_MAGIC) && result) {
00397
00398
00399
00400 if ((field_index >= 0) && (field_index < row->result->count)) {
00401
00402
00403
00404 width = sqlite3_column_bytes(row->result->data, field_index);
00405
00406
00407
00408 if (sqlite3_column_text(row->result->data, field_index) != NULL) {
00409
00410 *result = MemMalloc(width + 1);
00411 memcpy(*result, sqlite3_column_text(row->result->data, field_index), width);
00412 (*result)[width] = 0;
00413 }
00414 else {
00415 *result = NULL;
00416 }
00417 }
00418 else {
00419
00420
00421
00422 status = MsgLog(DBS_INVINDEX, field_index, row->result->count);
00423 }
00424
00425 }
00426 else {
00427
00428
00429
00430 status = MsgLog(DBS_INVARG, "DbString");
00431 }
00432
00433 return status;
00434
00435 }
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449 void DbStringFree(char* string)
00450 {
00451 MemFree(string);
00452 }
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464 int DbBeginTransaction(void)
00465 {
00466 const char* sql = "begin transaction";
00467 return DbExecuteSqlNoResult(DbHandle(), sql);
00468 }
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480 int DbCommit(void)
00481 {
00482 const char* sql = "commit transaction";
00483 return DbExecuteSqlNoResult(DbHandle(), sql);
00484 }
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496 int DbRollback(void)
00497 {
00498 const char* sql = "rollback transaction";
00499 return DbExecuteSqlNoResult(DbHandle(), sql);
00500 }