summaryrefslogtreecommitdiff
path: root/lib/ephy-sqlite-connection.c
blob: 06574936fd7e2563c26212afc86393b5b1ede251 (plain)
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
 *  Copyright © 2011 Igalia S.L.
 *
 *  This file is part of Epiphany.
 *
 *  Epiphany 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  Epiphany 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 Epiphany.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"
#include "ephy-sqlite-connection.h"

#include "ephy-lib-type-builtins.h"

#include <errno.h>
#include <glib/gstdio.h>
#include <sqlite3.h>

struct _EphySQLiteConnection {
  GObject parent_instance;

  char *database_path;
  sqlite3 *database;
  EphySQLiteConnectionMode mode;
};

G_DEFINE_TYPE (EphySQLiteConnection, ephy_sqlite_connection, G_TYPE_OBJECT);

enum {
  PROP_0,
  PROP_MODE,
  PROP_DATABASE_PATH,
  LAST_PROP
};

static GParamSpec *obj_properties[LAST_PROP];

static void
ephy_sqlite_connection_finalize (GObject *self)
{
  g_free (EPHY_SQLITE_CONNECTION (self)->database_path);
  ephy_sqlite_connection_close (EPHY_SQLITE_CONNECTION (self));
  G_OBJECT_CLASS (ephy_sqlite_connection_parent_class)->finalize (self);
}

static void
ephy_sqlite_connection_set_property (GObject      *object,
                                     guint         property_id,
                                     const GValue *value,
                                     GParamSpec   *pspec)
{
  EphySQLiteConnection *self = EPHY_SQLITE_CONNECTION (object);

  switch (property_id) {
    case PROP_MODE:
      self->mode = g_value_get_enum (value);
      break;
    case PROP_DATABASE_PATH:
      self->database_path = g_value_dup_string (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (self, property_id, pspec);
      break;
  }
}

static void
ephy_sqlite_connection_class_init (EphySQLiteConnectionClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->finalize = ephy_sqlite_connection_finalize;
  gobject_class->set_property = ephy_sqlite_connection_set_property;

  obj_properties[PROP_MODE] =
    g_param_spec_enum ("mode",
                       "SQLite connection mode",
                       "Whether the SQLite connection is read-only or writable",
                       EPHY_TYPE_SQ_LITE_CONNECTION_MODE,
                       EPHY_SQLITE_CONNECTION_MODE_READWRITE,
                       G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS);

  obj_properties[PROP_DATABASE_PATH] =
    g_param_spec_string ("database-path",
                         "Database path",
                         "The path of the SQLite database file",
                         NULL,
                         G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS);

  g_object_class_install_properties (gobject_class, LAST_PROP, obj_properties);
}

static void
ephy_sqlite_connection_init (EphySQLiteConnection *self)
{
  self->database = NULL;
}

GQuark ephy_sqlite_error_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_literal (ephy_sqlite_error_quark (), 0, string);
}

EphySQLiteConnection *
ephy_sqlite_connection_new (EphySQLiteConnectionMode  mode,
                            const char               *database_path)
{
  return EPHY_SQLITE_CONNECTION (g_object_new (EPHY_TYPE_SQLITE_CONNECTION,
                                               "mode", mode,
                                               "database-path", database_path,
                                               NULL));
}

gboolean
ephy_sqlite_connection_open (EphySQLiteConnection  *self,
                             GError               **error)
{
  if (self->database) {
    set_error_from_string ("Connection already open.", error);
    return FALSE;
  }

  if (sqlite3_open_v2 (self->database_path,
                       &self->database,
                       self->mode == EPHY_SQLITE_CONNECTION_MODE_MEMORY ? SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY
                                                                        : SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
                       NULL) != SQLITE_OK) {
    ephy_sqlite_connection_get_error (self, error);
    self->database = NULL;
    return FALSE;
  }

  /* Create a local copy of current database for in memory usage */
  if (self->mode == EPHY_SQLITE_CONNECTION_MODE_MEMORY) {
    sqlite3 *init_db;
    int rc;

    rc = sqlite3_open_v2 (self->database_path, &init_db, SQLITE_OPEN_READONLY, 0);
    if (rc == SQLITE_OK) {
      sqlite3_backup *backup;

      backup = sqlite3_backup_init (self->database, "main", init_db, "main");
      rc = sqlite3_backup_step (backup, -1);

      if (rc != SQLITE_DONE)
        g_warning ("Failed to copy history to in-memory database: %s", sqlite3_errstr (rc));

      sqlite3_backup_finish (backup);
    }

    sqlite3_close (init_db);
  }

  return TRUE;
}

void
ephy_sqlite_connection_close (EphySQLiteConnection *self)
{
  if (self->database) {
    sqlite3_close (self->database);
    self->database = NULL;
  }
}

void
ephy_sqlite_connection_delete_database (EphySQLiteConnection *self)
{
  char *journal;

  g_assert (EPHY_IS_SQLITE_CONNECTION (self));

  if (g_file_test (self->database_path, G_FILE_TEST_EXISTS) && g_unlink (self->database_path) == -1)
    g_warning ("Failed to delete database at %s: %s", self->database_path, g_strerror (errno));

  journal = g_strdup_printf ("%s-journal", self->database_path);
  if (g_file_test (journal, G_FILE_TEST_EXISTS) && g_unlink (journal) == -1)
    g_warning ("Failed to delete database journal at %s: %s", journal, g_strerror (errno));

  g_free (journal);
}

void
ephy_sqlite_connection_get_error (EphySQLiteConnection  *self,
                                  GError               **error)
{
  if (error)
    *error = g_error_new_literal (ephy_sqlite_error_quark (), sqlite3_errcode (self->database), sqlite3_errmsg (self->database));
}

gboolean
ephy_sqlite_connection_execute (EphySQLiteConnection  *self,
                                const char            *sql,
                                GError               **error)
{
  if (self->database == NULL) {
    set_error_from_string ("Connection not open.", error);
    return FALSE;
  }

  if (sqlite3_exec (self->database, sql, NULL, NULL, NULL) != SQLITE_OK) {
    ephy_sqlite_connection_get_error (self, error);
    return FALSE;
  }
  return TRUE;
}

EphySQLiteStatement *
ephy_sqlite_connection_create_statement (EphySQLiteConnection  *self,
                                         const char            *sql,
                                         GError               **error)
{
  sqlite3_stmt *prepared_statement;

  if (self->database == NULL) {
    set_error_from_string ("Connection not open.", error);
    return NULL;
  }

  if (sqlite3_prepare_v2 (self->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->database);
}

void
ephy_sqlite_connection_enable_foreign_keys (EphySQLiteConnection *self)
{
  GError *error = NULL;

  g_assert (EPHY_IS_SQLITE_CONNECTION (self));

  ephy_sqlite_connection_execute (self, "PRAGMA foreign_keys=ON", &error);
  if (error) {
    g_warning ("Failed to enable foreign keys pragma: %s", error->message);
    g_error_free (error);
  }
}

gboolean
ephy_sqlite_connection_begin_transaction (EphySQLiteConnection  *self,
                                          GError               **error)
{
  return ephy_sqlite_connection_execute (self, "BEGIN TRANSACTION", 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_warning ("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_warning ("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_warning ("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;
}