1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "native_sqlite.h"
17 
18 namespace DistributedDB {
CreateDataBase(const std::string & dbUri)19 sqlite3 *NativeSqlite::CreateDataBase(const std::string &dbUri)
20 {
21     LOGD("Create database: %s", dbUri.c_str());
22     sqlite3 *db = nullptr;
23     int r = sqlite3_open_v2(dbUri.c_str(), &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nullptr);
24     if (r != SQLITE_OK) {
25         LOGE("Open database [%s] failed. %d", dbUri.c_str(), r);
26         if (db != nullptr) {
27             (void)sqlite3_close_v2(db);
28             db = nullptr;
29         }
30     }
31     return db;
32 }
33 
ExecSql(sqlite3 * db,const std::string & sql)34 int NativeSqlite::ExecSql(sqlite3 *db, const std::string &sql)
35 {
36     if (db == nullptr || sql.empty()) {
37         return -E_INVALID_ARGS;
38     }
39     char *errMsg = nullptr;
40     int errCode = sqlite3_exec(db, sql.c_str(), nullptr, nullptr, &errMsg);
41     if (errCode != SQLITE_OK && errMsg != nullptr) {
42         LOGE("Execute sql failed. %d err: %s", errCode, errMsg);
43     }
44     sqlite3_free(errMsg);
45     return errCode;
46 }
47 
ExecSql(sqlite3 * db,const std::string & sql,const std::function<int (sqlite3_stmt *)> & bindCallback,const std::function<int (sqlite3_stmt *)> & resultCallback)48 int NativeSqlite::ExecSql(sqlite3 *db, const std::string &sql, const std::function<int (sqlite3_stmt *)> &bindCallback,
49     const std::function<int (sqlite3_stmt *)> &resultCallback)
50 {
51     if (db == nullptr || sql.empty()) {
52         return -E_INVALID_ARGS;
53     }
54 
55     bool bindFinish = true;
56     sqlite3_stmt *stmt = nullptr;
57     int ret = sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, nullptr);
58     if (ret != SQLITE_OK) {
59         goto END;
60     }
61 
62     do {
63         if (bindCallback) {
64             ret = bindCallback(stmt);
65             if (ret != E_OK && ret != -E_UNFINISHED) {
66                 goto END;
67             }
68             bindFinish = (ret != -E_UNFINISHED);
69         }
70 
71         while (true) {
72             ret = sqlite3_step(stmt);
73             if (ret == SQLITE_DONE) {
74                 ret = E_OK; // step finished
75                 break;
76             } else if (ret != SQLITE_ROW) {
77                 goto END; // step return error
78             }
79             if (resultCallback == nullptr) {
80                 continue;
81             }
82             ret = resultCallback(stmt);
83             if (ret != E_OK) {
84                 goto END;
85             }
86             // continue step stmt while callback return E_OK
87         }
88         (void)sqlite3_reset(stmt);
89     } while (!bindFinish);
90 
91 END:
92     (void)sqlite3_finalize(stmt);
93     return ret;
94 }
95 }