Bug 628922 - layout should use cached nsIAccessibilityService, r=davidb, sr=roc,...
[mozilla-central.git] / storage / src / mozStorageStatementWrapper.cpp
blob680ba7140e5fb55780f70bccdd8f06f4257afa6a
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 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is Oracle Corporation code.
18 * The Initial Developer of the Original Code is
19 * Oracle Corporation
20 * Portions created by the Initial Developer are Copyright (C) 2004
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Vladimir Vukicevic <vladimir.vukicevic@oracle.com>
25 * Shawn Wilsher <me@shawnwilsher.com>
27 * Alternatively, the contents of this file may be used under the terms of
28 * either the GNU General Public License Version 2 or later (the "GPL"), or
29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
41 #include "nsString.h"
43 #include "mozStoragePrivateHelpers.h"
44 #include "mozStorageStatementWrapper.h"
45 #include "mozStorageStatementParams.h"
46 #include "mozStorageStatementRow.h"
48 #include "sqlite3.h"
50 namespace mozilla {
51 namespace storage {
53 ////////////////////////////////////////////////////////////////////////////////
54 //// StatementWrapper
56 StatementWrapper::StatementWrapper()
57 : mStatement(nsnull)
61 StatementWrapper::~StatementWrapper()
63 mStatement = nsnull;
66 NS_IMPL_ISUPPORTS2(
67 StatementWrapper,
68 mozIStorageStatementWrapper,
69 nsIXPCScriptable
72 ////////////////////////////////////////////////////////////////////////////////
73 //// mozIStorageStatementWrapper
75 NS_IMETHODIMP
76 StatementWrapper::Initialize(mozIStorageStatement *aStatement)
78 NS_ASSERTION(mStatement == nsnull, "StatementWrapper is already initialized");
79 NS_ENSURE_ARG_POINTER(aStatement);
81 mStatement = static_cast<Statement *>(aStatement);
83 // fetch various things we care about
84 (void)mStatement->GetParameterCount(&mParamCount);
85 (void)mStatement->GetColumnCount(&mResultColumnCount);
87 for (unsigned int i = 0; i < mResultColumnCount; i++) {
88 const void *name = ::sqlite3_column_name16(nativeStatement(), i);
89 (void)mColumnNames.AppendElement(nsDependentString(static_cast<const PRUnichar*>(name)));
92 return NS_OK;
95 NS_IMETHODIMP
96 StatementWrapper::GetStatement(mozIStorageStatement **_statement)
98 NS_IF_ADDREF(*_statement = mStatement);
99 return NS_OK;
102 NS_IMETHODIMP
103 StatementWrapper::Reset()
105 if (!mStatement)
106 return NS_ERROR_FAILURE;
108 return mStatement->Reset();
111 NS_IMETHODIMP
112 StatementWrapper::Step(PRBool *_hasMoreResults)
114 if (!mStatement)
115 return NS_ERROR_FAILURE;
117 PRBool hasMore = PR_FALSE;
118 nsresult rv = mStatement->ExecuteStep(&hasMore);
119 if (NS_SUCCEEDED(rv) && !hasMore) {
120 *_hasMoreResults = PR_FALSE;
121 (void)mStatement->Reset();
122 return NS_OK;
125 *_hasMoreResults = hasMore;
126 return rv;
129 NS_IMETHODIMP
130 StatementWrapper::Execute()
132 if (!mStatement)
133 return NS_ERROR_FAILURE;
135 return mStatement->Execute();
138 NS_IMETHODIMP
139 StatementWrapper::GetRow(mozIStorageStatementRow **_row)
141 NS_ENSURE_ARG_POINTER(_row);
143 if (!mStatement)
144 return NS_ERROR_FAILURE;
146 PRInt32 state;
147 mStatement->GetState(&state);
148 if (state != mozIStorageStatement::MOZ_STORAGE_STATEMENT_EXECUTING)
149 return NS_ERROR_FAILURE;
151 if (!mStatementRow) {
152 mStatementRow = new StatementRow(mStatement);
153 NS_ENSURE_TRUE(mStatementRow, NS_ERROR_OUT_OF_MEMORY);
156 NS_ADDREF(*_row = mStatementRow);
157 return NS_OK;
160 NS_IMETHODIMP
161 StatementWrapper::GetParams(mozIStorageStatementParams **_params)
163 NS_ENSURE_ARG_POINTER(_params);
165 if (!mStatementParams) {
166 mStatementParams = new StatementParams(mStatement);
167 NS_ENSURE_TRUE(mStatementParams, NS_ERROR_OUT_OF_MEMORY);
170 NS_ADDREF(*_params = mStatementParams);
171 return NS_OK;
174 ////////////////////////////////////////////////////////////////////////////////
175 //// nsIXPCScriptable
177 #define XPC_MAP_CLASSNAME StatementWrapper
178 #define XPC_MAP_QUOTED_CLASSNAME "StatementWrapper"
179 #define XPC_MAP_WANT_CALL
180 #define XPC_MAP_FLAGS nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE | \
181 nsIXPCScriptable::USE_JSSTUB_FOR_SETPROPERTY
182 #include "xpc_map_end.h"
184 NS_IMETHODIMP
185 StatementWrapper::Call(nsIXPConnectWrappedNative *aWrapper,
186 JSContext *aCtx,
187 JSObject *aScopeObj,
188 PRUint32 aArgc,
189 jsval *aArgv,
190 jsval *_vp,
191 PRBool *_retval)
193 if (!mStatement)
194 return NS_ERROR_FAILURE;
196 if (aArgc != mParamCount) {
197 *_retval = PR_FALSE;
198 return NS_ERROR_FAILURE;
201 // reset
202 (void)mStatement->Reset();
204 // bind parameters
205 for (int i = 0; i < (int)aArgc; i++) {
206 nsCOMPtr<nsIVariant> variant(convertJSValToVariant(aCtx, aArgv[i]));
207 if (!variant ||
208 NS_FAILED(mStatement->BindByIndex(i, variant))) {
209 *_retval = PR_FALSE;
210 return NS_ERROR_INVALID_ARG;
214 // if there are no results, we just execute
215 if (mResultColumnCount == 0)
216 (void)mStatement->Execute();
218 *_vp = JSVAL_TRUE;
219 *_retval = PR_TRUE;
220 return NS_OK;
223 } // namespace storage
224 } // namespace mozilla