• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "logger.h"
17 #include "rdb_errno.h"
18 #include "rdb_helper.h"
19 #include "rdb_predicates.h"
20 #include "rdb_sql_utils.h"
21 #include "relational_cursor.h"
22 #include "relational_store_error_code.h"
23 #include "relational_predicates.h"
24 #include "relational_store_impl.h"
25 #include "relational_predicates_objects.h"
26 #include "relational_values_bucket.h"
27 #include "sqlite_global_config.h"
28 
29 using namespace OHOS::RdbNdk;
30 constexpr int RDB_STORE_CID = 1234560; // The class id used to uniquely identify the OH_Rdb_Store class.
OH_Rdb_CreateValueObject()31 OH_VObject *OH_Rdb_CreateValueObject()
32 {
33     return new (std::nothrow) RelationalPredicatesObjects();
34 }
35 
OH_Rdb_CreateValuesBucket()36 OH_VBucket *OH_Rdb_CreateValuesBucket()
37 {
38     return new (std::nothrow) RelationalValuesBucket();
39 }
40 
OH_Rdb_CreatePredicates(const char * table)41 OH_Predicates *OH_Rdb_CreatePredicates(const char *table)
42 {
43     if (table == nullptr) {
44         return nullptr;
45     }
46     return new (std::nothrow) RelationalPredicate(table);
47 }
48 
RelationalStore(std::shared_ptr<OHOS::NativeRdb::RdbStore> store)49 OHOS::RdbNdk::RelationalStore::RelationalStore(std::shared_ptr<OHOS::NativeRdb::RdbStore> store) : store_(store)
50 {
51     id = RDB_STORE_CID;
52 }
53 
54 class MainOpenCallback : public OHOS::NativeRdb::RdbOpenCallback {
55 public:
56     int OnCreate(OHOS::NativeRdb::RdbStore &rdbStore) override;
57     int OnUpgrade(OHOS::NativeRdb::RdbStore &rdbStore, int oldVersion, int newVersion) override;
58 };
59 
OnCreate(OHOS::NativeRdb::RdbStore & rdbStore)60 int MainOpenCallback::OnCreate(OHOS::NativeRdb::RdbStore &rdbStore)
61 {
62     return OHOS::NativeRdb::E_OK;
63 }
64 
OnUpgrade(OHOS::NativeRdb::RdbStore & rdbStore,int oldVersion,int newVersion)65 int MainOpenCallback::OnUpgrade(OHOS::NativeRdb::RdbStore &rdbStore, int oldVersion, int newVersion)
66 {
67     return OHOS::NativeRdb::E_OK;
68 }
69 
GetRelationalStore(OH_Rdb_Store * store)70 RelationalStore *GetRelationalStore(OH_Rdb_Store *store)
71 {
72     if (store == nullptr || store->id != RDB_STORE_CID) {
73         LOG_ERROR("store is invalid. is null %{public}d", (store == nullptr));
74         return nullptr;
75     }
76     return static_cast<RelationalStore *>(store);
77 }
78 
OH_Rdb_GetOrOpen(const OH_Rdb_Config * config,int * errCode)79 OH_Rdb_Store *OH_Rdb_GetOrOpen(const OH_Rdb_Config *config, int *errCode)
80 {
81     if (config == nullptr || config->selfSize != sizeof(OH_Rdb_Config) || errCode == nullptr) {
82         LOG_ERROR("Parameters set error:config is NULL ? %{public}d and config size is %{public}zu or "
83                   "errCode is NULL ? %{public}d ",
84             (config == nullptr), sizeof(OH_Rdb_Config), (errCode == nullptr));
85         return nullptr;
86     }
87 
88     std::string realPath = OHOS::NativeRdb::RdbSqlUtils::GetDefaultDatabasePath(config->dataBaseDir,
89         config->storeName, *errCode);
90     if (*errCode != 0) {
91         LOG_ERROR("Get database path failed, ret %{public}d ", *errCode);
92         return nullptr;
93     }
94     OHOS::NativeRdb::RdbStoreConfig rdbStoreConfig(realPath);
95     rdbStoreConfig.SetSecurityLevel(OHOS::NativeRdb::SecurityLevel(config->securityLevel));
96     rdbStoreConfig.SetEncryptStatus(config->isEncrypt);
97     if (config->bundleName != nullptr) {
98         rdbStoreConfig.SetBundleName(config->bundleName);
99     }
100     rdbStoreConfig.SetName(config->storeName);
101 
102     MainOpenCallback callback;
103     std::shared_ptr<OHOS::NativeRdb::RdbStore> store =
104         OHOS::NativeRdb::RdbHelper::GetRdbStore(rdbStoreConfig, -1, callback, *errCode);
105     if (store == nullptr) {
106         LOG_ERROR("Get RDB Store fail %{public}s", realPath.c_str());
107         return nullptr;
108     }
109     return new (std::nothrow) RelationalStore(store);
110 }
111 
OH_Rdb_CloseStore(OH_Rdb_Store * store)112 int OH_Rdb_CloseStore(OH_Rdb_Store *store)
113 {
114     auto rdbStore = GetRelationalStore(store);
115     if (rdbStore == nullptr) {
116         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
117     }
118     delete rdbStore;
119     return OH_Rdb_ErrCode::RDB_OK;
120 }
121 
OH_Rdb_DeleteStore(const OH_Rdb_Config * config)122 int OH_Rdb_DeleteStore(const OH_Rdb_Config *config)
123 {
124     if (config == nullptr || config->dataBaseDir == nullptr || config->storeName == nullptr) {
125         LOG_ERROR("Parameters set error:path is NULL ? %{public}d", (config == nullptr));
126         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
127     }
128     int errCode = OHOS::NativeRdb::E_OK;
129     std::string realPath = OHOS::NativeRdb::RdbSqlUtils::GetDefaultDatabasePath(config->dataBaseDir,
130         config->storeName, errCode);
131     if (errCode != OHOS::NativeRdb::E_OK) {
132         return errCode;
133     }
134     return OHOS::NativeRdb::RdbHelper::DeleteRdbStore(realPath);
135 }
136 
OH_Rdb_Insert(OH_Rdb_Store * store,const char * table,OH_VBucket * valuesBucket)137 int OH_Rdb_Insert(OH_Rdb_Store *store, const char *table, OH_VBucket *valuesBucket)
138 {
139     auto rdbStore = GetRelationalStore(store);
140     auto bucket = RelationalValuesBucket::GetSelf(valuesBucket);
141     if (rdbStore == nullptr || table == nullptr || bucket == nullptr) {
142         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
143     }
144     int64_t rowId = -1;
145     rdbStore->GetStore()->Insert(rowId, table, bucket->Get());
146     return rowId >= 0 ? rowId : OH_Rdb_ErrCode::RDB_ERR;
147 }
148 
OH_Rdb_Update(OH_Rdb_Store * store,OH_VBucket * valueBucket,OH_Predicates * predicates)149 int OH_Rdb_Update(OH_Rdb_Store *store, OH_VBucket *valueBucket, OH_Predicates *predicates)
150 {
151     auto rdbStore = GetRelationalStore(store);
152     auto predicate = RelationalPredicate::GetSelf(predicates);
153     auto bucket = RelationalValuesBucket::GetSelf(valueBucket);
154     if (rdbStore == nullptr || predicate == nullptr || bucket == nullptr) {
155         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
156     }
157     int updatedRows = -1;
158     rdbStore->GetStore()->Update(updatedRows, bucket->Get(), predicate->Get());
159     return updatedRows >= 0 ? updatedRows : OH_Rdb_ErrCode::RDB_ERR;
160 }
161 
OH_Rdb_Delete(OH_Rdb_Store * store,OH_Predicates * predicates)162 int OH_Rdb_Delete(OH_Rdb_Store *store, OH_Predicates *predicates)
163 {
164     auto rdbStore = GetRelationalStore(store);
165     auto predicate = RelationalPredicate::GetSelf(predicates);
166     if (rdbStore == nullptr || predicate == nullptr) {
167         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
168     }
169     int deletedRows = -1;
170     rdbStore->GetStore()->Delete(deletedRows, predicate->Get());
171     return deletedRows >= 0 ? deletedRows : OH_Rdb_ErrCode::RDB_ERR;
172 }
173 
OH_Rdb_Query(OH_Rdb_Store * store,OH_Predicates * predicates,const char * const * columnNames,int length)174 OH_Cursor *OH_Rdb_Query(OH_Rdb_Store *store, OH_Predicates *predicates, const char *const *columnNames, int length)
175 {
176     auto rdbStore = GetRelationalStore(store);
177     auto predicate = RelationalPredicate::GetSelf(predicates);
178     if (rdbStore == nullptr || predicate == nullptr) {
179         return nullptr;
180     }
181     std::vector<std::string> columns;
182     if (columnNames != nullptr && length > 0) {
183         columns.reserve(length);
184         for (int i = 0; i < length; i++) {
185             columns.push_back(columnNames[i]);
186         }
187     }
188 
189     std::shared_ptr<OHOS::NativeRdb::ResultSet> resultSet =
190         rdbStore->GetStore()->QueryByStep(predicate->Get(), columns);
191     if (resultSet == nullptr) {
192         return nullptr;
193     }
194     return new (std::nothrow) RelationalCursor(std::move(resultSet));
195 }
196 
OH_Rdb_ExecuteQuery(OH_Rdb_Store * store,const char * sql)197 OH_Cursor *OH_Rdb_ExecuteQuery(OH_Rdb_Store *store, const char *sql)
198 {
199     auto rdbStore = GetRelationalStore(store);
200     if (rdbStore == nullptr || sql == nullptr) {
201         return nullptr;
202     }
203     std::shared_ptr<OHOS::NativeRdb::ResultSet> resultSet =
204         rdbStore->GetStore()->QuerySql(sql, std::vector<std::string>{});
205     if (resultSet == nullptr) {
206         return nullptr;
207     }
208     return new OHOS::RdbNdk::RelationalCursor(std::move(resultSet));
209 }
210 
OH_Rdb_Execute(OH_Rdb_Store * store,const char * sql)211 int OH_Rdb_Execute(OH_Rdb_Store *store, const char *sql)
212 {
213     auto rdbStore = GetRelationalStore(store);
214     if (rdbStore == nullptr || sql == nullptr) {
215         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
216     }
217     return rdbStore->GetStore()->ExecuteSql(sql, std::vector<OHOS::NativeRdb::ValueObject>{});
218 }
219 
OH_Rdb_BeginTransaction(OH_Rdb_Store * store)220 int OH_Rdb_BeginTransaction(OH_Rdb_Store *store)
221 {
222     auto rdbStore = GetRelationalStore(store);
223     if (rdbStore == nullptr) {
224         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
225     }
226     return rdbStore->GetStore()->BeginTransaction();
227 }
228 
OH_Rdb_RollBack(OH_Rdb_Store * store)229 int OH_Rdb_RollBack(OH_Rdb_Store *store)
230 {
231     auto rdbStore = GetRelationalStore(store);
232     if (rdbStore == nullptr) {
233         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
234     }
235     return rdbStore->GetStore()->RollBack();
236 }
237 
OH_Rdb_Commit(OH_Rdb_Store * store)238 int OH_Rdb_Commit(OH_Rdb_Store *store)
239 {
240     auto rdbStore = GetRelationalStore(store);
241     if (rdbStore == nullptr) {
242         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
243     }
244     return rdbStore->GetStore()->Commit();
245 }
246 
OH_Rdb_Backup(OH_Rdb_Store * store,const char * databasePath)247 int OH_Rdb_Backup(OH_Rdb_Store *store, const char *databasePath)
248 {
249     auto rdbStore = GetRelationalStore(store);
250     if (rdbStore == nullptr || databasePath == nullptr) {
251         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
252     }
253     return rdbStore->GetStore()->Backup(databasePath);
254 }
255 
OH_Rdb_Restore(OH_Rdb_Store * store,const char * databasePath)256 int OH_Rdb_Restore(OH_Rdb_Store *store, const char *databasePath)
257 {
258     auto rdbStore = GetRelationalStore(store);
259     if (rdbStore == nullptr || databasePath == nullptr) {
260         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
261     }
262     return rdbStore->GetStore()->Restore(databasePath);
263 }
264 
OH_Rdb_GetVersion(OH_Rdb_Store * store,int * version)265 int OH_Rdb_GetVersion(OH_Rdb_Store *store, int *version)
266 {
267     auto rdbStore = GetRelationalStore(store);
268     if (rdbStore == nullptr || version == nullptr) {
269         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
270     }
271     return rdbStore->GetStore()->GetVersion(*version);
272 }
273 
OH_Rdb_SetVersion(OH_Rdb_Store * store,int version)274 int OH_Rdb_SetVersion(OH_Rdb_Store *store, int version)
275 {
276     auto rdbStore = GetRelationalStore(store);
277     if (rdbStore == nullptr) {
278         return OH_Rdb_ErrCode::RDB_E_INVALID_ARGS;
279     }
280     return rdbStore->GetStore()->SetVersion(version);
281 }