Bug 1845017 - Disable the TestPHCExhaustion test r=glandium
[gecko.git] / storage / variantToSQLiteT_impl.h
blobcadaa3f176657083864f9328f021553c3ce61802
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
10 // sqlite3_T_* prior.
12 ////////////////////////////////////////////////////////////////////////////////
13 //// variantToSQLiteT Implementation
15 template <typename T>
16 int variantToSQLiteT(T aObj, nsIVariant* aValue) {
17 // Allow to return nullptr not wrapped to nsIVariant for speed.
18 if (!aValue) return sqlite3_T_null(aObj);
20 uint16_t valueType = aValue->GetDataType();
21 switch (valueType) {
22 case nsIDataType::VTYPE_INT8:
23 case nsIDataType::VTYPE_INT16:
24 case nsIDataType::VTYPE_INT32:
25 case nsIDataType::VTYPE_UINT8:
26 case nsIDataType::VTYPE_UINT16: {
27 int32_t value;
28 nsresult rv = aValue->GetAsInt32(&value);
29 NS_ENSURE_SUCCESS(rv, SQLITE_MISMATCH);
30 return sqlite3_T_int(aObj, value);
32 case nsIDataType::VTYPE_UINT32: // Try to preserve full range
33 case nsIDataType::VTYPE_INT64:
34 // Data loss possible, but there is no unsigned types in SQLite
35 case nsIDataType::VTYPE_UINT64: {
36 int64_t value;
37 nsresult rv = aValue->GetAsInt64(&value);
38 NS_ENSURE_SUCCESS(rv, SQLITE_MISMATCH);
39 return sqlite3_T_int64(aObj, value);
41 case nsIDataType::VTYPE_FLOAT:
42 case nsIDataType::VTYPE_DOUBLE: {
43 double value;
44 nsresult rv = aValue->GetAsDouble(&value);
45 NS_ENSURE_SUCCESS(rv, SQLITE_MISMATCH);
46 return sqlite3_T_double(aObj, value);
48 case nsIDataType::VTYPE_BOOL: {
49 bool value;
50 nsresult rv = aValue->GetAsBool(&value);
51 NS_ENSURE_SUCCESS(rv, SQLITE_MISMATCH);
52 return sqlite3_T_int(aObj, value ? 1 : 0);
54 case nsIDataType::VTYPE_CHAR:
55 case nsIDataType::VTYPE_CHAR_STR:
56 case nsIDataType::VTYPE_STRING_SIZE_IS:
57 case nsIDataType::VTYPE_UTF8STRING:
58 case nsIDataType::VTYPE_CSTRING: {
59 nsAutoCString value;
60 // GetAsAUTF8String should never perform conversion when coming from
61 // 8-bit string types, and thus can accept strings with arbitrary encoding
62 // (including UTF8 and ASCII).
63 nsresult rv = aValue->GetAsAUTF8String(value);
64 NS_ENSURE_SUCCESS(rv, SQLITE_MISMATCH);
65 return sqlite3_T_text(aObj, value);
67 case nsIDataType::VTYPE_WCHAR:
68 case nsIDataType::VTYPE_WCHAR_STR:
69 case nsIDataType::VTYPE_WSTRING_SIZE_IS:
70 case nsIDataType::VTYPE_ASTRING: {
71 nsAutoString value;
72 // GetAsAString does proper conversion to UCS2 from all string-like types.
73 // It can be used universally without problems (unless someone implements
74 // their own variant, but that's their problem).
75 nsresult rv = aValue->GetAsAString(value);
76 NS_ENSURE_SUCCESS(rv, SQLITE_MISMATCH);
77 return sqlite3_T_text16(aObj, value);
79 case nsIDataType::VTYPE_VOID:
80 case nsIDataType::VTYPE_EMPTY:
81 case nsIDataType::VTYPE_EMPTY_ARRAY:
82 return sqlite3_T_null(aObj);
83 case nsIDataType::VTYPE_ARRAY: {
84 uint16_t arrayType;
85 nsIID iid;
86 uint32_t count;
87 void* data;
88 nsresult rv = aValue->GetAsArray(&arrayType, &iid, &count, &data);
89 NS_ENSURE_SUCCESS(rv, SQLITE_MISMATCH);
91 // Check to make sure it's a supported type.
92 NS_ASSERTION(arrayType == nsIDataType::VTYPE_UINT8,
93 "Invalid type passed! You may leak!");
94 if (arrayType != nsIDataType::VTYPE_UINT8) {
95 // Technically this could leak with certain data types, but somebody was
96 // being stupid passing us this anyway.
97 free(data);
98 return SQLITE_MISMATCH;
101 // Finally do our thing. The function should free the array accordingly!
102 int rc = sqlite3_T_blob(aObj, data, count);
103 return rc;
105 // Maybe, it'll be possible to convert these
106 // in future too.
107 case nsIDataType::VTYPE_ID:
108 case nsIDataType::VTYPE_INTERFACE:
109 case nsIDataType::VTYPE_INTERFACE_IS:
110 default:
111 return SQLITE_MISMATCH;
113 return SQLITE_OK;