2008-07-11 23:47:28 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2009-04-13 20:29:41 +04:00
|
|
|
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2008-07-11 23:47:28 +04:00
|
|
|
|
|
|
|
#include "nsString.h"
|
|
|
|
|
|
|
|
#include "sqlite3.h"
|
2009-05-09 04:29:56 +04:00
|
|
|
#include "mozStoragePrivateHelpers.h"
|
2009-05-13 01:08:49 +04:00
|
|
|
#include "Variant.h"
|
2008-07-11 23:47:28 +04:00
|
|
|
#include "mozStorageRow.h"
|
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace storage {
|
2009-04-10 21:20:12 +04:00
|
|
|
|
2008-07-11 23:47:28 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2009-05-09 04:29:56 +04:00
|
|
|
//// Row
|
2008-07-11 23:47:28 +04:00
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
nsresult Row::initialize(sqlite3_stmt *aStatement) {
|
2008-07-11 23:47:28 +04:00
|
|
|
// Get the number of results
|
2009-05-09 04:29:56 +04:00
|
|
|
mNumCols = ::sqlite3_column_count(aStatement);
|
2008-07-11 23:47:28 +04:00
|
|
|
|
|
|
|
// Start copying over values
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < mNumCols; i++) {
|
2008-07-11 23:47:28 +04:00
|
|
|
// Store the value
|
2012-07-30 18:20:58 +04:00
|
|
|
nsIVariant *variant = nullptr;
|
2009-05-09 04:29:56 +04:00
|
|
|
int type = ::sqlite3_column_type(aStatement, i);
|
2008-07-11 23:47:28 +04:00
|
|
|
switch (type) {
|
|
|
|
case SQLITE_INTEGER:
|
2009-05-09 04:29:56 +04:00
|
|
|
variant = new IntegerVariant(::sqlite3_column_int64(aStatement, i));
|
2008-07-11 23:47:28 +04:00
|
|
|
break;
|
|
|
|
case SQLITE_FLOAT:
|
2009-05-09 04:29:56 +04:00
|
|
|
variant = new FloatVariant(::sqlite3_column_double(aStatement, i));
|
2008-07-11 23:47:28 +04:00
|
|
|
break;
|
|
|
|
case SQLITE_TEXT: {
|
Bug 1543295 - Pass the text length when creating an `nsString` from a SQLite text result. r=mak
This commit updates mozStorage to always:
* Pass the length, using sqlite3_{column, value}_bytes16, when
creating an nsDependentString from a pointer.
* Call sqlite3_{column, value}_bytes{16} after
sqlite3_{column, value}_{text, blob, text16}, per the
recommendation in https://www.sqlite.org/c3ref/column_blob.html.
Some callers did this before, or in unclear order, since C++ doesn't
specify one for evaluating function arguments.
* Pass the byte length to sqlite3_result_text16.
Differential Revision: https://phabricator.services.mozilla.com/D26848
--HG--
extra : moz-landing-system : lando
2019-04-11 06:19:39 +03:00
|
|
|
const char16_t *value = static_cast<const char16_t *>(
|
|
|
|
::sqlite3_column_text16(aStatement, i));
|
|
|
|
nsDependentString str(
|
|
|
|
value, ::sqlite3_column_bytes16(aStatement, i) / sizeof(char16_t));
|
2009-04-10 21:20:12 +04:00
|
|
|
variant = new TextVariant(str);
|
2008-07-11 23:47:28 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SQLITE_NULL:
|
2009-04-10 21:20:12 +04:00
|
|
|
variant = new NullVariant();
|
2008-07-11 23:47:28 +04:00
|
|
|
break;
|
|
|
|
case SQLITE_BLOB: {
|
2009-05-09 04:29:56 +04:00
|
|
|
const void *data = ::sqlite3_column_blob(aStatement, i);
|
Bug 1543295 - Pass the text length when creating an `nsString` from a SQLite text result. r=mak
This commit updates mozStorage to always:
* Pass the length, using sqlite3_{column, value}_bytes16, when
creating an nsDependentString from a pointer.
* Call sqlite3_{column, value}_bytes{16} after
sqlite3_{column, value}_{text, blob, text16}, per the
recommendation in https://www.sqlite.org/c3ref/column_blob.html.
Some callers did this before, or in unclear order, since C++ doesn't
specify one for evaluating function arguments.
* Pass the byte length to sqlite3_result_text16.
Differential Revision: https://phabricator.services.mozilla.com/D26848
--HG--
extra : moz-landing-system : lando
2019-04-11 06:19:39 +03:00
|
|
|
int size = ::sqlite3_column_bytes(aStatement, i);
|
2009-04-10 21:20:12 +04:00
|
|
|
variant = new BlobVariant(std::pair<const void *, int>(data, size));
|
2008-07-11 23:47:28 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
NS_ENSURE_TRUE(variant, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
// Insert into our storage array
|
|
|
|
NS_ENSURE_TRUE(mData.InsertObjectAt(variant, i), NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
// Associate the name (if any) with the index
|
2009-05-09 04:29:56 +04:00
|
|
|
const char *name = ::sqlite3_column_name(aStatement, i);
|
2008-07-11 23:47:28 +04:00
|
|
|
if (!name) break;
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString colName(name);
|
2008-07-11 23:47:28 +04:00
|
|
|
mNameHashtable.Put(colName, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-05-09 04:29:56 +04:00
|
|
|
/**
|
|
|
|
* Note: This object is only ever accessed on one thread at a time. It it not
|
|
|
|
* threadsafe, but it does need threadsafe AddRef and Release.
|
|
|
|
*/
|
|
|
|
NS_IMPL_ISUPPORTS(Row, mozIStorageRow, mozIStorageValueArray)
|
|
|
|
|
2008-07-11 23:47:28 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// mozIStorageRow
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
Row::GetResultByIndex(uint32_t aIndex, nsIVariant **_result) {
|
2009-05-09 04:29:56 +04:00
|
|
|
ENSURE_INDEX_VALUE(aIndex, mNumCols);
|
2008-07-11 23:47:28 +04:00
|
|
|
NS_ADDREF(*_result = mData.ObjectAt(aIndex));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2009-05-09 04:29:56 +04:00
|
|
|
Row::GetResultByName(const nsACString &aName, nsIVariant **_result) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t index;
|
2008-07-11 23:47:28 +04:00
|
|
|
NS_ENSURE_TRUE(mNameHashtable.Get(aName, &index), NS_ERROR_NOT_AVAILABLE);
|
|
|
|
return GetResultByIndex(index, _result);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// mozIStorageValueArray
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
Row::GetNumEntries(uint32_t *_entries) {
|
2008-07-11 23:47:28 +04:00
|
|
|
*_entries = mNumCols;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
Row::GetTypeOfIndex(uint32_t aIndex, int32_t *_type) {
|
2009-05-09 04:29:56 +04:00
|
|
|
ENSURE_INDEX_VALUE(aIndex, mNumCols);
|
2008-07-11 23:47:28 +04:00
|
|
|
|
2018-11-20 04:20:05 +03:00
|
|
|
uint16_t type = mData.ObjectAt(aIndex)->GetDataType();
|
2008-07-11 23:47:28 +04:00
|
|
|
switch (type) {
|
|
|
|
case nsIDataType::VTYPE_INT32:
|
|
|
|
case nsIDataType::VTYPE_INT64:
|
|
|
|
*_type = mozIStorageValueArray::VALUE_TYPE_INTEGER;
|
|
|
|
break;
|
|
|
|
case nsIDataType::VTYPE_DOUBLE:
|
|
|
|
*_type = mozIStorageValueArray::VALUE_TYPE_FLOAT;
|
|
|
|
break;
|
|
|
|
case nsIDataType::VTYPE_ASTRING:
|
|
|
|
*_type = mozIStorageValueArray::VALUE_TYPE_TEXT;
|
|
|
|
break;
|
|
|
|
case nsIDataType::VTYPE_ARRAY:
|
|
|
|
*_type = mozIStorageValueArray::VALUE_TYPE_BLOB;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*_type = mozIStorageValueArray::VALUE_TYPE_NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
Row::GetInt32(uint32_t aIndex, int32_t *_value) {
|
2009-05-09 04:29:56 +04:00
|
|
|
ENSURE_INDEX_VALUE(aIndex, mNumCols);
|
2008-07-11 23:47:28 +04:00
|
|
|
return mData.ObjectAt(aIndex)->GetAsInt32(_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
Row::GetInt64(uint32_t aIndex, int64_t *_value) {
|
2009-05-09 04:29:56 +04:00
|
|
|
ENSURE_INDEX_VALUE(aIndex, mNumCols);
|
2008-07-11 23:47:28 +04:00
|
|
|
return mData.ObjectAt(aIndex)->GetAsInt64(_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
Row::GetDouble(uint32_t aIndex, double *_value) {
|
2009-05-09 04:29:56 +04:00
|
|
|
ENSURE_INDEX_VALUE(aIndex, mNumCols);
|
2008-07-11 23:47:28 +04:00
|
|
|
return mData.ObjectAt(aIndex)->GetAsDouble(_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
Row::GetUTF8String(uint32_t aIndex, nsACString &_value) {
|
2009-05-09 04:29:56 +04:00
|
|
|
ENSURE_INDEX_VALUE(aIndex, mNumCols);
|
2008-07-11 23:47:28 +04:00
|
|
|
return mData.ObjectAt(aIndex)->GetAsAUTF8String(_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
Row::GetString(uint32_t aIndex, nsAString &_value) {
|
2009-05-09 04:29:56 +04:00
|
|
|
ENSURE_INDEX_VALUE(aIndex, mNumCols);
|
2008-07-11 23:47:28 +04:00
|
|
|
return mData.ObjectAt(aIndex)->GetAsAString(_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
Row::GetBlob(uint32_t aIndex, uint32_t *_size, uint8_t **_blob) {
|
2009-05-09 04:29:56 +04:00
|
|
|
ENSURE_INDEX_VALUE(aIndex, mNumCols);
|
2008-07-11 23:47:28 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint16_t type;
|
2008-09-23 22:03:48 +04:00
|
|
|
nsIID interfaceIID;
|
|
|
|
return mData.ObjectAt(aIndex)->GetAsArray(&type, &interfaceIID, _size,
|
2008-07-11 23:47:28 +04:00
|
|
|
reinterpret_cast<void **>(_blob));
|
|
|
|
}
|
|
|
|
|
2015-04-29 19:03:15 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
Row::GetBlobAsString(uint32_t aIndex, nsAString &aValue) {
|
|
|
|
return DoGetBlobAsString(this, aIndex, aValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Row::GetBlobAsUTF8String(uint32_t aIndex, nsACString &aValue) {
|
|
|
|
return DoGetBlobAsString(this, aIndex, aValue);
|
|
|
|
}
|
|
|
|
|
2008-07-11 23:47:28 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
Row::GetIsNull(uint32_t aIndex, bool *_isNull) {
|
2009-05-09 04:29:56 +04:00
|
|
|
ENSURE_INDEX_VALUE(aIndex, mNumCols);
|
2009-06-25 02:04:49 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(_isNull);
|
2008-07-11 23:47:28 +04:00
|
|
|
|
2018-11-20 04:20:05 +03:00
|
|
|
uint16_t type = mData.ObjectAt(aIndex)->GetDataType();
|
2009-06-25 02:04:49 +04:00
|
|
|
*_isNull = type == nsIDataType::VTYPE_EMPTY;
|
2008-07-11 23:47:28 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
Row::GetSharedUTF8String(uint32_t, uint32_t *, char const **) {
|
2008-07-11 23:47:28 +04:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
Row::GetSharedString(uint32_t, uint32_t *, const char16_t **) {
|
2008-07-11 23:47:28 +04:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
Row::GetSharedBlob(uint32_t, uint32_t *, const uint8_t **) {
|
2008-07-11 23:47:28 +04:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
2009-05-09 04:29:56 +04:00
|
|
|
|
|
|
|
} // namespace storage
|
|
|
|
} // namespace mozilla
|