• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_RDB_STORE_H
17 #define NATIVE_RDB_RDB_STORE_H
18 
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 #include "abs_rdb_predicates.h"
24 #include "abs_shared_result_set.h"
25 #include "result_set.h"
26 #include "value_object.h"
27 #include "values_bucket.h"
28 #include "rdb_types.h"
29 
30 namespace OHOS::NativeRdb {
31 enum class ConflictResolution {
32     ON_CONFLICT_NONE = 0,
33     ON_CONFLICT_ROLLBACK,
34     ON_CONFLICT_ABORT,
35     ON_CONFLICT_FAIL,
36     ON_CONFLICT_IGNORE,
37     ON_CONFLICT_REPLACE,
38 };
39 
40 class RdbStore {
41 public:
42     using SyncOption = DistributedRdb::SyncOption;
43     using SyncCallback = DistributedRdb::SyncCallback;
44     using SubscribeMode = DistributedRdb::SubscribeMode;
45     using SubscribeOption = DistributedRdb::SubscribeOption;
46     using DropOption = DistributedRdb::DropOption;
47     using RdbStoreObserver = DistributedRdb::RdbStoreObserver;
48 
~RdbStore()49     virtual ~RdbStore() {};
50     virtual int Insert(int64_t &outRowId, const std::string &table, const ValuesBucket &initialValues) = 0;
51     virtual int Replace(int64_t &outRowId, const std::string &table, const ValuesBucket &initialValues) = 0;
52     virtual int InsertWithConflictResolution(int64_t &outRowId, const std::string &table,
53         const ValuesBucket &initialValues,
54         ConflictResolution conflictResolution = ConflictResolution::ON_CONFLICT_NONE) = 0;
55     virtual int Update(int &changedRows, const std::string &table, const ValuesBucket &values,
56         const std::string &whereClause = "",
57         const std::vector<std::string> &whereArgs = std::vector<std::string>()) = 0;
58     virtual int UpdateWithConflictResolution(int &changedRows, const std::string &table, const ValuesBucket &values,
59         const std::string &whereClause = "", const std::vector<std::string> &whereArgs = std::vector<std::string>(),
60         ConflictResolution conflictResolution = ConflictResolution::ON_CONFLICT_NONE) = 0;
61     virtual int Delete(int &deletedRows, const std::string &table, const std::string &whereClause = "",
62         const std::vector<std::string> &whereArgs = std::vector<std::string>()) = 0;
63     virtual std::unique_ptr<AbsSharedResultSet> Query(int &errCode, bool distinct, const std::string &table,
64         const std::vector<std::string> &columns, const std::string &selection = "",
65         const std::vector<std::string> &selectionArgs = std::vector<std::string>(), const std::string &groupBy = "",
66         const std::string &having = "", const std::string &orderBy = "", const std::string &limit = "") = 0;
67     virtual std::unique_ptr<AbsSharedResultSet> QuerySql(
68         const std::string &sql, const std::vector<std::string> &selectionArgs = std::vector<std::string>()) = 0;
69     virtual std::unique_ptr<ResultSet> QueryByStep(
70         const std::string &sql, const std::vector<std::string> &selectionArgs = std::vector<std::string>()) = 0;
71     virtual int ExecuteSql(
72         const std::string &sql, const std::vector<ValueObject> &bindArgs = std::vector<ValueObject>()) = 0;
73     virtual int ExecuteAndGetLong(int64_t &outValue, const std::string &sql,
74         const std::vector<ValueObject> &bindArgs = std::vector<ValueObject>()) = 0;
75     virtual int ExecuteAndGetString(std::string &outValue, const std::string &sql,
76         const std::vector<ValueObject> &bindArgs = std::vector<ValueObject>()) = 0;
77     virtual int ExecuteForLastInsertedRowId(int64_t &outValue, const std::string &sql,
78         const std::vector<ValueObject> &bindArgs = std::vector<ValueObject>()) = 0;
79     virtual int ExecuteForChangedRowCount(int64_t &outValue, const std::string &sql,
80         const std::vector<ValueObject> &bindArgs = std::vector<ValueObject>()) = 0;
81     virtual int Backup(const std::string databasePath, const std::vector<uint8_t> destEncryptKey) = 0;
82     virtual int Attach(
83         const std::string &alias, const std::string &pathName, const std::vector<uint8_t> destEncryptKey) = 0;
84 
85     virtual int Count(int64_t &outValue, const AbsRdbPredicates &predicates) = 0;
86     virtual std::unique_ptr<AbsSharedResultSet> Query(
87         const AbsRdbPredicates &predicates, const std::vector<std::string> columns) = 0;
88     virtual int Update(int &changedRows, const ValuesBucket &values, const AbsRdbPredicates &predicates) = 0;
89     virtual int Delete(int &deletedRows, const AbsRdbPredicates &predicates) = 0;
90 
91     virtual int GetVersion(int &version) = 0;
92     virtual int SetVersion(int version) = 0;
93     virtual int BeginTransaction() = 0;
94     virtual int RollBack() = 0;
95     virtual int Commit() = 0;
96     virtual int MarkAsCommit() = 0;
97     virtual int EndTransaction() = 0;
98     virtual bool IsInTransaction() = 0;
99     virtual int ChangeEncryptKey(const std::vector<uint8_t> &newKey) = 0;
100     virtual std::string GetPath() = 0;
101     virtual bool IsHoldingConnection() = 0;
102     virtual bool IsOpen() const = 0;
103     virtual bool IsReadOnly() const = 0;
104     virtual bool IsMemoryRdb() const = 0;
105     virtual int ChangeDbFileForRestore(const std::string newPath, const std::string backupPath,
106         const std::vector<uint8_t> &newKey) = 0;
107 
108     virtual bool SetDistributedTables(const std::vector<std::string>& tables) = 0;
109 
110     virtual std::string ObtainDistributedTableName(const std::string& device, const std::string& table) = 0;
111 
112     virtual bool Sync(const SyncOption& option, const AbsRdbPredicates& predicate, const SyncCallback& callback) = 0;
113 
114     virtual bool Subscribe(const SubscribeOption& option, RdbStoreObserver *observer) = 0;
115 
116     virtual bool UnSubscribe(const SubscribeOption& option, RdbStoreObserver *observer) = 0;
117 
118     // user must use UDID
119     virtual bool DropDeviceData(const std::vector<std::string>& devices, const DropOption& option) = 0;
120 };
121 }
122 #endif
123