TAC support for memory optimization
[platform/core/dotnet/launcher.git] / NativeLauncher / util / db_manager.cc
1 /*
2  * Copyright (c) 2016 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 #include "db_manager.h"
18 #include "log.h"
19
20 sqlite3* dbCreate(std::string path)
21 {
22         sqlite3 *sqlite = NULL;
23         char *error;
24         int ret = sqlite3_open(path.c_str(), &sqlite);
25         if (ret != SQLITE_OK) {
26                 _ERR("Sqlite error : [%d] : path [%s]", ret, path.c_str());
27                 return 0;
28         }
29         ret = sqlite3_exec(sqlite, "PRAGMA journal_mode = PERSIST", NULL, NULL, &error);
30         if (ret != SQLITE_OK) {
31                 _ERR("Sqlite error(%d) : %s", ret, error);
32                 sqlite3_free(error);
33                 return 0;
34         }
35         ret = sqlite3_exec(sqlite, CREATE_TAC_DB_TABLE, NULL, NULL, &error);
36         if (ret != SQLITE_OK) {
37                 _ERR("Sqlite error(%d) : %s", ret, error);
38                 sqlite3_free(error);
39                 return 0;
40         }
41         return sqlite;
42 }
43
44 bool dbOpen(sqlite3 *tac_db, std::string path)
45 {
46         if (!tac_db) {
47                 int ret = sqlite3_open(path.c_str(), &tac_db);
48                 if (ret != SQLITE_OK) {
49                         _ERR("Sqlite error : [%d] : path [%s]", ret, path.c_str());
50                         return false;
51                 }
52         }
53         return true;
54 }
55
56 bool dbClose(sqlite3 *tac_db)
57 {
58         if (tac_db) {
59                 sqlite3_exec(tac_db, "COMMIT;", NULL, NULL, NULL);
60                 sqlite3_close(tac_db);
61                 tac_db = NULL;
62         }
63         return true;
64 }
65
66 bool dbRollback(sqlite3 *tac_db)
67 {
68         if (tac_db) {
69                 sqlite3_exec(tac_db, "ROLLBACK;", NULL, NULL, NULL);
70                 sqlite3_close(tac_db);
71                 tac_db = NULL;
72         }
73         return true;
74 }
75
76 bool dbUpdate(sqlite3 *tac_db, std::string path, std::string query)
77 {
78         sqlite3_stmt *stmt;
79         if (!dbOpen(tac_db, path)) {
80                 return false;
81         }
82         int ret = sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
83         ret = sqlite3_prepare(tac_db, query.c_str(), QUERY_MAX_LEN , &stmt, NULL);
84         if (ret != SQLITE_OK) {
85                 _ERR("Sqlite error : [%s,%s]", query.c_str(), sqlite3_errmsg(tac_db));
86                 goto ERROR;
87         }
88         ret = sqlite3_step(stmt);
89         if (ret != SQLITE_DONE && ret != SQLITE_ROW && ret != SQLITE_OK) {
90                 _ERR("Sqlite error [%d]", ret);
91                 goto ERROR;
92         }
93         ret = sqlite3_finalize(stmt);
94         if (ret != SQLITE_OK) {
95                 _ERR("Sqlite error [%d]", ret);
96                 dbClose(tac_db);
97                 return false;
98         }
99         return true;
100 ERROR:
101         ret = sqlite3_finalize(stmt);
102         if (ret != SQLITE_OK) {
103                 _ERR("Sqlite error [%d]", ret);
104         }
105         dbClose(tac_db);
106         return false;
107 }
108
109 bool dbInsert(sqlite3 *tac_db, std::string path, std::string query)
110 {
111         sqlite3_stmt *stmt;
112         if (!dbOpen(tac_db, path)) {
113                 return false;
114         }
115         int ret = sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
116         ret = sqlite3_prepare(tac_db, query.c_str(), QUERY_MAX_LEN , &stmt, NULL);
117         if (ret != SQLITE_OK) {
118                 _ERR("Sqlite error : [%s,%s]", query.c_str(), sqlite3_errmsg(tac_db));
119                 goto ERROR;
120         }
121         ret = sqlite3_step(stmt);
122         if (ret != SQLITE_DONE && ret != SQLITE_ROW && ret != SQLITE_OK) {
123                 _ERR("Sqlite error [%d]", ret);
124                 goto ERROR;
125         }
126         ret = sqlite3_finalize(stmt);
127         if (ret != SQLITE_OK) {
128                 _ERR("Sqlite error [%d]", ret);
129                 dbClose(tac_db);
130                 return false;
131         }
132         return true;
133 ERROR:
134         ret = sqlite3_finalize(stmt);
135         if (ret != SQLITE_OK) {
136                 _ERR("Sqlite error [%d]", ret);
137         }
138         dbClose(tac_db);
139         return false;
140 }
141
142 std::vector<std::string> dbSelect(sqlite3 *tac_db, std::string path, std::string query)
143 {
144         std::vector<std::string> updateDB;
145         sqlite3_stmt* stmt;
146         const char* str = NULL;
147         if (!dbOpen(tac_db, path)) {
148                 return updateDB;
149         }
150         int ret = sqlite3_prepare_v2(tac_db, query.c_str(), strlen(query.c_str()), &stmt, NULL);
151         if (ret != SQLITE_OK) {
152                 _ERR("Sqlite error : [%s,%s]", query.c_str(), sqlite3_errmsg(tac_db));
153                 goto ERROR;
154         }
155         while (sqlite3_step(stmt) == SQLITE_ROW) {
156                 //str = (const char *) sqlite3_column_text(stmt, 1);
157                 //_DBG("pkgid : %s", (!str || !strlen(str)) ? NULL : strdup(str));
158                 //str = (const char *) sqlite3_column_text(stmt, 3);
159                 //_DBG("name : %s", (!str || !strlen(str)) ? NULL : strdup(str));
160                 //str = (const char *) sqlite3_column_text(stmt, 4);
161                 //_DBG("version : %s", (!str || !strlen(str)) ? NULL : strdup(str));
162                 str = (const char *) sqlite3_column_text(stmt, 2);
163                 _DBG("Nuget : %s", (!str || !strlen(str)) ? NULL : strdup(str));
164                 updateDB.push_back((!str || !strlen(str)) ? NULL : strdup(str));
165         }
166         ret = sqlite3_finalize(stmt);
167         if (ret != SQLITE_OK) {
168                 _ERR("Sqlite error [%d]", ret);
169                 dbClose(tac_db);
170                 return updateDB;
171         }
172         return updateDB;
173 ERROR:
174         ret = sqlite3_finalize(stmt);
175         if (ret != SQLITE_OK) {
176                 _ERR("Sqlite error [%d]", ret);
177         }
178         dbClose(tac_db);
179         return updateDB;
180 }
181
182 bool dbDelete(sqlite3 *tac_db, std::string path, std::string query)
183 {
184         sqlite3_stmt *stmt;
185         if (!dbOpen(tac_db, path)) {
186                 return false;
187         }
188         int ret = sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
189         ret = sqlite3_prepare(tac_db, query.c_str(), QUERY_MAX_LEN , &stmt, NULL);
190         if (ret != SQLITE_OK) {
191                 _ERR("Sqlite error : [%s,%s]", query.c_str(), sqlite3_errmsg(tac_db));
192                 goto ERROR;
193         }
194         ret = sqlite3_step(stmt);
195         if (ret != SQLITE_DONE && ret != SQLITE_ROW && ret != SQLITE_OK) {
196                 _ERR("Sqlite error [%d]", ret);
197                 goto ERROR;
198         }
199         ret = sqlite3_finalize(stmt);
200         if (ret != SQLITE_OK) {
201                 _ERR("Sqlite error [%d]", ret);
202                 dbClose(tac_db);
203                 return false;
204         }
205         return true;
206 ERROR:
207         ret = sqlite3_finalize(stmt);
208         if (ret != SQLITE_OK) {
209                 _ERR("Sqlite error [%d]", ret);
210         }
211         dbClose(tac_db);
212         return false;
213 }