2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 * @defgroup databasei database
18 * @{ @ingroup database
24 typedef enum db_type_t db_type_t
;
25 typedef enum db_driver_t db_driver_t
;
26 typedef struct database_t database_t
;
28 #include <utils/enumerator.h>
31 * Database column types
34 /** integer type, argument is an "int" */
36 /** unsigned integer, argument is an "u_int" */
38 /** string type, argument is a "char*" */
40 /** binary large object type, argument is a "chunk_t" */
42 /** floating point, argument is a "double" */
44 /** NULL, takes no argument */
49 * Database implementation type.
52 /** SQLite database */
59 * Interface for a database implementation.
62 int affected, rowid, aint;
65 enumerator_t *enumerator;
67 db = lib->database->create("mysql://user:pass@host/database");
68 affected = db->execute(db, &rowid, "INSERT INTO table VALUES (?, ?)",
69 DB_INT, 77, DB_TEXT, "a text");
70 printf("inserted %d row, new row ID: %d\n", affected, rowid);
72 enumerator = db->query(db, "SELECT aint, atext FROM table WHERE aint > ?",
73 DB_INT, 10, // 1 argument to SQL string
74 DB_INT, DB_TEXT); // 2 enumerated types in query
77 while (enumerator->enumerate(enumerator, &aint, &atext))
79 printf("%d: %s\n", aint, atext);
81 enumerator->destroy(enumerator);
88 * Run a query which returns rows, such as a SELECT.
90 * @param sql sql query string, containing '?' placeholders
91 * @param ... list of sql placeholder db_type_t followed by its value,
92 * followed by enumerators arguments as db_type_t's
93 * @return enumerator as defined with arguments, NULL on failure
95 enumerator_t
* (*query
)(database_t
*this, char *sql
, ...);
98 * Execute a query which dows not return rows, such as INSERT.
100 * @param rowid pointer to write inserted AUTO_INCREMENT row ID, or NULL
101 * @param sql sql string, containing '?' placeholders
102 * @param ... list of sql placeholder db_type_t followed by its value
103 * @return number of affected rows, < 0 on failure
105 int (*execute
)(database_t
*this, int *rowid
, char *sql
, ...);
108 * Get the database implementation type.
110 * To allow driver specific SQL or performance optimizations each database
111 * implementations can be queried for its type.
113 * @return database implementation type
115 db_driver_t (*get_driver
)(database_t
*this);
118 * Destroy a database connection.
120 void (*destroy
)(database_t
*this);
123 #endif /* DATABASE_H_ @}*/