]> granicus.if.org Git - php/commitdiff
cdb now allows multiple key-value pairs with same key
authorMarcus Boerger <helly@php.net>
Sun, 3 Nov 2002 16:43:07 +0000 (16:43 +0000)
committerMarcus Boerger <helly@php.net>
Sun, 3 Nov 2002 16:43:07 +0000 (16:43 +0000)
ext/dba/dba.c
ext/dba/dba_cdb.c
ext/dba/php_dba.h
ext/dba/tests/dba_cdb_read.phpt

index 63a32bb705fd3fbb6dea7d16d2eacf045b8a9ac3..94fa8894ff0893a1b2eb03f487b87ed00f9c890f 100644 (file)
@@ -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;
index e4b2a77de4226d12771822d7332d0dac0a82cae1..37b525135dd2fb209690af684553f3e06b874f19 100644 (file)
@@ -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);
                
index 832e66638c3357bb022654b273c1f1e610dd5c0c..886ccbea0bcf9d459bef1a3ebecd0aeb91c9d653 100644 (file)
@@ -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) \
index 6ead2f34d9a9be507978e1a8d3ddcb378f16ad79..045de9574a485cfaadd8f0d5ebfa14679831ed15 100644 (file)
@@ -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