Bug 628949 - Update visible region / glass regions after we paint. r=roc a=2.0.
[mozilla-central.git] / storage / src / mozStorageRow.cpp
blobff0135d05dd8a888cac6ddb1fea4707533236b47
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
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 "mozStoragePrivateHelpers.h"
44 #include "Variant.h"
45 #include "mozStorageRow.h"
47 namespace mozilla {
48 namespace storage {
50 ////////////////////////////////////////////////////////////////////////////////
51 //// Row
53 nsresult
54 Row::initialize(sqlite3_stmt *aStatement)
56 // Initialize the hash table
57 NS_ENSURE_TRUE(mNameHashtable.Init(), NS_ERROR_OUT_OF_MEMORY);
59 // Get the number of results
60 mNumCols = ::sqlite3_column_count(aStatement);
62 // Start copying over values
63 for (PRUint32 i = 0; i < mNumCols; i++) {
64 // Store the value
65 nsIVariant *variant = nsnull;
66 int type = ::sqlite3_column_type(aStatement, i);
67 switch (type) {
68 case SQLITE_INTEGER:
69 variant = new IntegerVariant(::sqlite3_column_int64(aStatement, i));
70 break;
71 case SQLITE_FLOAT:
72 variant = new FloatVariant(::sqlite3_column_double(aStatement, i));
73 break;
74 case SQLITE_TEXT:
76 nsDependentString str(
77 static_cast<const PRUnichar *>(::sqlite3_column_text16(aStatement, i))
79 variant = new TextVariant(str);
80 break;
82 case SQLITE_NULL:
83 variant = new NullVariant();
84 break;
85 case SQLITE_BLOB:
87 int size = ::sqlite3_column_bytes(aStatement, i);
88 const void *data = ::sqlite3_column_blob(aStatement, i);
89 variant = new BlobVariant(std::pair<const void *, int>(data, size));
90 break;
92 default:
93 return NS_ERROR_UNEXPECTED;
95 NS_ENSURE_TRUE(variant, NS_ERROR_OUT_OF_MEMORY);
97 // Insert into our storage array
98 NS_ENSURE_TRUE(mData.InsertObjectAt(variant, i), NS_ERROR_OUT_OF_MEMORY);
100 // Associate the name (if any) with the index
101 const char *name = ::sqlite3_column_name(aStatement, i);
102 if (!name) break;
103 nsCAutoString colName(name);
104 mNameHashtable.Put(colName, i);
107 return NS_OK;
111 * Note: This object is only ever accessed on one thread at a time. It it not
112 * threadsafe, but it does need threadsafe AddRef and Release.
114 NS_IMPL_THREADSAFE_ISUPPORTS2(
115 Row,
116 mozIStorageRow,
117 mozIStorageValueArray
120 ////////////////////////////////////////////////////////////////////////////////
121 //// mozIStorageRow
123 NS_IMETHODIMP
124 Row::GetResultByIndex(PRUint32 aIndex,
125 nsIVariant **_result)
127 ENSURE_INDEX_VALUE(aIndex, mNumCols);
128 NS_ADDREF(*_result = mData.ObjectAt(aIndex));
129 return NS_OK;
132 NS_IMETHODIMP
133 Row::GetResultByName(const nsACString &aName,
134 nsIVariant **_result)
136 PRUint32 index;
137 NS_ENSURE_TRUE(mNameHashtable.Get(aName, &index), NS_ERROR_NOT_AVAILABLE);
138 return GetResultByIndex(index, _result);
141 ////////////////////////////////////////////////////////////////////////////////
142 //// mozIStorageValueArray
144 NS_IMETHODIMP
145 Row::GetNumEntries(PRUint32 *_entries)
147 *_entries = mNumCols;
148 return NS_OK;
151 NS_IMETHODIMP
152 Row::GetTypeOfIndex(PRUint32 aIndex,
153 PRInt32 *_type)
155 ENSURE_INDEX_VALUE(aIndex, mNumCols);
157 PRUint16 type;
158 (void)mData.ObjectAt(aIndex)->GetDataType(&type);
159 switch (type) {
160 case nsIDataType::VTYPE_INT32:
161 case nsIDataType::VTYPE_INT64:
162 *_type = mozIStorageValueArray::VALUE_TYPE_INTEGER;
163 break;
164 case nsIDataType::VTYPE_DOUBLE:
165 *_type = mozIStorageValueArray::VALUE_TYPE_FLOAT;
166 break;
167 case nsIDataType::VTYPE_ASTRING:
168 *_type = mozIStorageValueArray::VALUE_TYPE_TEXT;
169 break;
170 case nsIDataType::VTYPE_ARRAY:
171 *_type = mozIStorageValueArray::VALUE_TYPE_BLOB;
172 break;
173 default:
174 *_type = mozIStorageValueArray::VALUE_TYPE_NULL;
175 break;
177 return NS_OK;
180 NS_IMETHODIMP
181 Row::GetInt32(PRUint32 aIndex,
182 PRInt32 *_value)
184 ENSURE_INDEX_VALUE(aIndex, mNumCols);
185 return mData.ObjectAt(aIndex)->GetAsInt32(_value);
188 NS_IMETHODIMP
189 Row::GetInt64(PRUint32 aIndex,
190 PRInt64 *_value)
192 ENSURE_INDEX_VALUE(aIndex, mNumCols);
193 return mData.ObjectAt(aIndex)->GetAsInt64(_value);
196 NS_IMETHODIMP
197 Row::GetDouble(PRUint32 aIndex,
198 double *_value)
200 ENSURE_INDEX_VALUE(aIndex, mNumCols);
201 return mData.ObjectAt(aIndex)->GetAsDouble(_value);
204 NS_IMETHODIMP
205 Row::GetUTF8String(PRUint32 aIndex,
206 nsACString &_value)
208 ENSURE_INDEX_VALUE(aIndex, mNumCols);
209 return mData.ObjectAt(aIndex)->GetAsAUTF8String(_value);
212 NS_IMETHODIMP
213 Row::GetString(PRUint32 aIndex,
214 nsAString &_value)
216 ENSURE_INDEX_VALUE(aIndex, mNumCols);
217 return mData.ObjectAt(aIndex)->GetAsAString(_value);
220 NS_IMETHODIMP
221 Row::GetBlob(PRUint32 aIndex,
222 PRUint32 *_size,
223 PRUint8 **_blob)
225 ENSURE_INDEX_VALUE(aIndex, mNumCols);
227 PRUint16 type;
228 nsIID interfaceIID;
229 return mData.ObjectAt(aIndex)->GetAsArray(&type, &interfaceIID, _size,
230 reinterpret_cast<void **>(_blob));
233 NS_IMETHODIMP
234 Row::GetIsNull(PRUint32 aIndex,
235 PRBool *_isNull)
237 ENSURE_INDEX_VALUE(aIndex, mNumCols);
238 NS_ENSURE_ARG_POINTER(_isNull);
240 PRUint16 type;
241 (void)mData.ObjectAt(aIndex)->GetDataType(&type);
242 *_isNull = type == nsIDataType::VTYPE_EMPTY;
243 return NS_OK;
246 NS_IMETHODIMP
247 Row::GetSharedUTF8String(PRUint32,
248 PRUint32 *,
249 char const **)
251 return NS_ERROR_NOT_IMPLEMENTED;
254 NS_IMETHODIMP
255 Row::GetSharedString(PRUint32,
256 PRUint32 *,
257 const PRUnichar **)
259 return NS_ERROR_NOT_IMPLEMENTED;
262 NS_IMETHODIMP
263 Row::GetSharedBlob(PRUint32,
264 PRUint32 *,
265 const PRUint8 **)
267 return NS_ERROR_NOT_IMPLEMENTED;
270 } // namespace storage
271 } // namespace mozilla