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 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is mozilla.org code.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * Mozilla Corporation
20 : * Portions created by the Initial Developer are Copyright (C) 2009
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Vladimir Vukicevic <vladimir.vukicevic@oracle.com>
25 : * Shawn Wilsher <me@shawnwilsher.com>
26 : *
27 : * Alternatively, the contents of this file may be used under the terms of
28 : * either the GNU General Public License Version 2 or later (the "GPL"), or
29 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 : * in which case the provisions of the GPL or the LGPL are applicable instead
31 : * of those above. If you wish to allow use of your version of this file only
32 : * under the terms of either the GPL or the LGPL, and not to allow others to
33 : * use your version of this file under the terms of the MPL, indicate your
34 : * decision by deleting the provisions above and replace them with the notice
35 : * and other provisions required by the GPL or the LGPL. If you do not delete
36 : * the provisions above, a recipient may use your version of this file under
37 : * the terms of any one of the MPL, the GPL or the LGPL.
38 : *
39 : * ***** END LICENSE BLOCK ***** */
40 :
41 : // Note: we are already in the namepace mozilla::storage
42 :
43 : // Note 2: whoever #includes this file must provide implementations of
44 : // sqlite3_T_* prior.
45 :
46 : ////////////////////////////////////////////////////////////////////////////////
47 : //// variantToSQLiteT Implementation
48 :
49 : template <typename T>
50 : int
51 468217 : variantToSQLiteT(T aObj,
52 : nsIVariant *aValue)
53 : {
54 : // Allow to return NULL not wrapped to nsIVariant for speed.
55 468217 : if (!aValue)
56 2 : return sqlite3_T_null(aObj);
57 :
58 : PRUint16 type;
59 468215 : (void)aValue->GetDataType(&type);
60 468211 : switch (type) {
61 : case nsIDataType::VTYPE_INT8:
62 : case nsIDataType::VTYPE_INT16:
63 : case nsIDataType::VTYPE_INT32:
64 : case nsIDataType::VTYPE_UINT8:
65 : case nsIDataType::VTYPE_UINT16:
66 : {
67 : PRInt32 value;
68 756 : nsresult rv = aValue->GetAsInt32(&value);
69 756 : NS_ENSURE_SUCCESS(rv, SQLITE_MISMATCH);
70 756 : return sqlite3_T_int(aObj, value);
71 : }
72 : case nsIDataType::VTYPE_UINT32: // Try to preserve full range
73 : case nsIDataType::VTYPE_INT64:
74 : // Data loss possible, but there is no unsigned types in SQLite
75 : case nsIDataType::VTYPE_UINT64:
76 : {
77 : PRInt64 value;
78 236404 : nsresult rv = aValue->GetAsInt64(&value);
79 236401 : NS_ENSURE_SUCCESS(rv, SQLITE_MISMATCH);
80 236401 : return sqlite3_T_int64(aObj, value);
81 : }
82 : case nsIDataType::VTYPE_FLOAT:
83 : case nsIDataType::VTYPE_DOUBLE:
84 : {
85 : double value;
86 12768 : nsresult rv = aValue->GetAsDouble(&value);
87 12768 : NS_ENSURE_SUCCESS(rv, SQLITE_MISMATCH);
88 12768 : return sqlite3_T_double(aObj, value);
89 : }
90 : case nsIDataType::VTYPE_BOOL:
91 : {
92 : bool value;
93 382 : nsresult rv = aValue->GetAsBool(&value);
94 382 : NS_ENSURE_SUCCESS(rv, SQLITE_MISMATCH);
95 382 : return sqlite3_T_int(aObj, value ? 1 : 0);
96 : }
97 : case nsIDataType::VTYPE_CHAR:
98 : case nsIDataType::VTYPE_CHAR_STR:
99 : case nsIDataType::VTYPE_STRING_SIZE_IS:
100 : case nsIDataType::VTYPE_UTF8STRING:
101 : case nsIDataType::VTYPE_CSTRING:
102 : {
103 217056 : nsCAutoString value;
104 : // GetAsAUTF8String should never perform conversion when coming from
105 : // 8-bit string types, and thus can accept strings with arbitrary encoding
106 : // (including UTF8 and ASCII).
107 108528 : nsresult rv = aValue->GetAsAUTF8String(value);
108 108528 : NS_ENSURE_SUCCESS(rv, SQLITE_MISMATCH);
109 108528 : return sqlite3_T_text(aObj, value);
110 : }
111 : case nsIDataType::VTYPE_WCHAR:
112 : case nsIDataType::VTYPE_DOMSTRING:
113 : case nsIDataType::VTYPE_WCHAR_STR:
114 : case nsIDataType::VTYPE_WSTRING_SIZE_IS:
115 : case nsIDataType::VTYPE_ASTRING:
116 : {
117 149964 : nsAutoString value;
118 : // GetAsAString does proper conversion to UCS2 from all string-like types.
119 : // It can be used universally without problems (unless someone implements
120 : // their own variant, but that's their problem).
121 74982 : nsresult rv = aValue->GetAsAString(value);
122 74982 : NS_ENSURE_SUCCESS(rv, SQLITE_MISMATCH);
123 74982 : return sqlite3_T_text16(aObj, value);
124 : }
125 : case nsIDataType::VTYPE_VOID:
126 : case nsIDataType::VTYPE_EMPTY:
127 : case nsIDataType::VTYPE_EMPTY_ARRAY:
128 26343 : return sqlite3_T_null(aObj);
129 : case nsIDataType::VTYPE_ARRAY:
130 : {
131 : PRUint16 type;
132 : nsIID iid;
133 : PRUint32 count;
134 : void *data;
135 8044 : nsresult rv = aValue->GetAsArray(&type, &iid, &count, &data);
136 8044 : NS_ENSURE_SUCCESS(rv, SQLITE_MISMATCH);
137 :
138 : // Check to make sure it's a supported type.
139 8044 : NS_ASSERTION(type == nsIDataType::VTYPE_UINT8,
140 : "Invalid type passed! You may leak!");
141 8044 : if (type != nsIDataType::VTYPE_UINT8) {
142 : // Technically this could leak with certain data types, but somebody was
143 : // being stupid passing us this anyway.
144 0 : NS_Free(data);
145 0 : return SQLITE_MISMATCH;
146 : }
147 :
148 : // Finally do our thing. The function should free the array accordingly!
149 8044 : int rc = sqlite3_T_blob(aObj, data, count);
150 8044 : return rc;
151 : }
152 : // Maybe, it'll be possible to convert these
153 : // in future too.
154 : case nsIDataType::VTYPE_ID:
155 : case nsIDataType::VTYPE_INTERFACE:
156 : case nsIDataType::VTYPE_INTERFACE_IS:
157 : default:
158 4 : return SQLITE_MISMATCH;
159 : }
160 : return SQLITE_OK;
161 : }
|