• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef NATIVE_RDB_SQLITE_ERRNO_H
17 #define NATIVE_RDB_SQLITE_ERRNO_H
18 
19 #include <sqlite3sym.h>
20 
21 #include <map>
22 
23 #include "rdb_errno.h"
24 
25 #ifndef SQLITE_META_RECOVERED
26 #define SQLITE_META_RECOVERED 66
27 #endif
28 namespace OHOS {
29 namespace NativeRdb {
30 struct SQLiteErrorCode {
31     int sqliteError;
32     int rdbError;
33 };
34 static constexpr SQLiteErrorCode SQLiteErrorCodes[] = {
35     { SQLITE_ERROR, E_SQLITE_ERROR },
36     { SQLITE_PERM, E_SQLITE_PERM },
37     { SQLITE_ABORT, E_SQLITE_ABORT },
38     { SQLITE_BUSY, E_SQLITE_BUSY },
39     { SQLITE_LOCKED, E_SQLITE_LOCKED },
40     { SQLITE_NOMEM, E_SQLITE_NOMEM },
41     { SQLITE_READONLY, E_SQLITE_READONLY },
42     { SQLITE_IOERR, E_SQLITE_IOERR },
43     { SQLITE_CORRUPT, E_SQLITE_CORRUPT },
44     { SQLITE_FULL, E_SQLITE_FULL },
45     { SQLITE_CANTOPEN, E_SQLITE_CANTOPEN },
46     { SQLITE_SCHEMA, E_SQLITE_SCHEMA },
47     { SQLITE_TOOBIG, E_SQLITE_TOOBIG },
48     { SQLITE_CONSTRAINT, E_SQLITE_CONSTRAINT },
49     { SQLITE_MISMATCH, E_SQLITE_MISMATCH },
50     { SQLITE_MISUSE, E_SQLITE_MISUSE },
51     { SQLITE_NOTADB, E_SQLITE_CORRUPT },
52     { SQLITE_META_RECOVERED, E_SQLITE_META_RECOVERED },
53     { SQLITE_ROW, E_OK },
54     { SQLITE_DONE, E_NO_MORE_ROWS },
55 };
56 
IsIncreasing()57 static constexpr bool IsIncreasing()
58 {
59     for (size_t i = 1; i < sizeof(SQLiteErrorCodes) / sizeof(SQLiteErrorCode); i++) {
60         if (SQLiteErrorCodes[i].sqliteError <= SQLiteErrorCodes[i - 1].sqliteError) {
61             return false;
62         }
63     }
64     return true;
65 }
66 // JS_ERROR_CODE_MSGS must ensure increment
67 static_assert(IsIncreasing());
68 
GetRdbErrorCode(int32_t errorCode)69 static int GetRdbErrorCode(int32_t errorCode)
70 {
71     auto jsErrorCode = SQLiteErrorCode{ errorCode, -errorCode };
72     auto iter = std::lower_bound(SQLiteErrorCodes,
73         SQLiteErrorCodes + sizeof(SQLiteErrorCodes) / sizeof(SQLiteErrorCodes[0]), jsErrorCode,
74         [](const SQLiteErrorCode &jsErrorCode1, const SQLiteErrorCode &jsErrorCode2) {
75             return jsErrorCode1.sqliteError < jsErrorCode2.sqliteError;
76         });
77     if (iter < SQLiteErrorCodes + sizeof(SQLiteErrorCodes) / sizeof(SQLiteErrorCodes[0]) &&
78         iter->sqliteError == errorCode) {
79         return iter->rdbError;
80     }
81     return -errorCode;
82 }
83 class SQLiteError {
84 public:
ErrNo(int sqliteErr)85     static int ErrNo(int sqliteErr)
86     {
87         return GetRdbErrorCode(sqliteErr);
88     }
89 };
90 } // namespace NativeRdb
91 } // namespace OHOS
92 
93 #endif
94