summaryrefslogtreecommitdiff
path: root/database.c (plain)
blob: 4f1860611e68d60a3cd7dac8f530b445b52f287c
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
/* -*- indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* Store GConf defaults in a SQLite database
 * Copyright (C) 2009 Red Hat Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */
#include "gconf-defaults-hack.h"
#include "hippo-sqlite-utils.h"

struct _GConfDefaultsHackDatabase
{
  sqlite3 *db;
};

GConfDefaultsHackDatabase *
gconf_defaults_hack_database_open (const char *filename)
{
  GConfDefaultsHackDatabase *database;
  int sqlite_result;
  sqlite3 *db;

  sqlite_result = sqlite3_open(filename, &db);

  if (sqlite_result != SQLITE_OK)
    {
      g_warning ("Could not open GConf defaults cache %s: %s",
                 filename, sqlite3_errmsg (db));
      sqlite3_close (db);
      return NULL;
    }

  database = g_new0 (GConfDefaultsHackDatabase, 1);
  database->db = db;

  return database;
}

void
gconf_defaults_hack_database_close (GConfDefaultsHackDatabase *database)
{
  sqlite3_close (database->db);
  g_free (database);
}

void
gconf_defaults_hack_database_begin_transaction (GConfDefaultsHackDatabase *database)
{
  hippo_sqlite_execute_sql (database->db,
                            "BEGIN TRANSACTION",
                            NULL);
}

void
gconf_defaults_hack_database_commit (GConfDefaultsHackDatabase *database)
{
  hippo_sqlite_execute_sql (database->db,
                            "COMMIT",
                            NULL);
}

void
gconf_defaults_hack_database_clear (GConfDefaultsHackDatabase *database)
{
  hippo_sqlite_execute_sql (database->db,
                            "DROP TABLE IF EXISTS Defaults",
                            NULL);
  hippo_sqlite_execute_sql (database->db,
                            "CREATE TABLE Defaults ("
                            "key TEXT UNIQUE,"
                            "type TEXT,"
                            "list_type TEXT,"
                            "car_type TEXT,"
                            "cdr_type TEXT,"
                            "default_value TEXT"
                            ")",
                            NULL);
}

GConfDefaultsHackEntry *
gconf_defaults_hack_database_get (GConfDefaultsHackDatabase *database,
                                  const char                *key)
{
  GConfDefaultsHackEntry entry = { 0 };

  if (!hippo_sqlite_execute_sql_single_result (database->db,
                                               "SELECT "
                                               "type, list_type, car_type, cdr_type, default_value "
                                               "FROM Defaults WHERE key = :key",
                                               "s:key", key,
                                               NULL,
                                               "sssss",
					       &entry.type,
                                               &entry.list_type,
                                               &entry.car_type,
                                               &entry.cdr_type,
                                               &entry.default_value))
    return NULL;

  return g_slice_dup (GConfDefaultsHackEntry, &entry);
}

void
gconf_defaults_hack_database_put (GConfDefaultsHackDatabase *database,
                                  const char                *key,
                                  GConfDefaultsHackEntry    *entry)
{
  if (!hippo_sqlite_execute_sql (database->db,
                                 "INSERT OR IGNORE into Defaults "
                                 "(key, type, list_type, car_type, cdr_type, default_value) "
                                 "VALUES (:key, :type, :list_type, :car_type, :cdr_type, :default_value)",
                                 "s:key", key,
                                 "s:type", entry->type,
                                 "s:list_type", entry->list_type,
                                 "s:car_type", entry->car_type,
                                 "s:cdr_type", entry->cdr_type,
                                 "s:default_value", entry->default_value,
                                 NULL))
    g_print("%s\n", key);
}

void
gconf_defaults_hack_entry_free (GConfDefaultsHackEntry *entry)
{
  g_free (entry->type);
  g_free (entry->list_type);
  g_free (entry->car_type);
  g_free (entry->cdr_type);
  g_free (entry->default_value);

  g_slice_free (GConfDefaultsHackEntry, entry);
}