2
* Copyright (C) 2006 Apple Computer, 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 "SQLDatabase.h"
31
#include "SQLStatement.h"
36
const int SQLResultError = SQLITE_ERROR;
37
const int SQLResultDone = SQLITE_DONE;
38
const int SQLResultOk = SQLITE_OK;
39
const int SQLResultRow = SQLITE_ROW;
41
SQLDatabase::SQLDatabase()
47
bool SQLDatabase::open(const String& filename)
51
//SQLite expects a null terminator on its UTF16 strings
52
m_path = filename.copy();
54
m_lastError = sqlite3_open16(m_path.charactersWithNullTermination(), &m_db);
55
if (m_lastError != SQLITE_OK) {
56
LOG_ERROR("SQLite database failed to load from %s\nCause - %s", filename.ascii().data(),
57
sqlite3_errmsg(m_db));
61
if (!SQLStatement(*this, "PRAGMA temp_store = MEMORY;").executeCommand())
62
LOG_ERROR("SQLite database could not set temp_store to memory");
67
void SQLDatabase::close()
76
void SQLDatabase::setFullsync(bool fsync)
79
executeCommand("PRAGMA fullfsync = 1;");
81
executeCommand("PRAGMA fullfsync = 0;");
84
void SQLDatabase::setSynchronous(SynchronousPragma sync)
86
executeCommand(String::format("PRAGMA synchronous = %i", sync));
89
void SQLDatabase::setBusyTimeout(int ms)
92
sqlite3_busy_timeout(m_db, ms);
94
LOG(SQLDatabase, "BusyTimeout set on non-open database");
97
void SQLDatabase::setBusyHandler(int(*handler)(void*, int))
100
sqlite3_busy_handler(m_db, handler, NULL);
102
LOG(SQLDatabase, "Busy handler set on non-open database");
105
bool SQLDatabase::executeCommand(const String& sql)
107
return SQLStatement(*this, sql).executeCommand();
110
bool SQLDatabase::returnsAtLeastOneResult(const String& sql)
112
return SQLStatement(*this, sql).returnsAtLeastOneResult();
115
bool SQLDatabase::tableExists(const String& tablename)
120
String statement = "SELECT name FROM sqlite_master WHERE type = 'table' AND name = '" + tablename + "';";
122
SQLStatement sql(*this, statement);
124
return sql.step() == SQLITE_ROW;
127
void SQLDatabase::clearAllTables()
129
String query = "SELECT name FROM sqlite_master WHERE type='table';";
130
Vector<String> tables;
131
if (!SQLStatement(*this, query).returnTextResults16(0, tables)) {
132
LOG(SQLDatabase, "Unable to retrieve list of tables from database");
136
for (Vector<String>::iterator table = tables.begin(); table != tables.end(); ++table ) {
137
if (*table == "sqlite_sequence")
139
if (!executeCommand("DROP TABLE " + *table))
140
LOG(SQLDatabase, "Unable to drop table %s", (*table).ascii().data());
144
void SQLDatabase::runVacuumCommand()
146
if (!executeCommand("VACUUM;"))
147
LOG(SQLDatabase, "Unable to vacuum database - %s", lastErrorMsg());
150
int64_t SQLDatabase::lastInsertRowID()
154
return sqlite3_last_insert_rowid(m_db);
157
int SQLDatabase::lastChanges()
161
return sqlite3_changes(m_db);
164
int SQLDatabase::lastError()
166
return m_db ? sqlite3_errcode(m_db) : SQLITE_ERROR;
169
const char* SQLDatabase::lastErrorMsg()
171
return sqlite3_errmsg(m_db);
174
} // namespace WebCore