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