php-src/ext/dbplus/php_dbplus.c

1558 lines
35 KiB
C
Raw Normal View History

2001-07-06 00:04:03 +00:00
/*
+----------------------------------------------------------------------+
2001-12-11 15:32:16 +00:00
| PHP Version 4 |
2001-07-06 00:04:03 +00:00
+----------------------------------------------------------------------+
2002-02-28 08:29:35 +00:00
| Copyright (c) 1997-2002 The PHP Group |
2001-07-06 00:04:03 +00: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. |
+----------------------------------------------------------------------+
2002-02-28 08:29:35 +00:00
| Author: Hartmut Holzgraefe <hartmut@six.de> |
2001-07-06 00:04:03 +00:00
+----------------------------------------------------------------------+
*/
#include "php.h"
#include "php_ini.h"
#include "php_dbplus.h"
#include "ext/standard/php_string.h"
#include <saccess.h>
#include <relation.h>
#include <dblight.h>
2001-09-16 15:18:03 +00:00
/* missing prototypes in dbplus header files */
void string_to_scalop(char *op, enum scalop *sop);
field * string_to_field(char *val, attribute *ap, int flags);
void cdb_tcl(int,char *,char **, int *);
relf * aql_exec(char *, char *);
tuple *rnext(relf *);
2001-07-06 00:04:03 +00:00
#define _STRING(x) (Z_STRVAL_PP(x))
#define _INT(x) (Z_LVAL_PP(x))
#define _HASH(x) (Z_ARRVAL_PP(x))
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
#define DBPLUS_FETCH_RESOURCE(r, z) ZEND_FETCH_RESOURCE(r, relf *, z, -1, "dbplus_relation", le_dbplus_relation); \
if(!r) RETURN_LONG(ERR_UNKNOWN);
2001-07-06 00:04:03 +00:00
static int
2001-08-26 14:26:36 +00:00
var2tuple(relf *r, zval **zv, tuple *t)
2001-07-06 00:04:03 +00:00
{
2001-08-26 14:26:36 +00:00
register attribute *ap ;
unsigned deg ;
zval **element;
if (Z_TYPE_PP(zv)!=IS_ARRAY)
2001-08-26 14:26:36 +00:00
return 1;
rtupinit(r, t);
ap = r->r_atts;
deg = r->r_rel.rel_deg;
do {
if(SUCCESS!=zend_hash_find(Z_ARRVAL_PP(zv), ap->att_name, strlen(ap->att_name)+1, (void **)&element)) {
2001-08-26 14:26:36 +00:00
continue;
}
if (! *element) {
return 1;
}
2001-08-26 14:26:36 +00:00
switch(ap->att_type) {
2001-08-26 14:26:36 +00:00
case FT_SHORT:
/* short integer */
2001-08-26 14:26:36 +00:00
convert_to_long_ex(element);
AFFIX(ap, t)->f_short = (short) Z_LVAL_PP(element);
2001-08-26 14:26:36 +00:00
break;
case FT_UNSIGNED:
/* unsigned short integer */
2001-08-26 14:26:36 +00:00
convert_to_long_ex(element);
AFFIX(ap, t)->f_unsigned = (unsigned) Z_LVAL_PP(element);
2001-08-26 14:26:36 +00:00
break;
2001-08-26 14:26:36 +00:00
case FT_LONG:
/* 32bit signed long */
case FT_SEQUENCE:
/* unique sequence number -> just a long to outsiders */
2001-08-26 14:26:36 +00:00
convert_to_long_ex(element);
AFFIX(ap, t)->f_long = (long4) Z_LVAL_PP(element);
2001-08-26 14:26:36 +00:00
break;
2001-08-26 14:26:36 +00:00
case FT_DATE:
/* date -> long containing YYYYMMDD */
2001-08-26 14:26:36 +00:00
convert_to_long_ex(element);
AFFIX(ap, t)->f_date = (long4) Z_LVAL_PP(element);
2001-08-26 14:26:36 +00:00
break;
case FT_TIME:
/* time as unix timestamp */
convert_to_long_ex(element);
AFFIX(ap, t)->f_time = (long4) Z_LVAL_PP(element);
break;
2001-08-26 14:26:36 +00:00
case FT_FLOAT:
/* single prec. floating point */
2001-08-26 14:26:36 +00:00
convert_to_double_ex(element);
AFFIX(ap, t)->f_float = (float) Z_DVAL_PP(element);
2001-08-26 14:26:36 +00:00
break;
2001-08-26 14:26:36 +00:00
case FT_DOUBLE:
/* double prec. floating point */
2001-08-26 14:26:36 +00:00
convert_to_double_ex(element);
AFFIX(ap, t)->f_double = (double) Z_DVAL_PP(element);
2001-08-26 14:26:36 +00:00
break;
case FT_STRING:
case FT_DEUTSCH:
case FT_CHAR:
case FT_ANSI:
case FT_ISO:
case FT_ISOL:
/* different variants of Strings */
convert_to_string_ex(element);
afput(ap, t, (field *)0, Z_STRVAL_PP(element));
2001-08-26 14:26:36 +00:00
break;
default:
php_error(E_WARNING,"%s is of yet unsupported type %d",ap->att_name,ap->att_type);
2001-08-26 14:26:36 +00:00
break;
}
} while (ap++, --deg);
return 0;
2001-07-06 00:04:03 +00:00
}
static int
tuple2var(relf * r, tuple * t, zval **zv)
{
register attribute *ap ;
unsigned deg ;
2001-08-26 14:26:36 +00:00
zval *element;
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
zval_dtor(*zv);
if (array_init(*zv) == FAILURE) {
return 1;
}
2001-07-06 00:04:03 +00:00
ap = r->r_atts;
deg = r->r_rel.rel_deg;
do {
MAKE_STD_ZVAL(element); Z_TYPE_P(element)=IS_NULL;
2001-08-26 14:26:36 +00:00
switch(ap->att_type) {
case FT_SHORT:
ZVAL_LONG(element, AFFIX(ap, t)->f_short);
break;
case FT_UNSIGNED:
ZVAL_LONG(element, AFFIX(ap, t)->f_unsigned);
break;
case FT_LONG:
case FT_SEQUENCE:
ZVAL_LONG(element, AFFIX(ap, t)->f_long);
break;
case FT_TIME:
ZVAL_LONG(element, AFFIX(ap, t)->f_time);
break;
2001-08-26 14:26:36 +00:00
case FT_FLOAT:
ZVAL_DOUBLE(element, AFFIX(ap, t)->f_float);
break;
case FT_DOUBLE:
ZVAL_DOUBLE(element, AFFIX(ap, t)->f_double);
break;
case FT_STRING:
case FT_DEUTSCH:
case FT_CHAR:
ZVAL_STRING(element, AFVAR(ap, t)->f_string, 1);
break;
default:
php_error(E_WARNING,"%s is of yet unsupported type %d",ap->att_name,ap->att_type);
break;
2001-08-26 14:26:36 +00:00
}
if(Z_TYPE_P(element)!=IS_NULL)
zend_hash_update(Z_ARRVAL_PP(zv),
2001-08-26 14:26:36 +00:00
ap->att_name,
strlen(ap->att_name)+1,
(void *)&element,
sizeof(zval*),
NULL);
2001-07-06 00:04:03 +00:00
} while (ap++, --deg);
return 0;
}
static constraint *
ary2constr(relf * r, zval** constr)
{
2001-08-26 14:26:36 +00:00
attribute *ap;
static constraint c;
field *f;
enum scalop sop;
char * dom;
char * val;
char * op;
zval **zdata;
/* init first */
db_coninit(r, &c);
if (Z_TYPE_PP(constr) != IS_ARRAY) {
2001-08-26 14:26:36 +00:00
php_error(E_WARNING, "Constraint is not an array");
return NULL;
}
zend_hash_internal_pointer_reset(_HASH(constr));
if(zend_hash_get_current_data(_HASH(constr), (void **)&zdata)!=SUCCESS) {
php_error(E_WARNING, "Constraint array is empty");
return NULL;
}
switch(Z_TYPE_PP(zdata)) {
2001-08-26 14:26:36 +00:00
case IS_STRING: /* constraints in plain string array */
if (_HASH(constr)->nNumOfElements%3) {
php_error(E_WARNING, "Constraint array has to have triples of strings");
return NULL;
}
do {
convert_to_string_ex(zdata);
dom = _STRING(zdata);
zend_hash_move_forward(_HASH(constr));
zend_hash_get_current_data(_HASH(constr), (void **)&zdata);
convert_to_string_ex(zdata);
op = _STRING(zdata);
zend_hash_move_forward(_HASH(constr));
zend_hash_get_current_data(_HASH(constr), (void **)&zdata);
convert_to_string_ex(zdata);
val = _STRING(zdata);
zend_hash_move_forward(_HASH(constr));
if (!(ap = (attribute *) attno (r, dom))) {
fprintf(stderr, "Invalid domain \"%s\"\n", dom);
return 0;
}
/* operator */
string_to_scalop(op, &sop);
/* value */
f = string_to_field(val, ap, 0);
(void) db_constrain(r, &c, dom, sop, f ? f : (field *) val);
} while(SUCCESS==zend_hash_get_current_data(_HASH(constr), (void **)&zdata));
break;
case IS_ARRAY:
{
zval **entry;
for(zend_hash_internal_pointer_reset(_HASH(constr));
SUCCESS==zend_hash_get_current_data(_HASH(constr), (void **)&zdata);
zend_hash_move_forward(_HASH(constr))) {
if(!(Z_TYPE_PP(zdata)==IS_ARRAY)) {
2001-08-26 14:26:36 +00:00
php_error(E_WARNING, "Constraint array element not an array");
return NULL;
}
if(_HASH(zdata)->nNumOfElements!=3) {
php_error(E_WARNING, "Constraint array element not an array of size 3");
return NULL;
}
zend_hash_internal_pointer_reset(_HASH(zdata));
zend_hash_get_current_data(_HASH(zdata), (void **)&entry);
convert_to_string_ex(entry);
dom=_STRING(entry);
zend_hash_move_forward(_HASH(zdata));
zend_hash_get_current_data(_HASH(zdata), (void **)&entry);
convert_to_string_ex(entry);
op=_STRING(entry);
zend_hash_move_forward(_HASH(zdata));
zend_hash_get_current_data(_HASH(zdata), (void **)&entry);
convert_to_string_ex(entry);
val=_STRING(entry);
if (!(ap = (attribute *) attno (r, dom))) {
fprintf(stderr, "Invalid domain \"%s\"\n", dom);
return 0;
}
/* operator */
string_to_scalop(op, &sop);
/* value */
f = string_to_field(val, ap, 0);
(void) db_constrain(r, &c, dom, sop, f ? f : (field *) val);
}
}
break;
default:
/* TODO error-handling */
return NULL;
}
return &c;
2001-07-06 00:04:03 +00:00
}
/* {{{ proto int dbplus_add(int relation, array tuple)
2001-12-30 07:29:11 +00:00
Adds a tuple to a relation */
2001-07-06 00:04:03 +00:00
PHP_FUNCTION(dbplus_add)
{
2001-09-16 15:18:03 +00:00
zval **relation, **data;
2001-08-26 14:26:36 +00:00
enum errorcond stat = ERR_UNKNOWN;
relf *r;
tuple t;
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &relation, &data) == FAILURE){
WRONG_PARAM_COUNT;
}
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
DBPLUS_FETCH_RESOURCE(r, relation);
convert_to_array_ex(data);
if(var2tuple(r, data, &t))
RETURN_LONG(ERR_UNKNOWN);
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
stat=cdb_add(r, &t);
if(stat==ERR_NOERR) {
tuple2var(r, &t, data);
}
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
RETURN_LONG(stat);
2001-07-06 00:04:03 +00:00
}
/* }}} */
2001-09-17 19:55:22 +00:00
/* {{{ proto resource dbplus_aql(string query [, string server [, string dbpath]])
2001-12-30 07:29:11 +00:00
Performs AQL query */
2001-07-06 00:04:03 +00:00
PHP_FUNCTION(dbplus_aql)
{
2001-08-26 14:26:36 +00:00
int argc;
zval **query, **server, **dbpath;
relf *r;
argc = ZEND_NUM_ARGS();
if (argc < 1 || argc > 3 || zend_get_parameters_ex(argc, &query, &server, &dbpath) == FAILURE){
WRONG_PARAM_COUNT;
}
switch (argc) {
case 3:
convert_to_string_ex(dbpath);
php_error(E_WARNING, "Arg dbpath: %s", _STRING(dbpath));
/* Fall-through. */
case 2:
convert_to_string_ex(server);
php_error(E_WARNING, "Arg server: %s", _STRING(server));
/* Fall-through. */
case 1:
convert_to_string_ex(query);
php_error(E_WARNING, "Arg query: %s", _STRING(query));
break;
}
r = cdb_aql((argc>=2)?_STRING(server):"localhost",
_STRING(query),
(argc==3)?_STRING(dbpath):NULL);
if(r == NULL) {
/* TODO error handling */
RETURN_FALSE;
}
ZEND_REGISTER_RESOURCE(return_value, r, le_dbplus_relation);
2001-07-06 00:04:03 +00:00
}
/* }}} */
/* {{{ proto string dbplus_chdir([string newdir])
2001-12-30 07:29:11 +00:00
Gets/Sets database virtual current directory */
2001-07-06 00:04:03 +00:00
PHP_FUNCTION(dbplus_chdir)
{
2001-08-26 14:26:36 +00:00
int argc;
char *p;
zval **newdir;
argc = ZEND_NUM_ARGS();
switch(argc) {
case 0:
break;
case 1:
if(zend_get_parameters_ex(1, &newdir) == FAILURE) {
WRONG_PARAM_COUNT;
} else {
convert_to_string_ex(newdir);
}
break;
default:
WRONG_PARAM_COUNT;
}
p = cdb_chdir((argc)?_STRING(newdir):NULL);
if(p) {
RETURN_STRING(p, 1);
} else {
RETURN_FALSE;
}
2001-07-06 00:04:03 +00:00
}
/* }}} */
/* {{{ proto int dbplus_close(int relation)
2001-12-30 07:29:11 +00:00
Closes a relation */
2001-07-06 00:04:03 +00:00
PHP_FUNCTION(dbplus_close)
{
2001-08-26 14:26:36 +00:00
relf *r;
zval **relation;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &relation) == FAILURE){
WRONG_PARAM_COUNT;
}
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
DBPLUS_FETCH_RESOURCE(r, relation);
2001-07-06 00:04:03 +00:00
zend_list_delete(Z_LVAL_PP(relation));
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
RETURN_TRUE;
2001-07-06 00:04:03 +00:00
}
/* }}} */
/* {{{ proto int dbplus_curr(int relation, array tuple)
2001-12-30 07:29:11 +00:00
Gets current tuple from relation */
2001-07-06 00:04:03 +00:00
PHP_FUNCTION(dbplus_curr)
{
2001-08-26 14:26:36 +00:00
zval **relation, **tname;
relf *r;
tuple t;
int stat;
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &relation, &tname) == FAILURE){
WRONG_PARAM_COUNT;
}
DBPLUS_FETCH_RESOURCE(r, relation);
stat = cdb_current(r, &t);
if(stat==ERR_NOERR) {
tuple2var(r, &t, tname);
}
RETURN_LONG(stat);
2001-07-06 00:04:03 +00:00
}
/* }}} */
/* {{{ proto string dbplus_errcode(int err)
2001-12-30 07:29:11 +00:00
Gets error string for given errorcode or last error */
2001-07-06 00:04:03 +00:00
PHP_FUNCTION(dbplus_errcode)
{
2001-08-26 14:26:36 +00:00
zval **err;
int errno;
switch (ZEND_NUM_ARGS()) {
case 0:
errno=-1;
break;
case 1:
if( zend_get_parameters_ex(1, &err) == FAILURE){
WRONG_PARAM_COUNT;
}
convert_to_long_ex(err);
errno = _INT(err);
}
if(errno==-1) errno = Acc_error;
RETURN_STRING(dbErrorMsg(errno, NULL), 1);
2001-07-06 00:04:03 +00:00
}
/* }}} */
/* {{{ proto int dbplus_errno(void)
2001-12-30 07:29:11 +00:00
Gets error code for last operation */
2001-07-06 00:04:03 +00:00
PHP_FUNCTION(dbplus_errno)
{
2001-08-26 14:26:36 +00:00
RETURN_LONG(Acc_error);
2001-07-06 00:04:03 +00:00
}
/* }}} */
/* {{{ proto int dbplus_find(int relation, array constr, mixed tuple)
2001-12-30 07:29:11 +00:00
Sets a constraint on a relation */
2001-07-06 00:04:03 +00:00
PHP_FUNCTION(dbplus_find)
{
2001-08-26 14:26:36 +00:00
relf *r;
zval **relation, **constr, **data;
constraint *c;
tuple t;
int stat;
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &relation, &constr, &data) == FAILURE){
WRONG_PARAM_COUNT;
}
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
DBPLUS_FETCH_RESOURCE(r, relation);
2001-07-06 00:04:03 +00:00
if (Z_TYPE_PP(constr) != IS_ARRAY) {
2001-08-26 14:26:36 +00:00
php_error(E_WARNING, "Constraint is not an array");
RETURN_LONG(ERR_UNKNOWN);
}
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
convert_to_array_ex(data);
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
c = ary2constr(r, constr);
if (!c){
RETURN_LONG(ERR_USER);
}
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
stat = cdb_find(r, &t, c);
if(stat==ERR_NOERR)
tuple2var(r, &t, data);
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
RETURN_LONG(stat);
2001-07-06 00:04:03 +00:00
}
/* }}} */
/* {{{ proto int dbplus_first(int relation, array tuple)
2001-12-30 07:29:11 +00:00
Gets first tuple from relation */
2001-07-06 00:04:03 +00:00
PHP_FUNCTION(dbplus_first)
{
2001-08-26 14:26:36 +00:00
zval **relation, **tname;
relf *r;
tuple t;
int stat;
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &relation, &tname) == FAILURE){
WRONG_PARAM_COUNT;
}
DBPLUS_FETCH_RESOURCE(r, relation);
stat = cdb_first(r, &t);
if(stat==ERR_NOERR) {
tuple2var(r, &t, tname);
}
RETURN_LONG(stat);
2001-07-06 00:04:03 +00:00
}
/* }}} */
/* {{{ proto int dbplus_flush(int relation)
??? */
PHP_FUNCTION(dbplus_flush)
{
2001-08-26 14:26:36 +00:00
relf *r;
zval **relation;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &relation) == FAILURE){
WRONG_PARAM_COUNT;
}
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
DBPLUS_FETCH_RESOURCE(r, relation);
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
RETURN_LONG(cdb_flush(r));
2001-07-06 00:04:03 +00:00
}
/* }}} */
/* {{{ proto int dbplus_freealllocks(void)
2001-12-30 07:29:11 +00:00
Frees all locks held by this client */
2001-07-06 00:04:03 +00:00
PHP_FUNCTION(dbplus_freealllocks)
{
2001-08-26 14:26:36 +00:00
RETURN_LONG(cdbFreeAllLocks());
2001-07-06 00:04:03 +00:00
}
/* }}} */
/* {{{ proto int dbplus_freelock(int relation, array tuple)
2001-12-30 07:29:11 +00:00
Releases write lock on tuple */
2001-07-06 00:04:03 +00:00
PHP_FUNCTION(dbplus_freelock)
{
2001-09-16 15:18:03 +00:00
zval **relation, **data;
2001-08-26 14:26:36 +00:00
enum errorcond stat = ERR_UNKNOWN;
relf *r;
tuple t;
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &relation, &data) == FAILURE){
WRONG_PARAM_COUNT;
}
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
DBPLUS_FETCH_RESOURCE(r, relation);
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
convert_to_array_ex(data);
if(var2tuple(r, data, &t))
RETURN_LONG(ERR_UNKNOWN);
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
stat=cdb_freelock(r, &t);
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
RETURN_LONG(stat);
2001-07-06 00:04:03 +00:00
}
/* }}} */
/* {{{ proto int dbplus_freerlocks(int relation)
2001-12-30 07:29:11 +00:00
Frees all locks on given relation */
2001-07-06 00:04:03 +00:00
PHP_FUNCTION(dbplus_freerlocks)
{
2001-08-26 14:26:36 +00:00
relf *r;
zval **relation;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &relation) == FAILURE){
WRONG_PARAM_COUNT;
}
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
DBPLUS_FETCH_RESOURCE(r, relation);
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
RETURN_LONG(cdb_freerlocks(r));
2001-07-06 00:04:03 +00:00
}
/* }}} */
/* {{{ proto int dbplus_getlock(int relation, array tuple)
2001-12-30 07:29:11 +00:00
Requests locking of tuple */
2001-07-06 00:04:03 +00:00
PHP_FUNCTION(dbplus_getlock)
{
2001-09-16 15:18:03 +00:00
zval **relation, **data;
2001-08-26 14:26:36 +00:00
enum errorcond stat = ERR_UNKNOWN;
relf *r;
tuple t;
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &relation, &data) == FAILURE){
WRONG_PARAM_COUNT;
}
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
DBPLUS_FETCH_RESOURCE(r, relation);
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
convert_to_array_ex(data);
if(var2tuple(r, data, &t))
RETURN_LONG(ERR_UNKNOWN);
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
stat=cdb_getlock(r, &t);
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
RETURN_LONG(stat);
2001-07-06 00:04:03 +00:00
}
/* }}} */
/* {{{ proto int dbplus_getunique(int handle, int uniqueid)
2001-12-30 07:29:11 +00:00
Gets a id number unique to a relation */
2001-07-06 00:04:03 +00:00
PHP_FUNCTION(dbplus_getunique)
{
2001-08-26 14:26:36 +00:00
relf *r;
zval **relation, **uniqueid;
long l;
int stat;
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &relation, &uniqueid) == FAILURE){
WRONG_PARAM_COUNT;
}
DBPLUS_FETCH_RESOURCE(r, relation);
stat = cdb_getunique(r, &l, 1);
if(!stat) {
ZVAL_LONG(*uniqueid,l);
}
RETURN_LONG(stat);
2001-07-06 00:04:03 +00:00
}
/* }}} */
/* {{{ proto int dbplus_info(int relation, string key, array &result)
??? */
PHP_FUNCTION(dbplus_info)
{
2001-08-26 14:26:36 +00:00
zval **relation, **key, **result, *element;
relf *r;
register attribute *ap;
unsigned deg;
if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &relation, &key, &result) == FAILURE){
WRONG_PARAM_COUNT;
}
DBPLUS_FETCH_RESOURCE(r, relation);
ap = r->r_atts;
deg = r->r_rel.rel_deg;
convert_to_string_ex(key);
zval_dtor(*result);
if (array_init(*result) == FAILURE)
RETURN_LONG(ERR_USER);
if(!strcmp("atts", Z_STRVAL_PP(key))) {
2001-08-26 14:26:36 +00:00
do {
MAKE_STD_ZVAL(element);
ZVAL_STRING(element, ap->att_name, 1);
zend_hash_update(Z_ARRVAL_PP(result),
2001-08-26 14:26:36 +00:00
ap->att_name,
strlen(ap->att_name)+1,
(void *)&element,
sizeof(zval*),
NULL);
} while (ap++, deg--);
RETURN_LONG(ERR_NOERR);
}
RETURN_LONG(ERR_USER);
2001-07-06 00:04:03 +00:00
}
/* }}} */
/* {{{ proto int dbplus_last(int relation, array tuple)
2001-12-30 07:29:11 +00:00
Gets last tuple from relation */
2001-07-06 00:04:03 +00:00
PHP_FUNCTION(dbplus_last)
{
2001-08-26 14:26:36 +00:00
zval **relation, **tname;
relf *r;
tuple t;
int stat;
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &relation, &tname) == FAILURE){
WRONG_PARAM_COUNT;
}
DBPLUS_FETCH_RESOURCE(r, relation);
stat = cdb_last(r, &t);
if(stat==ERR_NOERR) {
tuple2var(r, &t, tname);
}
RETURN_LONG(stat);
2001-07-06 00:04:03 +00:00
}
/* }}} */
/* {{{ proto int dbplus_lockrel(int relation)
2001-12-30 07:29:11 +00:00
Requests write lock on relation */
2001-07-06 00:04:03 +00:00
PHP_FUNCTION(dbplus_lockrel)
{
2001-08-26 14:26:36 +00:00
relf *r;
zval **relation;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &relation) == FAILURE){
WRONG_PARAM_COUNT;
}
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
DBPLUS_FETCH_RESOURCE(r, relation);
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
RETURN_LONG(cdb_lockrel(r));
2001-07-06 00:04:03 +00:00
}
/* }}} */
/* {{{ proto int dbplus_next(int relation, array &tname)
2001-12-30 07:29:11 +00:00
Gets next tuple from relation */
2001-07-06 00:04:03 +00:00
PHP_FUNCTION(dbplus_next)
{
2001-09-16 15:18:03 +00:00
zval **relation, **tname;
2001-08-26 14:26:36 +00:00
relf *r;
tuple *t;
2001-08-26 14:26:36 +00:00
int stat;
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &relation, &tname) == FAILURE){
WRONG_PARAM_COUNT;
}
DBPLUS_FETCH_RESOURCE(r, relation);
if(r->r_sid = 9999) {
t = rnext(r);
stat = Acc_error;
} else {
t = (tuple *) pmalloc(sizeof(tuple));
stat = cdb_next(r, t);
}
2001-08-26 14:26:36 +00:00
if(stat==ERR_NOERR) {
tuple2var(r, t, tname);
2001-08-26 14:26:36 +00:00
}
RETURN_LONG(stat);
2001-07-06 00:04:03 +00:00
}
/* }}} */
2001-09-17 19:55:22 +00:00
/* {{{ proto resource dbplus_open(string name)
2001-12-30 07:29:11 +00:00
Opens a relation file */
2001-07-06 00:04:03 +00:00
PHP_FUNCTION(dbplus_open)
{
2001-08-26 14:26:36 +00:00
relf *r;
zval **tname;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &tname) == FAILURE){
WRONG_PARAM_COUNT;
}
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
convert_to_string_ex(tname);
2001-07-06 00:04:03 +00:00
r = cdb_open(Z_STRVAL_PP(tname), 1, 1);
2001-08-26 14:26:36 +00:00
if(r == NULL) {
/* TODO error handling */
RETURN_FALSE;
}
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
ZEND_REGISTER_RESOURCE(return_value, r, le_dbplus_relation);
2001-07-06 00:04:03 +00:00
}
/* }}} */
/* {{{ proto int dbplus_prev(int relation, array tuple)
2001-12-30 07:29:11 +00:00
Gets previous tuple from relation */
2001-07-06 00:04:03 +00:00
PHP_FUNCTION(dbplus_prev)
{
2001-08-26 14:26:36 +00:00
zval **relation, **tname;
relf *r;
tuple t;
int stat;
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &relation, &tname) == FAILURE){
WRONG_PARAM_COUNT;
}
DBPLUS_FETCH_RESOURCE(r, relation);
stat = cdb_previous(r, &t);
if(stat==ERR_NOERR) {
tuple2var(r, &t, tname);
}
RETURN_LONG(stat);
2001-07-06 00:04:03 +00:00
}
/* }}} */
/* {{{ proto int dbplus_rchperm(int relation, int mask, string user, string group)
2001-12-30 07:29:11 +00:00
??? */
2001-07-06 00:04:03 +00:00
PHP_FUNCTION(dbplus_rchperm)
{
2001-08-26 14:26:36 +00:00
relf *r;
zval **relation, **mask, **user, **group;
if (ZEND_NUM_ARGS() != 4 || zend_get_parameters_ex(4, &relation, &mask, &user, &group) == FAILURE){
WRONG_PARAM_COUNT;
}
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
DBPLUS_FETCH_RESOURCE(r, relation);
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
convert_to_long_ex(mask);
convert_to_string_ex(user);
convert_to_string_ex(group);
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
RETURN_LONG(cdbRchperm(r, _INT(mask), _STRING(user), _STRING(group)));
2001-07-06 00:04:03 +00:00
}
/* }}} */
2001-09-17 19:55:22 +00:00
/* {{{ proto resource dbplus_rcreate(string name, mixed domlist [, int overwrite])
2001-12-30 07:29:11 +00:00
Creates a new DB++ relation */
PHP_FUNCTION(dbplus_rcreate)
{
2001-08-26 14:26:36 +00:00
zval **name, **domlist, **overwrite;
relf *r=NULL;
int flag, ndoms, argc = ZEND_NUM_ARGS();
attdef *at0;
switch(argc) {
case 3:
if(zend_get_parameters_ex(3, &name, &domlist, &overwrite) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long_ex(overwrite);
flag=_INT(overwrite);
break;
case 2:
if(zend_get_parameters_ex(3, &name, &domlist) == FAILURE) {
WRONG_PARAM_COUNT;
}
flag=0;
break;
default:
WRONG_PARAM_COUNT;
break;
}
convert_to_string_ex(name);
switch ( Z_TYPE_PP(domlist) ) {
case IS_STRING:
convert_to_string_ex(domlist);
break;
case IS_ARRAY:
{
zval tmp;
ZVAL_STRING(&tmp," ",0);
php_implode(&tmp,*domlist,*domlist);
}
break;
default:
}
2001-08-26 14:26:36 +00:00
at0 = create2att(_STRING(domlist), &ndoms);
if (at0) {
r = cdbRcreate(_STRING(name), 0666, 0, ndoms, at0, flag);
dbxfree((char *) at0);
}
if(r == NULL) {
/* TODO error handling */
RETURN_FALSE;
}
ZEND_REGISTER_RESOURCE(return_value, r, le_dbplus_relation);
}
/* }}} */
2001-09-17 19:55:22 +00:00
/* {{{ proto resource dbplus_rcrtexact(string name, resource relation [, boolean overwrite])
2001-12-30 07:29:11 +00:00
Creates an exact but empty copy of a relation including indices */
PHP_FUNCTION(dbplus_rcrtexact)
{
2001-08-26 14:26:36 +00:00
zval **name, **relation, **overwrite;
relf *r;
int f,argc = ZEND_NUM_ARGS();
switch(argc) {
case 3:
if(zend_get_parameters_ex(3, &name, &relation, &overwrite) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long_ex(overwrite);
f=_INT(overwrite);
break;
case 2:
if(zend_get_parameters_ex(3, &name, &relation) == FAILURE) {
WRONG_PARAM_COUNT;
}
f=0;
break;
default:
WRONG_PARAM_COUNT;
break;
}
convert_to_string_ex(name);
DBPLUS_FETCH_RESOURCE(r, relation);
r = cdbRcrtexact(_STRING(name), 0666, r, f);
if(r == NULL) {
/* TODO error handling */
RETURN_FALSE;
}
ZEND_REGISTER_RESOURCE(return_value, r, le_dbplus_relation);
}
/* }}} */
2001-09-17 19:55:22 +00:00
/* {{{ proto resource dbplus_rcrtlike(string name, int handle [, int overwrite])
2001-12-30 07:29:11 +00:00
Creates an empty copy of a relation with default indices */
PHP_FUNCTION(dbplus_rcrtlike)
{
2001-08-26 14:26:36 +00:00
zval **name, **relation, **overwrite;
relf *r;
int f,argc = ZEND_NUM_ARGS();
switch(argc) {
case 3:
if(zend_get_parameters_ex(3, &name, &relation, &overwrite) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long_ex(overwrite);
f=_INT(overwrite);
break;
case 2:
if(zend_get_parameters_ex(3, &name, &relation) == FAILURE) {
WRONG_PARAM_COUNT;
}
f=0;
break;
default:
WRONG_PARAM_COUNT;
break;
}
convert_to_string_ex(name);
DBPLUS_FETCH_RESOURCE(r, relation);
r = cdbRcrtlike(_STRING(name), 0666, 0, r, f);
if(r == NULL) {
/* TODO error handling */
RETURN_FALSE;
}
ZEND_REGISTER_RESOURCE(return_value, r, le_dbplus_relation);
}
/* }}} */
/* {{{ proto int dbplus_resolve(string name)
2001-12-30 07:29:11 +00:00
Resolves host information for relation */
PHP_FUNCTION(dbplus_resolve)
{
2001-08-26 14:26:36 +00:00
zval **name, *element;
char * host;
char * host_path;
int sid;
2001-08-26 14:26:36 +00:00
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &name) == FAILURE){
WRONG_PARAM_COUNT;
}
2001-08-26 14:26:36 +00:00
convert_to_string_ex(name);
2001-08-26 14:26:36 +00:00
sid = cdb_resolve(_STRING(name), &host, &host_path);
if (sid <= 0)
RETURN_FALSE;
2001-08-26 14:26:36 +00:00
if (array_init(return_value) == FAILURE) {
RETURN_FALSE;
}
MAKE_STD_ZVAL(element); Z_TYPE_P(element)=IS_NULL;
2001-08-26 14:26:36 +00:00
ZVAL_LONG(element,sid);
zend_hash_update(Z_ARRVAL_P(return_value), "sid", 4,
&element, sizeof(zval *), NULL);
MAKE_STD_ZVAL(element); Z_TYPE_P(element)=IS_NULL;
2001-08-26 14:26:36 +00:00
ZVAL_STRING(element,host,1);
zend_hash_update(Z_ARRVAL_P(return_value), "host", 5,
&element, sizeof(zval *), NULL);
MAKE_STD_ZVAL(element); Z_TYPE_P(element)=IS_NULL;
2001-08-26 14:26:36 +00:00
ZVAL_STRING(element,host_path,1);
zend_hash_update(Z_ARRVAL_P(return_value), "host_path", 10,
&element, sizeof(zval *), NULL);
}
/* }}} */
2001-07-06 00:04:03 +00:00
/* {{{ proto int dbplus_restorepos(int relation, array tuple)
??? */
PHP_FUNCTION(dbplus_restorepos)
{
2001-08-26 14:26:36 +00:00
zval **relation, **tname;
relf *r;
tuple t;
int stat;
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &relation, &tname) == FAILURE){
WRONG_PARAM_COUNT;
}
DBPLUS_FETCH_RESOURCE(r, relation);
stat = cdb_next(r, &t);
if(stat==ERR_NOERR) {
tuple2var(r, &t, tname);
}
RETURN_LONG(stat);
2001-07-06 00:04:03 +00:00
}
/* }}} */
2001-09-17 19:55:22 +00:00
/* {{{ proto resource dbplus_rkeys(resource relation, mixed domlist)
2001-12-30 07:29:11 +00:00
Defines primary key for relation
2001-09-17 19:55:22 +00:00
*/
2001-07-06 00:04:03 +00:00
PHP_FUNCTION(dbplus_rkeys)
{
2001-08-26 14:26:36 +00:00
relf *r, *rnew;
zval **relation, **domlist, **zdata;
int nkeys=0;
char *name=NULL, *keys[40]; /* TODO hardcoded magic number ??? */
2001-08-26 14:26:36 +00:00
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &relation, &domlist) == FAILURE){
WRONG_PARAM_COUNT;
}
DBPLUS_FETCH_RESOURCE(r, relation);
switch(Z_TYPE_PP(domlist)) {
case IS_ARRAY:
2001-08-26 14:26:36 +00:00
convert_to_array_ex(domlist);
for(zend_hash_internal_pointer_reset(_HASH(domlist));
SUCCESS==zend_hash_get_current_data(_HASH(domlist), (void **)&zdata);
zend_hash_move_forward(_HASH(domlist))) {
if(Z_TYPE_PP(zdata)==IS_STRING)
2001-08-26 14:26:36 +00:00
keys[nkeys++] = _STRING(zdata);
else {
php_error(E_WARNING, "dbplus_rkeys: domlist array contains non-string value(s)");
2001-08-26 14:26:36 +00:00
Acc_error = ERR_USER;
RETURN_FALSE;
}
}
break;
case IS_STRING:
2001-08-26 14:26:36 +00:00
convert_to_string_ex(domlist);
keys[0] = _STRING(domlist);
nkeys = 1;
break;
default:
php_error(E_WARNING, "dbplus_rkeys: domlist has to be of type string or an array of strings");
Acc_error = ERR_USER;
RETURN_FALSE;
2001-08-26 14:26:36 +00:00
}
2001-08-26 14:26:36 +00:00
rnew = cdbRkeys(r, nkeys, keys);
if(name) efree(name);
if(rnew) {
/* TODO realy delete old relation resource ? */
#if 0
zend_list_delete(Z_LVAL_PP(relation));
#endif
2001-08-26 14:26:36 +00:00
ZEND_REGISTER_RESOURCE(return_value, rnew, le_dbplus_relation);
} else {
/* TODO error reporting */
RETURN_FALSE;
}
2001-07-06 00:04:03 +00:00
}
/* }}} */
2001-09-17 19:55:22 +00:00
/* {{{ proto resource dbplus_ropen(string name)
2001-12-30 07:29:11 +00:00
Opens relation file local */
2001-07-06 00:04:03 +00:00
PHP_FUNCTION(dbplus_ropen)
{
2001-08-26 14:26:36 +00:00
relf *r;
zval **tname;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &tname) == FAILURE){
WRONG_PARAM_COUNT;
}
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
convert_to_string_ex(tname);
2001-07-06 00:04:03 +00:00
r = ropen(Z_STRVAL_PP(tname), 0, 0);
2001-08-26 14:26:36 +00:00
if(r == NULL) {
/* TODO error handling */
RETURN_FALSE;
}
2001-07-06 00:04:03 +00:00
2001-09-21 21:57:41 +00:00
r->r_sid = 9999;
2001-08-26 14:26:36 +00:00
ZEND_REGISTER_RESOURCE(return_value, r, le_dbplus_relation);
2001-07-06 00:04:03 +00:00
}
/* }}} */
2001-12-30 07:29:11 +00:00
/* {{{ proto resource dbplus_rquery(string name, string dbpath)
??? */
2001-07-06 00:04:03 +00:00
PHP_FUNCTION(dbplus_rquery)
{
2001-08-26 14:26:36 +00:00
relf *r;
zval **name, **dbpath;
int argc = ZEND_NUM_ARGS();
2001-08-26 14:26:36 +00:00
2001-09-18 20:42:37 +00:00
if ((argc <1) || (argc>2) || (zend_get_parameters_ex(2, &name, &dbpath) == FAILURE)){
2001-08-26 14:26:36 +00:00
WRONG_PARAM_COUNT;
}
r = aql_exec(_STRING(name), (argc==2)?_STRING(dbpath):NULL);
if(!r) {
/* TODO error handling */
RETURN_FALSE;
}
r->r_sid = 9999;
2001-08-26 14:26:36 +00:00
ZEND_REGISTER_RESOURCE(return_value, r, le_dbplus_relation);
2001-07-06 00:04:03 +00:00
}
/* }}} */
/* {{{ proto int dbplus_rrename(int relation, string name)
2001-12-30 07:29:11 +00:00
??? */
2001-07-06 00:04:03 +00:00
PHP_FUNCTION(dbplus_rrename)
{
2001-08-26 14:26:36 +00:00
relf *r;
zval **relation, **name;
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &relation, &name) == FAILURE){
WRONG_PARAM_COUNT;
}
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
DBPLUS_FETCH_RESOURCE(r, relation);
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
convert_to_string_ex(name);
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
RETURN_LONG(cdbRrename(r, _STRING(name), 0));
2001-07-06 00:04:03 +00:00
}
/* }}} */
2001-09-17 19:55:22 +00:00
/* {{{ proto resource dbplus_rsecindex(resource relation, mixed domlist, int compact)
2001-12-30 07:29:11 +00:00
Creates an additional index on relation */
2001-07-06 00:04:03 +00:00
PHP_FUNCTION(dbplus_rsecindex)
{
2001-08-26 14:26:36 +00:00
relf *r, *rnew;
zval **relation, **domlist, **compact, **zdata;
int nkeys=0;
char *name=NULL, *keys[40]; /* TODO hardcoded magic number ??? */
2001-08-26 14:26:36 +00:00
if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &relation, &domlist, &compact) == FAILURE){
WRONG_PARAM_COUNT;
}
DBPLUS_FETCH_RESOURCE(r, relation);
switch ( Z_TYPE_PP(domlist) ) {
case IS_ARRAY:
2001-08-26 14:26:36 +00:00
convert_to_array_ex(domlist);
for(zend_hash_internal_pointer_reset(_HASH(domlist));
SUCCESS==zend_hash_get_current_data(_HASH(domlist), (void **)&zdata);
zend_hash_move_forward(_HASH(domlist))) {
if(Z_TYPE_PP(zdata)==IS_STRING)
2001-08-26 14:26:36 +00:00
keys[nkeys++] = _STRING(zdata);
else {
php_error(E_WARNING, "dbplus_rsecindex: domlist array contains non-string value(s)");
2001-08-26 14:26:36 +00:00
Acc_error = ERR_USER;
RETURN_FALSE;
}
}
break;
case IS_STRING:
2001-08-26 14:26:36 +00:00
convert_to_string_ex(domlist);
keys[0] = _STRING(domlist);
nkeys = 1;
break;
default:
php_error(E_WARNING, "dbplus_rsecindex: domlist has to be of type string or an array of strings");
Acc_error = ERR_USER;
RETURN_FALSE;
2001-08-26 14:26:36 +00:00
}
convert_to_long_ex(compact);
rnew = cdbRsecindex(r, nkeys, keys, _INT(compact));
if(name) efree(name);
if(rnew) {
/* TODO realy delete old relation resource ? */
zend_list_delete(Z_LVAL_PP(relation));
2001-08-26 14:26:36 +00:00
ZEND_REGISTER_RESOURCE(return_value, rnew, le_dbplus_relation);
} else {
/* TODO error reporting */
RETURN_FALSE;
}
2001-07-06 00:04:03 +00:00
}
/* }}} */
/* {{{ proto int dbplus_runlink(int relation)
2001-12-30 07:29:11 +00:00
Removes relation from filesystem */
2001-07-06 00:04:03 +00:00
PHP_FUNCTION(dbplus_runlink)
{
2001-08-26 14:26:36 +00:00
relf *r;
zval **relation;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &relation) == FAILURE){
WRONG_PARAM_COUNT;
}
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
DBPLUS_FETCH_RESOURCE(r, relation);
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
RETURN_LONG(cdbRunlink(&r));
2001-07-06 00:04:03 +00:00
}
/* }}} */
/* {{{ proto int dbplus_rzap(int relation, int truncate)
2001-12-30 07:29:11 +00:00
Removes all tuples from relation */
2001-07-06 00:04:03 +00:00
PHP_FUNCTION(dbplus_rzap)
{
2001-08-26 14:26:36 +00:00
/* todo: optional argument */
relf *r;
2001-09-16 15:18:03 +00:00
zval **relation;
2001-08-26 14:26:36 +00:00
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &relation) == FAILURE){
WRONG_PARAM_COUNT;
}
2001-08-26 14:26:36 +00:00
DBPLUS_FETCH_RESOURCE(r, relation);
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
RETURN_LONG(cdbRzap(r, 1));
2001-07-06 00:04:03 +00:00
}
/* }}} */
/* {{{ proto int dbplus_savepos(int relation)
??? */
PHP_FUNCTION(dbplus_savepos)
{
2001-08-26 14:26:36 +00:00
relf *r;
zval **relation;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &relation) == FAILURE){
WRONG_PARAM_COUNT;
}
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
DBPLUS_FETCH_RESOURCE(r, relation);
2001-07-06 00:04:03 +00:00
RETURN_LONG(cdb_savepos(r));
}
/* }}} */
/* {{{ proto int dbplus_setindex(int relation, string idx_name)
2001-12-30 07:29:11 +00:00
??? */
2001-07-06 00:04:03 +00:00
PHP_FUNCTION(dbplus_setindex)
{
2001-08-26 14:26:36 +00:00
relf *r;
zval **relation, **idx_name;
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &relation, &idx_name) == FAILURE){
WRONG_PARAM_COUNT;
}
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
DBPLUS_FETCH_RESOURCE(r, relation);
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
convert_to_string_ex(idx_name);
RETURN_LONG(cdb_setindex(r, _STRING(idx_name)));
2001-07-06 00:04:03 +00:00
}
/* }}} */
/* {{{ proto int dbplus_setindexbynumber(int relation, int idx_number)
??? */
PHP_FUNCTION(dbplus_setindexbynumber)
{
2001-08-26 14:26:36 +00:00
relf *r;
zval **relation, **idx_number;
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &relation, &idx_number) == FAILURE){
WRONG_PARAM_COUNT;
}
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
DBPLUS_FETCH_RESOURCE(r, relation);
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
convert_to_long_ex(idx_number);
RETURN_LONG(cdb_setindexbynumber(r, Z_LVAL_PP(idx_number)));
2001-07-06 00:04:03 +00:00
}
/* }}} */
2001-09-17 19:55:22 +00:00
/* {{{ proto resource dbplus_sql(string query, string server, string dbpath)
2001-12-30 07:29:11 +00:00
Performs SQL query */
2001-07-06 00:04:03 +00:00
PHP_FUNCTION(dbplus_sql)
{
2001-08-26 14:26:36 +00:00
int argc;
zval **query, **server, **dbpath;
relf *r;
argc = ZEND_NUM_ARGS();
if (argc < 1 || argc > 3 || zend_get_parameters_ex(argc, &query, &server, &dbpath) == FAILURE){
WRONG_PARAM_COUNT;
}
switch (argc) {
case 3:
convert_to_string_ex(dbpath);
php_error(E_WARNING, "Arg dbpath: %s", _STRING(dbpath));
/* Fall-through. */
case 2:
convert_to_string_ex(server);
php_error(E_WARNING, "Arg server: %s", _STRING(server));
/* Fall-through. */
case 1:
convert_to_string_ex(query);
php_error(E_WARNING, "Arg query: %s", _STRING(query));
break;
}
r = cdb_sql((argc>=2)?_STRING(server):"localhost",
_STRING(query),
(argc==3)?_STRING(dbpath):NULL);
if(r == NULL) {
/* TODO error handling */
RETURN_FALSE;
}
ZEND_REGISTER_RESOURCE(return_value, r, le_dbplus_relation);
2001-07-06 00:04:03 +00:00
}
/* }}} */
/* {{{ proto string dbplus_tcl(int sid, string script)
2001-12-30 07:29:11 +00:00
Executes server side TCL code */
PHP_FUNCTION(dbplus_tcl)
{
2001-08-26 14:26:36 +00:00
zval **sid, **script;
char *ret;
int result_type;
2001-08-26 14:26:36 +00:00
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &sid, &script) == FAILURE){
WRONG_PARAM_COUNT;
}
2001-08-26 14:26:36 +00:00
convert_to_long_ex(sid);
convert_to_string_ex(script);
2001-08-26 14:26:36 +00:00
cdb_tcl(_INT(sid),_STRING(script),&ret,&result_type);
2001-08-26 14:26:36 +00:00
if(ret) {
RETURN_STRING(ret,1);
} else {
RETURN_FALSE;
}
}
/* }}} */
2001-07-06 00:04:03 +00:00
/* {{{ proto int dbplus_tremove(int relation, array old [, array current])
2001-12-30 07:29:11 +00:00
Removes tuple and return new current tuple */
2001-07-06 00:04:03 +00:00
PHP_FUNCTION(dbplus_tremove)
{
2001-08-26 14:26:36 +00:00
zval **relation, **old, **current;
enum errorcond stat = ERR_UNKNOWN;
relf *r;
tuple told, tcurr;
int argc;
argc = ZEND_NUM_ARGS();
if ( argc<2 || argc>3 || zend_get_parameters_ex(2, &relation, &old, &current) == FAILURE){
WRONG_PARAM_COUNT;
}
DBPLUS_FETCH_RESOURCE(r, relation);
convert_to_array_ex(old);
if(var2tuple(r, old, &told))
RETURN_LONG(ERR_UNKNOWN);
stat=cdbTremove(r, &told, &tcurr);
if((stat==ERR_NOERR) && (argc==3))
tuple2var(r, &tcurr, current);
RETURN_LONG(stat);
2001-07-06 00:04:03 +00:00
}
/* }}} */
/* {{{ proto int dbplus_undo(int relation)
??? */
PHP_FUNCTION(dbplus_undo)
{
2001-08-26 14:26:36 +00:00
relf *r;
zval **relation;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &relation) == FAILURE){
WRONG_PARAM_COUNT;
}
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
DBPLUS_FETCH_RESOURCE(r, relation);
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
RETURN_LONG(cdb_undo(r));
2001-07-06 00:04:03 +00:00
}
/* }}} */
/* {{{ proto int dbplus_undoprepare(int relation)
??? */
PHP_FUNCTION(dbplus_undoprepare)
{
2001-08-26 14:26:36 +00:00
relf *r;
zval **relation;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &relation) == FAILURE){
WRONG_PARAM_COUNT;
}
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
DBPLUS_FETCH_RESOURCE(r, relation);
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
RETURN_LONG(cdb_undoprepare(r));
2001-07-06 00:04:03 +00:00
}
/* }}} */
/* {{{ proto int dbplus_unlockrel(int relation)
2001-12-30 07:29:11 +00:00
Gives up write lock on relation */
2001-07-06 00:04:03 +00:00
PHP_FUNCTION(dbplus_unlockrel)
{
2001-08-26 14:26:36 +00:00
relf *r;
zval **relation;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &relation) == FAILURE){
WRONG_PARAM_COUNT;
}
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
DBPLUS_FETCH_RESOURCE(r, relation);
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
RETURN_LONG(cdb_unlockrel(r));
2001-07-06 00:04:03 +00:00
}
/* }}} */
/* {{{ proto int dbplus_unselect(int relation)
2001-12-30 07:29:11 +00:00
Removes constraint from relation */
2001-07-06 00:04:03 +00:00
PHP_FUNCTION(dbplus_unselect)
{
2001-08-26 14:26:36 +00:00
relf *r;
zval **relation;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &relation) == FAILURE){
WRONG_PARAM_COUNT;
}
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
DBPLUS_FETCH_RESOURCE(r, relation);
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
RETURN_LONG(cdb_unselect(r));
2001-07-06 00:04:03 +00:00
}
/* }}} */
/* {{{ proto int dbplus_update(int relation, array old, array new)
2001-12-30 07:29:11 +00:00
Updates specified tuple in relation */
2001-07-06 00:04:03 +00:00
PHP_FUNCTION(dbplus_update)
{
2001-08-26 14:26:36 +00:00
zval **relation, **old, **new;
enum errorcond stat = ERR_UNKNOWN;
relf *r;
tuple told, tnew;
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &relation, &old, &new) == FAILURE){
WRONG_PARAM_COUNT;
}
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
DBPLUS_FETCH_RESOURCE(r, relation);
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
convert_to_array_ex(old);
convert_to_array_ex(new);
if(var2tuple(r, old, &told))
RETURN_LONG(ERR_UNKNOWN);
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
if(var2tuple(r, new, &tnew))
RETURN_LONG(ERR_UNKNOWN);
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
stat=cdb_update(r, &told, &tnew);
RETURN_LONG(stat);
2001-07-06 00:04:03 +00:00
}
/* }}} */
/* {{{ proto int dbplus_xlockrel(int relation)
2001-12-30 07:29:11 +00:00
Requests exclusive lock on relation */
2001-07-06 00:04:03 +00:00
PHP_FUNCTION(dbplus_xlockrel)
{
2001-08-26 14:26:36 +00:00
relf *r;
zval **relation;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &relation) == FAILURE){
WRONG_PARAM_COUNT;
}
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
DBPLUS_FETCH_RESOURCE(r, relation);
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
RETURN_LONG(cdb_xlockrel(r));
2001-07-06 00:04:03 +00:00
}
/* }}} */
/* {{{ proto int dbplus_xunlockrel(int relation)
2001-12-30 07:29:11 +00:00
Frees exclusive lock on relation */
2001-07-06 00:04:03 +00:00
PHP_FUNCTION(dbplus_xunlockrel)
{
2001-08-26 14:26:36 +00:00
relf *r;
zval **relation;
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &relation) == FAILURE){
WRONG_PARAM_COUNT;
}
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
DBPLUS_FETCH_RESOURCE(r, relation);
2001-07-06 00:04:03 +00:00
2001-08-26 14:26:36 +00:00
RETURN_LONG(cdb_xunlockrel(r));
2001-07-06 00:04:03 +00:00
}
/* }}} */
/*
* Local variables:
* tab-width: 2
* c-basic-offset: 2
* End:
*/