Avi Drissman | 69b874f | 2022-09-15 19:11:14 | [diff] [blame] | 1 | // Copyright 2022 The Chromium Authors |
Victor Costan | ab7a245 | 2022-03-21 23:06:08 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef SQL_SQLITE_RESULT_CODE_H_ |
| 6 | #define SQL_SQLITE_RESULT_CODE_H_ |
| 7 | |
| 8 | #include<iosfwd> |
Will Harris | b60653cc | 2023-06-13 22:32:23 | [diff] [blame] | 9 | #include<string> |
Victor Costan | ab7a245 | 2022-03-21 23:06:08 | [diff] [blame] | 10 | |
| 11 | #include"base/component_export.h" |
| 12 | #include"base/dcheck_is_on.h" |
| 13 | |
| 14 | namespace sql{ |
| 15 | |
| 16 | // Strongly typed enumeration of all known SQLite result codes. |
| 17 | // |
| 18 | // The meaning of the codes is listed at https://www.sqlite.org/rescode.html |
| 19 | // |
| 20 | // Chrome's SQLite expose SqliteResultCode and SqliteErrorCode instead of plain |
| 21 | // ints. This isolates the use of sqlite3.h to the SQLite wrapper code itself. |
| 22 | // |
| 23 | // The forwarding declaration here is sufficient for most usage. The values are |
| 24 | // defined in sqlite_result_code_values.h. |
| 25 | enumclassSqliteResultCode:int; |
| 26 | |
| 27 | // Strongly typed enumeration of all known SQLite error codes. |
| 28 | // |
| 29 | // Error codes are a subset of all the result codes. Therefore, every |
| 30 | // SqliteErrorCode is a valid SqliteResultCode. |
| 31 | // |
| 32 | // The forwarding declaration here is sufficient for most usage. The values are |
| 33 | // defined in sqlite_result_code_values.h. |
| 34 | enumclassSqliteErrorCode:int; |
| 35 | |
| 36 | // SQLite result codes, mapped into a more compact form for UMA logging. |
| 37 | // |
| 38 | // SQLite's (extended) result codes cover a wide range of integer values, and |
| 39 | // are not suitable for direct use with our UMA logging infrastructure. This |
| 40 | // enum compresses the range by removing gaps and by mapping multiple SQLite |
| 41 | // result codes to the same value where appropriate. |
| 42 | // |
| 43 | // The forwarding declaration here is sufficient for most headers. The values |
| 44 | // are defined in sqlite_result_code_values.h. |
| 45 | enumclassSqliteLoggedResultCode:int; |
| 46 | |
| 47 | // Converts an int returned by SQLite into a strongly typed result code. |
| 48 | // |
| 49 | // This method DCHECKs that `sqlite_result_code` is a known SQLite result code. |
| 50 | #if DCHECK_IS_ON() |
| 51 | COMPONENT_EXPORT(SQL) |
| 52 | SqliteResultCodeToSqliteResultCode(int sqlite_result_code); |
| 53 | #else |
| 54 | inlineSqliteResultCodeToSqliteResultCode(int sqlite_result_code){ |
| 55 | returnstatic_cast<SqliteResultCode>(sqlite_result_code); |
| 56 | } |
| 57 | #endif// DCHECK_IS_ON() |
| 58 | |
| 59 | // Converts a SqliteResultCode into a SqliteErrorCode. |
| 60 | // |
| 61 | // Callers should make sure that `sqlite_result_code` is indeed an error code, |
Victor Costan | f176d24 | 2022-03-22 05:31:22 | [diff] [blame] | 62 | // and does not indicate success. IsSqliteSuccessCode() could be used for this |
| 63 | // purpose. |
Victor Costan | ab7a245 | 2022-03-21 23:06:08 | [diff] [blame] | 64 | #if DCHECK_IS_ON() |
| 65 | COMPONENT_EXPORT(SQL) |
| 66 | SqliteErrorCodeToSqliteErrorCode(SqliteResultCode sqlite_error_code); |
| 67 | #else |
| 68 | inlineSqliteErrorCodeToSqliteErrorCode(SqliteResultCode sqlite_error_code){ |
| 69 | returnstatic_cast<SqliteErrorCode>(sqlite_error_code); |
| 70 | } |
| 71 | #endif// DCHECK_IS_ON() |
| 72 | |
Victor Costan | f176d24 | 2022-03-22 05:31:22 | [diff] [blame] | 73 | // Returns true if `sqlite_result_code` reports a successful operation. |
| 74 | // |
| 75 | // `sqlite_result_code` should only be passed to ToSqliteErrorCode() if this |
| 76 | // function returns false. |
| 77 | COMPONENT_EXPORT(SQL) |
| 78 | boolIsSqliteSuccessCode(SqliteResultCode sqlite_result_code); |
| 79 | |
Victor Costan | ab7a245 | 2022-03-21 23:06:08 | [diff] [blame] | 80 | // Helper for logging a SQLite result code to a UMA histogram. |
| 81 | // |
| 82 | // The histogram should be declared as enum="SqliteLoggedResultCode". |
| 83 | // |
| 84 | // Works for all result codes, including success codes and extended error codes. |
| 85 | // DCHECKs if provided result code should not occur in Chrome's usage of SQLite. |
| 86 | COMPONENT_EXPORT(SQL) |
Will Harris | b60653cc | 2023-06-13 22:32:23 | [diff] [blame] | 87 | voidUmaHistogramSqliteResult(const std::string& histogram_name, |
Victor Costan | ab7a245 | 2022-03-21 23:06:08 | [diff] [blame] | 88 | int sqlite_result_code); |
| 89 | |
| 90 | // Converts a SQLite result code into a UMA logging-friendly form. |
| 91 | // |
| 92 | // Works for all result codes, including success codes and extended error codes. |
| 93 | // DCHECKs if provided result code should not occur in Chrome's usage of SQLite. |
| 94 | // |
| 95 | // UmaHistogramSqliteResult() should be preferred for logging results to UMA. |
| 96 | COMPONENT_EXPORT(SQL) |
| 97 | SqliteLoggedResultCodeToSqliteLoggedResultCode(int sqlite_result_code); |
| 98 | |
| 99 | // Logging support. |
| 100 | COMPONENT_EXPORT(SQL) |
| 101 | std::ostream&operator<<(std::ostream& os,SqliteResultCode sqlite_result_code); |
| 102 | COMPONENT_EXPORT(SQL) |
| 103 | std::ostream&operator<<(std::ostream& os,SqliteErrorCode sqlite_error_code); |
| 104 | |
| 105 | // Called by unit tests. |
| 106 | // |
| 107 | // DCHECKs the representation invariants of the mapping table used to convert |
| 108 | // SQLite result codes to logging-friendly values. |
| 109 | COMPONENT_EXPORT(SQL)voidCheckSqliteLoggedResultCodeForTesting(); |
| 110 | |
| 111 | }// namespace sql |
| 112 | |
| 113 | #endif// SQL_SQLITE_RESULT_CODE_H_ |