Wed, 31 Dec 2014 13:27:57 +0100
Ignore runtime configuration files generated during quality assurance.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "nsString.h" |
michael@0 | 8 | |
michael@0 | 9 | #include "sqlite3.h" |
michael@0 | 10 | #include "mozStoragePrivateHelpers.h" |
michael@0 | 11 | #include "Variant.h" |
michael@0 | 12 | #include "mozStorageRow.h" |
michael@0 | 13 | |
michael@0 | 14 | namespace mozilla { |
michael@0 | 15 | namespace storage { |
michael@0 | 16 | |
michael@0 | 17 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 18 | //// Row |
michael@0 | 19 | |
michael@0 | 20 | nsresult |
michael@0 | 21 | Row::initialize(sqlite3_stmt *aStatement) |
michael@0 | 22 | { |
michael@0 | 23 | // Get the number of results |
michael@0 | 24 | mNumCols = ::sqlite3_column_count(aStatement); |
michael@0 | 25 | |
michael@0 | 26 | // Start copying over values |
michael@0 | 27 | for (uint32_t i = 0; i < mNumCols; i++) { |
michael@0 | 28 | // Store the value |
michael@0 | 29 | nsIVariant *variant = nullptr; |
michael@0 | 30 | int type = ::sqlite3_column_type(aStatement, i); |
michael@0 | 31 | switch (type) { |
michael@0 | 32 | case SQLITE_INTEGER: |
michael@0 | 33 | variant = new IntegerVariant(::sqlite3_column_int64(aStatement, i)); |
michael@0 | 34 | break; |
michael@0 | 35 | case SQLITE_FLOAT: |
michael@0 | 36 | variant = new FloatVariant(::sqlite3_column_double(aStatement, i)); |
michael@0 | 37 | break; |
michael@0 | 38 | case SQLITE_TEXT: |
michael@0 | 39 | { |
michael@0 | 40 | nsDependentString str( |
michael@0 | 41 | static_cast<const char16_t *>(::sqlite3_column_text16(aStatement, i)) |
michael@0 | 42 | ); |
michael@0 | 43 | variant = new TextVariant(str); |
michael@0 | 44 | break; |
michael@0 | 45 | } |
michael@0 | 46 | case SQLITE_NULL: |
michael@0 | 47 | variant = new NullVariant(); |
michael@0 | 48 | break; |
michael@0 | 49 | case SQLITE_BLOB: |
michael@0 | 50 | { |
michael@0 | 51 | int size = ::sqlite3_column_bytes(aStatement, i); |
michael@0 | 52 | const void *data = ::sqlite3_column_blob(aStatement, i); |
michael@0 | 53 | variant = new BlobVariant(std::pair<const void *, int>(data, size)); |
michael@0 | 54 | break; |
michael@0 | 55 | } |
michael@0 | 56 | default: |
michael@0 | 57 | return NS_ERROR_UNEXPECTED; |
michael@0 | 58 | } |
michael@0 | 59 | NS_ENSURE_TRUE(variant, NS_ERROR_OUT_OF_MEMORY); |
michael@0 | 60 | |
michael@0 | 61 | // Insert into our storage array |
michael@0 | 62 | NS_ENSURE_TRUE(mData.InsertObjectAt(variant, i), NS_ERROR_OUT_OF_MEMORY); |
michael@0 | 63 | |
michael@0 | 64 | // Associate the name (if any) with the index |
michael@0 | 65 | const char *name = ::sqlite3_column_name(aStatement, i); |
michael@0 | 66 | if (!name) break; |
michael@0 | 67 | nsAutoCString colName(name); |
michael@0 | 68 | mNameHashtable.Put(colName, i); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | return NS_OK; |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | /** |
michael@0 | 75 | * Note: This object is only ever accessed on one thread at a time. It it not |
michael@0 | 76 | * threadsafe, but it does need threadsafe AddRef and Release. |
michael@0 | 77 | */ |
michael@0 | 78 | NS_IMPL_ISUPPORTS( |
michael@0 | 79 | Row, |
michael@0 | 80 | mozIStorageRow, |
michael@0 | 81 | mozIStorageValueArray |
michael@0 | 82 | ) |
michael@0 | 83 | |
michael@0 | 84 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 85 | //// mozIStorageRow |
michael@0 | 86 | |
michael@0 | 87 | NS_IMETHODIMP |
michael@0 | 88 | Row::GetResultByIndex(uint32_t aIndex, |
michael@0 | 89 | nsIVariant **_result) |
michael@0 | 90 | { |
michael@0 | 91 | ENSURE_INDEX_VALUE(aIndex, mNumCols); |
michael@0 | 92 | NS_ADDREF(*_result = mData.ObjectAt(aIndex)); |
michael@0 | 93 | return NS_OK; |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | NS_IMETHODIMP |
michael@0 | 97 | Row::GetResultByName(const nsACString &aName, |
michael@0 | 98 | nsIVariant **_result) |
michael@0 | 99 | { |
michael@0 | 100 | uint32_t index; |
michael@0 | 101 | NS_ENSURE_TRUE(mNameHashtable.Get(aName, &index), NS_ERROR_NOT_AVAILABLE); |
michael@0 | 102 | return GetResultByIndex(index, _result); |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 106 | //// mozIStorageValueArray |
michael@0 | 107 | |
michael@0 | 108 | NS_IMETHODIMP |
michael@0 | 109 | Row::GetNumEntries(uint32_t *_entries) |
michael@0 | 110 | { |
michael@0 | 111 | *_entries = mNumCols; |
michael@0 | 112 | return NS_OK; |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | NS_IMETHODIMP |
michael@0 | 116 | Row::GetTypeOfIndex(uint32_t aIndex, |
michael@0 | 117 | int32_t *_type) |
michael@0 | 118 | { |
michael@0 | 119 | ENSURE_INDEX_VALUE(aIndex, mNumCols); |
michael@0 | 120 | |
michael@0 | 121 | uint16_t type; |
michael@0 | 122 | (void)mData.ObjectAt(aIndex)->GetDataType(&type); |
michael@0 | 123 | switch (type) { |
michael@0 | 124 | case nsIDataType::VTYPE_INT32: |
michael@0 | 125 | case nsIDataType::VTYPE_INT64: |
michael@0 | 126 | *_type = mozIStorageValueArray::VALUE_TYPE_INTEGER; |
michael@0 | 127 | break; |
michael@0 | 128 | case nsIDataType::VTYPE_DOUBLE: |
michael@0 | 129 | *_type = mozIStorageValueArray::VALUE_TYPE_FLOAT; |
michael@0 | 130 | break; |
michael@0 | 131 | case nsIDataType::VTYPE_ASTRING: |
michael@0 | 132 | *_type = mozIStorageValueArray::VALUE_TYPE_TEXT; |
michael@0 | 133 | break; |
michael@0 | 134 | case nsIDataType::VTYPE_ARRAY: |
michael@0 | 135 | *_type = mozIStorageValueArray::VALUE_TYPE_BLOB; |
michael@0 | 136 | break; |
michael@0 | 137 | default: |
michael@0 | 138 | *_type = mozIStorageValueArray::VALUE_TYPE_NULL; |
michael@0 | 139 | break; |
michael@0 | 140 | } |
michael@0 | 141 | return NS_OK; |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | NS_IMETHODIMP |
michael@0 | 145 | Row::GetInt32(uint32_t aIndex, |
michael@0 | 146 | int32_t *_value) |
michael@0 | 147 | { |
michael@0 | 148 | ENSURE_INDEX_VALUE(aIndex, mNumCols); |
michael@0 | 149 | return mData.ObjectAt(aIndex)->GetAsInt32(_value); |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | NS_IMETHODIMP |
michael@0 | 153 | Row::GetInt64(uint32_t aIndex, |
michael@0 | 154 | int64_t *_value) |
michael@0 | 155 | { |
michael@0 | 156 | ENSURE_INDEX_VALUE(aIndex, mNumCols); |
michael@0 | 157 | return mData.ObjectAt(aIndex)->GetAsInt64(_value); |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | NS_IMETHODIMP |
michael@0 | 161 | Row::GetDouble(uint32_t aIndex, |
michael@0 | 162 | double *_value) |
michael@0 | 163 | { |
michael@0 | 164 | ENSURE_INDEX_VALUE(aIndex, mNumCols); |
michael@0 | 165 | return mData.ObjectAt(aIndex)->GetAsDouble(_value); |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | NS_IMETHODIMP |
michael@0 | 169 | Row::GetUTF8String(uint32_t aIndex, |
michael@0 | 170 | nsACString &_value) |
michael@0 | 171 | { |
michael@0 | 172 | ENSURE_INDEX_VALUE(aIndex, mNumCols); |
michael@0 | 173 | return mData.ObjectAt(aIndex)->GetAsAUTF8String(_value); |
michael@0 | 174 | } |
michael@0 | 175 | |
michael@0 | 176 | NS_IMETHODIMP |
michael@0 | 177 | Row::GetString(uint32_t aIndex, |
michael@0 | 178 | nsAString &_value) |
michael@0 | 179 | { |
michael@0 | 180 | ENSURE_INDEX_VALUE(aIndex, mNumCols); |
michael@0 | 181 | return mData.ObjectAt(aIndex)->GetAsAString(_value); |
michael@0 | 182 | } |
michael@0 | 183 | |
michael@0 | 184 | NS_IMETHODIMP |
michael@0 | 185 | Row::GetBlob(uint32_t aIndex, |
michael@0 | 186 | uint32_t *_size, |
michael@0 | 187 | uint8_t **_blob) |
michael@0 | 188 | { |
michael@0 | 189 | ENSURE_INDEX_VALUE(aIndex, mNumCols); |
michael@0 | 190 | |
michael@0 | 191 | uint16_t type; |
michael@0 | 192 | nsIID interfaceIID; |
michael@0 | 193 | return mData.ObjectAt(aIndex)->GetAsArray(&type, &interfaceIID, _size, |
michael@0 | 194 | reinterpret_cast<void **>(_blob)); |
michael@0 | 195 | } |
michael@0 | 196 | |
michael@0 | 197 | NS_IMETHODIMP |
michael@0 | 198 | Row::GetIsNull(uint32_t aIndex, |
michael@0 | 199 | bool *_isNull) |
michael@0 | 200 | { |
michael@0 | 201 | ENSURE_INDEX_VALUE(aIndex, mNumCols); |
michael@0 | 202 | NS_ENSURE_ARG_POINTER(_isNull); |
michael@0 | 203 | |
michael@0 | 204 | uint16_t type; |
michael@0 | 205 | (void)mData.ObjectAt(aIndex)->GetDataType(&type); |
michael@0 | 206 | *_isNull = type == nsIDataType::VTYPE_EMPTY; |
michael@0 | 207 | return NS_OK; |
michael@0 | 208 | } |
michael@0 | 209 | |
michael@0 | 210 | NS_IMETHODIMP |
michael@0 | 211 | Row::GetSharedUTF8String(uint32_t, |
michael@0 | 212 | uint32_t *, |
michael@0 | 213 | char const **) |
michael@0 | 214 | { |
michael@0 | 215 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 216 | } |
michael@0 | 217 | |
michael@0 | 218 | NS_IMETHODIMP |
michael@0 | 219 | Row::GetSharedString(uint32_t, |
michael@0 | 220 | uint32_t *, |
michael@0 | 221 | const char16_t **) |
michael@0 | 222 | { |
michael@0 | 223 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 224 | } |
michael@0 | 225 | |
michael@0 | 226 | NS_IMETHODIMP |
michael@0 | 227 | Row::GetSharedBlob(uint32_t, |
michael@0 | 228 | uint32_t *, |
michael@0 | 229 | const uint8_t **) |
michael@0 | 230 | { |
michael@0 | 231 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 232 | } |
michael@0 | 233 | |
michael@0 | 234 | } // namespace storage |
michael@0 | 235 | } // namespace mozilla |