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/. */
7 // Note: we are already in the namepace mozilla::storage
9 // Note 2: whoever #includes this file must provide implementations of
12 ////////////////////////////////////////////////////////////////////////////////
13 //// variantToSQLiteT Implementation
17 variantToSQLiteT(T aObj
,
20 // Allow to return nullptr not wrapped to nsIVariant for speed.
22 return sqlite3_T_null(aObj
);
25 (void)aValue
->GetDataType(&type
);
27 case nsIDataType::VTYPE_INT8
:
28 case nsIDataType::VTYPE_INT16
:
29 case nsIDataType::VTYPE_INT32
:
30 case nsIDataType::VTYPE_UINT8
:
31 case nsIDataType::VTYPE_UINT16
:
34 nsresult rv
= aValue
->GetAsInt32(&value
);
35 NS_ENSURE_SUCCESS(rv
, SQLITE_MISMATCH
);
36 return sqlite3_T_int(aObj
, value
);
38 case nsIDataType::VTYPE_UINT32
: // Try to preserve full range
39 case nsIDataType::VTYPE_INT64
:
40 // Data loss possible, but there is no unsigned types in SQLite
41 case nsIDataType::VTYPE_UINT64
:
44 nsresult rv
= aValue
->GetAsInt64(&value
);
45 NS_ENSURE_SUCCESS(rv
, SQLITE_MISMATCH
);
46 return sqlite3_T_int64(aObj
, value
);
48 case nsIDataType::VTYPE_FLOAT
:
49 case nsIDataType::VTYPE_DOUBLE
:
52 nsresult rv
= aValue
->GetAsDouble(&value
);
53 NS_ENSURE_SUCCESS(rv
, SQLITE_MISMATCH
);
54 return sqlite3_T_double(aObj
, value
);
56 case nsIDataType::VTYPE_BOOL
:
59 nsresult rv
= aValue
->GetAsBool(&value
);
60 NS_ENSURE_SUCCESS(rv
, SQLITE_MISMATCH
);
61 return sqlite3_T_int(aObj
, value
? 1 : 0);
63 case nsIDataType::VTYPE_CHAR
:
64 case nsIDataType::VTYPE_CHAR_STR
:
65 case nsIDataType::VTYPE_STRING_SIZE_IS
:
66 case nsIDataType::VTYPE_UTF8STRING
:
67 case nsIDataType::VTYPE_CSTRING
:
70 // GetAsAUTF8String should never perform conversion when coming from
71 // 8-bit string types, and thus can accept strings with arbitrary encoding
72 // (including UTF8 and ASCII).
73 nsresult rv
= aValue
->GetAsAUTF8String(value
);
74 NS_ENSURE_SUCCESS(rv
, SQLITE_MISMATCH
);
75 return sqlite3_T_text(aObj
, value
);
77 case nsIDataType::VTYPE_WCHAR
:
78 case nsIDataType::VTYPE_DOMSTRING
:
79 case nsIDataType::VTYPE_WCHAR_STR
:
80 case nsIDataType::VTYPE_WSTRING_SIZE_IS
:
81 case nsIDataType::VTYPE_ASTRING
:
84 // GetAsAString does proper conversion to UCS2 from all string-like types.
85 // It can be used universally without problems (unless someone implements
86 // their own variant, but that's their problem).
87 nsresult rv
= aValue
->GetAsAString(value
);
88 NS_ENSURE_SUCCESS(rv
, SQLITE_MISMATCH
);
89 return sqlite3_T_text16(aObj
, value
);
91 case nsIDataType::VTYPE_VOID
:
92 case nsIDataType::VTYPE_EMPTY
:
93 case nsIDataType::VTYPE_EMPTY_ARRAY
:
94 return sqlite3_T_null(aObj
);
95 case nsIDataType::VTYPE_ARRAY
:
101 nsresult rv
= aValue
->GetAsArray(&type
, &iid
, &count
, &data
);
102 NS_ENSURE_SUCCESS(rv
, SQLITE_MISMATCH
);
104 // Check to make sure it's a supported type.
105 NS_ASSERTION(type
== nsIDataType::VTYPE_UINT8
,
106 "Invalid type passed! You may leak!");
107 if (type
!= nsIDataType::VTYPE_UINT8
) {
108 // Technically this could leak with certain data types, but somebody was
109 // being stupid passing us this anyway.
111 return SQLITE_MISMATCH
;
114 // Finally do our thing. The function should free the array accordingly!
115 int rc
= sqlite3_T_blob(aObj
, data
, count
);
118 // Maybe, it'll be possible to convert these
120 case nsIDataType::VTYPE_ID
:
121 case nsIDataType::VTYPE_INTERFACE
:
122 case nsIDataType::VTYPE_INTERFACE_IS
:
124 return SQLITE_MISMATCH
;