2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "modules/webdatabase/sqlite/SQLiteStatement.h"
30 #include "platform/Logging.h"
31 #include "modules/webdatabase/sqlite/SQLValue.h"
32 #include "wtf/Assertions.h"
33 #include "wtf/text/CString.h"
35 // SQLite 3.6.16 makes sqlite3_prepare_v2 automatically retry preparing the statement
36 // once if the database scheme has changed. We rely on this behavior.
37 #if SQLITE_VERSION_NUMBER < 3006016
38 #error SQLite version 3.6.16 or newer is required
43 SQLiteStatement::SQLiteStatement(SQLiteDatabase& db, const String& sql)
53 SQLiteStatement::~SQLiteStatement()
58 int SQLiteStatement::prepare()
60 ASSERT(!m_isPrepared);
62 CString query = m_query.stripWhiteSpace().utf8();
64 ThreadState::SafePointScope scope(ThreadState::HeapPointersOnStack);
66 WTF_LOG(SQLDatabase, "SQL - prepare - %s", query.data());
68 // Pass the length of the string including the null character to sqlite3_prepare_v2;
69 // this lets SQLite avoid an extra string copy.
70 size_t lengthIncludingNullCharacter = query.length() + 1;
73 int error = sqlite3_prepare_v2(m_database.sqlite3Handle(), query.data(), lengthIncludingNullCharacter, &m_statement, &tail);
75 if (error != SQLITE_OK)
76 WTF_LOG(SQLDatabase, "sqlite3_prepare16 failed (%i)\n%s\n%s", error, query.data(), sqlite3_errmsg(m_database.sqlite3Handle()));
77 else if (tail && *tail)
81 m_isPrepared = error == SQLITE_OK;
86 int SQLiteStatement::step()
88 ThreadState::SafePointScope scope(ThreadState::HeapPointersOnStack);
89 //ASSERT(m_isPrepared);
94 // The database needs to update its last changes count before each statement
95 // in order to compute properly the lastChanges() return value.
96 m_database.updateLastChangesCount();
98 WTF_LOG(SQLDatabase, "SQL - step - %s", m_query.ascii().data());
99 int error = sqlite3_step(m_statement);
100 if (error != SQLITE_DONE && error != SQLITE_ROW) {
101 WTF_LOG(SQLDatabase, "sqlite3_step failed (%i)\nQuery - %s\nError - %s",
102 error, m_query.ascii().data(), sqlite3_errmsg(m_database.sqlite3Handle()));
108 int SQLiteStatement::finalize()
111 m_isPrepared = false;
115 WTF_LOG(SQLDatabase, "SQL - finalize - %s", m_query.ascii().data());
116 int result = sqlite3_finalize(m_statement);
121 bool SQLiteStatement::executeCommand()
123 if (!m_statement && prepare() != SQLITE_OK)
125 ASSERT(m_isPrepared);
126 if (step() != SQLITE_DONE) {
134 int SQLiteStatement::bindText(int index, const String& text)
136 ASSERT(m_isPrepared);
138 ASSERT(static_cast<unsigned>(index) <= bindParameterCount());
140 // SQLite treats uses zero pointers to represent null strings, which means we need to make sure to map null WTFStrings to zero pointers.
141 ASSERT(!String().charactersWithNullTermination().data());
142 return sqlite3_bind_text16(m_statement, index, text.charactersWithNullTermination().data(), sizeof(UChar) * text.length(), SQLITE_TRANSIENT);
145 int SQLiteStatement::bindDouble(int index, double number)
147 ASSERT(m_isPrepared);
149 ASSERT(static_cast<unsigned>(index) <= bindParameterCount());
151 return sqlite3_bind_double(m_statement, index, number);
154 int SQLiteStatement::bindNull(int index)
156 ASSERT(m_isPrepared);
158 ASSERT(static_cast<unsigned>(index) <= bindParameterCount());
160 return sqlite3_bind_null(m_statement, index);
163 int SQLiteStatement::bindValue(int index, const SQLValue& value)
165 switch (value.type()) {
166 case SQLValue::StringValue:
167 return bindText(index, value.string());
168 case SQLValue::NumberValue:
169 return bindDouble(index, value.number());
170 case SQLValue::NullValue:
171 return bindNull(index);
174 ASSERT_NOT_REACHED();
178 unsigned SQLiteStatement::bindParameterCount() const
180 ASSERT(m_isPrepared);
183 return sqlite3_bind_parameter_count(m_statement);
186 int SQLiteStatement::columnCount()
188 ASSERT(m_isPrepared);
191 return sqlite3_data_count(m_statement);
194 String SQLiteStatement::getColumnName(int col)
198 if (prepareAndStep() != SQLITE_ROW)
200 if (columnCount() <= col)
202 return String(reinterpret_cast<const UChar*>(sqlite3_column_name16(m_statement, col)));
205 SQLValue SQLiteStatement::getColumnValue(int col)
209 if (prepareAndStep() != SQLITE_ROW)
211 if (columnCount() <= col)
214 // SQLite is typed per value. optional column types are
215 // "(mostly) ignored"
216 sqlite3_value* value = sqlite3_column_value(m_statement, col);
217 switch (sqlite3_value_type(value)) {
218 case SQLITE_INTEGER: // SQLValue and JS don't represent integers, so use FLOAT -case
220 return SQLValue(sqlite3_value_double(value));
221 case SQLITE_BLOB: // SQLValue and JS don't represent blobs, so use TEXT -case
223 const UChar* string = reinterpret_cast<const UChar*>(sqlite3_value_text16(value));
224 unsigned length = WTF::lengthOfNullTerminatedString(string);
225 return SQLValue(StringImpl::create8BitIfPossible(string, length));
232 ASSERT_NOT_REACHED();
236 String SQLiteStatement::getColumnText(int col)
240 if (prepareAndStep() != SQLITE_ROW)
242 if (columnCount() <= col)
244 const UChar* string = reinterpret_cast<const UChar*>(sqlite3_column_text16(m_statement, col));
245 return StringImpl::create8BitIfPossible(string, sqlite3_column_bytes16(m_statement, col) / sizeof(UChar));
248 int SQLiteStatement::getColumnInt(int col)
252 if (prepareAndStep() != SQLITE_ROW)
254 if (columnCount() <= col)
256 return sqlite3_column_int(m_statement, col);
259 int64_t SQLiteStatement::getColumnInt64(int col)
263 if (prepareAndStep() != SQLITE_ROW)
265 if (columnCount() <= col)
267 return sqlite3_column_int64(m_statement, col);