This source file includes following definitions.
- m_isPrepared
- prepare
- step
- finalize
- executeCommand
- bindText
- bindDouble
- bindNull
- bindValue
- bindParameterCount
- columnCount
- getColumnName
- getColumnValue
- getColumnText
- getColumnInt
- getColumnInt64
#include "config.h"
#include "modules/webdatabase/sqlite/SQLiteStatement.h"
#include <sqlite3.h>
#include "platform/Logging.h"
#include "modules/webdatabase/sqlite/SQLValue.h"
#include "wtf/Assertions.h"
#include "wtf/text/CString.h"
#if SQLITE_VERSION_NUMBER < 3006016
#error SQLite version 3.6.16 or newer is required
#endif
namespace WebCore {
SQLiteStatement::SQLiteStatement(SQLiteDatabase& db, const String& sql)
: m_database(db)
, m_query(sql)
, m_statement(0)
#ifndef NDEBUG
, m_isPrepared(false)
#endif
{
}
SQLiteStatement::~SQLiteStatement()
{
finalize();
}
int SQLiteStatement::prepare()
{
ASSERT(!m_isPrepared);
MutexLocker databaseLock(m_database.databaseMutex());
if (m_database.isInterrupted())
return SQLITE_INTERRUPT;
CString query = m_query.stripWhiteSpace().utf8();
WTF_LOG(SQLDatabase, "SQL - prepare - %s", query.data());
size_t lengthIncludingNullCharacter = query.length() + 1;
const char* tail;
int error = sqlite3_prepare_v2(m_database.sqlite3Handle(), query.data(), lengthIncludingNullCharacter, &m_statement, &tail);
if (error != SQLITE_OK)
WTF_LOG(SQLDatabase, "sqlite3_prepare16 failed (%i)\n%s\n%s", error, query.data(), sqlite3_errmsg(m_database.sqlite3Handle()));
if (tail && *tail)
error = SQLITE_ERROR;
#ifndef NDEBUG
m_isPrepared = error == SQLITE_OK;
#endif
return error;
}
int SQLiteStatement::step()
{
MutexLocker databaseLock(m_database.databaseMutex());
if (m_database.isInterrupted())
return SQLITE_INTERRUPT;
if (!m_statement)
return SQLITE_OK;
m_database.updateLastChangesCount();
WTF_LOG(SQLDatabase, "SQL - step - %s", m_query.ascii().data());
int error = sqlite3_step(m_statement);
if (error != SQLITE_DONE && error != SQLITE_ROW) {
WTF_LOG(SQLDatabase, "sqlite3_step failed (%i)\nQuery - %s\nError - %s",
error, m_query.ascii().data(), sqlite3_errmsg(m_database.sqlite3Handle()));
}
return error;
}
int SQLiteStatement::finalize()
{
#ifndef NDEBUG
m_isPrepared = false;
#endif
if (!m_statement)
return SQLITE_OK;
WTF_LOG(SQLDatabase, "SQL - finalize - %s", m_query.ascii().data());
int result = sqlite3_finalize(m_statement);
m_statement = 0;
return result;
}
bool SQLiteStatement::executeCommand()
{
if (!m_statement && prepare() != SQLITE_OK)
return false;
ASSERT(m_isPrepared);
if (step() != SQLITE_DONE) {
finalize();
return false;
}
finalize();
return true;
}
int SQLiteStatement::bindText(int index, const String& text)
{
ASSERT(m_isPrepared);
ASSERT(index > 0);
ASSERT(static_cast<unsigned>(index) <= bindParameterCount());
ASSERT(!String().charactersWithNullTermination().data());
return sqlite3_bind_text16(m_statement, index, text.charactersWithNullTermination().data(), sizeof(UChar) * text.length(), SQLITE_TRANSIENT);
}
int SQLiteStatement::bindDouble(int index, double number)
{
ASSERT(m_isPrepared);
ASSERT(index > 0);
ASSERT(static_cast<unsigned>(index) <= bindParameterCount());
return sqlite3_bind_double(m_statement, index, number);
}
int SQLiteStatement::bindNull(int index)
{
ASSERT(m_isPrepared);
ASSERT(index > 0);
ASSERT(static_cast<unsigned>(index) <= bindParameterCount());
return sqlite3_bind_null(m_statement, index);
}
int SQLiteStatement::bindValue(int index, const SQLValue& value)
{
switch (value.type()) {
case SQLValue::StringValue:
return bindText(index, value.string());
case SQLValue::NumberValue:
return bindDouble(index, value.number());
case SQLValue::NullValue:
return bindNull(index);
}
ASSERT_NOT_REACHED();
return SQLITE_ERROR;
}
unsigned SQLiteStatement::bindParameterCount() const
{
ASSERT(m_isPrepared);
if (!m_statement)
return 0;
return sqlite3_bind_parameter_count(m_statement);
}
int SQLiteStatement::columnCount()
{
ASSERT(m_isPrepared);
if (!m_statement)
return 0;
return sqlite3_data_count(m_statement);
}
String SQLiteStatement::getColumnName(int col)
{
ASSERT(col >= 0);
if (!m_statement)
if (prepareAndStep() != SQLITE_ROW)
return String();
if (columnCount() <= col)
return String();
return String(reinterpret_cast<const UChar*>(sqlite3_column_name16(m_statement, col)));
}
SQLValue SQLiteStatement::getColumnValue(int col)
{
ASSERT(col >= 0);
if (!m_statement)
if (prepareAndStep() != SQLITE_ROW)
return SQLValue();
if (columnCount() <= col)
return SQLValue();
sqlite3_value* value = sqlite3_column_value(m_statement, col);
switch (sqlite3_value_type(value)) {
case SQLITE_INTEGER:
case SQLITE_FLOAT:
return SQLValue(sqlite3_value_double(value));
case SQLITE_BLOB:
case SQLITE_TEXT: {
const UChar* string = reinterpret_cast<const UChar*>(sqlite3_value_text16(value));
unsigned length = WTF::lengthOfNullTerminatedString(string);
return SQLValue(StringImpl::create8BitIfPossible(string, length));
}
case SQLITE_NULL:
return SQLValue();
default:
break;
}
ASSERT_NOT_REACHED();
return SQLValue();
}
String SQLiteStatement::getColumnText(int col)
{
ASSERT(col >= 0);
if (!m_statement)
if (prepareAndStep() != SQLITE_ROW)
return String();
if (columnCount() <= col)
return String();
const UChar* string = reinterpret_cast<const UChar*>(sqlite3_column_text16(m_statement, col));
return StringImpl::create8BitIfPossible(string, sqlite3_column_bytes16(m_statement, col) / sizeof(UChar));
}
int SQLiteStatement::getColumnInt(int col)
{
ASSERT(col >= 0);
if (!m_statement)
if (prepareAndStep() != SQLITE_ROW)
return 0;
if (columnCount() <= col)
return 0;
return sqlite3_column_int(m_statement, col);
}
int64_t SQLiteStatement::getColumnInt64(int col)
{
ASSERT(col >= 0);
if (!m_statement)
if (prepareAndStep() != SQLITE_ROW)
return 0;
if (columnCount() <= col)
return 0;
return sqlite3_column_int64(m_statement, col);
}
}