mozStorageStatementParams.cpp (4076B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : 3 * This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "mozStorageStatementParams.h" 8 9 #include "nsJSUtils.h" 10 #include "nsString.h" 11 12 #include "jsapi.h" 13 14 #include "mozilla/ErrorResult.h" 15 #include "mozilla/dom/MozStorageStatementParamsBinding.h" 16 #include "mozStoragePrivateHelpers.h" 17 #include "mozStorageStatement.h" 18 19 namespace mozilla { 20 namespace storage { 21 22 //////////////////////////////////////////////////////////////////////////////// 23 //// StatementParams 24 25 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(StatementParams, mWindow) 26 27 NS_INTERFACE_TABLE_HEAD(StatementParams) 28 NS_WRAPPERCACHE_INTERFACE_TABLE_ENTRY 29 NS_INTERFACE_TABLE(StatementParams, nsISupports) 30 NS_INTERFACE_TABLE_TO_MAP_SEGUE_CYCLE_COLLECTION(StatementParams) 31 NS_INTERFACE_MAP_END 32 33 NS_IMPL_CYCLE_COLLECTING_ADDREF(StatementParams) 34 NS_IMPL_CYCLE_COLLECTING_RELEASE(StatementParams) 35 36 StatementParams::StatementParams(nsPIDOMWindowInner* aWindow, 37 Statement* aStatement) 38 : mWindow(aWindow), mStatement(aStatement), mParamCount(0) { 39 NS_ASSERTION(mStatement != nullptr, "mStatement is null"); 40 (void)mStatement->GetParameterCount(&mParamCount); 41 } 42 43 JSObject* StatementParams::WrapObject(JSContext* aCx, 44 JS::Handle<JSObject*> aGivenProto) { 45 return dom::MozStorageStatementParams_Binding::Wrap(aCx, this, aGivenProto); 46 } 47 48 void StatementParams::NamedGetter(JSContext* aCx, const nsAString& aName, 49 bool& aFound, 50 JS::MutableHandle<JS::Value> aResult, 51 mozilla::ErrorResult& aRv) { 52 if (!mStatement) { 53 aRv.Throw(NS_ERROR_NOT_INITIALIZED); 54 return; 55 } 56 57 // Unfortunately there's no API that lets us return the parameter value. 58 aFound = false; 59 } 60 61 void StatementParams::NamedSetter(JSContext* aCx, const nsAString& aName, 62 JS::Handle<JS::Value> aValue, 63 mozilla::ErrorResult& aRv) { 64 if (!mStatement) { 65 aRv.Throw(NS_ERROR_NOT_INITIALIZED); 66 return; 67 } 68 69 NS_ConvertUTF16toUTF8 name(aName); 70 71 // Check to see if there's a parameter with this name. 72 nsCOMPtr<nsIVariant> variant(convertJSValToVariant(aCx, aValue)); 73 if (!variant) { 74 aRv.Throw(NS_ERROR_UNEXPECTED); 75 return; 76 } 77 78 aRv = mStatement->BindByName(name, variant); 79 } 80 81 void StatementParams::GetSupportedNames(nsTArray<nsString>& aNames) { 82 if (!mStatement) { 83 return; 84 } 85 86 for (uint32_t i = 0; i < mParamCount; i++) { 87 // Get the name of our parameter. 88 nsAutoCString name; 89 nsresult rv = mStatement->GetParameterName(i, name); 90 if (NS_WARN_IF(NS_FAILED(rv))) { 91 return; 92 } 93 94 // But drop the first character, which is going to be a ':'. 95 name = Substring(name, 1); 96 aNames.AppendElement(NS_ConvertUTF8toUTF16(name)); 97 } 98 } 99 100 void StatementParams::IndexedGetter(JSContext* aCx, uint32_t aIndex, 101 bool& aFound, 102 JS::MutableHandle<JS::Value> aResult, 103 mozilla::ErrorResult& aRv) { 104 if (!mStatement) { 105 aRv.Throw(NS_ERROR_NOT_INITIALIZED); 106 return; 107 } 108 109 // Unfortunately there's no API that lets us return the parameter value. 110 aFound = false; 111 } 112 113 void StatementParams::IndexedSetter(JSContext* aCx, uint32_t aIndex, 114 JS::Handle<JS::Value> aValue, 115 mozilla::ErrorResult& aRv) { 116 if (!mStatement) { 117 aRv.Throw(NS_ERROR_NOT_INITIALIZED); 118 return; 119 } 120 121 nsCOMPtr<nsIVariant> variant(convertJSValToVariant(aCx, aValue)); 122 if (!variant) { 123 aRv.Throw(NS_ERROR_UNEXPECTED); 124 return; 125 } 126 127 aRv = mStatement->BindByIndex(aIndex, variant); 128 } 129 130 } // namespace storage 131 } // namespace mozilla