1 /*
2  * Copyright (c) 2021 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 "sqlite_storage_executor.h"
17 
18 #include "db_errno.h"
19 #include "log_print.h"
20 #include "sqlite_utils.h"
21 
22 namespace DistributedDB {
SQLiteStorageExecutor(sqlite3 * dbHandle,bool writable,bool isMemDb)23 SQLiteStorageExecutor::SQLiteStorageExecutor(sqlite3 *dbHandle, bool writable, bool isMemDb)
24     : StorageExecutor(writable),
25       dbHandle_(dbHandle),
26       isMemDb_(isMemDb)
27 {}
28 
~SQLiteStorageExecutor()29 SQLiteStorageExecutor::~SQLiteStorageExecutor()
30 {
31     if (dbHandle_ != nullptr) {
32         (void)sqlite3_close_v2(dbHandle_);
33         dbHandle_ = nullptr;
34     }
35 }
36 
Reset()37 int SQLiteStorageExecutor::Reset()
38 {
39     if (dbHandle_ != nullptr) {
40         // Set the handle to be valid, release the heap memory as much as possible.
41         sqlite3_db_release_memory(dbHandle_);
42         return E_OK;
43     }
44     return -E_INVALID_DB;
45 }
46 
GetDbHandle(sqlite3 * & dbHandle) const47 int SQLiteStorageExecutor::GetDbHandle(sqlite3 *&dbHandle) const
48 {
49     if (dbHandle_ == nullptr) {
50         LOGE("Can not get dbhandle from executor!");
51         return -E_NOT_FOUND;
52     }
53     dbHandle = dbHandle_;
54     return E_OK;
55 }
56 
IsMemory() const57 bool SQLiteStorageExecutor::IsMemory() const
58 {
59     return isMemDb_;
60 }
61 } // namespace DistributedDB
62