Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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 "nsError.h" |
michael@0 | 8 | #include "nsMemory.h" |
michael@0 | 9 | #include "nsString.h" |
michael@0 | 10 | |
michael@0 | 11 | #include "mozStoragePrivateHelpers.h" |
michael@0 | 12 | #include "mozStorageArgValueArray.h" |
michael@0 | 13 | |
michael@0 | 14 | namespace mozilla { |
michael@0 | 15 | namespace storage { |
michael@0 | 16 | |
michael@0 | 17 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 18 | //// ArgValueArray |
michael@0 | 19 | |
michael@0 | 20 | ArgValueArray::ArgValueArray(int32_t aArgc, |
michael@0 | 21 | sqlite3_value **aArgv) |
michael@0 | 22 | : mArgc(aArgc) |
michael@0 | 23 | , mArgv(aArgv) |
michael@0 | 24 | { |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | NS_IMPL_ISUPPORTS( |
michael@0 | 28 | ArgValueArray, |
michael@0 | 29 | mozIStorageValueArray |
michael@0 | 30 | ) |
michael@0 | 31 | |
michael@0 | 32 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 33 | //// mozIStorageValueArray |
michael@0 | 34 | |
michael@0 | 35 | NS_IMETHODIMP |
michael@0 | 36 | ArgValueArray::GetNumEntries(uint32_t *_size) |
michael@0 | 37 | { |
michael@0 | 38 | *_size = mArgc; |
michael@0 | 39 | return NS_OK; |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | NS_IMETHODIMP |
michael@0 | 43 | ArgValueArray::GetTypeOfIndex(uint32_t aIndex, |
michael@0 | 44 | int32_t *_type) |
michael@0 | 45 | { |
michael@0 | 46 | ENSURE_INDEX_VALUE(aIndex, mArgc); |
michael@0 | 47 | |
michael@0 | 48 | int t = ::sqlite3_value_type(mArgv[aIndex]); |
michael@0 | 49 | switch (t) { |
michael@0 | 50 | case SQLITE_INTEGER: |
michael@0 | 51 | *_type = VALUE_TYPE_INTEGER; |
michael@0 | 52 | break; |
michael@0 | 53 | case SQLITE_FLOAT: |
michael@0 | 54 | *_type = VALUE_TYPE_FLOAT; |
michael@0 | 55 | break; |
michael@0 | 56 | case SQLITE_TEXT: |
michael@0 | 57 | *_type = VALUE_TYPE_TEXT; |
michael@0 | 58 | break; |
michael@0 | 59 | case SQLITE_BLOB: |
michael@0 | 60 | *_type = VALUE_TYPE_BLOB; |
michael@0 | 61 | break; |
michael@0 | 62 | case SQLITE_NULL: |
michael@0 | 63 | *_type = VALUE_TYPE_NULL; |
michael@0 | 64 | break; |
michael@0 | 65 | default: |
michael@0 | 66 | return NS_ERROR_FAILURE; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | return NS_OK; |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | NS_IMETHODIMP |
michael@0 | 73 | ArgValueArray::GetInt32(uint32_t aIndex, |
michael@0 | 74 | int32_t *_value) |
michael@0 | 75 | { |
michael@0 | 76 | ENSURE_INDEX_VALUE(aIndex, mArgc); |
michael@0 | 77 | |
michael@0 | 78 | *_value = ::sqlite3_value_int(mArgv[aIndex]); |
michael@0 | 79 | return NS_OK; |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | NS_IMETHODIMP |
michael@0 | 83 | ArgValueArray::GetInt64(uint32_t aIndex, |
michael@0 | 84 | int64_t *_value) |
michael@0 | 85 | { |
michael@0 | 86 | ENSURE_INDEX_VALUE(aIndex, mArgc); |
michael@0 | 87 | |
michael@0 | 88 | *_value = ::sqlite3_value_int64(mArgv[aIndex]); |
michael@0 | 89 | return NS_OK; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | NS_IMETHODIMP |
michael@0 | 93 | ArgValueArray::GetDouble(uint32_t aIndex, |
michael@0 | 94 | double *_value) |
michael@0 | 95 | { |
michael@0 | 96 | ENSURE_INDEX_VALUE(aIndex, mArgc); |
michael@0 | 97 | |
michael@0 | 98 | *_value = ::sqlite3_value_double(mArgv[aIndex]); |
michael@0 | 99 | return NS_OK; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | NS_IMETHODIMP |
michael@0 | 103 | ArgValueArray::GetUTF8String(uint32_t aIndex, |
michael@0 | 104 | nsACString &_value) |
michael@0 | 105 | { |
michael@0 | 106 | ENSURE_INDEX_VALUE(aIndex, mArgc); |
michael@0 | 107 | |
michael@0 | 108 | if (::sqlite3_value_type(mArgv[aIndex]) == SQLITE_NULL) { |
michael@0 | 109 | // NULL columns should have IsVoid set to distinguish them from an empty |
michael@0 | 110 | // string. |
michael@0 | 111 | _value.Truncate(0); |
michael@0 | 112 | _value.SetIsVoid(true); |
michael@0 | 113 | } |
michael@0 | 114 | else { |
michael@0 | 115 | _value.Assign(reinterpret_cast<const char *>(::sqlite3_value_text(mArgv[aIndex])), |
michael@0 | 116 | ::sqlite3_value_bytes(mArgv[aIndex])); |
michael@0 | 117 | } |
michael@0 | 118 | return NS_OK; |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | NS_IMETHODIMP |
michael@0 | 122 | ArgValueArray::GetString(uint32_t aIndex, |
michael@0 | 123 | nsAString &_value) |
michael@0 | 124 | { |
michael@0 | 125 | ENSURE_INDEX_VALUE(aIndex, mArgc); |
michael@0 | 126 | |
michael@0 | 127 | if (::sqlite3_value_type(mArgv[aIndex]) == SQLITE_NULL) { |
michael@0 | 128 | // NULL columns should have IsVoid set to distinguish them from an empty |
michael@0 | 129 | // string. |
michael@0 | 130 | _value.Truncate(0); |
michael@0 | 131 | _value.SetIsVoid(true); |
michael@0 | 132 | } else { |
michael@0 | 133 | _value.Assign(static_cast<const char16_t *>(::sqlite3_value_text16(mArgv[aIndex])), |
michael@0 | 134 | ::sqlite3_value_bytes16(mArgv[aIndex]) / 2); |
michael@0 | 135 | } |
michael@0 | 136 | return NS_OK; |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | NS_IMETHODIMP |
michael@0 | 140 | ArgValueArray::GetBlob(uint32_t aIndex, |
michael@0 | 141 | uint32_t *_size, |
michael@0 | 142 | uint8_t **_blob) |
michael@0 | 143 | { |
michael@0 | 144 | ENSURE_INDEX_VALUE(aIndex, mArgc); |
michael@0 | 145 | |
michael@0 | 146 | int size = ::sqlite3_value_bytes(mArgv[aIndex]); |
michael@0 | 147 | void *blob = nsMemory::Clone(::sqlite3_value_blob(mArgv[aIndex]), size); |
michael@0 | 148 | NS_ENSURE_TRUE(blob, NS_ERROR_OUT_OF_MEMORY); |
michael@0 | 149 | |
michael@0 | 150 | *_blob = static_cast<uint8_t *>(blob); |
michael@0 | 151 | *_size = size; |
michael@0 | 152 | return NS_OK; |
michael@0 | 153 | } |
michael@0 | 154 | |
michael@0 | 155 | NS_IMETHODIMP |
michael@0 | 156 | ArgValueArray::GetIsNull(uint32_t aIndex, |
michael@0 | 157 | bool *_isNull) |
michael@0 | 158 | { |
michael@0 | 159 | // GetTypeOfIndex will check aIndex for us, so we don't have to. |
michael@0 | 160 | int32_t type; |
michael@0 | 161 | nsresult rv = GetTypeOfIndex(aIndex, &type); |
michael@0 | 162 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 163 | |
michael@0 | 164 | *_isNull = (type == VALUE_TYPE_NULL); |
michael@0 | 165 | return NS_OK; |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | NS_IMETHODIMP |
michael@0 | 169 | ArgValueArray::GetSharedUTF8String(uint32_t aIndex, |
michael@0 | 170 | uint32_t *_length, |
michael@0 | 171 | const char **_string) |
michael@0 | 172 | { |
michael@0 | 173 | if (_length) |
michael@0 | 174 | *_length = ::sqlite3_value_bytes(mArgv[aIndex]); |
michael@0 | 175 | |
michael@0 | 176 | *_string = reinterpret_cast<const char *>(::sqlite3_value_text(mArgv[aIndex])); |
michael@0 | 177 | return NS_OK; |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | NS_IMETHODIMP |
michael@0 | 181 | ArgValueArray::GetSharedString(uint32_t aIndex, |
michael@0 | 182 | uint32_t *_length, |
michael@0 | 183 | const char16_t **_string) |
michael@0 | 184 | { |
michael@0 | 185 | if (_length) |
michael@0 | 186 | *_length = ::sqlite3_value_bytes(mArgv[aIndex]); |
michael@0 | 187 | |
michael@0 | 188 | *_string = static_cast<const char16_t *>(::sqlite3_value_text16(mArgv[aIndex])); |
michael@0 | 189 | return NS_OK; |
michael@0 | 190 | } |
michael@0 | 191 | |
michael@0 | 192 | NS_IMETHODIMP |
michael@0 | 193 | ArgValueArray::GetSharedBlob(uint32_t aIndex, |
michael@0 | 194 | uint32_t *_size, |
michael@0 | 195 | const uint8_t **_blob) |
michael@0 | 196 | { |
michael@0 | 197 | *_size = ::sqlite3_value_bytes(mArgv[aIndex]); |
michael@0 | 198 | *_blob = static_cast<const uint8_t *>(::sqlite3_value_blob(mArgv[aIndex])); |
michael@0 | 199 | return NS_OK; |
michael@0 | 200 | } |
michael@0 | 201 | |
michael@0 | 202 | } // namespace storage |
michael@0 | 203 | } // namespace mozilla |