2008-07-11 19:47:28 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2009-04-13 16:29:41 +00:00
|
|
|
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
|
2012-05-21 11:12:37 +00: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 19:47:28 +00:00
|
|
|
|
|
|
|
#include "nsString.h"
|
|
|
|
|
|
|
|
#include "sqlite3.h"
|
2009-05-09 00:29:56 +00:00
|
|
|
#include "mozStoragePrivateHelpers.h"
|
2009-05-12 21:08:49 +00:00
|
|
|
#include "Variant.h"
|
2008-07-11 19:47:28 +00:00
|
|
|
#include "mozStorageRow.h"
|
|
|
|
|
2009-05-09 00:29:56 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace storage {
|
2009-04-10 17:20:12 +00:00
|
|
|
|
2008-07-11 19:47:28 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2009-05-09 00:29:56 +00:00
|
|
|
//// Row
|
2008-07-11 19:47:28 +00:00
|
|
|
|
|
|
|
nsresult
|
2009-05-09 00:29:56 +00:00
|
|
|
Row::initialize(sqlite3_stmt *aStatement)
|
2008-07-11 19:47:28 +00:00
|
|
|
{
|
|
|
|
// Get the number of results
|
2009-05-09 00:29:56 +00:00
|
|
|
mNumCols = ::sqlite3_column_count(aStatement);
|
2008-07-11 19:47:28 +00:00
|
|
|
|
|
|
|
// Start copying over values
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < mNumCols; i++) {
|
2008-07-11 19:47:28 +00:00
|
|
|
// Store the value
|
2012-07-30 14:20:58 +00:00
|
|
|
nsIVariant *variant = nullptr;
|
2009-05-09 00:29:56 +00:00
|
|
|
int type = ::sqlite3_column_type(aStatement, i);
|
2008-07-11 19:47:28 +00:00
|
|
|
switch (type) {
|
|
|
|
case SQLITE_INTEGER:
|
2009-05-09 00:29:56 +00:00
|
|
|
variant = new IntegerVariant(::sqlite3_column_int64(aStatement, i));
|
2008-07-11 19:47:28 +00:00
|
|
|
break;
|
|
|
|
case SQLITE_FLOAT:
|
2009-05-09 00:29:56 +00:00
|
|
|
variant = new FloatVariant(::sqlite3_column_double(aStatement, i));
|
2008-07-11 19:47:28 +00:00
|
|
|
break;
|
|
|
|
case SQLITE_TEXT:
|
|
|
|
{
|
2009-03-20 20:28:16 +00:00
|
|
|
nsDependentString str(
|
2014-01-04 15:02:17 +00:00
|
|
|
static_cast<const char16_t *>(::sqlite3_column_text16(aStatement, i))
|
2008-07-11 19:47:28 +00:00
|
|
|
);
|
2009-04-10 17:20:12 +00:00
|
|
|
variant = new TextVariant(str);
|
2008-07-11 19:47:28 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SQLITE_NULL:
|
2009-04-10 17:20:12 +00:00
|
|
|
variant = new NullVariant();
|
2008-07-11 19:47:28 +00:00
|
|
|
break;
|
|
|
|
case SQLITE_BLOB:
|
|
|
|
{
|
2009-05-09 00:29:56 +00:00
|
|
|
int size = ::sqlite3_column_bytes(aStatement, i);
|
|
|
|
const void *data = ::sqlite3_column_blob(aStatement, i);
|
2009-04-10 17:20:12 +00:00
|
|
|
variant = new BlobVariant(std::pair<const void *, int>(data, size));
|
2008-07-11 19:47:28 +00: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 00:29:56 +00:00
|
|
|
const char *name = ::sqlite3_column_name(aStatement, i);
|
2008-07-11 19:47:28 +00:00
|
|
|
if (!name) break;
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString colName(name);
|
2008-07-11 19:47:28 +00:00
|
|
|
mNameHashtable.Put(colName, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-05-09 00:29:56 +00: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.
|
|
|
|
*/
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(
|
2009-05-09 00:29:56 +00:00
|
|
|
Row,
|
|
|
|
mozIStorageRow,
|
|
|
|
mozIStorageValueArray
|
|
|
|
)
|
|
|
|
|
2008-07-11 19:47:28 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// mozIStorageRow
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
Row::GetResultByIndex(uint32_t aIndex,
|
2009-05-09 00:29:56 +00:00
|
|
|
nsIVariant **_result)
|
2008-07-11 19:47:28 +00:00
|
|
|
{
|
2009-05-09 00:29:56 +00:00
|
|
|
ENSURE_INDEX_VALUE(aIndex, mNumCols);
|
2008-07-11 19:47:28 +00:00
|
|
|
NS_ADDREF(*_result = mData.ObjectAt(aIndex));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2009-05-09 00:29:56 +00:00
|
|
|
Row::GetResultByName(const nsACString &aName,
|
|
|
|
nsIVariant **_result)
|
2008-07-11 19:47:28 +00:00
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t index;
|
2008-07-11 19:47:28 +00:00
|
|
|
NS_ENSURE_TRUE(mNameHashtable.Get(aName, &index), NS_ERROR_NOT_AVAILABLE);
|
|
|
|
return GetResultByIndex(index, _result);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// mozIStorageValueArray
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
Row::GetNumEntries(uint32_t *_entries)
|
2008-07-11 19:47:28 +00:00
|
|
|
{
|
|
|
|
*_entries = mNumCols;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
Row::GetTypeOfIndex(uint32_t aIndex,
|
|
|
|
int32_t *_type)
|
2008-07-11 19:47:28 +00:00
|
|
|
{
|
2009-05-09 00:29:56 +00:00
|
|
|
ENSURE_INDEX_VALUE(aIndex, mNumCols);
|
2008-07-11 19:47:28 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint16_t type;
|
2008-07-11 19:47:28 +00:00
|
|
|
(void)mData.ObjectAt(aIndex)->GetDataType(&type);
|
|
|
|
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 15:56:38 +00:00
|
|
|
Row::GetInt32(uint32_t aIndex,
|
|
|
|
int32_t *_value)
|
2008-07-11 19:47:28 +00:00
|
|
|
{
|
2009-05-09 00:29:56 +00:00
|
|
|
ENSURE_INDEX_VALUE(aIndex, mNumCols);
|
2008-07-11 19:47:28 +00:00
|
|
|
return mData.ObjectAt(aIndex)->GetAsInt32(_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
Row::GetInt64(uint32_t aIndex,
|
|
|
|
int64_t *_value)
|
2008-07-11 19:47:28 +00:00
|
|
|
{
|
2009-05-09 00:29:56 +00:00
|
|
|
ENSURE_INDEX_VALUE(aIndex, mNumCols);
|
2008-07-11 19:47:28 +00:00
|
|
|
return mData.ObjectAt(aIndex)->GetAsInt64(_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
Row::GetDouble(uint32_t aIndex,
|
2009-05-09 00:29:56 +00:00
|
|
|
double *_value)
|
2008-07-11 19:47:28 +00:00
|
|
|
{
|
2009-05-09 00:29:56 +00:00
|
|
|
ENSURE_INDEX_VALUE(aIndex, mNumCols);
|
2008-07-11 19:47:28 +00:00
|
|
|
return mData.ObjectAt(aIndex)->GetAsDouble(_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
Row::GetUTF8String(uint32_t aIndex,
|
2009-05-09 00:29:56 +00:00
|
|
|
nsACString &_value)
|
2008-07-11 19:47:28 +00:00
|
|
|
{
|
2009-05-09 00:29:56 +00:00
|
|
|
ENSURE_INDEX_VALUE(aIndex, mNumCols);
|
2008-07-11 19:47:28 +00:00
|
|
|
return mData.ObjectAt(aIndex)->GetAsAUTF8String(_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
Row::GetString(uint32_t aIndex,
|
2009-05-09 00:29:56 +00:00
|
|
|
nsAString &_value)
|
2008-07-11 19:47:28 +00:00
|
|
|
{
|
2009-05-09 00:29:56 +00:00
|
|
|
ENSURE_INDEX_VALUE(aIndex, mNumCols);
|
2008-07-11 19:47:28 +00:00
|
|
|
return mData.ObjectAt(aIndex)->GetAsAString(_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
Row::GetBlob(uint32_t aIndex,
|
|
|
|
uint32_t *_size,
|
|
|
|
uint8_t **_blob)
|
2008-07-11 19:47:28 +00:00
|
|
|
{
|
2009-05-09 00:29:56 +00:00
|
|
|
ENSURE_INDEX_VALUE(aIndex, mNumCols);
|
2008-07-11 19:47:28 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint16_t type;
|
2008-09-23 18:03:48 +00:00
|
|
|
nsIID interfaceIID;
|
|
|
|
return mData.ObjectAt(aIndex)->GetAsArray(&type, &interfaceIID, _size,
|
2008-07-11 19:47:28 +00:00
|
|
|
reinterpret_cast<void **>(_blob));
|
|
|
|
}
|
|
|
|
|
2015-04-29 16:03:15 +00: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 19:47:28 +00:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
Row::GetIsNull(uint32_t aIndex,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool *_isNull)
|
2008-07-11 19:47:28 +00:00
|
|
|
{
|
2009-05-09 00:29:56 +00:00
|
|
|
ENSURE_INDEX_VALUE(aIndex, mNumCols);
|
2009-06-24 22:04:49 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(_isNull);
|
2008-07-11 19:47:28 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint16_t type;
|
2008-07-11 19:47:28 +00:00
|
|
|
(void)mData.ObjectAt(aIndex)->GetDataType(&type);
|
2009-06-24 22:04:49 +00:00
|
|
|
*_isNull = type == nsIDataType::VTYPE_EMPTY;
|
2008-07-11 19:47:28 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
Row::GetSharedUTF8String(uint32_t,
|
|
|
|
uint32_t *,
|
2009-05-09 00:29:56 +00:00
|
|
|
char const **)
|
2008-07-11 19:47:28 +00:00
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
Row::GetSharedString(uint32_t,
|
|
|
|
uint32_t *,
|
2014-01-04 15:02:17 +00:00
|
|
|
const char16_t **)
|
2008-07-11 19:47:28 +00:00
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
Row::GetSharedBlob(uint32_t,
|
|
|
|
uint32_t *,
|
|
|
|
const uint8_t **)
|
2008-07-11 19:47:28 +00:00
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
2009-05-09 00:29:56 +00:00
|
|
|
|
|
|
|
} // namespace storage
|
|
|
|
} // namespace mozilla
|