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 SQLITE3_UTILS_H
17 #define SQLITE3_UTILS_H
18 
19 #ifndef _WIN32
20 #include <sqlite3sym.h>
21 #else
22 #include <sqlite3.h>
23 #endif
24 #include <stdbool.h>
25 #include <stdint.h>
26 
27 #include "softbus_common.h"
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 #ifndef DEFAULT_STORAGE_PATH
34 #define DEFAULT_STORAGE_PATH "/data/service/el1/public"
35 #endif
36 
37 #define DATABASE_NAME DEFAULT_STORAGE_PATH"/dsoftbus/dsoftbus.db3"
38 
39 typedef struct {
40     sqlite3 *db;
41     sqlite3_stmt *stmt;
42     uint32_t state;
43 } DbContext;
44 
45 typedef enum {
46     TABLE_TRUSTED_DEV_INFO,
47     TABLE_NAME_ID_MAX,
48 } TableNameID;
49 
50 typedef struct {
51     char accountHexHash[SHA_256_HEX_HASH_LEN];
52     char udid[UDID_BUF_LEN];
53 } TrustedDevInfoRecord;
54 
55 typedef enum {
56     CLOSE_TRANS_COMMIT = 0,
57     CLOSE_TRANS_ROLLBACK
58 } CloseTransactionType;
59 
60 /* read supports multithreading, and write only supports single thread {@link LOOP_TYPE_DEFAULT}. */
61 int32_t OpenDatabase(DbContext **ctx);
62 int32_t CloseDatabase(DbContext *ctx);
63 
64 int32_t CreateTable(DbContext *ctx, TableNameID id);
65 int32_t DeleteTable(DbContext *ctx, TableNameID id);
66 int32_t CheckTableExist(DbContext *ctx, TableNameID id, bool *isExist);
67 
68 int32_t InsertRecord(DbContext *ctx, TableNameID id, uint8_t *data);
69 int32_t RemoveRecordByKey(DbContext *ctx, TableNameID id, uint8_t *data);
70 int32_t RemoveAllRecord(DbContext *ctx, TableNameID id);
71 int32_t GetRecordNumByKey(DbContext *ctx, TableNameID id, uint8_t *data);
72 int32_t QueryRecordByKey(DbContext *ctx, TableNameID id, uint8_t *data, uint8_t **replyInfo, int infoNum);
73 
74 int32_t OpenTransaction(DbContext *ctx);
75 int32_t CloseTransaction(DbContext *ctx, CloseTransactionType type);
76 int32_t EncryptedDb(DbContext *ctx, const uint8_t *password, uint32_t len);
77 int32_t UpdateDbPassword(DbContext *ctx, const uint8_t *password, uint32_t len);
78 
79 int32_t BindParaInt(DbContext *ctx, int32_t idx, int32_t value);
80 int32_t BindParaInt64(DbContext *ctx, int32_t idx, int64_t value);
81 int32_t BindParaText(DbContext *ctx, int32_t idx, const char *value, uint32_t valueLen);
82 int32_t BindParaDouble(DbContext *ctx, int32_t idx, double value);
83 
84 int32_t GetQueryResultColCount(DbContext *ctx, int32_t *count);
85 int32_t GetQueryResultColText(DbContext *ctx, int32_t cidx, char *text, uint32_t len);
86 int32_t GetQueryResultColInt(DbContext *ctx, int32_t cidx, int32_t *value);
87 int32_t GetQueryResultColInt64(DbContext *ctx, int32_t cidx, int64_t *value);
88 int32_t GetQueryResultColDouble(DbContext *ctx, int32_t cidx, double *value);
89 
90 #ifdef __cplusplus
91 }
92 #endif
93 #endif