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"
29 #include "SQLStatement.h"
35 SQLDatabase::SQLDatabase()
41 bool SQLDatabase::open(const String& filename)
45 //SQLite expects a null terminator on its UTF16 strings
47 m_path.append(UChar(0));
49 m_lastError = sqlite3_open16(m_path.characters(), &m_db);
50 if (m_lastError != SQLITE_OK) {
51 LOG_ERROR("SQLite database failed to load from %s\nCause - %s", filename.ascii().data(),
52 sqlite3_errmsg(m_db));
56 if (!SQLStatement(*this, "PRAGMA temp_store = MEMORY;").executeCommand())
57 LOG_ERROR("SQLite database could not set temp_store to memory");
62 void SQLDatabase::close()
71 void SQLDatabase::setFullsync(bool fsync)
74 executeCommand("PRAGMA fullfsync = 1;");
76 executeCommand("PRAGMA fullfsync = 0;");
79 void SQLDatabase::setSynchronous(SynchronousPragma sync)
81 executeCommand(String::sprintf("PRAGMA synchronous = %i", sync));
84 void SQLDatabase::setBusyTimeout(int ms)
87 sqlite3_busy_timeout(m_db, ms);
89 LOG(IconDatabase, "BusyTimeout set on non-open database");
92 void SQLDatabase::setBusyHandler(int(*handler)(void*, int))
95 sqlite3_busy_handler(m_db, handler, NULL);
97 LOG(IconDatabase, "Busy handler set on non-open database");
100 bool SQLDatabase::executeCommand(const String& sql)
102 return SQLStatement(*this, sql).executeCommand();
105 bool SQLDatabase::returnsAtLeastOneResult(const String& sql)
107 return SQLStatement(*this, sql).returnsAtLeastOneResult();
110 bool SQLDatabase::tableExists(const String& tablename)
115 String statement = "SELECT name FROM sqlite_master WHERE type = 'table' AND name = '" + tablename + "';";
117 SQLStatement sql(*this, statement);
119 return sql.step() == SQLITE_ROW;
122 int64_t SQLDatabase::lastInsertRowID()
126 return sqlite3_last_insert_rowid(m_db);
129 } // namespace WebCore