4 * @brief Implementation of database_t.
9 * Copyright (C) 2007 Martin Willi
10 * Hochschule fuer Technik Rapperswil
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 #include <enumerator.h>
30 typedef struct private_database_t private_database_t
;
33 * private data of database
35 struct private_database_t
{
43 * SQLite database handle
49 * database enumerator implements enumerator_t
52 enumerator_t enumerator
;
57 * destroy a database enumerator
59 static void db_enumerator_destroy(db_enumerator_t
* this)
61 sqlite3_finalize(this->stmt
);
66 * create a database enumerator
68 static enumerator_t
*db_enumerator_create(bool(*enumerate
)(db_enumerator_t
*,void*,...),
71 db_enumerator_t
*this = malloc_thing(db_enumerator_t
);
72 this->enumerator
.enumerate
= (void*)enumerate
;
73 this->enumerator
.destroy
= (void*)db_enumerator_destroy
;
75 return &this->enumerator
;
79 * enumerator function for empty enumerator
81 static bool empty_enumerate(enumerator_t
*enumerator
, void *item
, ...)
87 * create an empty enumerator
89 static enumerator_t
* empty_enumerator_create()
91 enumerator_t
*this = malloc_thing(enumerator_t
);
92 this->enumerate
= empty_enumerate
;
93 this->destroy
= (void*)free
;
98 * Implementation of database_t.login.
100 static int login(private_database_t
*this, char *username
, char *password
)
105 if (sqlite3_prepare_v2(this->db
,
106 "SELECT oid FROM users WHERE username = ? AND password = ?;",
107 -1, &stmt
, NULL
) == SQLITE_OK
)
109 if (sqlite3_bind_text(stmt
, 1, username
, -1, SQLITE_STATIC
) == SQLITE_OK
&&
110 sqlite3_bind_text(stmt
, 2, password
, -1, SQLITE_STATIC
) == SQLITE_OK
&&
111 sqlite3_step(stmt
) == SQLITE_ROW
)
113 uid
= sqlite3_column_int(stmt
, 0);
115 sqlite3_finalize(stmt
);
121 * enumerate function for gateway enumrator
123 static bool gateway_enumerate(db_enumerator_t
* e
, int *id
, const char **name
,
124 int *port
, const char **address
)
126 if (sqlite3_step(e
->stmt
) == SQLITE_ROW
)
128 *id
= sqlite3_column_int(e
->stmt
, 0);
129 *name
= sqlite3_column_text(e
->stmt
, 1);
130 *port
= sqlite3_column_int(e
->stmt
, 2);
131 *address
= sqlite3_column_text(e
->stmt
, 3);
138 * Implementation of database_t.create_gateway_enumerator.
140 static enumerator_t
* create_gateway_enumerator(private_database_t
*this, int user
)
144 if (sqlite3_prepare_v2(this->db
,
145 "SELECT gateways.oid AS gid, name, port, address FROM "
146 "gateways, user_gateway AS ug ON gid = ug.gateway WHERE ug.user = ?;",
147 -1, &stmt
, NULL
) == SQLITE_OK
)
149 if (sqlite3_bind_int(stmt
, 1, user
) == SQLITE_OK
)
151 return db_enumerator_create((void*)gateway_enumerate
, stmt
);
153 sqlite3_finalize(stmt
);
155 return empty_enumerator_create();
159 * Implementation of database_t.destroy
161 static void destroy(private_database_t
*this)
163 sqlite3_close(this->db
);
170 database_t
*database_create(char *dbfile
)
172 private_database_t
*this = malloc_thing(private_database_t
);
174 this->public.login
= (int(*)(database_t
*, char *username
, char *password
))login
;
175 this->public.create_gateway_enumerator
= (enumerator_t
*(*)(database_t
*,int))create_gateway_enumerator
;
176 this->public.destroy
= (void(*)(database_t
*))destroy
;
178 if (sqlite3_open(dbfile
, &this->db
) != SQLITE_OK
)
183 return &this->public;