2 * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <stdlib.h> // alloc
21 //#include <unistd.h> // unlink
25 #include "download-provider.h"
26 #include "download-provider-db-defs.h"
27 #include "download-provider-db.h"
28 #include "download-provider-log.h"
29 #include "download-provider-utils.h"
32 static void __basic_property(sqlite3 *handle)
34 if (sqlite3_exec(handle, "PRAGMA journal_mode=PERSIST;", 0, 0, 0) != SQLITE_OK)
35 TRACE_ERROR("check property journal_mode:PERSIST");
36 if (sqlite3_exec(handle, "PRAGMA foreign_keys=ON;", 0, 0, 0) != SQLITE_OK)
37 TRACE_ERROR("check property foreign_keys:ON");
40 static void __dp_finalize(sqlite3_stmt *stmt)
43 if (sqlite3_finalize(stmt) != SQLITE_OK) {
44 sqlite3 *handle = sqlite3_db_handle(stmt);
45 TRACE_ERROR("sqlite3_finalize:%s", sqlite3_errmsg(handle));
50 static int __check_integrity(sqlite3 *handle)
52 sqlite3_stmt *stmt = NULL;
55 TRACE_ERROR("failed to check handle");
60 int ret = sqlite3_prepare_v2(handle, "PRAGMA integrity_check", -1, &stmt, NULL);
61 if (ret != SQLITE_OK && ret != SQLITE_BUSY) {
62 TRACE_ERROR("failed to check integrity:%s", sqlite3_errmsg(handle));
65 ret = sqlite3_step(stmt);
66 if (ret == SQLITE_ROW) {
67 const char *ret_val = (const char *)sqlite3_column_text(stmt, 0);
68 TRACE_INFO("ret_val: %s", ret_val);
69 if (ret_val && strcmp(ret_val, "ok") == 0) {
72 TRACE_ERROR("failed to check integrity");
82 static int __check_table(sqlite3 *handle, char *table)
84 sqlite3_stmt *stmt = NULL;
86 if (handle == 0 || table == NULL) {
87 TRACE_ERROR("check handle or table:%s", table);
91 char *query = sqlite3_mprintf("SELECT name FROM sqlite_master WHERE type='table' AND name='%s'", table);
93 TRACE_ERROR("failed to make query statement");
96 int ret = sqlite3_prepare_v2(handle, query, -1, &stmt, NULL);
99 if (ret != SQLITE_OK) {
100 TRACE_ERROR("sqlite3_prepare:%s", sqlite3_errmsg(handle));
103 if (result == 0 && sqlite3_step(stmt) != SQLITE_ROW) {
104 TRACE_DEBUG("not found table:%s", table);
111 static int __rebuild_client_tables(sqlite3 *handle)
115 if (__check_table(handle, DP_TABLE_LOGGING) < 0) {
116 ret = sqlite3_exec(handle, DP_SCHEMA_LOGGING, 0, 0, 0);
117 if (ret == SQLITE_OK)
118 ret = sqlite3_exec(handle, DP_SCHEMA_LOGGING_INDEX, 0, 0, 0);
120 if (ret == SQLITE_OK && __check_table(handle, DP_TABLE_DOWNLOAD) < 0)
121 ret = sqlite3_exec(handle, DP_SCHEMA_DOWNLOAD, 0, 0, 0);
122 if (ret == SQLITE_OK && __check_table(handle, DP_TABLE_REQUEST) < 0)
123 ret = sqlite3_exec(handle, DP_SCHEMA_REQUEST, 0, 0, 0);
124 if (ret == SQLITE_OK && __check_table(handle, DP_TABLE_HEADERS) < 0)
125 ret = sqlite3_exec(handle, DP_SCHEMA_HEADER, 0, 0, 0);
126 if (ret == SQLITE_OK && __check_table(handle, DP_TABLE_NOTIFICATION) < 0)
127 ret = sqlite3_exec(handle, DP_SCHEMA_NOTIFICATION, 0, 0, 0);
128 if (ret != SQLITE_OK) {
129 TRACE_ERROR("create tables:%d error:%s", ret, sqlite3_errmsg(handle));
135 static int __rebuild_client_manager_tables(sqlite3 *handle)
138 if (__check_table(handle, DP_TABLE_CLIENTS) < 0)
139 ret = sqlite3_exec(handle, DP_SCHEMA_CLIENTS, 0, 0, 0);
140 if (ret != SQLITE_OK) {
141 TRACE_ERROR("create tables:%d error:%s", ret, sqlite3_errmsg(handle));
147 static int __db_open(sqlite3 **handle, char *database, int *errorcode)
149 if (sqlite3_open_v2(database, handle, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK) {
150 int sqlerrcode = sqlite3_errcode(*handle);
151 TRACE_ERROR("database open error(%d):%s", sqlerrcode, sqlite3_errmsg(*handle));
152 if (sqlerrcode == SQLITE_CORRUPT) { // remove & re-create
153 TRACE_SECURE_INFO("unlink [%s]", database);
155 sqlerrcode = SQLITE_CANTOPEN;
157 if (sqlerrcode == SQLITE_CANTOPEN) {
159 // create empty database
160 if (sqlite3_open(database, handle) != SQLITE_OK) {
161 sqlerrcode = sqlite3_errcode(*handle);
162 TRACE_SECURE_ERROR("failed to create %s error:%d", database, sqlerrcode);
163 TRACE_ERROR("database new error(%d):%s", sqlerrcode, sqlite3_errmsg(*handle));
164 if (sqlerrcode == SQLITE_FULL || sqlerrcode == SQLITE_CANTOPEN) { // can not create temporary file for connection
165 *errorcode = DP_ERROR_NO_SPACE; // DP_ERROR_OUT_OF_MEMORY
167 *errorcode = DP_ERROR_DISK_BUSY;
174 TRACE_ERROR("can not handle this error(%d):%s", sqlerrcode, sqlite3_errmsg(*handle));
175 *errorcode = DP_ERROR_OUT_OF_MEMORY;
180 __basic_property(*handle);
184 int dp_db_check_connection(void *handle)
187 TRACE_ERROR("connection handler is null");
192 int ret = sqlite3_db_status(handle, SQLITE_DBSTATUS_STMT_USED, &pcur, &phighwater, 0);
193 if (ret != SQLITE_OK) {
194 TRACE_INFO("sql(%p) error:%d, used memory:%d, highwater:%d", handle, ret, pcur, phighwater);
200 int dp_db_open_client_manager(void **handle, int *errorcode)
203 char *database = sqlite3_mprintf("%s/%s", DATABASE_DIR, DP_DBFILE_CLIENTS);
204 if (database == NULL) {
205 TRACE_ERROR("failed to make clients database file path");
206 *errorcode = DP_ERROR_OUT_OF_MEMORY;
209 if (__db_open((sqlite3 **)handle, database, errorcode) < 0) {
210 TRACE_ERROR("failed to open clients database file");
213 if (__check_integrity(*handle) < 0) {
214 // recovery database file
215 TRACE_ERROR("Failed to check integrity: %s", database);
218 if (__db_open((sqlite3 **)handle, database, errorcode) < 0) {
219 TRACE_ERROR("failed to open clients database file");
224 // whenever open new handle, check all tables. it's simple
225 if (*handle && __rebuild_client_manager_tables(*handle) < 0) {
226 *errorcode = DP_ERROR_NO_SPACE;
227 dp_db_close(*handle);
231 sqlite3_free(database);
233 return *handle ? 0 : -1;
236 static char *__dp_db_get_client_db_path(char *pkgname)
240 return sqlite3_mprintf("%s/clients/.%s", DATABASE_DIR, pkgname);
243 // 0 : remove, -1: error or skip by diff_time
244 int dp_db_remove_database(char *pkgname, long now_time, long diff_time)
247 char *database = __dp_db_get_client_db_path(pkgname);
248 if (database == NULL) {
249 TRACE_ERROR("failed to make db file path");
253 // get modified time of database file.
254 long modified_time = dp_get_file_modified_time(database);
255 if (modified_time >= now_time) {
256 TRACE_ERROR("check system timezone %ld vs %ld", modified_time, now_time);
257 } else if ((now_time - modified_time) > diff_time) {
258 char *database_journal = sqlite3_mprintf("%s-journal", database);
259 if (database_journal == NULL) {
260 TRACE_ERROR("failed to make db journal file path");
262 if (dp_remove_file(database_journal) < 0) {
263 TRACE_ERROR("failed to remove db journal file path");
265 if (dp_remove_file(database) < 0)
266 TRACE_ERROR("failed to remove db file path");
270 sqlite3_free(database_journal);
273 sqlite3_free(database);
277 int dp_db_open_client_v2(void **handle, char *pkgname)
279 char *database = __dp_db_get_client_db_path(pkgname);
280 if (database == NULL) {
281 TRACE_ERROR("failed to make db file path");
284 if (sqlite3_open_v2(database, (sqlite3 **)handle, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK) {
285 int errorcode = sqlite3_errcode(*handle);
286 TRACE_ERROR("error(%d):%s", errorcode, sqlite3_errmsg(*handle));
288 sqlite3_free(database);
291 sqlite3_free(database);
292 __basic_property(*handle);
296 int dp_db_open_client(void **handle, char *pkgname, int *errorcode)
299 char *database = __dp_db_get_client_db_path(pkgname);
300 if (database == NULL) {
301 TRACE_ERROR("failed to make db file path");
302 *errorcode = DP_ERROR_OUT_OF_MEMORY;
305 if (__db_open((sqlite3 **)handle, database, errorcode) < 0) {
306 TRACE_SECURE_ERROR("failed to open %s", database);
309 // whenever open new handle, check all tables. it's simple
310 if (__rebuild_client_tables(*handle) < 0) {
311 *errorcode = DP_ERROR_NO_SPACE;
312 dp_db_close(*handle);
316 sqlite3_free(database);
318 return *handle ? 0 : -1;
321 void dp_db_close(void *handle)
324 // remove empty page of db
325 //sqlite3_exec(handle, "VACUUM;", 0, 0, 0);
326 if (sqlite3_close((sqlite3 *)handle) != SQLITE_OK)
327 TRACE_ERROR("check sqlite close");
331 void dp_db_reset(void *stmt)
334 sqlite3_stmt *stmtp = stmt;
335 sqlite3_clear_bindings(stmtp);
336 if (sqlite3_reset(stmtp) != SQLITE_OK) {
337 sqlite3 *handle = sqlite3_db_handle(stmtp);
338 TRACE_ERROR("reset:%s", sqlite3_errmsg(handle));
343 void dp_db_finalize(void *stmt)
345 __dp_finalize((sqlite3_stmt *)stmt);
348 int dp_db_get_errorcode(void *handle)
351 TRACE_ERROR("check connection handle");
352 return DP_ERROR_DISK_BUSY;
354 int errorcode = sqlite3_errcode((sqlite3 *)handle);
355 if (errorcode == SQLITE_FULL) {
356 TRACE_ERROR("SQLITE_FULL-NO_SPACE");
357 return DP_ERROR_NO_SPACE;
358 } else if (errorcode == SQLITE_TOOBIG ||
359 errorcode == SQLITE_LOCKED || errorcode == SQLITE_BUSY) {
360 TRACE_ERROR("DISK_BUSY %s", sqlite3_errmsg((sqlite3 *)handle));
361 return DP_ERROR_DISK_BUSY;
363 return DP_ERROR_NONE;
367 #define DP_DB_PARAM_NULL_CHECK do {\
369 TRACE_ERROR("check connection handle");\
374 #define DP_DB_BUFFER_NULL_CHECK(buffer) do {\
375 if (buffer == NULL) {\
376 TRACE_ERROR("check available memory");\
381 #define DP_DB_BASIC_EXCEPTION_CHECK do {\
382 if (errorcode != SQLITE_OK) {\
383 if ((*error = dp_db_get_errorcode(handle)) == DP_ERROR_NONE)\
384 *error = DP_ERROR_INVALID_PARAMETER;\
385 __dp_finalize(stmt);\
390 #define DP_DB_WRITE_STEP_EXCEPTION_CHECK do {\
391 errorcode = sqlite3_step(stmt);\
392 __dp_finalize(stmt);\
393 if (errorcode != SQLITE_DONE) {\
394 if ((*error = dp_db_get_errorcode(handle)) == DP_ERROR_NONE)\
395 *error = DP_ERROR_INVALID_PARAMETER;\
400 int dp_db_get_ids(void *handle, const char *table, char *idcolumn, int *ids, const char *where, const int limit, char *ordercolumn, char *ordering, int *error)
402 *error = DP_ERROR_INVALID_PARAMETER;
403 DP_DB_PARAM_NULL_CHECK;
404 int errorcode = SQLITE_OK;
406 sqlite3_stmt *stmt = NULL;
408 char *order_query = NULL;
409 if (ordercolumn != NULL)
410 order_query = sqlite3_mprintf("ORDER BY %s %s", ordercolumn, (ordering == NULL ? "ASC" : ordering));
412 if (idcolumn == NULL)
413 idcolumn = DP_DB_COL_ID;
415 char *query = sqlite3_mprintf("SELECT %s FROM %s %s %s LIMIT ?", idcolumn, table, (where == NULL ? "" : where), (order_query == NULL ? "" : order_query));
416 DP_DB_BUFFER_NULL_CHECK(query);
417 errorcode = sqlite3_prepare_v2(handle, query, -1, &stmt, NULL);
418 //TRACE_DEBUG("debug query:%s", query);
420 if (order_query != NULL)
421 sqlite3_free(order_query);
422 DP_DB_BASIC_EXCEPTION_CHECK;
424 errorcode = sqlite3_bind_int(stmt, 1, limit);
425 DP_DB_BASIC_EXCEPTION_CHECK;
427 *error = DP_ERROR_NONE;
428 while ((errorcode = sqlite3_step(stmt)) == SQLITE_ROW) {
429 if (sqlite3_column_type(stmt, 0) == SQLITE_INTEGER) {
430 int columnvalue = sqlite3_column_int(stmt, 0);
431 //TRACE_DEBUG("id(%d):%d", rows_count, columnvalue);
432 ids[rows_count++] = columnvalue;
439 int dp_db_get_crashed_ids(void *handle, const char *table, int *ids, const int limit, int *error)
442 //get ids if state is QUEUED, CONNECTING or DOWNLOADING with auto_download
443 char *where = sqlite3_mprintf("WHERE %s IS 1 AND (%s IS %d OR %s IS %d OR %s IS %d)",
444 DP_DB_COL_AUTO_DOWNLOAD,
445 DP_DB_COL_STATE, DP_STATE_DOWNLOADING,
446 DP_DB_COL_STATE, DP_STATE_CONNECTING,
447 DP_DB_COL_STATE, DP_STATE_QUEUED);
449 int rows_count = dp_db_get_ids(handle, table, DP_DB_COL_ID, ids, where, limit, NULL, NULL, error);
453 *error = DP_ERROR_OUT_OF_MEMORY;
458 int dp_db_check_duplicated_int(void *handle, const char *table, const char *column, const int value, int *error)
460 *error = DP_ERROR_INVALID_PARAMETER;
461 DP_DB_PARAM_NULL_CHECK;
462 int errorcode = SQLITE_OK;
464 sqlite3_stmt *stmt = NULL;
465 char *query = sqlite3_mprintf("SELECT count(*) FROM %s WHERE %s IS ?", table, column);
466 DP_DB_BUFFER_NULL_CHECK(query);
467 //TRACE_DEBUG("debug query:%s", query);
468 errorcode = sqlite3_prepare_v2(handle, query, -1, &stmt, NULL);
470 DP_DB_BASIC_EXCEPTION_CHECK;
472 errorcode = sqlite3_bind_int(stmt, 1, value);
473 DP_DB_BASIC_EXCEPTION_CHECK;
475 *error = DP_ERROR_NONE;
476 errorcode = sqlite3_step(stmt);
477 if (errorcode == SQLITE_ROW)
478 count = sqlite3_column_int(stmt, 0);
486 int dp_db_check_duplicated_string(void *handle, const int id, const char *table, const char *column, int is_like, const char *value, int *error)
488 *error = DP_ERROR_INVALID_PARAMETER;
489 DP_DB_PARAM_NULL_CHECK;
490 int errorcode = SQLITE_OK;
492 sqlite3_stmt *stmt = NULL;
494 char *id_check = NULL;
496 id_check = sqlite3_mprintf("AND %s IS ?", DP_DB_COL_ID);
500 query = sqlite3_mprintf("SELECT count(*) FROM %s WHERE %s LIKE ? %s", table, column, (id_check == NULL ? "" : id_check));
502 query = sqlite3_mprintf("SELECT count(*) FROM %s WHERE %s %s ? %s", table, column, (is_like == 0 ? "IS" : "IS NOT"), (id_check == NULL ? "" : id_check));
503 if (id_check != NULL)
504 sqlite3_free(id_check);
505 DP_DB_BUFFER_NULL_CHECK(query);
506 //TRACE_DEBUG("debug query:%s", query);
507 errorcode = sqlite3_prepare_v2(handle, query, -1, &stmt, NULL);
509 DP_DB_BASIC_EXCEPTION_CHECK;
511 errorcode = sqlite3_bind_text(stmt, 1, value, -1, SQLITE_STATIC);
512 DP_DB_BASIC_EXCEPTION_CHECK;
514 errorcode = sqlite3_bind_int(stmt, 2, id);
515 DP_DB_BASIC_EXCEPTION_CHECK;
518 *error = DP_ERROR_NONE;
519 errorcode = sqlite3_step(stmt);
520 if (errorcode == SQLITE_ROW)
521 count = sqlite3_column_int(stmt, 0);
529 int dp_db_update_client_info(void *handle, const char *pkgname, const char *smack, const int uid, const int gid, int *error)
531 *error = DP_ERROR_INVALID_PARAMETER;
532 DP_DB_PARAM_NULL_CHECK;
533 if (pkgname == NULL) {
534 TRACE_ERROR("check pkgname");
538 int is_update = dp_db_check_duplicated_string(handle, -1, DP_TABLE_CLIENTS, DP_DB_COL_PACKAGE, 0, pkgname, error);
540 if ((*error = dp_db_get_errorcode(handle)) == DP_ERROR_NONE)
541 *error = DP_ERROR_DISK_BUSY;
545 int errorcode = SQLITE_OK;
546 sqlite3_stmt *stmt = NULL;
550 query = sqlite3_mprintf(
551 "INSERT INTO %s (%s, %s, %s, %s, %s, %s, %s) VALUES (?, ?, %d, %d, 0, DATETIME('NOW'), DATETIME('NOW'))",
552 DP_TABLE_CLIENTS, DP_DB_COL_SMACK_LABEL, DP_DB_COL_PACKAGE, DP_DB_COL_UID,
553 DP_DB_COL_GID, DP_DB_COL_REQUEST_COUNT,
554 DP_DB_COL_CREATE_TIME, DP_DB_COL_ACCESS_TIME, uid, gid);
556 query = sqlite3_mprintf("UPDATE %s SET %s = ?, %s = %d, %s = %d, %s = DATETIME('NOW') WHERE %s IS ?",
557 DP_TABLE_CLIENTS, DP_DB_COL_SMACK_LABEL, DP_DB_COL_UID,
558 uid, DP_DB_COL_GID, gid, DP_DB_COL_ACCESS_TIME, DP_DB_COL_PACKAGE);
559 DP_DB_BUFFER_NULL_CHECK(query);
560 //TRACE_DEBUG("debug query:%s", query);
561 errorcode = sqlite3_prepare_v2(handle, query, -1, &stmt, NULL);
563 DP_DB_BASIC_EXCEPTION_CHECK;
566 errorcode = sqlite3_bind_text(stmt, 1, smack, -1, SQLITE_STATIC);
567 DP_DB_BASIC_EXCEPTION_CHECK;
569 errorcode = sqlite3_bind_text(stmt, 2, pkgname, -1, SQLITE_STATIC);
570 DP_DB_BASIC_EXCEPTION_CHECK;
572 *error = DP_ERROR_NONE;
573 DP_DB_WRITE_STEP_EXCEPTION_CHECK;
577 int dp_db_get_client_property_string(void *handle, const char *pkgname, const char *column, unsigned char **value, unsigned *length, int *error)
579 *error = DP_ERROR_INVALID_PARAMETER;
580 DP_DB_PARAM_NULL_CHECK;
581 if (pkgname == NULL || column == NULL || value == NULL || length == NULL) {
582 TRACE_ERROR("check materials for query");
586 int errorcode = SQLITE_OK;
587 sqlite3_stmt *stmt = NULL;
588 char *query = sqlite3_mprintf("SELECT %s FROM %s WHERE %s IS ? LIMIT 1", column, DP_TABLE_CLIENTS, DP_DB_COL_PACKAGE);
589 DP_DB_BUFFER_NULL_CHECK(query);
590 //TRACE_DEBUG("debug query:%s", query);
591 errorcode = sqlite3_prepare_v2(handle, query, -1, &stmt, NULL);
593 DP_DB_BASIC_EXCEPTION_CHECK;
595 errorcode = sqlite3_bind_text(stmt, 1, pkgname, -1, SQLITE_STATIC);
596 DP_DB_BASIC_EXCEPTION_CHECK;
598 *error = DP_ERROR_NONE;
599 errorcode = sqlite3_step(stmt);
601 if (errorcode == SQLITE_ROW) {
602 int data_type = sqlite3_column_type(stmt, 0);
603 if (data_type == SQLITE_TEXT) {
604 int getbytes = sqlite3_column_bytes(stmt, 0);
606 unsigned char *getstr = (unsigned char *)calloc(getbytes + 1, sizeof(unsigned char));
607 if (getstr != NULL) {
608 memcpy(getstr, sqlite3_column_text(stmt, 0), getbytes * sizeof(unsigned char));
609 getstr[getbytes] = '\0';
613 TRACE_ERROR("check available system memory");
614 *error = DP_ERROR_OUT_OF_MEMORY;
617 TRACE_DEBUG("no data");
618 *error = DP_ERROR_NO_DATA;
621 TRACE_ERROR("check column type:%d", data_type);
622 *error = DP_ERROR_NO_DATA;
624 } else if (errorcode == SQLITE_DONE) {
625 TRACE_DEBUG("no data");
626 *error = DP_ERROR_NO_DATA;
628 if ((*error = dp_db_get_errorcode(handle)) == DP_ERROR_NONE)
629 *error = DP_ERROR_ID_NOT_FOUND;
632 if (*error != DP_ERROR_NO_DATA && *error != DP_ERROR_NONE)
637 int dp_db_new_logging(void *handle, const int id, const int state, const int errorvalue, int *error)
639 *error = DP_ERROR_INVALID_PARAMETER;
640 DP_DB_PARAM_NULL_CHECK;
642 TRACE_ERROR("check id:%d", id);
645 int errorcode = SQLITE_OK;
646 sqlite3_stmt *stmt = NULL;
648 char *query = sqlite3_mprintf("INSERT INTO %s (%s, %s, %s, %s, %s) VALUES (?, ?, ?, DATETIME('now'), DATETIME('now'))",
649 DP_TABLE_LOGGING, DP_DB_COL_ID, DP_DB_COL_STATE,
650 DP_DB_COL_ERRORCODE, DP_DB_COL_CREATE_TIME, DP_DB_COL_ACCESS_TIME);
651 DP_DB_BUFFER_NULL_CHECK(query);
652 //TRACE_DEBUG("debug query:%s", query);
653 errorcode = sqlite3_prepare_v2(handle, query, -1, &stmt, NULL);
655 DP_DB_BASIC_EXCEPTION_CHECK;
657 errorcode = sqlite3_bind_int(stmt, 1, id);
658 DP_DB_BASIC_EXCEPTION_CHECK;
659 errorcode = sqlite3_bind_int(stmt, 2, state);
660 DP_DB_BASIC_EXCEPTION_CHECK;
661 errorcode = sqlite3_bind_int(stmt, 3, errorvalue);
662 DP_DB_BASIC_EXCEPTION_CHECK;
664 *error = DP_ERROR_NONE;
665 DP_DB_WRITE_STEP_EXCEPTION_CHECK;
669 int dp_db_update_logging(void *handle, const int id, const int state, const int errorvalue, int *error)
671 *error = DP_ERROR_INVALID_PARAMETER;
672 DP_DB_PARAM_NULL_CHECK;
674 TRACE_ERROR("check id:%d", id);
677 int errorcode = SQLITE_OK;
678 sqlite3_stmt *stmt = NULL;
680 char *query = sqlite3_mprintf("UPDATE %s SET %s = ?, %s = ?, %s = DATETIME('now') WHERE %s = ?",
681 DP_TABLE_LOGGING, DP_DB_COL_STATE, DP_DB_COL_ERRORCODE,
682 DP_DB_COL_ACCESS_TIME, DP_DB_COL_ID);
683 DP_DB_BUFFER_NULL_CHECK(query);
684 //TRACE_DEBUG("debug query:%s", query);
685 errorcode = sqlite3_prepare_v2(handle, query, -1, &stmt, NULL);
687 DP_DB_BASIC_EXCEPTION_CHECK;
689 errorcode = sqlite3_bind_int(stmt, 1, state);
690 DP_DB_BASIC_EXCEPTION_CHECK;
691 errorcode = sqlite3_bind_int(stmt, 2, errorvalue);
692 DP_DB_BASIC_EXCEPTION_CHECK;
693 errorcode = sqlite3_bind_int(stmt, 3, id);
694 DP_DB_BASIC_EXCEPTION_CHECK;
696 *error = DP_ERROR_NONE;
697 DP_DB_WRITE_STEP_EXCEPTION_CHECK;
701 // 0:integer, 1:bigint, 2:string, 3:blob
702 int dp_db_replace_property(void *handle, const int id, const char *table, const char *column, const void *value, const unsigned length, const unsigned valuetype, int *error)
704 *error = DP_ERROR_INVALID_PARAMETER;
705 DP_DB_PARAM_NULL_CHECK;
707 TRACE_ERROR("check id:%d", id);
710 if (table == NULL || column == NULL || value == NULL) {
711 TRACE_ERROR("check materials for query id:%d", id);
715 int is_update = dp_db_check_duplicated_int(handle, table, DP_DB_COL_ID, id, error);
717 if ((*error = dp_db_get_errorcode(handle)) == DP_ERROR_NONE)
718 *error = DP_ERROR_DISK_BUSY;
722 int errorcode = SQLITE_OK;
723 sqlite3_stmt *stmt = NULL;
727 query = sqlite3_mprintf("INSERT INTO %s (%s, %s) VALUES (?, ?)", table, column, DP_DB_COL_ID);
729 query = sqlite3_mprintf("UPDATE %s SET %s = ? WHERE %s IS ?", table, column, DP_DB_COL_ID);
730 DP_DB_BUFFER_NULL_CHECK(query);
731 //TRACE_DEBUG("debug query:%s", query);
732 errorcode = sqlite3_prepare_v2(handle, query, -1, &stmt, NULL);
734 DP_DB_BASIC_EXCEPTION_CHECK;
736 if (valuetype == 0) {
737 int *cast_value = (int *)value;
738 errorcode = sqlite3_bind_int(stmt, 1, *cast_value);
739 } else if (valuetype == 1) {
740 sqlite_int64 *cast_value = (sqlite_int64 *)value;
741 errorcode = sqlite3_bind_int64(stmt, 1, *cast_value);
742 } else if (valuetype == 2) {
743 errorcode = sqlite3_bind_text(stmt, 1, (char *)value, -1, SQLITE_STATIC);
744 } else if (valuetype == 3) {
745 errorcode = sqlite3_bind_blob(stmt, 1, value, (int)length, NULL);
747 TRACE_ERROR("invalid type:%d", valuetype);
751 DP_DB_BASIC_EXCEPTION_CHECK;
752 errorcode = sqlite3_bind_int(stmt, 2, id);
753 DP_DB_BASIC_EXCEPTION_CHECK;
755 *error = DP_ERROR_NONE;
756 DP_DB_WRITE_STEP_EXCEPTION_CHECK;
760 int dp_db_get_property_string(void *handle, const int id, const char *table, const char *column, unsigned char **value, unsigned *length, int *error)
762 *error = DP_ERROR_INVALID_PARAMETER;
763 DP_DB_PARAM_NULL_CHECK;
765 TRACE_ERROR("check id:%d", id);
768 if (table == NULL || column == NULL || value == NULL || length == NULL) {
769 TRACE_ERROR("check materials for query id:%d", id);
773 int errorcode = SQLITE_OK;
774 sqlite3_stmt *stmt = NULL;
775 char *query = sqlite3_mprintf("SELECT %s FROM %s WHERE %s = ? LIMIT 1", column, table, DP_DB_COL_ID);
776 DP_DB_BUFFER_NULL_CHECK(query);
777 //TRACE_DEBUG("debug query:%s", query);
778 errorcode = sqlite3_prepare_v2(handle, query, -1, &stmt, NULL);
780 DP_DB_BASIC_EXCEPTION_CHECK;
782 errorcode = sqlite3_bind_int(stmt, 1, id);
783 DP_DB_BASIC_EXCEPTION_CHECK;
785 *error = DP_ERROR_NONE;
786 errorcode = sqlite3_step(stmt);
788 if (errorcode == SQLITE_ROW) {
789 int data_type = sqlite3_column_type(stmt, 0);
790 if (data_type == SQLITE_TEXT) {
791 int getbytes = sqlite3_column_bytes(stmt, 0);
793 unsigned char *getstr = (unsigned char *)calloc(getbytes + 1, sizeof(unsigned char));
794 if (getstr != NULL) {
795 memcpy(getstr, sqlite3_column_text(stmt, 0), getbytes * sizeof(unsigned char));
796 getstr[getbytes] = '\0';
800 TRACE_ERROR("check available system memory");
801 *error = DP_ERROR_OUT_OF_MEMORY;
804 TRACE_DEBUG("no data");
805 *error = DP_ERROR_NO_DATA;
807 } else if (data_type == SQLITE_BLOB) {
808 int getbytes = sqlite3_column_bytes(stmt, 0);
810 unsigned char *getstr = (unsigned char *)calloc(getbytes, sizeof(unsigned char));
811 if (getstr != NULL) {
812 memcpy(getstr, sqlite3_column_blob(stmt, 0), getbytes * sizeof(unsigned char));
816 TRACE_ERROR("check available system memory");
817 *error = DP_ERROR_OUT_OF_MEMORY;
820 TRACE_DEBUG("no data");
821 *error = DP_ERROR_NO_DATA;
824 //TRACE_ERROR("check column type:%d", data_type);
825 *error = DP_ERROR_NO_DATA;
827 } else if (errorcode == SQLITE_DONE) {
828 TRACE_DEBUG("no data");
829 *error = DP_ERROR_NO_DATA;
831 if ((*error = dp_db_get_errorcode(handle)) == DP_ERROR_NONE)
832 *error = DP_ERROR_ID_NOT_FOUND;
835 if (*error != DP_ERROR_NO_DATA && *error != DP_ERROR_NONE)
840 int dp_db_get_property_int(void *handle, const int id, const char *table, const char *column, void *value, int *error)
842 *error = DP_ERROR_INVALID_PARAMETER;
843 DP_DB_PARAM_NULL_CHECK;
845 TRACE_ERROR("check id:%d", id);
848 if (table == NULL || column == NULL || value == NULL) {
849 TRACE_ERROR("check materials for query id:%d", id);
853 int errorcode = SQLITE_OK;
854 sqlite3_stmt *stmt = NULL;
855 char *query = sqlite3_mprintf("SELECT %s FROM %s WHERE %s = ? LIMIT 1", column, table, DP_DB_COL_ID);
856 DP_DB_BUFFER_NULL_CHECK(query);
857 //TRACE_DEBUG("debug query:%s", query);
858 errorcode = sqlite3_prepare_v2(handle, query, -1, &stmt, NULL);
860 DP_DB_BASIC_EXCEPTION_CHECK;
862 errorcode = sqlite3_bind_int(stmt, 1, id);
863 DP_DB_BASIC_EXCEPTION_CHECK;
865 *error = DP_ERROR_NONE;
866 errorcode = sqlite3_step(stmt);
867 if (errorcode == SQLITE_ROW) {
868 int data_type = sqlite3_column_type(stmt, 0);
869 if (data_type == SQLITE_INTEGER) {
870 int recv_int = sqlite3_column_int(stmt, 0);
873 } else if (data_type == SQLITE_FLOAT) {
874 unsigned long long recv_int = sqlite3_column_int64(stmt, 0);
875 unsigned long long *pvalue = value;
878 TRACE_ERROR("check column type:%d", data_type);
879 *error = DP_ERROR_NO_DATA;
881 } else if (errorcode == SQLITE_DONE) {
882 TRACE_DEBUG("no data");
883 *error = DP_ERROR_NO_DATA;
885 if ((*error = dp_db_get_errorcode(handle)) == DP_ERROR_NONE)
886 *error = DP_ERROR_ID_NOT_FOUND;
889 if (*error != DP_ERROR_NO_DATA && *error != DP_ERROR_NONE)
894 int dp_db_unset_property_string(void *handle, const int id, const char *table, const char *column, int *error)
896 *error = DP_ERROR_INVALID_PARAMETER;
897 DP_DB_PARAM_NULL_CHECK;
899 TRACE_ERROR("check id:%d", id);
902 if (table == NULL || column == NULL) {
903 TRACE_ERROR("check materials for query id:%d", id);
907 int is_update = dp_db_check_duplicated_int(handle, table, DP_DB_COL_ID, id, error);
909 if ((*error = dp_db_get_errorcode(handle)) == DP_ERROR_NONE)
910 *error = DP_ERROR_DISK_BUSY;
912 } else if (is_update == 0) {
913 *error = DP_ERROR_ID_NOT_FOUND;
917 int errorcode = SQLITE_OK;
918 sqlite3_stmt *stmt = NULL;
919 char *query = sqlite3_mprintf("UPDATE %s SET %s = NULL WHERE %s IS ?", table, column, DP_DB_COL_ID);
921 DP_DB_BUFFER_NULL_CHECK(query);
922 //TRACE_DEBUG("debug query:%s", query);
923 errorcode = sqlite3_prepare_v2(handle, query, -1, &stmt, NULL);
925 DP_DB_BASIC_EXCEPTION_CHECK;
926 errorcode = sqlite3_bind_int(stmt, 1, id);
927 DP_DB_BASIC_EXCEPTION_CHECK;
929 *error = DP_ERROR_NONE;
930 DP_DB_WRITE_STEP_EXCEPTION_CHECK;
934 // "DELETE FROM %s WHERE %s NOT IN (SELECT %s FROM %s ORDER BY %s %s LIMIT %d)"
935 int dp_db_delete(void *handle, const int id, const char *table, int *error)
937 *error = DP_ERROR_INVALID_PARAMETER;
938 DP_DB_PARAM_NULL_CHECK;
940 TRACE_ERROR("check id:%d", id);
944 TRACE_ERROR("check materials for query id:%d", id);
948 int is_update = dp_db_check_duplicated_int(handle, table, DP_DB_COL_ID, id, error);
950 if ((*error = dp_db_get_errorcode(handle)) == DP_ERROR_NONE)
951 *error = DP_ERROR_DISK_BUSY;
953 } else if (is_update == 0) {
954 *error = DP_ERROR_ID_NOT_FOUND;
958 int errorcode = SQLITE_OK;
959 sqlite3_stmt *stmt = NULL;
960 char *query = sqlite3_mprintf("DELETE FROM %s WHERE %s IS ?", table, DP_DB_COL_ID);
962 DP_DB_BUFFER_NULL_CHECK(query);
963 //TRACE_DEBUG("debug query:%s", query);
964 errorcode = sqlite3_prepare_v2(handle, query, -1, &stmt, NULL);
966 DP_DB_BASIC_EXCEPTION_CHECK;
967 errorcode = sqlite3_bind_int(stmt, 1, id);
968 DP_DB_BASIC_EXCEPTION_CHECK;
970 *error = DP_ERROR_NONE;
971 DP_DB_WRITE_STEP_EXCEPTION_CHECK;
975 int dp_db_new_header(void *handle, const int id, const char *field, const char *value, int *error)
977 *error = DP_ERROR_INVALID_PARAMETER;
978 DP_DB_PARAM_NULL_CHECK;
980 TRACE_ERROR("check id:%d", id);
984 TRACE_ERROR("check field:%s", field);
987 int errorcode = SQLITE_OK;
988 sqlite3_stmt *stmt = NULL;
990 char *query = sqlite3_mprintf("INSERT INTO %s (%s, %s, %s) VALUES (?, ?, ?)",
991 DP_TABLE_HEADERS, DP_DB_COL_ID, DP_DB_COL_HEADER_FIELD,
992 DP_DB_COL_HEADER_DATA);
993 DP_DB_BUFFER_NULL_CHECK(query);
994 //TRACE_DEBUG("debug query:%s", query);
995 errorcode = sqlite3_prepare_v2(handle, query, -1, &stmt, NULL);
997 DP_DB_BASIC_EXCEPTION_CHECK;
999 errorcode = sqlite3_bind_int(stmt, 1, id);
1000 DP_DB_BASIC_EXCEPTION_CHECK;
1001 errorcode = sqlite3_bind_text(stmt, 2, (char *)field, -1, SQLITE_STATIC);
1002 DP_DB_BASIC_EXCEPTION_CHECK;
1003 errorcode = sqlite3_bind_text(stmt, 3, (char *)value, -1, SQLITE_STATIC);
1004 DP_DB_BASIC_EXCEPTION_CHECK;
1006 *error = DP_ERROR_NONE;
1007 DP_DB_WRITE_STEP_EXCEPTION_CHECK;
1011 int dp_db_update_header(void *handle, const int id, const char *field, const char *value, int *error)
1013 *error = DP_ERROR_INVALID_PARAMETER;
1014 DP_DB_PARAM_NULL_CHECK;
1016 TRACE_ERROR("check id:%d", id);
1019 int errorcode = SQLITE_OK;
1020 sqlite3_stmt *stmt = NULL;
1022 char *query = sqlite3_mprintf("UPDATE %s SET %s = ? WHERE %s IS ? AND %s IS ?",
1023 DP_TABLE_HEADERS, DP_DB_COL_HEADER_DATA,
1024 DP_DB_COL_ID, DP_DB_COL_HEADER_FIELD);
1025 DP_DB_BUFFER_NULL_CHECK(query);
1026 //TRACE_DEBUG("debug query:%s", query);
1027 errorcode = sqlite3_prepare_v2(handle, query, -1, &stmt, NULL);
1028 sqlite3_free(query);
1029 DP_DB_BASIC_EXCEPTION_CHECK;
1031 errorcode = sqlite3_bind_text(stmt, 1, (char *)value, -1, SQLITE_STATIC);
1032 DP_DB_BASIC_EXCEPTION_CHECK;
1033 errorcode = sqlite3_bind_int(stmt, 2, id);
1034 DP_DB_BASIC_EXCEPTION_CHECK;
1035 errorcode = sqlite3_bind_text(stmt, 2, (char *)field, -1, SQLITE_STATIC);
1036 DP_DB_BASIC_EXCEPTION_CHECK;
1038 *error = DP_ERROR_NONE;
1039 DP_DB_WRITE_STEP_EXCEPTION_CHECK;
1043 int dp_db_get_header_value(void *handle, const int id, const char *field, unsigned char **value, unsigned *length, int *error)
1045 *error = DP_ERROR_INVALID_PARAMETER;
1046 DP_DB_PARAM_NULL_CHECK;
1048 TRACE_ERROR("check id:%d", id);
1051 if (field == NULL || value == NULL || length == NULL) {
1052 TRACE_ERROR("check materials for query id:%d", id);
1056 int errorcode = SQLITE_OK;
1057 sqlite3_stmt *stmt = NULL;
1058 char *query = sqlite3_mprintf("SELECT %s FROM %s WHERE %s IS ? AND %s IS ? LIMIT 1", DP_DB_COL_HEADER_DATA, DP_TABLE_HEADERS, DP_DB_COL_ID, DP_DB_COL_HEADER_FIELD);
1059 DP_DB_BUFFER_NULL_CHECK(query);
1060 //TRACE_DEBUG("debug query:%s", query);
1061 errorcode = sqlite3_prepare_v2(handle, query, -1, &stmt, NULL);
1062 sqlite3_free(query);
1063 DP_DB_BASIC_EXCEPTION_CHECK;
1065 errorcode = sqlite3_bind_int(stmt, 1, id);
1066 DP_DB_BASIC_EXCEPTION_CHECK;
1067 errorcode = sqlite3_bind_text(stmt, 2, (char *)field, -1, SQLITE_STATIC);
1068 DP_DB_BASIC_EXCEPTION_CHECK;
1070 *error = DP_ERROR_NONE;
1071 errorcode = sqlite3_step(stmt);
1073 if (errorcode == SQLITE_ROW) {
1074 int data_type = sqlite3_column_type(stmt, 0);
1075 if (data_type == SQLITE_TEXT) {
1076 int getbytes = sqlite3_column_bytes(stmt, 0);
1078 unsigned char *getstr = (unsigned char *)calloc(getbytes + 1, sizeof(unsigned char));
1079 if (getstr != NULL) {
1080 memcpy(getstr, sqlite3_column_text(stmt, 0), getbytes * sizeof(unsigned char));
1081 getstr[getbytes] = '\0';
1085 TRACE_ERROR("check available system memory");
1086 *error = DP_ERROR_OUT_OF_MEMORY;
1089 TRACE_DEBUG("no data");
1090 *error = DP_ERROR_NO_DATA;
1093 TRACE_ERROR("check column type:%d", data_type);
1094 *error = DP_ERROR_NO_DATA;
1096 } else if (errorcode == SQLITE_DONE) {
1097 TRACE_DEBUG("no data");
1098 *error = DP_ERROR_NO_DATA;
1100 if ((*error = dp_db_get_errorcode(handle)) == DP_ERROR_NONE)
1101 *error = DP_ERROR_ID_NOT_FOUND;
1103 __dp_finalize(stmt);
1104 if (*error != DP_ERROR_NO_DATA && *error != DP_ERROR_NONE)
1109 // not supprot blob as column & value for additional condition
1110 int dp_db_cond_delete(void *handle, const int id, const char *table, const char *column, const void *value, const unsigned valuetype, int *error)
1112 *error = DP_ERROR_INVALID_PARAMETER;
1113 DP_DB_PARAM_NULL_CHECK;
1115 TRACE_ERROR("check id:%d", id);
1118 if (table == NULL || column == NULL || value == NULL) {
1119 TRACE_ERROR("check materials for query id:%d", id);
1123 int errorcode = SQLITE_OK;
1124 sqlite3_stmt *stmt = NULL;
1125 char *query = sqlite3_mprintf("DELETE FROM %s WHERE %s IS ? AND %s IS ?", table, DP_DB_COL_ID, column);
1127 DP_DB_BUFFER_NULL_CHECK(query);
1128 //TRACE_DEBUG("debug query:%s", query);
1129 errorcode = sqlite3_prepare_v2(handle, query, -1, &stmt, NULL);
1130 sqlite3_free(query);
1131 DP_DB_BASIC_EXCEPTION_CHECK;
1132 errorcode = sqlite3_bind_int(stmt, 1, id);
1133 DP_DB_BASIC_EXCEPTION_CHECK;
1135 if (valuetype == 0) {
1136 int *cast_value = (int *)value;
1137 errorcode = sqlite3_bind_int(stmt, 2, *cast_value);
1138 } else if (valuetype == 1) {
1139 sqlite_int64 *cast_value = (sqlite_int64 *)value;
1140 errorcode = sqlite3_bind_int64(stmt, 2, *cast_value);
1141 } else if (valuetype == 2) {
1142 errorcode = sqlite3_bind_text(stmt, 2, (char *)value, -1, SQLITE_STATIC);
1144 DP_DB_BASIC_EXCEPTION_CHECK;
1146 *error = DP_ERROR_NONE;
1147 DP_DB_WRITE_STEP_EXCEPTION_CHECK;
1151 int dp_db_get_cond_ids(void *handle, const char *table, const char *getcolumn, const char *column, const int value, int *ids, const int limit, int *error)
1153 *error = DP_ERROR_INVALID_PARAMETER;
1154 DP_DB_PARAM_NULL_CHECK;
1155 int errorcode = SQLITE_OK;
1157 sqlite3_stmt *stmt = NULL;
1158 char *query = sqlite3_mprintf("SELECT %s FROM %s WHERE %s IS ?", getcolumn, table, column);
1159 DP_DB_BUFFER_NULL_CHECK(query);
1160 //TRACE_DEBUG("debug query:%s", query);
1161 errorcode = sqlite3_prepare_v2(handle, query, -1, &stmt, NULL);
1162 sqlite3_free(query);
1163 DP_DB_BASIC_EXCEPTION_CHECK;
1165 errorcode = sqlite3_bind_int(stmt, 1, value);
1166 DP_DB_BASIC_EXCEPTION_CHECK;
1168 *error = DP_ERROR_NONE;
1169 while ((errorcode = sqlite3_step(stmt)) == SQLITE_ROW) {
1170 if (sqlite3_column_type(stmt, 0) == SQLITE_INTEGER) {
1171 int columnvalue = sqlite3_column_int(stmt, 0);
1172 //TRACE_DEBUG("id(%d):%d", rows_count, columnvalue);
1173 ids[rows_count++] = columnvalue;
1176 __dp_finalize(stmt);
1180 int dp_db_get_cond_string(void *handle, const char *table, char *wherecolumn, const int wherevalue, const char *getcolumn, unsigned char **value, unsigned *length, int *error)
1182 *error = DP_ERROR_INVALID_PARAMETER;
1183 DP_DB_PARAM_NULL_CHECK;
1184 if (table == NULL || getcolumn == NULL || value == NULL || length == NULL) {
1185 TRACE_ERROR("check materials for query");
1189 if (wherecolumn == NULL)
1190 wherecolumn = DP_DB_COL_ID;
1192 int errorcode = SQLITE_OK;
1193 sqlite3_stmt *stmt = NULL;
1194 char *query = sqlite3_mprintf("SELECT %s FROM %s WHERE %s = ? LIMIT 1", getcolumn, table, wherecolumn);
1195 DP_DB_BUFFER_NULL_CHECK(query);
1196 //TRACE_DEBUG("debug query:%s", query);
1197 errorcode = sqlite3_prepare_v2(handle, query, -1, &stmt, NULL);
1198 sqlite3_free(query);
1199 DP_DB_BASIC_EXCEPTION_CHECK;
1201 errorcode = sqlite3_bind_int(stmt, 1, wherevalue);
1202 DP_DB_BASIC_EXCEPTION_CHECK;
1204 *error = DP_ERROR_NONE;
1205 errorcode = sqlite3_step(stmt);
1207 if (errorcode == SQLITE_ROW) {
1208 int data_type = sqlite3_column_type(stmt, 0);
1209 if (data_type == SQLITE_TEXT) {
1210 int getbytes = sqlite3_column_bytes(stmt, 0);
1212 unsigned char *getstr = (unsigned char *)calloc(getbytes + 1, sizeof(unsigned char));
1213 if (getstr != NULL) {
1214 memcpy(getstr, sqlite3_column_text(stmt, 0), getbytes * sizeof(unsigned char));
1215 getstr[getbytes] = '\0';
1219 TRACE_ERROR("check available system memory");
1220 *error = DP_ERROR_OUT_OF_MEMORY;
1223 TRACE_DEBUG("no data");
1224 *error = DP_ERROR_NO_DATA;
1226 } else if (data_type == SQLITE_BLOB) {
1227 int getbytes = sqlite3_column_bytes(stmt, 0);
1229 unsigned char *getstr = (unsigned char *)calloc(getbytes, sizeof(unsigned char));
1230 if (getstr != NULL) {
1231 memcpy(getstr, sqlite3_column_blob(stmt, 0), getbytes * sizeof(unsigned char));
1235 TRACE_ERROR("check available system memory");
1236 *error = DP_ERROR_OUT_OF_MEMORY;
1239 TRACE_DEBUG("no data");
1240 *error = DP_ERROR_NO_DATA;
1243 TRACE_ERROR("check column type:%d", data_type);
1244 *error = DP_ERROR_NO_DATA;
1246 } else if (errorcode == SQLITE_DONE) {
1247 TRACE_DEBUG("no data");
1248 *error = DP_ERROR_NO_DATA;
1250 if ((*error = dp_db_get_errorcode(handle)) == DP_ERROR_NONE)
1251 *error = DP_ERROR_ID_NOT_FOUND;
1253 __dp_finalize(stmt);
1254 if (*error != DP_ERROR_NO_DATA && *error != DP_ERROR_NONE)
1259 int dp_db_limit_rows(void *handle, const char *table, int limit, int *error)
1261 *error = DP_ERROR_INVALID_PARAMETER;
1262 DP_DB_PARAM_NULL_CHECK;
1263 if (table == NULL) {
1264 TRACE_ERROR("check materials for query");
1268 TRACE_ERROR("check limitation:%d", limit);
1272 int errorcode = SQLITE_OK;
1273 sqlite3_stmt *stmt = NULL;
1274 char *query = sqlite3_mprintf("DELETE FROM %s WHERE %s NOT IN (SELECT %s FROM %s ORDER BY %s ASC LIMIT ?)", table, DP_DB_COL_ID, DP_DB_COL_ID, table, DP_DB_COL_CREATE_TIME);
1275 DP_DB_BUFFER_NULL_CHECK(query);
1276 //TRACE_DEBUG("debug query:%s", query);
1277 errorcode = sqlite3_prepare_v2(handle, query, -1, &stmt, NULL);
1278 sqlite3_free(query);
1279 DP_DB_BASIC_EXCEPTION_CHECK;
1281 errorcode = sqlite3_bind_int(stmt, 1, limit);
1282 DP_DB_BASIC_EXCEPTION_CHECK;
1284 // apply "ON DELETE CASCADE"
1286 *error = DP_ERROR_NONE;
1287 DP_DB_WRITE_STEP_EXCEPTION_CHECK;
1291 int dp_db_limit_time(void *handle, const char *table, int hours, int *error)
1293 *error = DP_ERROR_INVALID_PARAMETER;
1294 DP_DB_PARAM_NULL_CHECK;
1295 if (table == NULL) {
1296 TRACE_ERROR("check materials for query");
1300 TRACE_ERROR("check limit time:%d", hours);
1304 int errorcode = SQLITE_OK;
1305 sqlite3_stmt *stmt = NULL;
1306 char *query = sqlite3_mprintf("DELETE FROM %s WHERE %s < DATETIME('now','-%d hours')", table, DP_DB_COL_CREATE_TIME, hours);
1307 DP_DB_BUFFER_NULL_CHECK(query);
1308 //TRACE_DEBUG("debug query:%s", query);
1309 errorcode = sqlite3_prepare_v2(handle, query, -1, &stmt, NULL);
1310 sqlite3_free(query);
1311 DP_DB_BASIC_EXCEPTION_CHECK;
1312 *error = DP_ERROR_NONE;
1313 DP_DB_WRITE_STEP_EXCEPTION_CHECK;
1317 int dp_db_get_http_headers_list(void *handle, int id, char **headers, int *error)
1319 int errorcode = SQLITE_OK;
1320 int headers_index = 0;
1321 sqlite3_stmt *stmt = NULL;
1322 *error = DP_ERROR_NONE;
1323 DP_DB_PARAM_NULL_CHECK;
1326 TRACE_ERROR("[CHECK ID]");
1327 *error = DP_ERROR_INVALID_PARAMETER;
1332 sqlite3_prepare_v2(handle,
1333 "SELECT header_field, header_data FROM header WHERE id = ?",
1336 DP_DB_BASIC_EXCEPTION_CHECK;
1338 errorcode = sqlite3_bind_int(stmt, 1, id);
1340 DP_DB_BASIC_EXCEPTION_CHECK;
1342 while ((errorcode = sqlite3_step(stmt)) == SQLITE_ROW) {
1343 int buffer_length = 0;
1344 char *header_field = (char *)(sqlite3_column_text(stmt, 0));
1345 char *header_data = (char *)(sqlite3_column_text(stmt, 1));
1347 // REF : http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
1348 buffer_length = strlen(header_field) + strlen(header_data) + 1;
1349 char *headers_buffer = calloc(buffer_length + 1, sizeof(char));
1350 if (headers_buffer == NULL) {
1351 TRACE_ERROR("[CALLOC] headers_buffer");
1354 int len = snprintf(headers_buffer, buffer_length + 1,
1355 "%s:%s", header_field, header_data);
1357 free(headers_buffer);
1360 headers_buffer[len] = '\0';
1362 headers[headers_index++] = headers_buffer;
1365 __dp_finalize(stmt);
1367 return headers_index;
1370 int dp_db_get_max_download_id(void *handle, const char *table, int *pvalue, int *error)
1373 int errorcode = SQLITE_OK;
1374 sqlite3_stmt *stmt = NULL;
1375 *error = DP_ERROR_NONE;
1376 DP_DB_PARAM_NULL_CHECK;
1378 char *query = sqlite3_mprintf("SELECT MAX(%s) FROM %s", DP_DB_COL_ID, table);
1379 DP_DB_BUFFER_NULL_CHECK(query);
1380 //TRACE_DEBUG("debug query:%s", query);
1381 errorcode = sqlite3_prepare_v2(handle, query, -1, &stmt, NULL);
1382 DP_DB_BASIC_EXCEPTION_CHECK;
1384 errorcode = sqlite3_step(stmt);
1385 if (errorcode == SQLITE_ROW) {
1386 int data_type = sqlite3_column_type(stmt, 0);
1387 if (data_type == SQLITE_INTEGER) {
1388 int recv_int = sqlite3_column_int(stmt, 0);
1390 } else if (data_type == SQLITE_NULL) {
1391 /* table has no any entry */
1392 TRACE_DEBUG("table has no entry");
1393 *error = DP_ERROR_NO_DATA;
1395 TRACE_ERROR("check column type:%d", data_type);
1396 *error = DP_ERROR_INVALID_PARAMETER;
1398 } else if (errorcode == SQLITE_DONE) {
1399 TRACE_DEBUG("no data");
1400 *error = DP_ERROR_NO_DATA;
1402 if ((*error = dp_db_get_errorcode(handle)) == DP_ERROR_NONE) {
1403 TRACE_ERROR("ERROR :: unknown");
1404 *error = DP_ERROR_DISK_BUSY;
1407 __dp_finalize(stmt);
1408 if (*error != DP_ERROR_NONE)