diff options
-rw-r--r-- | ext/dba/dba.c | 38 | ||||
-rw-r--r-- | ext/dba/dba_cdb.c | 6 | ||||
-rw-r--r-- | ext/dba/php_dba.h | 2 | ||||
-rw-r--r-- | ext/dba/tests/dba_cdb_read.phpt | 44 |
4 files changed, 73 insertions, 17 deletions
diff --git a/ext/dba/dba.c b/ext/dba/dba.c index 63a32bb705..94fa8894ff 100644 --- a/ext/dba/dba.c +++ b/ext/dba/dba.c @@ -81,7 +81,7 @@ typedef struct dba_handler { char *name; int (*open)(dba_info * TSRMLS_DC); void (*close)(dba_info *); - char* (*fetch)(dba_info *, char *, int, int *); + char* (*fetch)(dba_info *, char *, int, int, int *); int (*update)(dba_info *, char *, int, char *, int, int); int (*exists)(dba_info *, char *, int); int (*delete)(dba_info *, char *, int); @@ -112,11 +112,34 @@ typedef struct dba_handler { } \ convert_to_string_ex(key) +#define DBA_GET2_3 \ + zval **key; \ + zval **tmp; \ + int skip = 0; \ + switch(ac) { \ + case 2: \ + if (zend_get_parameters_ex(ac, &key, &id) != SUCCESS) { \ + WRONG_PARAM_COUNT; \ + } \ + break; \ + case 3: \ + if (zend_get_parameters_ex(ac, &key, &tmp, &id) != SUCCESS) { \ + WRONG_PARAM_COUNT; \ + } \ + convert_to_long_ex(tmp); \ + skip = Z_LVAL_PP(tmp); \ + break; \ + default: \ + WRONG_PARAM_COUNT; \ + } \ + convert_to_string_ex(key) + #define DBA_ID_GET \ ZEND_FETCH_RESOURCE2(info, dba_info *, id, -1, "DBA identifier", le_db, le_pdb); -#define DBA_ID_GET1 DBA_ID_PARS; DBA_GET1; DBA_ID_GET -#define DBA_ID_GET2 DBA_ID_PARS; DBA_GET2; DBA_ID_GET +#define DBA_ID_GET1 DBA_ID_PARS; DBA_GET1; DBA_ID_GET +#define DBA_ID_GET2 DBA_ID_PARS; DBA_GET2; DBA_ID_GET +#define DBA_ID_GET2_3 DBA_ID_PARS; DBA_GET2_3; DBA_ID_GET /* a DBA handler must have specific routines */ @@ -412,15 +435,18 @@ PHP_FUNCTION(dba_exists) } /* }}} */ -/* {{{ proto string dba_fetch(string key, int handle) +/* {{{ proto string dba_fetch(string key, [int skip ,] int handle) Fetches the data associated with key */ PHP_FUNCTION(dba_fetch) { char *val; int len = 0; - DBA_ID_GET2; + DBA_ID_GET2_3; - if((val = info->hnd->fetch(info, VALLEN(key), &len)) != NULL) { + if (ac==3 && strcmp(info->hnd->name, "cdb")) { + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Handler %s does not support optional skip parameter", info->hnd->name); + } + if((val = info->hnd->fetch(info, VALLEN(key), skip, &len)) != NULL) { RETURN_STRINGL(val, len, 0); } RETURN_FALSE; diff --git a/ext/dba/dba_cdb.c b/ext/dba/dba_cdb.c index e4b2a77de4..37b525135d 100644 --- a/ext/dba/dba_cdb.c +++ b/ext/dba/dba_cdb.c @@ -95,7 +95,13 @@ DBA_FETCH_FUNC(cdb) unsigned int len; char *new_entry = NULL; +// cdb_findstart(&cdb->c); if (cdb_find(&cdb->c, key, keylen) == 1) { + while(skip--) { + if (cdb_findnext(&cdb->c, key, keylen) != 1) { + return NULL; + } + } len = cdb_datalen(&cdb->c); new_entry = emalloc(len+1); diff --git a/ext/dba/php_dba.h b/ext/dba/php_dba.h index 832e66638c..886ccbea0b 100644 --- a/ext/dba/php_dba.h +++ b/ext/dba/php_dba.h @@ -52,7 +52,7 @@ extern zend_module_entry dba_module_entry; #define DBA_CLOSE_FUNC(x) \ void dba_close_##x(dba_info *info) #define DBA_FETCH_FUNC(x) \ - char *dba_fetch_##x(dba_info *info, char *key, int keylen, int *newlen) + char *dba_fetch_##x(dba_info *info, char *key, int keylen, int skip, int *newlen) #define DBA_UPDATE_FUNC(x) \ int dba_update_##x(dba_info *info, char *key, int keylen, char *val, int vallen, int mode) #define DBA_EXISTS_FUNC(x) \ diff --git a/ext/dba/tests/dba_cdb_read.phpt b/ext/dba/tests/dba_cdb_read.phpt index 6ead2f34d9..045de9574a 100644 --- a/ext/dba/tests/dba_cdb_read.phpt +++ b/ext/dba/tests/dba_cdb_read.phpt @@ -11,17 +11,18 @@ DBA CDB handler test (read only) $handler = 'cdb'; $db_file = dirname(__FILE__).'/test.cdb'; if (($db_file=dba_open($db_file, "r", $handler))!==FALSE) { + // read key sequence $a = dba_firstkey($db_file); - $i=0; - echo "?$a"; - while($a) { + $count= 0; + $keys = $a; + while($a) { $a = dba_nextkey($db_file); - echo $a; - $i++; + $keys .= $a; + $count++; } - echo "\n"; - echo $i; - for ($i=1; $i<5; $i++) { + // display number of entries and key existance + echo $count; + for ($i=1; $i<8; $i++) { echo dba_exists($i, $db_file) ? "Y" : "N"; } echo "\n="; @@ -29,6 +30,26 @@ DBA CDB handler test (read only) echo dba_fetch(2, $db_file); echo dba_fetch(3, $db_file); echo dba_fetch(4, $db_file); + echo "\n#"; + echo dba_fetch(1, $db_file); + echo dba_fetch(1, $db_file); + echo dba_fetch(1, $db_file); + echo dba_fetch(1, $db_file); + echo "\n?".$keys; + // with skip = 0 dba_fetch must fetch the first result + echo "\n#"; + $skip = array(); + for ($i=0; $i < strlen($keys); $i++) { + $key = substr($keys, $i, 1); + $skip[$key] = 0; + echo dba_fetch($key, $db_file); + } + echo "\n="; + for ($i=0; $i < strlen($keys); $i++) { + $key = substr($keys, $i, 1); + echo dba_fetch($key, $skip[$key], $db_file); + $skip[$key]++; + } dba_close($db_file); } else { echo "Error creating database\n"; @@ -36,6 +57,9 @@ DBA CDB handler test (read only) ?> --EXPECT-- database handler: cdb +7YYYYNNN +=1234 +#1111 ?1212314 -7YYYY -=1234
\ No newline at end of file +#1212314 +=1231324
\ No newline at end of file |