2009-05-09 00:29:57 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* 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/. */
|
2009-05-09 00:29:57 +00:00
|
|
|
|
|
|
|
// Note: we are already in the namepace mozilla::storage
|
|
|
|
|
2010-02-22 21:57:45 +00:00
|
|
|
// Note 2: whoever #includes this file must provide implementations of
|
|
|
|
// sqlite3_T_* prior.
|
2009-05-09 00:29:57 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// variantToSQLiteT Implementation
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
int
|
|
|
|
variantToSQLiteT(T aObj,
|
|
|
|
nsIVariant *aValue)
|
|
|
|
{
|
2013-07-31 15:44:58 +00:00
|
|
|
// Allow to return nullptr not wrapped to nsIVariant for speed.
|
2009-05-09 00:29:57 +00:00
|
|
|
if (!aValue)
|
|
|
|
return sqlite3_T_null(aObj);
|
|
|
|
|
2016-06-08 05:08:41 +00:00
|
|
|
uint16_t valueType;
|
|
|
|
aValue->GetDataType(&valueType);
|
|
|
|
switch (valueType) {
|
2009-05-09 00:29:57 +00:00
|
|
|
case nsIDataType::VTYPE_INT8:
|
|
|
|
case nsIDataType::VTYPE_INT16:
|
|
|
|
case nsIDataType::VTYPE_INT32:
|
|
|
|
case nsIDataType::VTYPE_UINT8:
|
|
|
|
case nsIDataType::VTYPE_UINT16:
|
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t value;
|
2009-05-09 00:29:57 +00:00
|
|
|
nsresult rv = aValue->GetAsInt32(&value);
|
|
|
|
NS_ENSURE_SUCCESS(rv, SQLITE_MISMATCH);
|
|
|
|
return sqlite3_T_int(aObj, value);
|
|
|
|
}
|
|
|
|
case nsIDataType::VTYPE_UINT32: // Try to preserve full range
|
|
|
|
case nsIDataType::VTYPE_INT64:
|
|
|
|
// Data loss possible, but there is no unsigned types in SQLite
|
|
|
|
case nsIDataType::VTYPE_UINT64:
|
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t value;
|
2009-05-09 00:29:57 +00:00
|
|
|
nsresult rv = aValue->GetAsInt64(&value);
|
|
|
|
NS_ENSURE_SUCCESS(rv, SQLITE_MISMATCH);
|
|
|
|
return sqlite3_T_int64(aObj, value);
|
|
|
|
}
|
|
|
|
case nsIDataType::VTYPE_FLOAT:
|
|
|
|
case nsIDataType::VTYPE_DOUBLE:
|
|
|
|
{
|
|
|
|
double value;
|
|
|
|
nsresult rv = aValue->GetAsDouble(&value);
|
|
|
|
NS_ENSURE_SUCCESS(rv, SQLITE_MISMATCH);
|
|
|
|
return sqlite3_T_double(aObj, value);
|
|
|
|
}
|
|
|
|
case nsIDataType::VTYPE_BOOL:
|
|
|
|
{
|
2011-09-29 06:19:26 +00:00
|
|
|
bool value;
|
2009-05-09 00:29:57 +00:00
|
|
|
nsresult rv = aValue->GetAsBool(&value);
|
|
|
|
NS_ENSURE_SUCCESS(rv, SQLITE_MISMATCH);
|
|
|
|
return sqlite3_T_int(aObj, value ? 1 : 0);
|
|
|
|
}
|
|
|
|
case nsIDataType::VTYPE_CHAR:
|
|
|
|
case nsIDataType::VTYPE_CHAR_STR:
|
|
|
|
case nsIDataType::VTYPE_STRING_SIZE_IS:
|
|
|
|
case nsIDataType::VTYPE_UTF8STRING:
|
|
|
|
case nsIDataType::VTYPE_CSTRING:
|
2010-02-22 21:57:45 +00:00
|
|
|
{
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString value;
|
2010-02-22 21:57:45 +00:00
|
|
|
// GetAsAUTF8String should never perform conversion when coming from
|
|
|
|
// 8-bit string types, and thus can accept strings with arbitrary encoding
|
|
|
|
// (including UTF8 and ASCII).
|
|
|
|
nsresult rv = aValue->GetAsAUTF8String(value);
|
|
|
|
NS_ENSURE_SUCCESS(rv, SQLITE_MISMATCH);
|
|
|
|
return sqlite3_T_text(aObj, value);
|
|
|
|
}
|
|
|
|
case nsIDataType::VTYPE_WCHAR:
|
|
|
|
case nsIDataType::VTYPE_DOMSTRING:
|
|
|
|
case nsIDataType::VTYPE_WCHAR_STR:
|
|
|
|
case nsIDataType::VTYPE_WSTRING_SIZE_IS:
|
2009-05-09 00:29:57 +00:00
|
|
|
case nsIDataType::VTYPE_ASTRING:
|
|
|
|
{
|
|
|
|
nsAutoString value;
|
|
|
|
// GetAsAString does proper conversion to UCS2 from all string-like types.
|
|
|
|
// It can be used universally without problems (unless someone implements
|
|
|
|
// their own variant, but that's their problem).
|
|
|
|
nsresult rv = aValue->GetAsAString(value);
|
|
|
|
NS_ENSURE_SUCCESS(rv, SQLITE_MISMATCH);
|
|
|
|
return sqlite3_T_text16(aObj, value);
|
|
|
|
}
|
|
|
|
case nsIDataType::VTYPE_VOID:
|
|
|
|
case nsIDataType::VTYPE_EMPTY:
|
2009-05-09 00:29:57 +00:00
|
|
|
case nsIDataType::VTYPE_EMPTY_ARRAY:
|
2009-05-09 00:29:57 +00:00
|
|
|
return sqlite3_T_null(aObj);
|
2009-05-09 00:29:57 +00:00
|
|
|
case nsIDataType::VTYPE_ARRAY:
|
|
|
|
{
|
2016-06-08 05:08:41 +00:00
|
|
|
uint16_t arrayType;
|
2009-05-09 00:29:57 +00:00
|
|
|
nsIID iid;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t count;
|
2009-05-09 00:29:57 +00:00
|
|
|
void *data;
|
2016-06-08 05:08:41 +00:00
|
|
|
nsresult rv = aValue->GetAsArray(&arrayType, &iid, &count, &data);
|
2009-05-09 00:29:57 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, SQLITE_MISMATCH);
|
|
|
|
|
|
|
|
// Check to make sure it's a supported type.
|
2016-06-08 05:08:41 +00:00
|
|
|
NS_ASSERTION(arrayType == nsIDataType::VTYPE_UINT8,
|
2009-05-09 00:29:57 +00:00
|
|
|
"Invalid type passed! You may leak!");
|
2016-06-08 05:08:41 +00:00
|
|
|
if (arrayType != nsIDataType::VTYPE_UINT8) {
|
2009-05-09 00:29:57 +00:00
|
|
|
// Technically this could leak with certain data types, but somebody was
|
|
|
|
// being stupid passing us this anyway.
|
2015-04-01 05:29:55 +00:00
|
|
|
free(data);
|
2009-05-09 00:29:57 +00:00
|
|
|
return SQLITE_MISMATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally do our thing. The function should free the array accordingly!
|
|
|
|
int rc = sqlite3_T_blob(aObj, data, count);
|
|
|
|
return rc;
|
|
|
|
}
|
2009-05-09 00:29:57 +00:00
|
|
|
// Maybe, it'll be possible to convert these
|
|
|
|
// in future too.
|
|
|
|
case nsIDataType::VTYPE_ID:
|
|
|
|
case nsIDataType::VTYPE_INTERFACE:
|
|
|
|
case nsIDataType::VTYPE_INTERFACE_IS:
|
|
|
|
default:
|
|
|
|
return SQLITE_MISMATCH;
|
|
|
|
}
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|