• 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 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 #define LNN_DEFAULT_USERID 100
39 #define LNN_INT32_NUM_STR_MAX_LEN 11
40 
41 typedef struct {
42     sqlite3 *db;
43     sqlite3_stmt *stmt;
44     uint32_t state;
45 } DbContext;
46 
47 typedef enum {
48     TABLE_TRUSTED_DEV_INFO,
49     TABLE_NAME_ID_MAX,
50 } TableNameID;
51 
52 typedef struct {
53     char accountHexHash[SHA_256_HEX_HASH_LEN + LNN_INT32_NUM_STR_MAX_LEN + 1];
54     char udid[UDID_BUF_LEN];
55     int32_t userId;
56 } TrustedDevInfoRecord;
57 
58 typedef enum {
59     CLOSE_TRANS_COMMIT = 0,
60     CLOSE_TRANS_ROLLBACK
61 } CloseTransactionType;
62 
63 /* read supports multithreading, and write only supports single thread {@link LOOP_TYPE_DEFAULT}. */
64 int32_t OpenDatabase(DbContext **ctx);
65 int32_t CloseDatabase(DbContext *ctx);
66 
67 int32_t CreateTable(DbContext *ctx, TableNameID id);
68 int32_t DeleteTable(DbContext *ctx, TableNameID id);
69 int32_t CheckTableExist(DbContext *ctx, TableNameID id, bool *isExist);
70 
71 int32_t InsertRecord(DbContext *ctx, TableNameID id, uint8_t *data);
72 int32_t RemoveRecordByKey(DbContext *ctx, TableNameID id, uint8_t *data);
73 int32_t RemoveAllRecord(DbContext *ctx, TableNameID id);
74 int32_t GetRecordNumByKey(DbContext *ctx, TableNameID id, uint8_t *data);
75 int32_t QueryRecordByKey(DbContext *ctx, TableNameID id, uint8_t *data, uint8_t **replyInfo, int infoNum);
76 
77 int32_t OpenTransaction(DbContext *ctx);
78 int32_t CloseTransaction(DbContext *ctx, CloseTransactionType type);
79 int32_t EncryptedDb(DbContext *ctx, const uint8_t *password, uint32_t len);
80 int32_t UpdateDbPassword(DbContext *ctx, const uint8_t *password, uint32_t len);
81 
82 int32_t BindParaInt(DbContext *ctx, int32_t idx, int32_t value);
83 int32_t BindParaInt64(DbContext *ctx, int32_t idx, int64_t value);
84 int32_t BindParaText(DbContext *ctx, int32_t idx, const char *value, uint32_t valueLen);
85 int32_t BindParaDouble(DbContext *ctx, int32_t idx, double value);
86 
87 int32_t GetQueryResultColCount(DbContext *ctx, int32_t *count);
88 int32_t GetQueryResultColText(DbContext *ctx, int32_t cidx, char *text, uint32_t len);
89 int32_t GetQueryResultColInt(DbContext *ctx, int32_t cidx, int32_t *value);
90 int32_t GetQueryResultColInt64(DbContext *ctx, int32_t cidx, int64_t *value);
91 int32_t GetQueryResultColDouble(DbContext *ctx, int32_t cidx, double *value);
92 
93 #ifdef __cplusplus
94 }
95 #endif
96 #endif