~ubuntu-branches/ubuntu/maverick/webkit/maverick

« back to all changes in this revision

Viewing changes to WebCore/loader/icon/SQLDatabase.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Mike Hommey
  • Date: 2007-08-19 15:54:12 UTC
  • Revision ID: james.westby@ubuntu.com-20070819155412-uxxg1h9plpghmtbi
Tags: upstream-0~svn25144
ImportĀ upstreamĀ versionĀ 0~svn25144

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2006 Apple Computer, Inc.  All rights reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions
 
6
 * are met:
 
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.
 
12
 *
 
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. 
 
24
 */
 
25
 
 
26
#include "config.h"
 
27
#include "SQLDatabase.h"
 
28
 
 
29
#include "Logging.h"
 
30
#include <sqlite3.h>
 
31
#include "SQLStatement.h"
 
32
 
 
33
 
 
34
namespace WebCore {
 
35
 
 
36
const int SQLResultError = SQLITE_ERROR;
 
37
const int SQLResultDone = SQLITE_DONE;
 
38
const int SQLResultOk = SQLITE_OK;
 
39
const int SQLResultRow = SQLITE_ROW;
 
40
 
 
41
SQLDatabase::SQLDatabase()
 
42
    : m_db(0)
 
43
{
 
44
 
 
45
}
 
46
 
 
47
bool SQLDatabase::open(const String& filename)
 
48
{
 
49
    close();
 
50
    
 
51
    //SQLite expects a null terminator on its UTF16 strings
 
52
    m_path = filename.copy();
 
53
    
 
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));
 
58
        sqlite3_close(m_db);
 
59
        m_db = 0;
 
60
    }
 
61
    if (!SQLStatement(*this, "PRAGMA temp_store = MEMORY;").executeCommand())
 
62
        LOG_ERROR("SQLite database could not set temp_store to memory");
 
63
 
 
64
    return isOpen();
 
65
}
 
66
 
 
67
void SQLDatabase::close()
 
68
{
 
69
    if (m_db) {
 
70
        sqlite3_close(m_db);
 
71
        m_path.truncate(0);
 
72
        m_db = 0;
 
73
    }
 
74
}
 
75
 
 
76
void SQLDatabase::setFullsync(bool fsync) 
 
77
{
 
78
    if (fsync) 
 
79
        executeCommand("PRAGMA fullfsync = 1;");
 
80
    else
 
81
        executeCommand("PRAGMA fullfsync = 0;");
 
82
}
 
83
 
 
84
void SQLDatabase::setSynchronous(SynchronousPragma sync)
 
85
{
 
86
    executeCommand(String::format("PRAGMA synchronous = %i", sync));
 
87
}
 
88
 
 
89
void SQLDatabase::setBusyTimeout(int ms)
 
90
{
 
91
    if (m_db)
 
92
        sqlite3_busy_timeout(m_db, ms);
 
93
    else
 
94
        LOG(SQLDatabase, "BusyTimeout set on non-open database");
 
95
}
 
96
 
 
97
void SQLDatabase::setBusyHandler(int(*handler)(void*, int))
 
98
{
 
99
    if (m_db)
 
100
        sqlite3_busy_handler(m_db, handler, NULL);
 
101
    else
 
102
        LOG(SQLDatabase, "Busy handler set on non-open database");
 
103
}
 
104
 
 
105
bool SQLDatabase::executeCommand(const String& sql)
 
106
{
 
107
    return SQLStatement(*this, sql).executeCommand();
 
108
}
 
109
 
 
110
bool SQLDatabase::returnsAtLeastOneResult(const String& sql)
 
111
{
 
112
    return SQLStatement(*this, sql).returnsAtLeastOneResult();
 
113
}
 
114
 
 
115
bool SQLDatabase::tableExists(const String& tablename)
 
116
{
 
117
    if (!isOpen())
 
118
        return false;
 
119
        
 
120
    String statement = "SELECT name FROM sqlite_master WHERE type = 'table' AND name = '" + tablename + "';";
 
121
    
 
122
    SQLStatement sql(*this, statement);
 
123
    sql.prepare();
 
124
    return sql.step() == SQLITE_ROW;
 
125
}
 
126
 
 
127
void SQLDatabase::clearAllTables()
 
128
{
 
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");
 
133
        return;
 
134
    }
 
135
    
 
136
    for (Vector<String>::iterator table = tables.begin(); table != tables.end(); ++table ) {
 
137
        if (*table == "sqlite_sequence")
 
138
            continue;
 
139
        if (!executeCommand("DROP TABLE " + *table))
 
140
            LOG(SQLDatabase, "Unable to drop table %s", (*table).ascii().data());
 
141
    }
 
142
}
 
143
 
 
144
void SQLDatabase::runVacuumCommand()
 
145
{
 
146
    if (!executeCommand("VACUUM;"))
 
147
        LOG(SQLDatabase, "Unable to vacuum database - %s", lastErrorMsg());
 
148
}
 
149
 
 
150
int64_t SQLDatabase::lastInsertRowID()
 
151
{
 
152
    if (!m_db)
 
153
        return 0;
 
154
    return sqlite3_last_insert_rowid(m_db);
 
155
}
 
156
 
 
157
int SQLDatabase::lastChanges()
 
158
{
 
159
    if (!m_db)
 
160
        return 0;
 
161
    return sqlite3_changes(m_db);
 
162
}
 
163
 
 
164
int SQLDatabase::lastError()
 
165
{
 
166
    return m_db ? sqlite3_errcode(m_db) : SQLITE_ERROR;
 
167
}
 
168
 
 
169
const char* SQLDatabase::lastErrorMsg()
 
170
 
171
    return sqlite3_errmsg(m_db);
 
172
}
 
173
 
 
174
} // namespace WebCore
 
175
 
 
176