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
16 * The Original Code is Oracle Corporation code.
18 * The Initial Developer of the Original Code is
20 * Portions created by the Initial Developer are Copyright (C) 2004
21 * the Initial Developer. All Rights Reserved.
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 ***** */
43 #include "mozStoragePrivateHelpers.h"
44 #include "mozStorageStatementWrapper.h"
45 #include "mozStorageStatementParams.h"
46 #include "mozStorageStatementRow.h"
53 ////////////////////////////////////////////////////////////////////////////////
56 StatementWrapper::StatementWrapper()
61 StatementWrapper::~StatementWrapper()
68 mozIStorageStatementWrapper
,
72 ////////////////////////////////////////////////////////////////////////////////
73 //// mozIStorageStatementWrapper
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
)));
96 StatementWrapper::GetStatement(mozIStorageStatement
**_statement
)
98 NS_IF_ADDREF(*_statement
= mStatement
);
103 StatementWrapper::Reset()
106 return NS_ERROR_FAILURE
;
108 return mStatement
->Reset();
112 StatementWrapper::Step(PRBool
*_hasMoreResults
)
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();
125 *_hasMoreResults
= hasMore
;
130 StatementWrapper::Execute()
133 return NS_ERROR_FAILURE
;
135 return mStatement
->Execute();
139 StatementWrapper::GetRow(mozIStorageStatementRow
**_row
)
141 NS_ENSURE_ARG_POINTER(_row
);
144 return NS_ERROR_FAILURE
;
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
);
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
);
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"
185 StatementWrapper::Call(nsIXPConnectWrappedNative
*aWrapper
,
194 return NS_ERROR_FAILURE
;
196 if (aArgc
!= mParamCount
) {
198 return NS_ERROR_FAILURE
;
202 (void)mStatement
->Reset();
205 for (int i
= 0; i
< (int)aArgc
; i
++) {
206 nsCOMPtr
<nsIVariant
> variant(convertJSValToVariant(aCtx
, aArgv
[i
]));
208 NS_FAILED(mStatement
->BindByIndex(i
, variant
))) {
210 return NS_ERROR_INVALID_ARG
;
214 // if there are no results, we just execute
215 if (mResultColumnCount
== 0)
216 (void)mStatement
->Execute();
223 } // namespace storage
224 } // namespace mozilla