• 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_SESSION_H
17 #define NATIVE_RDB_RDB_STORE_SESSION_H
18 
19 #include <stack>
20 #include <iostream>
21 #include <memory>
22 
23 #include "sqlite_connection.h"
24 #include "sqlite_connection_pool.h"
25 #include "value_object.h"
26 #include "transaction_observer.h"
27 
28 namespace OHOS {
29 namespace NativeRdb {
30 
31 class StoreSession {
32 public:
33     explicit StoreSession(SqliteConnectionPool &connectionPool);
34     ~StoreSession();
35     int ExecuteSql(const std::string &sql, const std::vector<ValueObject> &bindArgs);
36     int ExecuteForChangedRowCount(int &changedRows, const std::string &sql, const std::vector<ValueObject> &bindArgs);
37     int ExecuteForLastInsertedRowId(
38         int64_t &outRowId, const std::string &sql, const std::vector<ValueObject> &bindArgs);
39     int ExecuteGetLong(int64_t &outValue, const std::string &sql, const std::vector<ValueObject> &bindArgs);
40     int ExecuteGetString(std::string &outValue, const std::string &sql, const std::vector<ValueObject> &bindArgs);
41     int Backup(const std::string databasePath, const std::vector<uint8_t> destEncryptKey);
42     bool IsHoldingConnection() const;
43     int GiveConnectionTemporarily(long milliseconds);
44     int CheckNoTransaction() const;
45     int BeginTransaction(TransactionObserver *transactionObserver);
46     int MarkAsCommitWithObserver(TransactionObserver *transactionObserver);
47     int EndTransactionWithObserver(TransactionObserver *transactionObserver);
48     int Attach(const std::string &alias, const std::string &pathName, const std::vector<uint8_t> destEncryptKey);
49     int MarkAsCommit();
50     int EndTransaction();
51     bool IsInTransaction() const;
52     std::shared_ptr<SqliteStatement> BeginStepQuery(
53         int &errCode, const std::string &sql, const std::vector<std::string> &selectionArgs);
54     int EndStepQuery();
55     int PrepareAndGetInfo(const std::string &sql, bool &outIsReadOnly, int &numParameters,
56         std::vector<std::string> &columnNames);
57     int ExecuteForSharedBlock(int &rowNum, std::string sql, const std::vector<ValueObject> &bindArgs,
58         AppDataFwk::SharedBlock *sharedBlock, int startPos, int requiredPos, bool isCountAllRows);
59 
60     int BeginTransaction();
61     int Commit();
62     int RollBack();
63 
64 private:
65 
66     void AcquireConnection(bool isReadOnly);
67     void ReleaseConnection();
68     int BeginExecuteSql(const std::string &sql);
69     SqliteConnectionPool &connectionPool;
70     SqliteConnection *connection;
71     int connectionUseCount;
72     bool isInStepQuery;
73 
74     const std::string ATTACH_BACKUP_SQL = "ATTACH ? AS backup KEY ?";
75     const std::string ATTACH_SQL = "ATTACH ? AS ? KEY ?";
76     const std::string EXPORT_SQL = "SELECT export_database('backup')";
77     const std::string DETACH_BACKUP_SQL = "detach backup";
78 };
79 
80 } // namespace NativeRdb
81 } // namespace OHOS
82 #endif