php-src/ext/dba/dba.c

519 lines
11 KiB
C
Raw Normal View History

1999-07-21 23:12:32 +08:00
/*
+----------------------------------------------------------------------+
| PHP version 4.0 |
1999-07-21 23:12:32 +08:00
+----------------------------------------------------------------------+
| Copyright (c) 1997-2001 The PHP Group |
1999-07-21 23:12:32 +08:00
+----------------------------------------------------------------------+
| This source file is subject to version 2.02 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| http://www.php.net/license/2_02.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
1999-07-21 23:12:32 +08:00
+----------------------------------------------------------------------+
2000-07-10 18:09:15 +08:00
| Authors: Sascha Schumann <sascha@schumann.cx> |
1999-07-21 23:12:32 +08:00
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
1999-07-21 23:12:32 +08:00
#include "php.h"
#if HAVE_DBA
#include "php_dba.h"
#include "ext/standard/info.h"
1999-07-21 23:12:32 +08:00
#include "php_gdbm.h"
#include "php_ndbm.h"
#include "php_dbm.h"
#include "php_cdb.h"
#include "php_db2.h"
#include "php_db3.h"
1999-07-21 23:12:32 +08:00
/* {{{ dba_functions[]
*/
1999-07-21 23:12:32 +08:00
function_entry dba_functions[] = {
PHP_FE(dba_open, NULL)
PHP_FE(dba_popen, NULL)
PHP_FE(dba_close, NULL)
PHP_FE(dba_delete, NULL)
PHP_FE(dba_exists, NULL)
PHP_FE(dba_fetch, NULL)
PHP_FE(dba_insert, NULL)
PHP_FE(dba_replace, NULL)
PHP_FE(dba_firstkey, NULL)
PHP_FE(dba_nextkey, NULL)
PHP_FE(dba_optimize, NULL)
PHP_FE(dba_sync, NULL)
{NULL,NULL,NULL}
};
/* }}} */
1999-07-21 23:12:32 +08:00
1999-07-28 01:09:35 +08:00
static PHP_MINIT_FUNCTION(dba);
static PHP_MSHUTDOWN_FUNCTION(dba);
static PHP_MINFO_FUNCTION(dba);
1999-07-21 23:12:32 +08:00
zend_module_entry dba_module_entry = {
"dba", dba_functions,
PHP_MINIT(dba),
PHP_MSHUTDOWN(dba),
NULL, NULL,
PHP_MINFO(dba),
STANDARD_MODULE_PROPERTIES
1999-07-21 23:12:32 +08:00
};
2000-05-02 11:38:26 +08:00
#ifdef COMPILE_DL_DBA
ZEND_GET_MODULE(dba)
#endif
1999-07-21 23:12:32 +08:00
typedef struct dba_handler {
char *name;
int (*open)(dba_info *);
void (*close)(dba_info *);
char* (*fetch)(dba_info *, char *, int, int *);
int (*update)(dba_info *, char *, int, char *, int, int);
int (*exists)(dba_info *, char *, int);
int (*delete)(dba_info *, char *, int);
char* (*firstkey)(dba_info *, int *);
char* (*nextkey)(dba_info *, int *);
int (*optimize)(dba_info *);
int (*sync)(dba_info *);
} dba_handler;
/* {{{ macromania */
#define DBA_ID_PARS \
pval **id; \
dba_info *info = NULL; \
int type, ac = ZEND_NUM_ARGS()
1999-07-21 23:12:32 +08:00
/* these are used to get the standard arguments */
#define DBA_GET1 \
1999-12-19 06:40:35 +08:00
if(ac != 1 || zend_get_parameters_ex(ac, &id) != SUCCESS) { \
WRONG_PARAM_COUNT; \
1999-07-21 23:12:32 +08:00
}
#define DBA_GET2 \
pval **key; \
1999-12-19 06:40:35 +08:00
if(ac != 2 || zend_get_parameters_ex(ac, &key, &id) != SUCCESS) { \
WRONG_PARAM_COUNT; \
} \
convert_to_string_ex(key)
1999-07-21 23:12:32 +08:00
#define DBA_IF_NOT_CORRECT_TYPE(link_id) \
1999-12-18 03:51:39 +08:00
info = zend_list_find(link_id, &type); \
1999-07-21 23:12:32 +08:00
if(!info || (type != GLOBAL(le_db) && type != GLOBAL(le_pdb)))
#define DBA_ID_GET \
convert_to_long_ex(id); \
2000-11-23 05:47:15 +08:00
DBA_IF_NOT_CORRECT_TYPE(Z_LVAL_PP(id)) { \
php_error(E_WARNING, "Unable to find DBA identifier %d", \
2000-11-23 05:47:15 +08:00
Z_LVAL_PP(id)); \
RETURN_FALSE; \
1999-07-21 23:12:32 +08:00
}
#define DBA_ID_GET1 DBA_ID_PARS; DBA_GET1; DBA_ID_GET
#define DBA_ID_GET2 DBA_ID_PARS; DBA_GET2; DBA_ID_GET
/* a DBA handler must have specific routines */
#define DBA_HND(x) \
{\
#x, dba_open_##x, dba_close_##x, dba_fetch_##x, dba_update_##x, \
dba_exists_##x, dba_delete_##x, dba_firstkey_##x, dba_nextkey_##x, \
dba_optimize_##x, dba_sync_##x \
},
/* check whether the user has write access */
#define DBA_WRITE_CHECK \
if(info->mode != DBA_WRITER && info->mode != DBA_TRUNC && info->mode != DBA_CREAT) { \
php_error(E_WARNING, "you cannot perform a modification to a database without proper access"); \
1999-07-21 23:12:32 +08:00
RETURN_FALSE; \
}
#define GLOBAL(a) a
/* }}} */
/* {{{ globals */
static dba_handler handler[] = {
#if DBA_GDBM
DBA_HND(gdbm)
#endif
#if DBA_DBM
DBA_HND(dbm)
#endif
#if DBA_NDBM
DBA_HND(ndbm)
#endif
#if DBA_CDB
DBA_HND(cdb)
#endif
#if DBA_DB2
DBA_HND(db2)
#endif
#if DBA_DB3
DBA_HND(db3)
1999-07-21 23:12:32 +08:00
#endif
{ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
};
static int le_db;
static int le_pdb;
static HashTable ht_keys;
/* }}} */
/* {{{ dba_close
*/
2000-10-27 01:38:01 +08:00
static void dba_close(dba_info *info)
1999-07-21 23:12:32 +08:00
{
if(info->hnd) info->hnd->close(info);
if(info->path) free(info->path);
free(info);
}
/* }}} */
/* {{{ dba_close_rsrc
*/
2000-10-27 01:38:01 +08:00
static void dba_close_rsrc(zend_rsrc_list_entry *rsrc)
{
dba_info *info = (dba_info *)rsrc->ptr;
dba_close(info);
}
/* }}} */
2000-10-27 01:38:01 +08:00
/* {{{ PHP_MINIT_FUNCTION
*/
1999-07-28 01:09:35 +08:00
static PHP_MINIT_FUNCTION(dba)
1999-07-21 23:12:32 +08:00
{
zend_hash_init(&ht_keys, 0, NULL, NULL, 1);
2000-10-27 01:38:01 +08:00
GLOBAL(le_db) = zend_register_list_destructors_ex(dba_close_rsrc, NULL, "dba", module_number);
GLOBAL(le_pdb) = zend_register_list_destructors_ex(NULL, dba_close_rsrc, "dba persistent", module_number);
1999-07-21 23:12:32 +08:00
return SUCCESS;
}
/* }}} */
1999-07-21 23:12:32 +08:00
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
1999-07-28 01:09:35 +08:00
static PHP_MSHUTDOWN_FUNCTION(dba)
1999-07-21 23:12:32 +08:00
{
zend_hash_destroy(&ht_keys);
1999-07-21 23:12:32 +08:00
return SUCCESS;
}
/* }}} */
1999-07-21 23:12:32 +08:00
#include "ext/standard/php_smart_str.h"
2000-12-01 09:25:36 +08:00
/* {{{ PHP_MINFO_FUNCTION
*/
1999-07-28 01:09:35 +08:00
static PHP_MINFO_FUNCTION(dba)
1999-07-21 23:12:32 +08:00
{
dba_handler *hptr;
smart_str handlers = {0};
2000-12-01 09:25:36 +08:00
1999-07-21 23:12:32 +08:00
for(hptr = handler; hptr->name; hptr++) {
smart_str_appends(&handlers, hptr->name);
smart_str_appendc(&handlers, ' ');
2000-12-01 09:25:36 +08:00
}
php_info_print_table_start();
php_info_print_table_row(2, "DBA support", "enabled");
if (handlers.c) {
smart_str_0(&handlers);
php_info_print_table_row(2, "Supported handlers", handlers.c);
smart_str_free(&handlers);
} else {
php_info_print_table_row(2, "Supported handlers", "none");
}
2000-12-01 09:25:36 +08:00
php_info_print_table_end();
1999-07-21 23:12:32 +08:00
}
/* }}} */
2000-12-01 09:25:36 +08:00
/* {{{ php_dba_update
*/
1999-12-18 05:34:28 +08:00
static void php_dba_update(INTERNAL_FUNCTION_PARAMETERS, int mode)
1999-07-21 23:12:32 +08:00
{
DBA_ID_PARS;
pval **val, **key;
1999-07-21 23:12:32 +08:00
1999-12-19 06:40:35 +08:00
if(ac != 3 || zend_get_parameters_ex(ac, &key, &val, &id) != SUCCESS) {
1999-07-21 23:12:32 +08:00
WRONG_PARAM_COUNT;
}
convert_to_string_ex(key);
convert_to_string_ex(val);
1999-07-21 23:12:32 +08:00
DBA_ID_GET;
DBA_WRITE_CHECK;
if(info->hnd->update(info, VALLEN(key), VALLEN(val), mode) == SUCCESS)
RETURN_TRUE;
RETURN_FALSE;
}
/* }}} */
1999-07-21 23:12:32 +08:00
#define FREENOW if(args) efree(args); if(key) efree(key)
/* {{{ php_dba_open
*/
1999-12-18 05:34:28 +08:00
static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, int persistent)
1999-07-21 23:12:32 +08:00
{
pval ***args = (pval ***) NULL;
int ac = ZEND_NUM_ARGS();
1999-07-21 23:12:32 +08:00
dba_mode_t modenr;
dba_info *info;
dba_handler *hptr;
char *key = NULL;
int keylen = 0;
int listid;
int i;
if(ac < 3) {
WRONG_PARAM_COUNT;
}
/* we pass additional args to the respective handler */
args = emalloc(ac * sizeof(pval *));
1999-12-19 06:40:35 +08:00
if(zend_get_parameters_array_ex(ac, args) != SUCCESS) {
1999-07-21 23:12:32 +08:00
FREENOW;
WRONG_PARAM_COUNT;
}
/* we only take string arguments */
for(i = 0; i < ac; i++) {
convert_to_string_ex(args[i]);
2000-11-23 05:47:15 +08:00
keylen += Z_STRLEN_PP(args[i]);
1999-07-21 23:12:32 +08:00
}
if(persistent) {
/* calculate hash */
key = emalloc(keylen);
keylen = 0;
for(i = 0; i < ac; i++) {
2000-11-23 05:47:15 +08:00
memcpy(key+keylen,Z_STRVAL_PP(args[i]),Z_STRLEN_PP(args[i]));
keylen += Z_STRLEN_PP(args[i]);
1999-07-21 23:12:32 +08:00
}
if(zend_hash_find(&ht_keys, key, keylen, (void **) &info) == SUCCESS) {
1999-07-21 23:12:32 +08:00
FREENOW;
1999-12-18 03:51:39 +08:00
RETURN_LONG(zend_list_insert(info, GLOBAL(le_pdb)));
1999-07-21 23:12:32 +08:00
}
}
for(hptr = handler; hptr->name &&
strcasecmp(hptr->name, (*args[2])->value.str.val); hptr++);
1999-07-21 23:12:32 +08:00
if(!hptr->name) {
php_error(E_WARNING, "no such handler: %s", (*args[2])->value.str.val);
1999-07-21 23:12:32 +08:00
FREENOW;
RETURN_FALSE;
}
switch((*args[1])->value.str.val[0]) {
1999-07-21 23:12:32 +08:00
case 'c':
modenr = DBA_CREAT;
break;
case 'w':
modenr = DBA_WRITER;
break;
case 'r':
modenr = DBA_READER;
break;
case 'n':
modenr = DBA_TRUNC;
break;
default:
php_error(E_WARNING,"illegal DBA mode: %s",(*args[1])->value.str.val);
1999-07-21 23:12:32 +08:00
FREENOW;
RETURN_FALSE;
}
info = malloc(sizeof(*info));
memset(info, 0, sizeof(info));
info->path = strdup((*args[0])->value.str.val);
1999-07-21 23:12:32 +08:00
info->mode = modenr;
info->argc = ac - 3;
info->argv = args + 3;
1999-07-21 23:39:28 +08:00
info->hnd = NULL;
1999-07-21 23:12:32 +08:00
if(hptr->open(info) != SUCCESS) {
dba_close(info);
php_error(E_WARNING, "driver initialization failed");
1999-07-21 23:12:32 +08:00
FREENOW;
RETURN_FALSE;
}
info->hnd = hptr;
info->argc = 0;
info->argv = NULL;
1999-12-18 03:51:39 +08:00
listid = zend_list_insert(info, persistent?GLOBAL(le_pdb):GLOBAL(le_db));
1999-07-21 23:12:32 +08:00
if(persistent) {
zend_hash_update(&ht_keys, key, keylen, info, sizeof(*info), NULL);
1999-07-21 23:12:32 +08:00
}
FREENOW;
RETURN_LONG(listid);
}
/* }}} */
#undef FREENOW
1999-07-21 23:12:32 +08:00
2000-07-14 02:44:57 +08:00
/* {{{ proto int dba_popen(string path, string mode, string handlername [, string ...])
2000-02-25 00:30:42 +08:00
Opens path using the specified handler in mode persistently */
1999-07-21 23:12:32 +08:00
PHP_FUNCTION(dba_popen)
{
1999-12-18 05:34:28 +08:00
php_dba_open(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
1999-07-21 23:12:32 +08:00
}
/* }}} */
2000-07-14 02:44:57 +08:00
/* {{{ proto int dba_open(string path, string mode, string handlername [, string ...])
2000-02-25 00:30:42 +08:00
Opens path using the specified handler in mode*/
1999-07-21 23:12:32 +08:00
PHP_FUNCTION(dba_open)
{
1999-12-18 05:34:28 +08:00
php_dba_open(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
1999-07-21 23:12:32 +08:00
}
/* }}} */
/* {{{ proto void dba_close(int handle)
2000-02-25 00:30:42 +08:00
Closes database */
1999-07-21 23:12:32 +08:00
PHP_FUNCTION(dba_close)
{
DBA_ID_GET1;
2000-11-23 05:47:15 +08:00
zend_list_delete(Z_LVAL_PP(id));
1999-07-21 23:12:32 +08:00
}
/* }}} */
/* {{{ proto bool dba_exists(string key, int handle)
2000-02-25 00:30:42 +08:00
Checks, if the specified key exists */
1999-07-21 23:12:32 +08:00
PHP_FUNCTION(dba_exists)
{
DBA_ID_GET2;
if(info->hnd->exists(info, VALLEN(key)) == SUCCESS) {
RETURN_TRUE;
}
RETURN_FALSE;
}
/* }}} */
/* {{{ proto string dba_fetch(string key, int handle)
2000-02-25 00:30:42 +08:00
Fetches the data associated with key */
1999-07-21 23:12:32 +08:00
PHP_FUNCTION(dba_fetch)
{
char *val;
int len = 0;
DBA_ID_GET2;
if((val = info->hnd->fetch(info, VALLEN(key), &len)) != NULL) {
RETURN_STRINGL(val, len, 0);
}
RETURN_FALSE;
}
/* }}} */
/* {{{ proto string dba_firstkey(int handle)
2000-02-25 00:30:42 +08:00
Resets the internal key pointer and returns the first key */
1999-07-21 23:12:32 +08:00
PHP_FUNCTION(dba_firstkey)
{
char *fkey;
int len;
DBA_ID_GET1;
fkey = info->hnd->firstkey(info, &len);
if(fkey)
RETURN_STRINGL(fkey, len, 0);
RETURN_FALSE;
}
/* }}} */
/* {{{ proto string dba_nextkey(int handle)
2000-02-25 00:30:42 +08:00
Returns the next key */
1999-07-21 23:12:32 +08:00
PHP_FUNCTION(dba_nextkey)
{
char *nkey;
int len;
DBA_ID_GET1;
nkey = info->hnd->nextkey(info, &len);
if(nkey)
RETURN_STRINGL(nkey, len, 0);
RETURN_FALSE;
}
/* }}} */
/* {{{ proto bool dba_delete(string key, int handle)
2000-02-25 00:30:42 +08:00
Deletes the entry associated with key */
1999-07-21 23:12:32 +08:00
PHP_FUNCTION(dba_delete)
{
DBA_ID_GET2;
DBA_WRITE_CHECK;
if(info->hnd->delete(info, VALLEN(key)) == SUCCESS)
RETURN_TRUE;
RETURN_FALSE;
}
/* }}} */
/* {{{ proto bool dba_insert(string key, string value, int handle)
2000-02-25 00:30:42 +08:00
Inserts value as key, returns false, if key exists already */
1999-07-21 23:12:32 +08:00
PHP_FUNCTION(dba_insert)
{
1999-12-18 05:34:28 +08:00
php_dba_update(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
1999-07-21 23:12:32 +08:00
}
/* }}} */
/* {{{ proto bool dba_replace(string key, string value, int handle)
2000-02-25 00:30:42 +08:00
Inserts value as key, replaces key, if key exists already */
1999-07-21 23:12:32 +08:00
PHP_FUNCTION(dba_replace)
{
1999-12-18 05:34:28 +08:00
php_dba_update(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
1999-07-21 23:12:32 +08:00
}
/* }}} */
/* {{{ proto bool dba_optimize(int handle)
2000-02-25 00:30:42 +08:00
Optimizes (e.g. clean up, vacuum) database */
1999-07-21 23:12:32 +08:00
PHP_FUNCTION(dba_optimize)
{
DBA_ID_GET1;
DBA_WRITE_CHECK;
if(info->hnd->optimize(info) == SUCCESS) {
RETURN_TRUE;
}
RETURN_FALSE;
}
/* }}} */
/* {{{ proto bool dba_sync(int handle)
2000-02-25 00:30:42 +08:00
Synchronizes database */
1999-07-21 23:12:32 +08:00
PHP_FUNCTION(dba_sync)
{
DBA_ID_GET1;
if(info->hnd->sync(info) == SUCCESS) {
RETURN_TRUE;
}
RETURN_FALSE;
}
/* }}} */
#endif
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 tw=78 fdm=marker
* vim<600: sw=4 ts=4 tw=78
*/