2 * Copyright (c) 2024 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.
19 #include "db_manager.h"
20 #include "tac_common.h"
26 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
29 #define __STR(x) __XSTR(x)
30 static const char* __DOTNET_DIR = __STR(DOTNET_DIR);
34 static sqlite3 *tac_db = NULL;
35 static sqlite3 *tlc_db = NULL;
37 // callback function of "sqlite3_exec"
38 int sqliteCb(void *count, int argc, char **argv, char **colName)
47 tac_db = createDB(TAC_APP_LIST_DB, CREATE_TAC_DB_TABLE);
49 _ERR("Sqlite create error. So restore the database.");
50 if (tac_restoreDB() != TAC_ERROR_NONE) {
51 _ERR("Sqlite create error");
54 tac_db = createDB(TAC_APP_LIST_DB, CREATE_TAC_DB_TABLE);
56 _ERR("Sqlite create error");
60 sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
66 tac_db = openDB(TAC_APP_LIST_DB);
68 _ERR("Sqlite open error. So restore the database.");
69 if (tac_restoreDB() != TAC_ERROR_NONE) {
70 _ERR("Sqlite open error");
73 tac_db = openDB(TAC_APP_LIST_DB);
75 _ERR("Sqlite open error");
79 sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
83 int tac_insertDB(const std::string& pkgId, const std::string& np, const std::string& tac_name, const std::string& tac_version)
85 char *sql = sqlite3_mprintf(
86 "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
87 "VALUES (%Q, %Q, %Q, %Q);", pkgId.c_str(), np.c_str(), tac_name.c_str(), tac_version.c_str());
88 if (!insertDB(tac_db, sql)) {
89 _ERR("Sqlite insert error");
97 int tac_countDB(const std::string& pkgId, const std::string& tac_name, const std::string& unp, int& count)
101 sql = sqlite3_mprintf("SELECT COUNT(NUGET) FROM TAC WHERE PKGID = %Q AND NAME = %Q;", pkgId.c_str(), tac_name.c_str());
103 sql = sqlite3_mprintf("SELECT COUNT(NUGET) FROM TAC WHERE NUGET = %Q;", unp.c_str());
105 if (sqlite3_exec(tac_db, sql, sqliteCb, &count, NULL) != SQLITE_OK) {
106 _ERR("Sqlite select error");
114 std::vector<std::string> tac_selectDB(const std::string& pkgId)
116 char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId.c_str());
117 std::vector<std::string> update_tac = selectDB(tac_db, sql);
122 int tac_updateDB(const std::string& pkgId, const std::string& np, const std::string& tac_name, const std::string& tac_version)
124 char *sql = sqlite3_mprintf(
125 "UPDATE TAC SET NAME = %Q, VERSION = %Q, NUGET = %Q WHERE PKGID = %Q AND NAME = %Q;",
126 tac_name.c_str(), tac_version.c_str(), np.c_str(), pkgId.c_str(), tac_name.c_str());
127 if (!updateDB(tac_db, sql)) {
128 _ERR("Sqlite update error");
136 int tac_deleteDB(const std::string& pkgId, const std::string& unp)
140 sql = sqlite3_mprintf("DELETE FROM TAC WHERE PKGID = %Q;", pkgId.c_str());
142 sql = sqlite3_mprintf("DELETE FROM TAC WHERE PKGID = %Q AND NUGET = %Q;", pkgId.c_str(), unp.c_str());
144 if (!deleteDB(tac_db, sql)) {
145 _ERR("Sqlite delete error");
153 void tac_rollbackDB()
173 if (!createDir(TLC_LIBRARIES_DIR)) {
174 _ERR("Cannot create directory: %s", TLC_LIBRARIES_DIR);
177 copySmackAndOwnership(__DOTNET_DIR, TLC_LIBRARIES_DIR);
179 tlc_db = createDB(TLC_APP_LIST_DB, CREATE_TLC_DB_TABLE);
181 _ERR("Sqlite create error. So restore the database.");
182 if (tlc_restoreDB() != TAC_ERROR_NONE) {
183 _ERR("Sqlite create error");
186 tlc_db = createDB(TLC_APP_LIST_DB, CREATE_TLC_DB_TABLE);
188 _ERR("Sqlite create error");
192 sqlite3_exec(tlc_db, "BEGIN;", NULL, NULL, NULL);
198 tlc_db = openDB(TLC_APP_LIST_DB);
200 _ERR("Sqlite open error. So restore the database.");
201 if (tlc_restoreDB() != TAC_ERROR_NONE) {
202 _ERR("Sqlite open error");
205 tlc_db = openDB(TLC_APP_LIST_DB);
207 _ERR("Sqlite open error");
211 sqlite3_exec(tlc_db, "BEGIN;", NULL, NULL, NULL);
215 int tlc_insertDB(const std::string& pkgId, const std::string& fileSha)
217 char *sql = sqlite3_mprintf("INSERT INTO TLC (PKGID, LIBRARY) VALUES (%Q, %Q);", pkgId.c_str(), fileSha.c_str());
218 if (!insertDB(tlc_db, sql)) {
219 _ERR("Sqlite insert error");
227 int tlc_countDB(const std::string& pkgId, const std::string& ulp, int& count)
229 char *sql = sqlite3_mprintf("SELECT COUNT(LIBRARY) FROM TLC WHERE LIBRARY = %Q;", ulp.c_str());
230 if (sqlite3_exec(tlc_db, sql, sqliteCb, &count, NULL) != SQLITE_OK) {
231 _ERR("Sqlite select error");
239 std::vector<std::string> tlc_selectDB(const std::string& pkgId)
241 char *sql = sqlite3_mprintf("SELECT * FROM TLC WHERE PKGID = %Q;", pkgId.c_str());
242 std::vector<std::string> update_tlc = selectDB(tlc_db, sql);
247 int tlc_deleteDB(const std::string& pkgId)
249 char *sql = sqlite3_mprintf("DELETE FROM TLC WHERE PKGID = %Q;", pkgId.c_str());
250 if (!deleteDB(tlc_db, sql)) {
251 _ERR("Sqlite delete error");
259 void tlc_rollbackDB()