Updated the internal API for database engines

This commit is contained in:
Pierre Pronchery 2012-11-30 16:15:34 +01:00
parent 06c0de2204
commit 7bce8aef0d
13 changed files with 88 additions and 61 deletions

View File

@ -19,6 +19,7 @@
# define LIBDATABASE_DATABASE_H
# include "Database/database.h"
# include "Database/engine.h"
#endif /* !LIBDATABASE_DATABASE_H */

View File

@ -18,13 +18,12 @@
#ifndef LIBDATABASE_DATABASE_DATABASE_H
# define LIBDATABASE_DATABASE_DATABASE_H
# include <stdarg.h>
# include <stdint.h>
# include <System.h>
/* Database */
/* private */
/* public */
/* types */
typedef enum _DatabaseType
{
@ -40,31 +39,6 @@ typedef struct _DatabaseStatement DatabaseStatement;
typedef int (*DatabaseCallback)(void * data, int argc, char ** argv,
char ** columns);
typedef struct _DatabasePlugin DatabasePlugin;
typedef struct _DatabasePluginDefinition
{
char const * name;
char const * description;
/* essential */
DatabasePlugin * (*init)(Config * config, char const * section);
void (*destroy)(DatabasePlugin * plugin);
/* accessors */
int64_t (*get_last_id)(DatabasePlugin * plugin);
/* useful */
int (*query)(DatabasePlugin * plugin, char const * query,
DatabaseCallback callback, void * data);
/* prepared statements */
DatabaseStatement * (*prepare_new)(DatabasePlugin * plugin,
char const * query);
void (*prepare_delete)(DatabasePlugin * plugin,
DatabaseStatement * statement);
int (*prepare_query)(DatabasePlugin * plugin,
DatabaseStatement * statement,
DatabaseCallback callback, void * data,
va_list args);
} DatabasePluginDefinition;
/* public */
/* functions */
@ -76,12 +50,11 @@ void database_delete(Database * database);
int64_t database_get_last_id(Database * database);
/* useful */
DatabaseStatement * database_prepare_new(
Database * database, char const * query);
DatabaseStatement * database_prepare_new(Database * database,
char const * query);
void database_prepare_delete(Database * database,
DatabaseStatement * statement);
int database_prepare_query(Database * database,
DatabaseStatement * statement,
int database_prepare_query(Database * database, DatabaseStatement * statement,
DatabaseCallback callback, void * data, ...);
int database_query(Database * database, char const * query,

55
include/Database/engine.h Normal file
View File

@ -0,0 +1,55 @@
/* $Id$ */
/* Copyright (c) 2012 Pierre Pronchery <khorben@defora.org> */
/* This file is part of DeforaOS Database libDatabase */
/* 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, version 3 of the License.
*
* 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, see <http://www.gnu.org/licenses/>. */
#ifndef LIBDATABASE_DATABASE_ENGINE_H
# define LIBDATABASE_DATABASE_ENGINE_H
# include <stdarg.h>
# include <stdint.h>
# include <System.h>
# include "database.h"
/* DatabaseEngine */
/* public */
/* types */
typedef struct _DatabaseEngine DatabaseEngine;
typedef struct _DatabaseEngineDefinition
{
char const * name;
char const * description;
/* essential */
DatabaseEngine * (*init)(Config * config, char const * section);
void (*destroy)(DatabaseEngine * engine);
/* accessors */
int64_t (*get_last_id)(DatabaseEngine * engine);
/* useful */
int (*query)(DatabaseEngine * engine, char const * query,
DatabaseCallback callback, void * data);
/* prepared statements */
DatabaseStatement * (*prepare_new)(DatabaseEngine * engine,
char const * query);
void (*prepare_delete)(DatabaseEngine * engine,
DatabaseStatement * statement);
int (*prepare_query)(DatabaseEngine * engine,
DatabaseStatement * statement,
DatabaseCallback callback, void * data,
va_list args);
} DatabaseEngineDefinition;
#endif /* !LIBDATABASE_DATABASE_ENGINE_H */

View File

@ -4,7 +4,7 @@ PREFIX = /usr/local
DESTDIR =
LIBDIR = $(PREFIX)/lib
CC ?= cc
CPPFLAGSF= -I ../include/Database
CPPFLAGSF= -I ../include
CPPFLAGS?=
CFLAGSF = -W -fPIC
CFLAGS = -Wall -g -O2 -pedantic

View File

@ -17,7 +17,7 @@
#include <string.h>
#include <System.h>
#include "database.h"
#include "Database.h"
#include "../config.h"
@ -27,8 +27,8 @@
struct _Database
{
Plugin * plugin;
DatabasePluginDefinition * dplugin;
DatabasePlugin * database;
DatabaseEngineDefinition * dplugin;
DatabaseEngine * database;
};
@ -75,24 +75,22 @@ int64_t database_get_last_id(Database * database)
/* database_prepare_new */
DatabaseStatement * database_prepare_new(
Database * database, char const * query)
DatabaseStatement * database_prepare_new(Database * database,
char const * query)
{
return database->dplugin->prepare_new(database->database, query);
}
/* database_prepare_delete */
void database_prepare_delete(Database * database,
DatabaseStatement * statement)
void database_prepare_delete(Database * database, DatabaseStatement * statement)
{
database->dplugin->prepare_delete(database->database, statement);
}
/* database_prepare_query */
int database_prepare_query(Database * database,
DatabaseStatement * statement,
int database_prepare_query(Database * database, DatabaseStatement * statement,
DatabaseCallback callback, void * data, ...)
{
int ret;

View File

@ -3,7 +3,7 @@ PREFIX = /usr/local
DESTDIR =
LIBDIR = $(PREFIX)/lib
CC ?= cc
CPPFLAGSF= -I ../../include/Database
CPPFLAGSF= -I ../../include
CPPFLAGS?=
CFLAGSF = -W -fPIC
CFLAGS = -Wall -g -O2 -pedantic

View File

@ -22,7 +22,7 @@
#endif
#include <string.h>
#include <System.h>
#include "database.h"
#include "Database/engine.h"
#include "../../config.h"
/* constants */
@ -37,11 +37,11 @@
/* PDO */
/* private */
/* types */
typedef struct _DatabasePlugin
typedef struct _DatabaseEngine
{
Plugin * plugin;
DatabasePluginDefinition * dplugin;
DatabasePlugin * database;
DatabaseEngineDefinition * dplugin;
DatabaseEngine * database;
} PDO;
typedef struct _DatabaseStatement
@ -69,7 +69,7 @@ static int _pdo_prepare_query(PDO * pdo, PDOStatement * statement,
/* public */
/* variables */
DatabasePluginDefinition database =
DatabaseEngineDefinition database =
{
"PDO",
NULL,

View File

@ -24,13 +24,13 @@
#include <errno.h>
#include <libpq-fe.h>
#include <System.h>
#include "database.h"
#include "Database/engine.h"
/* PgSQL */
/* private */
/* types */
typedef struct _DatabasePlugin
typedef struct _DatabaseEngine
{
PGconn * handle;
Oid last;
@ -68,7 +68,7 @@ static int _pgsql_prepare_query(PgSQL * pgsql, PgSQLStatement * statement,
/* public */
/* variables */
DatabasePluginDefinition database =
DatabaseEngineDefinition database =
{
"PostgreSQL",
NULL,

View File

@ -1,5 +1,5 @@
targets=pdo,pgsql,sqlite2,sqlite3,template
cppflags_force=-I ../../include/Database
cppflags_force=-I ../../include
cflags_force=-W -fPIC
cflags=-Wall -g -O2 -pedantic
dist=Makefile

View File

@ -23,13 +23,13 @@
#include <string.h>
#include <sqlite.h>
#include <System.h>
#include "database.h"
#include "Database/engine.h"
/* SQLite2 */
/* private */
/* types */
typedef struct _DatabasePlugin
typedef struct _DatabaseEngine
{
sqlite * handle;
} SQLite2;
@ -61,7 +61,7 @@ static int _sqlite2_prepare_query(SQLite2 * sqlite,
/* public */
/* variables */
DatabasePluginDefinition database =
DatabaseEngineDefinition database =
{
"SQLite2",
NULL,

View File

@ -23,13 +23,13 @@
#include <errno.h>
#include <sqlite3.h>
#include <System.h>
#include "database.h"
#include "Database/engine.h"
/* SQLite3 */
/* private */
/* types */
typedef struct _DatabasePlugin
typedef struct _DatabaseEngine
{
sqlite3 * handle;
} SQLite3;
@ -62,7 +62,7 @@ static int _sqlite3_prepare_query(SQLite3 * sqlite3,
/* public */
/* variables */
DatabasePluginDefinition database =
DatabaseEngineDefinition database =
{
"SQLite3",
NULL,

View File

@ -22,13 +22,13 @@
#endif
#include <string.h>
#include <System.h>
#include "database.h"
#include "Database/engine.h"
/* Template */
/* private */
/* types */
typedef struct _DatabasePlugin
typedef struct _DatabaseEngine
{
void * handle;
} Template;
@ -60,7 +60,7 @@ static int _template_prepare_query(Template * template,
/* public */
/* variables */
DatabasePluginDefinition database =
DatabaseEngineDefinition database =
{
"Template",
NULL,

View File

@ -1,6 +1,6 @@
subdirs=engines
targets=libDatabase
cppflags_force=-I ../include/Database
cppflags_force=-I ../include
cflags_force=-W -fPIC
cflags=-Wall -g -O2 -pedantic
dist=Makefile