Move db initialize func and make deinit func
[platform/core/security/cert-svc.git] / vcore / server / src / cert-server-db.c
1 /**
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *  http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /**
17  * @file     cert-server-db.c
18  * @author   Kyungwook Tak (k.tak@samsung.com)
19  * @version  1.0
20  * @brief    cert server db utils.
21  */
22
23 #include <db-util.h>
24 #include <cert-server-debug.h>
25 #include <cert-server-db.h>
26
27 sqlite3 *cert_store_db = NULL;
28
29 int initialize_db(void)
30 {
31         int result = CERTSVC_SUCCESS;
32
33         if (cert_store_db != NULL)
34                 return CERTSVC_SUCCESS;
35
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);
39                 cert_store_db = NULL;
40                 return CERTSVC_FAIL;
41         }
42
43         return CERTSVC_SUCCESS;
44 }
45
46 void deinitialize_db(void)
47 {
48         if (cert_store_db == NULL)
49                 return;
50
51         db_util_close(cert_store_db);
52         cert_store_db = NULL;
53 }
54
55 int execute_insert_update_query(const char *query)
56 {
57         if (!cert_store_db) {
58                 SLOGE("Database not initialised.");
59                 return CERTSVC_WRONG_ARGUMENT;
60         }
61
62         if (!query) {
63                 SLOGE("Query is NULL.");
64                 return CERTSVC_WRONG_ARGUMENT;
65         }
66
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.");
71                 return CERTSVC_FAIL;
72         }
73
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);
78                 return CERTSVC_FAIL;
79         }
80
81         /* Committing the transaction */
82         result = sqlite3_exec(cert_store_db, "COMMIT", NULL, NULL, NULL);
83         if (result) {
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.");
88
89                 return CERTSVC_FAIL;
90         }
91
92         SLOGD("Transaction Commit and End.");
93
94         return CERTSVC_SUCCESS;
95 }
96
97 int execute_select_query(const char *query, sqlite3_stmt **stmt)
98 {
99         if (!cert_store_db || !query)
100                 return CERTSVC_WRONG_ARGUMENT;
101
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);
105                 return CERTSVC_FAIL;
106         }
107
108         *stmt = stmts;
109         return CERTSVC_SUCCESS;
110 }