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
5 : *
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/
10 : *
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.
15 : *
16 : * The Original Code is Oracle Corporation code.
17 : *
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.
22 : *
23 : * Contributor(s):
24 : * Vladimir Vukicevic <vladimir.vukicevic@oracle.com>
25 : * Shawn Wilsher <me@shawnwilsher.com>
26 : *
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.
38 : *
39 : * ***** END LICENSE BLOCK ***** */
40 :
41 : #include "nsString.h"
42 :
43 : #include "mozStoragePrivateHelpers.h"
44 : #include "mozStorageStatementWrapper.h"
45 : #include "mozStorageStatementParams.h"
46 : #include "mozStorageStatementRow.h"
47 :
48 : #include "sqlite3.h"
49 :
50 : namespace mozilla {
51 : namespace storage {
52 :
53 : ////////////////////////////////////////////////////////////////////////////////
54 : //// StatementWrapper
55 :
56 67 : StatementWrapper::StatementWrapper()
57 67 : : mStatement(nsnull)
58 : {
59 67 : }
60 :
61 134 : StatementWrapper::~StatementWrapper()
62 : {
63 67 : mStatement = nsnull;
64 67 : }
65 :
66 1876 : NS_IMPL_ISUPPORTS2(
67 : StatementWrapper,
68 : mozIStorageStatementWrapper,
69 : nsIXPCScriptable
70 : )
71 :
72 : ////////////////////////////////////////////////////////////////////////////////
73 : //// mozIStorageStatementWrapper
74 :
75 : NS_IMETHODIMP
76 67 : StatementWrapper::Initialize(mozIStorageStatement *aStatement)
77 : {
78 67 : NS_ASSERTION(mStatement == nsnull, "StatementWrapper is already initialized");
79 67 : NS_ENSURE_ARG_POINTER(aStatement);
80 :
81 67 : mStatement = static_cast<Statement *>(aStatement);
82 : PRInt32 state;
83 67 : (void)mStatement->GetState(&state);
84 67 : NS_ENSURE_TRUE(state != mozIStorageStatement::MOZ_STORAGE_STATEMENT_INVALID, NS_ERROR_FAILURE);
85 :
86 : // fetch various things we care about
87 67 : (void)mStatement->GetParameterCount(&mParamCount);
88 67 : (void)mStatement->GetColumnCount(&mResultColumnCount);
89 :
90 113 : for (unsigned int i = 0; i < mResultColumnCount; i++) {
91 46 : const void *name = ::sqlite3_column_name16(nativeStatement(), i);
92 46 : (void)mColumnNames.AppendElement(nsDependentString(static_cast<const PRUnichar*>(name)));
93 : }
94 :
95 67 : return NS_OK;
96 : }
97 :
98 : NS_IMETHODIMP
99 65 : StatementWrapper::GetStatement(mozIStorageStatement **_statement)
100 : {
101 65 : NS_IF_ADDREF(*_statement = mStatement);
102 65 : return NS_OK;
103 : }
104 :
105 : NS_IMETHODIMP
106 37 : StatementWrapper::Reset()
107 : {
108 37 : if (!mStatement)
109 0 : return NS_ERROR_FAILURE;
110 :
111 37 : return mStatement->Reset();
112 : }
113 :
114 : NS_IMETHODIMP
115 38 : StatementWrapper::Step(bool *_hasMoreResults)
116 : {
117 38 : if (!mStatement)
118 0 : return NS_ERROR_FAILURE;
119 :
120 38 : bool hasMore = false;
121 38 : nsresult rv = mStatement->ExecuteStep(&hasMore);
122 38 : if (NS_SUCCEEDED(rv) && !hasMore) {
123 0 : *_hasMoreResults = false;
124 0 : (void)mStatement->Reset();
125 0 : return NS_OK;
126 : }
127 :
128 38 : *_hasMoreResults = hasMore;
129 38 : return rv;
130 : }
131 :
132 : NS_IMETHODIMP
133 26 : StatementWrapper::Execute()
134 : {
135 26 : if (!mStatement)
136 0 : return NS_ERROR_FAILURE;
137 :
138 26 : return mStatement->Execute();
139 : }
140 :
141 : NS_IMETHODIMP
142 44 : StatementWrapper::GetRow(mozIStorageStatementRow **_row)
143 : {
144 44 : NS_ENSURE_ARG_POINTER(_row);
145 :
146 44 : if (!mStatement)
147 0 : return NS_ERROR_FAILURE;
148 :
149 : PRInt32 state;
150 44 : mStatement->GetState(&state);
151 44 : if (state != mozIStorageStatement::MOZ_STORAGE_STATEMENT_EXECUTING)
152 0 : return NS_ERROR_FAILURE;
153 :
154 44 : if (!mStatementRow) {
155 76 : mStatementRow = new StatementRow(mStatement);
156 38 : NS_ENSURE_TRUE(mStatementRow, NS_ERROR_OUT_OF_MEMORY);
157 : }
158 :
159 44 : NS_ADDREF(*_row = mStatementRow);
160 44 : return NS_OK;
161 : }
162 :
163 : NS_IMETHODIMP
164 27 : StatementWrapper::GetParams(mozIStorageStatementParams **_params)
165 : {
166 27 : NS_ENSURE_ARG_POINTER(_params);
167 :
168 27 : if (!mStatementParams) {
169 30 : mStatementParams = new StatementParams(mStatement);
170 15 : NS_ENSURE_TRUE(mStatementParams, NS_ERROR_OUT_OF_MEMORY);
171 : }
172 :
173 27 : NS_ADDREF(*_params = mStatementParams);
174 27 : return NS_OK;
175 : }
176 :
177 : ////////////////////////////////////////////////////////////////////////////////
178 : //// nsIXPCScriptable
179 :
180 : #define XPC_MAP_CLASSNAME StatementWrapper
181 : #define XPC_MAP_QUOTED_CLASSNAME "StatementWrapper"
182 : #define XPC_MAP_WANT_CALL
183 : #define XPC_MAP_FLAGS nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE | \
184 : nsIXPCScriptable::USE_JSSTUB_FOR_SETPROPERTY
185 : #include "xpc_map_end.h"
186 :
187 : NS_IMETHODIMP
188 0 : StatementWrapper::Call(nsIXPConnectWrappedNative *aWrapper,
189 : JSContext *aCtx,
190 : JSObject *aScopeObj,
191 : PRUint32 aArgc,
192 : jsval *aArgv,
193 : jsval *_vp,
194 : bool *_retval)
195 : {
196 0 : if (!mStatement)
197 0 : return NS_ERROR_FAILURE;
198 :
199 0 : if (aArgc != mParamCount) {
200 0 : *_retval = false;
201 0 : return NS_ERROR_FAILURE;
202 : }
203 :
204 : // reset
205 0 : (void)mStatement->Reset();
206 :
207 : // bind parameters
208 0 : for (int i = 0; i < (int)aArgc; i++) {
209 0 : nsCOMPtr<nsIVariant> variant(convertJSValToVariant(aCtx, aArgv[i]));
210 0 : if (!variant ||
211 0 : NS_FAILED(mStatement->BindByIndex(i, variant))) {
212 0 : *_retval = false;
213 0 : return NS_ERROR_INVALID_ARG;
214 : }
215 : }
216 :
217 : // if there are no results, we just execute
218 0 : if (mResultColumnCount == 0)
219 0 : (void)mStatement->Execute();
220 :
221 0 : *_vp = JSVAL_TRUE;
222 0 : *_retval = true;
223 0 : return NS_OK;
224 : }
225 :
226 : } // namespace storage
227 : } // namespace mozilla
|