2009-05-09 04:29:57 +04: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 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/. */
|
2009-05-09 04:29:57 +04:00
|
|
|
|
|
|
|
// Note: we are already in the namepace mozilla::storage
|
|
|
|
|
2010-02-23 00:57:45 +03:00
|
|
|
// Note 2: whoever #includes this file must provide implementations of
|
|
|
|
// sqlite3_T_* prior.
|
2009-05-09 04:29:57 +04:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// variantToSQLiteT Implementation
|
|
|
|
|
|
|
|
template <typename T>
|
2019-05-01 11:47:10 +03:00
|
|
|
int variantToSQLiteT(T aObj, nsIVariant* aValue) {
|
2013-07-31 19:44:58 +04:00
|
|
|
// Allow to return nullptr not wrapped to nsIVariant for speed.
|
2009-05-09 04:29:57 +04:00
|
|
|
if (!aValue) return sqlite3_T_null(aObj);
|
|
|
|
|
2018-11-20 04:20:05 +03:00
|
|
|
uint16_t valueType = aValue->GetDataType();
|
2016-06-08 08:08:41 +03:00
|
|
|
switch (valueType) {
|
2009-05-09 04:29:57 +04:00
|
|
|
case nsIDataType::VTYPE_INT8:
|
|
|
|
case nsIDataType::VTYPE_INT16:
|
|
|
|
case nsIDataType::VTYPE_INT32:
|
|
|
|
case nsIDataType::VTYPE_UINT8:
|
|
|
|
case nsIDataType::VTYPE_UINT16: {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t value;
|
2009-05-09 04:29:57 +04: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 19:56:38 +04:00
|
|
|
int64_t value;
|
2009-05-09 04:29:57 +04: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 10:19:26 +04:00
|
|
|
bool value;
|
2009-05-09 04:29:57 +04: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: {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString value;
|
2010-02-23 00:57:45 +03: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_WCHAR_STR:
|
|
|
|
case nsIDataType::VTYPE_WSTRING_SIZE_IS:
|
2009-05-09 04:29:57 +04: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 04:29:57 +04:00
|
|
|
case nsIDataType::VTYPE_EMPTY_ARRAY:
|
2009-05-09 04:29:57 +04:00
|
|
|
return sqlite3_T_null(aObj);
|
2009-05-09 04:29:57 +04:00
|
|
|
case nsIDataType::VTYPE_ARRAY: {
|
2016-06-08 08:08:41 +03:00
|
|
|
uint16_t arrayType;
|
2009-05-09 04:29:57 +04:00
|
|
|
nsIID iid;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t count;
|
2019-05-01 11:47:10 +03:00
|
|
|
void* data;
|
2016-06-08 08:08:41 +03:00
|
|
|
nsresult rv = aValue->GetAsArray(&arrayType, &iid, &count, &data);
|
2009-05-09 04:29:57 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, SQLITE_MISMATCH);
|
|
|
|
|
|
|
|
// Check to make sure it's a supported type.
|
2016-06-08 08:08:41 +03:00
|
|
|
NS_ASSERTION(arrayType == nsIDataType::VTYPE_UINT8,
|
2009-05-09 04:29:57 +04:00
|
|
|
"Invalid type passed! You may leak!");
|
2016-06-08 08:08:41 +03:00
|
|
|
if (arrayType != nsIDataType::VTYPE_UINT8) {
|
2009-05-09 04:29:57 +04:00
|
|
|
// Technically this could leak with certain data types, but somebody was
|
|
|
|
// being stupid passing us this anyway.
|
2015-04-01 08:29:55 +03:00
|
|
|
free(data);
|
2009-05-09 04:29:57 +04: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 04:29:57 +04: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;
|
|
|
|
}
|