2 * Copyright (c) 2015 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.
17 * @file cert-server-db.c
18 * @author Kyungwook Tak (k.tak@samsung.com)
20 * @brief cert server db utils.
24 #include <cert-server-debug.h>
25 #include <cert-server-db.h>
27 sqlite3 *cert_store_db = NULL;
29 int initialize_db(void)
31 int result = CERTSVC_SUCCESS;
33 if (cert_store_db != NULL)
34 return CERTSVC_SUCCESS;
36 result = db_util_open(CERTSVC_SYSTEM_STORE_DB, &cert_store_db, 0);
37 if (result != SQLITE_OK) {
38 SLOGE("opening %s failed!", CERTSVC_SYSTEM_STORE_DB);
43 return CERTSVC_SUCCESS;
46 void deinitialize_db(void)
48 if (cert_store_db == NULL)
51 db_util_close(cert_store_db);
55 int execute_insert_update_query(const char *query)
58 SLOGE("Database not initialised.");
59 return CERTSVC_WRONG_ARGUMENT;
63 SLOGE("Query is NULL.");
64 return CERTSVC_WRONG_ARGUMENT;
67 /* Begin transaction */
68 int result = sqlite3_exec(cert_store_db, "BEGIN EXCLUSIVE", NULL, NULL, NULL);
69 if (result != SQLITE_OK) {
70 SLOGE("Failed to begin transaction.");
74 /* Executing command */
75 result = sqlite3_exec(cert_store_db, query, NULL, NULL, NULL);
76 if (result != SQLITE_OK) {
77 SLOGE("Failed to execute query (%s).", query);
81 /* Committing the transaction */
82 result = sqlite3_exec(cert_store_db, "COMMIT", NULL, NULL, NULL);
84 SLOGE("Failed to commit transaction. Roll back now.");
85 result = sqlite3_exec(cert_store_db, "ROLLBACK", NULL, NULL, NULL);
86 if (result != SQLITE_OK)
87 SLOGE("Failed to commit transaction. Roll back now.");
92 SLOGD("Transaction Commit and End.");
94 return CERTSVC_SUCCESS;
97 int execute_select_query(const char *query, sqlite3_stmt **stmt)
99 if (!cert_store_db || !query)
100 return CERTSVC_WRONG_ARGUMENT;
102 sqlite3_stmt *stmts = NULL;
103 if (sqlite3_prepare_v2(cert_store_db, query, strlen(query), &stmts, NULL) != SQLITE_OK) {
104 SLOGE("sqlite3_prepare_v2 failed [%s].", query);
109 return CERTSVC_SUCCESS;