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 #ifndef NATIVE_RDB_SQLITE_CONNECTION_H 17 #define NATIVE_RDB_SQLITE_CONNECTION_H 18 19 #include <mutex> 20 #include <memory> 21 #include <vector> 22 23 #include "sqlite3sym.h" 24 #include "rdb_store_config.h" 25 #include "sqlite_statement.h" 26 #include "value_object.h" 27 #include "shared_block.h" 28 29 namespace OHOS { 30 namespace NativeRdb { 31 32 class SqliteConnection { 33 public: 34 static SqliteConnection *Open(const RdbStoreConfig &config, bool isWriteConnection, int &errCode); 35 ~SqliteConnection(); 36 bool IsWriteConnection() const; 37 int Prepare(const std::string &sql, bool &outIsReadOnly); 38 int PrepareAndGetInfo(const std::string &sql, bool &outIsReadOnly, int &numParameters, 39 std::vector<std::string> &columnNames); 40 int ExecuteSql(const std::string &sql, const std::vector<ValueObject> &bindArgs = std::vector<ValueObject>()); 41 int ExecuteForChangedRowCount(int &changedRows, const std::string &sql, const std::vector<ValueObject> &bindArgs); 42 int ExecuteForLastInsertedRowId(int64_t &outRowId, const std::string &sql, 43 const std::vector<ValueObject> &bindArgs); 44 int ExecuteGetLong(int64_t &outValue, const std::string &sql, 45 const std::vector<ValueObject> &bindArgs = std::vector<ValueObject>()); 46 int ExecuteGetString(std::string &outValue, const std::string &sql, 47 const std::vector<ValueObject> &bindArgs = std::vector<ValueObject>()); 48 std::shared_ptr<SqliteStatement> BeginStepQuery(int &errCode, const std::string &sql, 49 const std::vector<ValueObject> &args) const; 50 int DesFinalize(); 51 int EndStepQuery(); 52 void SetInTransaction(bool transaction); 53 bool IsInTransaction(); 54 int LimitWalSize(); 55 #ifdef RDB_SUPPORT_ICU 56 int ConfigLocale(const std::string localeStr); 57 #endif 58 int ExecuteForSharedBlock(int &rowNum, std::string sql, const std::vector<ValueObject> &bindArgs, 59 AppDataFwk::SharedBlock *sharedBlock, int startPos, int requiredPos, bool isCountAllRows); 60 61 private: 62 static constexpr const char *MERGE_ASSETS_FUNC = "merge_assets"; 63 explicit SqliteConnection(bool isWriteConnection); 64 int InnerOpen(const RdbStoreConfig &config); 65 int GetDbPath(const RdbStoreConfig &config, std::string &dbPath); 66 int Config(const RdbStoreConfig &config); 67 int SetPageSize(const RdbStoreConfig &config); 68 int SetEncryptKey(const RdbStoreConfig &config); 69 int SetJournalMode(const RdbStoreConfig &config); 70 int SetJournalSizeLimit(const RdbStoreConfig &config); 71 int SetAutoCheckpoint(const RdbStoreConfig &config); 72 int SetWalSyncMode(const std::string &syncMode); 73 int PrepareAndBind(const std::string &sql, const std::vector<ValueObject> &bindArgs); 74 void LimitPermission(const std::string &dbPath) const; 75 76 int SetPersistWal(); 77 int SetBusyTimeout(int timeout); 78 79 int RegDefaultFunctions(sqlite3 *dbHandle); 80 static void MergeAssets(sqlite3_context *ctx, int argc, sqlite3_value **argv); 81 static void CompAssets(std::map<std::string, ValueObject::Asset> &oldAssets, std::map<std::string, 82 ValueObject::Asset> &newAssets); 83 static void MergeAsset(ValueObject::Asset &oldAsset, ValueObject::Asset &newAsset); 84 85 int SetCustomFunctions(const RdbStoreConfig &config); 86 int SetCustomScalarFunction(const std::string &functionName, int argc, ScalarFunction *function); 87 88 sqlite3 *dbHandle; 89 bool isWriteConnection; 90 bool isReadOnly; 91 SqliteStatement statement; 92 std::shared_ptr<SqliteStatement> stepStatement; 93 std::string filePath; 94 int openFlags; 95 std::mutex rdbMutex; 96 bool inTransaction_; 97 std::map<std::string, ScalarFunctionInfo> customScalarFunctions_; 98 99 static constexpr int DEFAULT_BUSY_TIMEOUT_MS = 2000; 100 }; 101 102 } // namespace NativeRdb 103 } // namespace OHOS 104 #endif 105