1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
/*
* Copyright © 2011 Igalia S.L.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "ephy-sqlite-connection.h"
#include <sqlite3.h>
struct _EphySQLiteConnectionPrivate {
sqlite3 *database;
};
#define EPHY_SQLITE_CONNECTION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), EPHY_TYPE_SQLITE_CONNECTION, EphySQLiteConnectionPrivate))
G_DEFINE_TYPE (EphySQLiteConnection, ephy_sqlite_connection, G_TYPE_OBJECT);
static void
ephy_sqlite_connection_finalize (GObject *self)
{
ephy_sqlite_connection_close (EPHY_SQLITE_CONNECTION (self));
G_OBJECT_CLASS (ephy_sqlite_connection_parent_class)->dispose (self);
}
static void
ephy_sqlite_connection_class_init (EphySQLiteConnectionClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = ephy_sqlite_connection_finalize;
g_type_class_add_private (object_class, sizeof (EphySQLiteConnectionPrivate));
}
static void
ephy_sqlite_connection_init (EphySQLiteConnection *self)
{
self->priv = EPHY_SQLITE_CONNECTION_GET_PRIVATE (self);
self->priv->database = NULL;
}
static GQuark get_ephy_sqlite_quark (void)
{
return g_quark_from_static_string ("ephy-sqlite");
}
static void
set_error_from_string (const char* string, GError **error)
{
if (error)
*error = g_error_new (get_ephy_sqlite_quark (), 0, string, 0);
}
EphySQLiteConnection *
ephy_sqlite_connection_new ()
{
return EPHY_SQLITE_CONNECTION (g_object_new (EPHY_TYPE_SQLITE_CONNECTION, NULL));
}
gboolean
ephy_sqlite_connection_open (EphySQLiteConnection *self, const gchar *filename, GError **error)
{
EphySQLiteConnectionPrivate *priv = self->priv;
if (priv->database) {
set_error_from_string ("Connection already open.", error);
return FALSE;
}
if (sqlite3_open (filename, &priv->database) != SQLITE_OK) {
ephy_sqlite_connection_get_error (self, error);
priv->database = NULL;
return FALSE;
}
return TRUE;
}
void
ephy_sqlite_connection_close (EphySQLiteConnection *self)
{
EphySQLiteConnectionPrivate *priv = self->priv;
if (priv->database) {
sqlite3_close (priv->database);
priv->database = NULL;
}
}
void
ephy_sqlite_connection_get_error (EphySQLiteConnection *self, GError **error)
{
if (error)
*error = g_error_new (get_ephy_sqlite_quark (), 0, sqlite3_errmsg (self->priv->database), 0);
}
gboolean
ephy_sqlite_connection_execute (EphySQLiteConnection *self, const char *sql, GError **error)
{
EphySQLiteConnectionPrivate *priv = self->priv;
if (priv->database == NULL) {
set_error_from_string ("Connection not open.", error);
return FALSE;
}
return sqlite3_exec (priv->database, sql, NULL, NULL, NULL) == SQLITE_OK;
}
EphySQLiteStatement *
ephy_sqlite_connection_create_statement (EphySQLiteConnection *self, const char *sql, GError **error)
{
EphySQLiteConnectionPrivate *priv = self->priv;
sqlite3_stmt *prepared_statement;
if (priv->database == NULL) {
set_error_from_string ("Connection not open.", error);
return NULL;
}
if (sqlite3_prepare_v2 (priv->database, sql, -1, &prepared_statement, NULL) != SQLITE_OK) {
ephy_sqlite_connection_get_error (self, error);
return NULL;
}
return EPHY_SQLITE_STATEMENT (g_object_new (EPHY_TYPE_SQLITE_STATEMENT,
"prepared-statement", prepared_statement,
"connection", self,
NULL));
}
gint64
ephy_sqlite_connection_get_last_insert_id (EphySQLiteConnection *self)
{
return sqlite3_last_insert_rowid (self->priv->database);
}
gboolean
ephy_sqlite_connection_begin_transaction (EphySQLiteConnection *self, GError **error)
{
return ephy_sqlite_connection_execute (self, "BEGIN TRANSACTION", error);
}
gboolean
ephy_sqlite_connection_rollback_transaction (EphySQLiteConnection *self, GError **error)
{
return ephy_sqlite_connection_execute (self, "ROLLBACK", error);
}
gboolean
ephy_sqlite_connection_commit_transaction (EphySQLiteConnection *self, GError **error)
{
return ephy_sqlite_connection_execute (self, "COMMIT", error);
}
gboolean
ephy_sqlite_connection_table_exists (EphySQLiteConnection *self, const char *table_name)
{
GError *error = NULL;
gboolean table_exists = FALSE;
EphySQLiteStatement *statement = ephy_sqlite_connection_create_statement (self,
"SELECT COUNT(type) FROM sqlite_master WHERE type='table' and name=?", &error);
if (error) {
g_error ("Could not detect table existence: %s", error->message);
g_error_free (error);
return FALSE;
}
ephy_sqlite_statement_bind_string (statement, 0, table_name, &error);
if (error) {
g_object_unref (statement);
g_error ("Could not detect table existence: %s", error->message);
g_error_free (error);
return FALSE;
}
ephy_sqlite_statement_step (statement, &error);
if (error) {
g_object_unref (statement);
g_error ("Could not detect table existence: %s", error->message);
g_error_free (error);
return FALSE;
}
table_exists = ephy_sqlite_statement_get_column_as_int (statement, 0);
g_object_unref (statement);
return table_exists;
}
|