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