Bug 435531 ? problem saving login when form or existing login is password-only. r...
[wine-gecko.git] / storage / src / mozStorageRow.cpp
blobbcab47d1806fe5b0248654399c83fba0c38657c8
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=2 sts=2 expandtab
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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.
16 * The Original Code is mozilla.org code.
18 * The Initial Developer of the Original Code is
19 * Mozilla Corporation
20 * Portions created by the Initial Developer are Copyright (C) 2008
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Shawn Wilsher <me@shawnwilsher.com> (Original Author)
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 #include "nsString.h"
42 #include "sqlite3.h"
43 #include "mozStorageVariant.h"
44 #include "mozStorageRow.h"
46 ////////////////////////////////////////////////////////////////////////////////
47 //// mozStorageRow
49 /**
50 * Note: This object is only ever accessed on one thread at a time. It it not
51 * threadsafe, but it does need threadsafe AddRef and Release.
53 NS_IMPL_THREADSAFE_ISUPPORTS2(
54 mozStorageRow,
55 mozIStorageRow,
56 mozIStorageValueArray
59 nsresult
60 mozStorageRow::initialize(sqlite3_stmt *aStatement)
62 // Initialize the hash table
63 NS_ENSURE_TRUE(mNameHashtable.Init(), NS_ERROR_OUT_OF_MEMORY);
65 // Get the number of results
66 mNumCols = sqlite3_column_count(aStatement);
68 // Start copying over values
69 for (PRUint32 i = 0; i < mNumCols; i++) {
70 // Store the value
71 nsIVariant *variant = nsnull;
72 int type = sqlite3_column_type(aStatement, i);
73 switch (type) {
74 case SQLITE_INTEGER:
75 variant = new mozStorageInteger(sqlite3_column_int64(aStatement, i));
76 break;
77 case SQLITE_FLOAT:
78 variant = new mozStorageFloat(sqlite3_column_double(aStatement, i));
79 break;
80 case SQLITE_TEXT:
82 nsAutoString str(
83 static_cast<const PRUnichar *>(sqlite3_column_text16(aStatement, i))
85 variant = new mozStorageText(str);
86 break;
88 case SQLITE_NULL:
89 variant = new mozStorageNull();
90 break;
91 case SQLITE_BLOB:
93 int size = sqlite3_column_bytes(aStatement, i);
94 const void *data = sqlite3_column_blob(aStatement, i);
95 variant = new mozStorageBlob(std::pair<const void *, int>(data, size));
96 break;
98 default:
99 return NS_ERROR_UNEXPECTED;
101 NS_ENSURE_TRUE(variant, NS_ERROR_OUT_OF_MEMORY);
103 // Insert into our storage array
104 NS_ENSURE_TRUE(mData.InsertObjectAt(variant, i), NS_ERROR_OUT_OF_MEMORY);
106 // Associate the name (if any) with the index
107 const char *name = sqlite3_column_name(aStatement, i);
108 if (!name) break;
109 nsCAutoString colName(name);
110 mNameHashtable.Put(colName, i);
113 return NS_OK;
116 ////////////////////////////////////////////////////////////////////////////////
117 //// mozIStorageRow
119 NS_IMETHODIMP
120 mozStorageRow::GetResultByIndex(PRUint32 aIndex, nsIVariant **_result)
122 if (aIndex >= mNumCols)
123 return NS_ERROR_ILLEGAL_VALUE;
125 NS_ADDREF(*_result = mData.ObjectAt(aIndex));
126 return NS_OK;
129 NS_IMETHODIMP
130 mozStorageRow::GetResultByName(const nsACString &aName, nsIVariant **_result)
132 PRUint32 index;
133 NS_ENSURE_TRUE(mNameHashtable.Get(aName, &index), NS_ERROR_NOT_AVAILABLE);
134 return GetResultByIndex(index, _result);
137 ////////////////////////////////////////////////////////////////////////////////
138 //// mozIStorageValueArray
140 NS_IMETHODIMP
141 mozStorageRow::GetNumEntries(PRUint32 *_entries)
143 *_entries = mNumCols;
144 return NS_OK;
147 NS_IMETHODIMP
148 mozStorageRow::GetTypeOfIndex(PRUint32 aIndex, PRInt32 *_type)
150 if (aIndex >= mNumCols)
151 return NS_ERROR_ILLEGAL_VALUE;
153 PRUint16 type;
154 (void)mData.ObjectAt(aIndex)->GetDataType(&type);
155 switch (type) {
156 case nsIDataType::VTYPE_INT32:
157 case nsIDataType::VTYPE_INT64:
158 *_type = mozIStorageValueArray::VALUE_TYPE_INTEGER;
159 break;
160 case nsIDataType::VTYPE_DOUBLE:
161 *_type = mozIStorageValueArray::VALUE_TYPE_FLOAT;
162 break;
163 case nsIDataType::VTYPE_ASTRING:
164 *_type = mozIStorageValueArray::VALUE_TYPE_TEXT;
165 break;
166 case nsIDataType::VTYPE_ARRAY:
167 *_type = mozIStorageValueArray::VALUE_TYPE_BLOB;
168 break;
169 default:
170 *_type = mozIStorageValueArray::VALUE_TYPE_NULL;
171 break;
173 return NS_OK;
176 NS_IMETHODIMP
177 mozStorageRow::GetInt32(PRUint32 aIndex, PRInt32 *_value)
179 if (aIndex >= mNumCols)
180 return NS_ERROR_ILLEGAL_VALUE;
182 return mData.ObjectAt(aIndex)->GetAsInt32(_value);
185 NS_IMETHODIMP
186 mozStorageRow::GetInt64(PRUint32 aIndex, PRInt64 *_value)
188 if (aIndex >= mNumCols)
189 return NS_ERROR_ILLEGAL_VALUE;
191 return mData.ObjectAt(aIndex)->GetAsInt64(_value);
194 NS_IMETHODIMP
195 mozStorageRow::GetDouble(PRUint32 aIndex, double *_value)
197 if (aIndex >= mNumCols)
198 return NS_ERROR_ILLEGAL_VALUE;
200 return mData.ObjectAt(aIndex)->GetAsDouble(_value);
203 NS_IMETHODIMP
204 mozStorageRow::GetUTF8String(PRUint32 aIndex, nsACString &_value)
206 if (aIndex >= mNumCols)
207 return NS_ERROR_ILLEGAL_VALUE;
209 return mData.ObjectAt(aIndex)->GetAsAUTF8String(_value);
212 NS_IMETHODIMP
213 mozStorageRow::GetString(PRUint32 aIndex, nsAString &_value)
215 if (aIndex >= mNumCols)
216 return NS_ERROR_ILLEGAL_VALUE;
218 return mData.ObjectAt(aIndex)->GetAsAString(_value);
221 NS_IMETHODIMP
222 mozStorageRow::GetBlob(PRUint32 aIndex, PRUint32 *_size, PRUint8 **_blob)
224 if (aIndex >= mNumCols)
225 return NS_ERROR_ILLEGAL_VALUE;
227 PRUint16 type;
228 nsIID interface;
229 return mData.ObjectAt(aIndex)->GetAsArray(&type, &interface, _size,
230 reinterpret_cast<void **>(_blob));
233 NS_IMETHODIMP
234 mozStorageRow::GetIsNull(PRUint32 aIndex, PRBool *_isNull)
236 if (aIndex >= mNumCols)
237 return NS_ERROR_ILLEGAL_VALUE;
239 PRUint16 type;
240 (void)mData.ObjectAt(aIndex)->GetDataType(&type);
241 *_isNull = type == nsIDataType::VTYPE_VOID;
242 return NS_OK;
245 NS_IMETHODIMP
246 mozStorageRow::GetSharedUTF8String(PRUint32, PRUint32 *, char const **)
248 return NS_ERROR_NOT_IMPLEMENTED;
251 NS_IMETHODIMP
252 mozStorageRow::GetSharedString(PRUint32, PRUint32 *, const PRUnichar **)
254 return NS_ERROR_NOT_IMPLEMENTED;
257 NS_IMETHODIMP
258 mozStorageRow::GetSharedBlob(PRUint32, PRUint32 *, const PRUint8 **)
260 return NS_ERROR_NOT_IMPLEMENTED;