/src/mozilla-central/storage/variantToSQLiteT_impl.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
2 | | * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : |
3 | | * This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | // Note: we are already in the namepace mozilla::storage |
8 | | |
9 | | // Note 2: whoever #includes this file must provide implementations of |
10 | | // sqlite3_T_* prior. |
11 | | |
12 | | //////////////////////////////////////////////////////////////////////////////// |
13 | | //// variantToSQLiteT Implementation |
14 | | |
15 | | template <typename T> |
16 | | int |
17 | | variantToSQLiteT(T aObj, |
18 | | nsIVariant *aValue) |
19 | 0 | { |
20 | 0 | // Allow to return nullptr not wrapped to nsIVariant for speed. |
21 | 0 | if (!aValue) |
22 | 0 | return sqlite3_T_null(aObj); |
23 | 0 | |
24 | 0 | uint16_t valueType; |
25 | 0 | aValue->GetDataType(&valueType); |
26 | 0 | switch (valueType) { |
27 | 0 | case nsIDataType::VTYPE_INT8: |
28 | 0 | case nsIDataType::VTYPE_INT16: |
29 | 0 | case nsIDataType::VTYPE_INT32: |
30 | 0 | case nsIDataType::VTYPE_UINT8: |
31 | 0 | case nsIDataType::VTYPE_UINT16: |
32 | 0 | { |
33 | 0 | int32_t value; |
34 | 0 | nsresult rv = aValue->GetAsInt32(&value); |
35 | 0 | NS_ENSURE_SUCCESS(rv, SQLITE_MISMATCH); |
36 | 0 | return sqlite3_T_int(aObj, value); |
37 | 0 | } |
38 | 0 | case nsIDataType::VTYPE_UINT32: // Try to preserve full range |
39 | 0 | case nsIDataType::VTYPE_INT64: |
40 | 0 | // Data loss possible, but there is no unsigned types in SQLite |
41 | 0 | case nsIDataType::VTYPE_UINT64: |
42 | 0 | { |
43 | 0 | int64_t value; |
44 | 0 | nsresult rv = aValue->GetAsInt64(&value); |
45 | 0 | NS_ENSURE_SUCCESS(rv, SQLITE_MISMATCH); |
46 | 0 | return sqlite3_T_int64(aObj, value); |
47 | 0 | } |
48 | 0 | case nsIDataType::VTYPE_FLOAT: |
49 | 0 | case nsIDataType::VTYPE_DOUBLE: |
50 | 0 | { |
51 | 0 | double value; |
52 | 0 | nsresult rv = aValue->GetAsDouble(&value); |
53 | 0 | NS_ENSURE_SUCCESS(rv, SQLITE_MISMATCH); |
54 | 0 | return sqlite3_T_double(aObj, value); |
55 | 0 | } |
56 | 0 | case nsIDataType::VTYPE_BOOL: |
57 | 0 | { |
58 | 0 | bool value; |
59 | 0 | nsresult rv = aValue->GetAsBool(&value); |
60 | 0 | NS_ENSURE_SUCCESS(rv, SQLITE_MISMATCH); |
61 | 0 | return sqlite3_T_int(aObj, value ? 1 : 0); |
62 | 0 | } |
63 | 0 | case nsIDataType::VTYPE_CHAR: |
64 | 0 | case nsIDataType::VTYPE_CHAR_STR: |
65 | 0 | case nsIDataType::VTYPE_STRING_SIZE_IS: |
66 | 0 | case nsIDataType::VTYPE_UTF8STRING: |
67 | 0 | case nsIDataType::VTYPE_CSTRING: |
68 | 0 | { |
69 | 0 | nsAutoCString value; |
70 | 0 | // GetAsAUTF8String should never perform conversion when coming from |
71 | 0 | // 8-bit string types, and thus can accept strings with arbitrary encoding |
72 | 0 | // (including UTF8 and ASCII). |
73 | 0 | nsresult rv = aValue->GetAsAUTF8String(value); |
74 | 0 | NS_ENSURE_SUCCESS(rv, SQLITE_MISMATCH); |
75 | 0 | return sqlite3_T_text(aObj, value); |
76 | 0 | } |
77 | 0 | case nsIDataType::VTYPE_WCHAR: |
78 | 0 | case nsIDataType::VTYPE_DOMSTRING: |
79 | 0 | case nsIDataType::VTYPE_WCHAR_STR: |
80 | 0 | case nsIDataType::VTYPE_WSTRING_SIZE_IS: |
81 | 0 | case nsIDataType::VTYPE_ASTRING: |
82 | 0 | { |
83 | 0 | nsAutoString value; |
84 | 0 | // GetAsAString does proper conversion to UCS2 from all string-like types. |
85 | 0 | // It can be used universally without problems (unless someone implements |
86 | 0 | // their own variant, but that's their problem). |
87 | 0 | nsresult rv = aValue->GetAsAString(value); |
88 | 0 | NS_ENSURE_SUCCESS(rv, SQLITE_MISMATCH); |
89 | 0 | return sqlite3_T_text16(aObj, value); |
90 | 0 | } |
91 | 0 | case nsIDataType::VTYPE_VOID: |
92 | 0 | case nsIDataType::VTYPE_EMPTY: |
93 | 0 | case nsIDataType::VTYPE_EMPTY_ARRAY: |
94 | 0 | return sqlite3_T_null(aObj); |
95 | 0 | case nsIDataType::VTYPE_ARRAY: |
96 | 0 | { |
97 | 0 | uint16_t arrayType; |
98 | 0 | nsIID iid; |
99 | 0 | uint32_t count; |
100 | 0 | void *data; |
101 | 0 | nsresult rv = aValue->GetAsArray(&arrayType, &iid, &count, &data); |
102 | 0 | NS_ENSURE_SUCCESS(rv, SQLITE_MISMATCH); |
103 | 0 |
|
104 | 0 | // Check to make sure it's a supported type. |
105 | 0 | NS_ASSERTION(arrayType == nsIDataType::VTYPE_UINT8, |
106 | 0 | "Invalid type passed! You may leak!"); |
107 | 0 | if (arrayType != nsIDataType::VTYPE_UINT8) { |
108 | 0 | // Technically this could leak with certain data types, but somebody was |
109 | 0 | // being stupid passing us this anyway. |
110 | 0 | free(data); |
111 | 0 | return SQLITE_MISMATCH; |
112 | 0 | } |
113 | 0 | |
114 | 0 | // Finally do our thing. The function should free the array accordingly! |
115 | 0 | int rc = sqlite3_T_blob(aObj, data, count); |
116 | 0 | return rc; |
117 | 0 | } |
118 | 0 | // Maybe, it'll be possible to convert these |
119 | 0 | // in future too. |
120 | 0 | case nsIDataType::VTYPE_ID: |
121 | 0 | case nsIDataType::VTYPE_INTERFACE: |
122 | 0 | case nsIDataType::VTYPE_INTERFACE_IS: |
123 | 0 | default: |
124 | 0 | return SQLITE_MISMATCH; |
125 | 0 | } |
126 | 0 | return SQLITE_OK; |
127 | 0 | } Unexecuted instantiation: mozStorageBindingParams.cpp:int mozilla::storage::(anonymous namespace)::variantToSQLiteT<mozilla::storage::(anonymous namespace)::BindingColumnData>(mozilla::storage::(anonymous namespace)::BindingColumnData, nsIVariant*) Unexecuted instantiation: mozStorageConnection.cpp:int mozilla::storage::(anonymous namespace)::variantToSQLiteT<sqlite3_context*>(sqlite3_context*, nsIVariant*) |