2009-04-08 16:24:25 +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-09-12 21:30:37 +00:00
|
|
|
|
|
|
|
#include "nsMemory.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
|
|
|
|
#include "mozStorageStatementRow.h"
|
2009-04-08 16:24:25 +00:00
|
|
|
#include "mozStorageStatement.h"
|
2008-09-12 21:30:37 +00:00
|
|
|
|
|
|
|
#include "jsapi.h"
|
|
|
|
|
2009-04-08 16:24:25 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace storage {
|
2008-09-12 21:30:37 +00:00
|
|
|
|
2009-04-08 16:24:25 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// StatementRow
|
2008-09-12 21:30:37 +00:00
|
|
|
|
2009-05-09 00:29:56 +00:00
|
|
|
StatementRow::StatementRow(Statement *aStatement)
|
|
|
|
: mStatement(aStatement)
|
2008-09-12 21:30:37 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(
|
2009-04-08 16:24:25 +00:00
|
|
|
StatementRow,
|
|
|
|
mozIStorageStatementRow,
|
|
|
|
nsIXPCScriptable
|
|
|
|
)
|
2008-09-12 21:30:37 +00:00
|
|
|
|
2009-04-08 16:24:25 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// nsIXPCScriptable
|
2008-09-12 21:30:37 +00:00
|
|
|
|
2009-04-08 16:24:25 +00:00
|
|
|
#define XPC_MAP_CLASSNAME StatementRow
|
|
|
|
#define XPC_MAP_QUOTED_CLASSNAME "StatementRow"
|
|
|
|
#define XPC_MAP_WANT_GETPROPERTY
|
2014-11-08 00:07:12 +00:00
|
|
|
#define XPC_MAP_WANT_RESOLVE
|
2009-04-08 16:24:25 +00:00
|
|
|
#define XPC_MAP_FLAGS nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE
|
|
|
|
#include "xpc_map_end.h"
|
2008-09-12 21:30:37 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2009-04-08 16:24:25 +00:00
|
|
|
StatementRow::GetProperty(nsIXPConnectWrappedNative *aWrapper,
|
|
|
|
JSContext *aCtx,
|
|
|
|
JSObject *aScopeObj,
|
2010-07-15 06:19:36 +00:00
|
|
|
jsid aId,
|
2015-07-18 19:45:35 +00:00
|
|
|
JS::Value *_vp,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool *_retval)
|
2008-09-12 21:30:37 +00:00
|
|
|
{
|
2009-04-08 16:24:25 +00:00
|
|
|
NS_ENSURE_TRUE(mStatement, NS_ERROR_NOT_INITIALIZED);
|
2008-11-19 19:33:00 +00:00
|
|
|
|
2013-05-12 13:10:39 +00:00
|
|
|
JS::RootedObject scope(aCtx, aScopeObj);
|
2010-07-15 06:19:36 +00:00
|
|
|
if (JSID_IS_STRING(aId)) {
|
2010-11-11 20:40:29 +00:00
|
|
|
::JSAutoByteString idBytes(aCtx, JSID_TO_STRING(aId));
|
|
|
|
NS_ENSURE_TRUE(!!idBytes, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
nsDependentCString jsid(idBytes.ptr());
|
2008-09-12 21:30:41 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t idx;
|
2009-04-08 16:24:25 +00:00
|
|
|
nsresult rv = mStatement->GetColumnIndex(jsid, &idx);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t type;
|
2009-04-08 16:24:25 +00:00
|
|
|
rv = mStatement->GetTypeOfIndex(idx, &type);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2008-09-12 21:30:41 +00:00
|
|
|
|
2009-04-08 16:24:25 +00:00
|
|
|
if (type == mozIStorageValueArray::VALUE_TYPE_INTEGER ||
|
|
|
|
type == mozIStorageValueArray::VALUE_TYPE_FLOAT) {
|
|
|
|
double dval;
|
|
|
|
rv = mStatement->GetDouble(idx, &dval);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-08-02 00:59:47 +00:00
|
|
|
*_vp = ::JS_NumberValue(dval);
|
2009-04-08 16:24:25 +00:00
|
|
|
}
|
|
|
|
else if (type == mozIStorageValueArray::VALUE_TYPE_TEXT) {
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t bytes;
|
2014-07-22 04:43:21 +00:00
|
|
|
const char16_t *sval = reinterpret_cast<const char16_t *>(
|
2010-03-24 07:32:40 +00:00
|
|
|
static_cast<mozIStorageStatement *>(mStatement)->
|
|
|
|
AsSharedWString(idx, &bytes)
|
2009-04-08 16:24:25 +00:00
|
|
|
);
|
|
|
|
JSString *str = ::JS_NewUCStringCopyN(aCtx, sval, bytes / 2);
|
|
|
|
if (!str) {
|
2011-10-17 14:59:28 +00:00
|
|
|
*_retval = false;
|
2009-04-08 16:24:25 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-06-30 18:20:56 +00:00
|
|
|
_vp->setString(str);
|
2009-04-08 16:24:25 +00:00
|
|
|
}
|
|
|
|
else if (type == mozIStorageValueArray::VALUE_TYPE_BLOB) {
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t length;
|
|
|
|
const uint8_t *blob = static_cast<mozIStorageStatement *>(mStatement)->
|
2010-03-24 07:32:40 +00:00
|
|
|
AsSharedBlob(idx, &length);
|
2014-02-12 10:50:46 +00:00
|
|
|
JSObject *obj = ::JS_NewArrayObject(aCtx, length);
|
2009-04-08 16:24:25 +00:00
|
|
|
if (!obj) {
|
2011-10-17 14:59:28 +00:00
|
|
|
*_retval = false;
|
2009-04-08 16:24:25 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-07-01 04:09:46 +00:00
|
|
|
_vp->setObject(*obj);
|
2009-04-08 16:24:25 +00:00
|
|
|
|
|
|
|
// Copy the blob over to the JS array.
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < length; i++) {
|
2015-03-02 13:31:15 +00:00
|
|
|
if (!::JS_DefineElement(aCtx, scope, i, blob[i], JSPROP_ENUMERATE)) {
|
2011-10-17 14:59:28 +00:00
|
|
|
*_retval = false;
|
2009-04-08 16:24:25 +00:00
|
|
|
return NS_OK;
|
2008-09-12 21:30:37 +00:00
|
|
|
}
|
2009-04-08 16:24:25 +00:00
|
|
|
}
|
2008-09-12 21:30:37 +00:00
|
|
|
}
|
2009-04-08 16:24:25 +00:00
|
|
|
else if (type == mozIStorageValueArray::VALUE_TYPE_NULL) {
|
2015-06-30 01:36:44 +00:00
|
|
|
_vp->setNull();
|
2008-09-12 21:30:37 +00:00
|
|
|
}
|
2009-04-08 16:24:25 +00:00
|
|
|
else {
|
|
|
|
NS_ERROR("unknown column type returned, what's going on?");
|
|
|
|
}
|
|
|
|
}
|
2008-09-12 21:30:41 +00:00
|
|
|
|
2009-04-08 16:24:25 +00:00
|
|
|
return NS_OK;
|
2008-09-12 21:30:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-11-08 00:07:12 +00:00
|
|
|
StatementRow::Resolve(nsIXPConnectWrappedNative *aWrapper,
|
|
|
|
JSContext *aCtx,
|
|
|
|
JSObject *aScopeObj,
|
|
|
|
jsid aId,
|
|
|
|
bool *aResolvedp,
|
|
|
|
bool *_retval)
|
2008-09-12 21:30:37 +00:00
|
|
|
{
|
2013-12-20 23:58:36 +00:00
|
|
|
JS::Rooted<JSObject*> scopeObj(aCtx, aScopeObj);
|
|
|
|
|
2009-04-08 16:24:25 +00:00
|
|
|
NS_ENSURE_TRUE(mStatement, NS_ERROR_NOT_INITIALIZED);
|
2009-06-11 20:18:58 +00:00
|
|
|
// We do not throw at any point after this because we want to allow the
|
|
|
|
// prototype chain to be checked for the property.
|
2008-09-12 21:30:37 +00:00
|
|
|
|
2010-07-15 06:19:36 +00:00
|
|
|
if (JSID_IS_STRING(aId)) {
|
2010-11-11 20:40:29 +00:00
|
|
|
::JSAutoByteString idBytes(aCtx, JSID_TO_STRING(aId));
|
|
|
|
NS_ENSURE_TRUE(!!idBytes, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
nsDependentCString name(idBytes.ptr());
|
2008-09-12 21:30:37 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t idx;
|
2009-04-08 16:24:25 +00:00
|
|
|
nsresult rv = mStatement->GetColumnIndex(name, &idx);
|
2009-06-11 20:18:58 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// It's highly likely that the name doesn't exist, so let the JS engine
|
|
|
|
// check the prototype chain and throw if that doesn't have the property
|
|
|
|
// either.
|
2014-11-08 00:07:12 +00:00
|
|
|
*aResolvedp = false;
|
2009-06-11 20:18:58 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2008-09-12 21:30:37 +00:00
|
|
|
|
2014-04-30 09:10:33 +00:00
|
|
|
JS::Rooted<jsid> id(aCtx, aId);
|
2015-05-01 19:03:17 +00:00
|
|
|
*_retval = ::JS_DefinePropertyById(aCtx, scopeObj, id, JS::UndefinedHandleValue,
|
|
|
|
JSPROP_RESOLVING);
|
2014-11-08 00:07:12 +00:00
|
|
|
*aResolvedp = true;
|
2009-06-11 20:18:58 +00:00
|
|
|
return NS_OK;
|
2009-04-08 16:24:25 +00:00
|
|
|
}
|
2008-09-12 21:30:37 +00:00
|
|
|
|
2009-04-08 16:24:25 +00:00
|
|
|
return NS_OK;
|
2008-09-12 21:30:37 +00:00
|
|
|
}
|
|
|
|
|
2009-04-08 16:24:25 +00:00
|
|
|
} // namespace storage
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace mozilla
|