Back to index

php5  5.3.10
dba_db1.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Author: Shen Cheng-Da <cdsheen@gmail.com>                            |
00016    +----------------------------------------------------------------------+
00017  */
00018 
00019 /* $Id: dba_db1.c 321634 2012-01-01 13:15:04Z felipe $ */
00020 
00021 #ifdef HAVE_CONFIG_H
00022 #include "config.h"
00023 #endif
00024 
00025 #include "php.h"
00026 
00027 #if DBA_DB1
00028 #include "php_db1.h"
00029 
00030 #ifdef DB1_INCLUDE_FILE
00031 #include DB1_INCLUDE_FILE
00032 #endif
00033 
00034 #include <sys/types.h>
00035 #include <sys/stat.h>
00036 #include <fcntl.h>
00037 
00038 #define DB1_DATA dba_db1_data *dba = info->dbf
00039 #define DB1_GKEY DBT gkey; gkey.data = (char *) key; gkey.size = keylen
00040 
00041 typedef struct {
00042        DB  *dbp;
00043 } dba_db1_data;
00044 
00045 DBA_OPEN_FUNC(db1)
00046 {
00047        dba_db1_data  *dba;
00048        DB            *db;
00049 
00050        int gmode;
00051        int filemode = 0644;
00052 
00053        if (info->argc > 0) {
00054               convert_to_long_ex(info->argv[0]);
00055               filemode = Z_LVAL_PP(info->argv[0]);
00056        }
00057 
00058        gmode = 0;
00059        switch (info->mode) {
00060               case DBA_READER:
00061                      gmode = O_RDONLY;
00062                      break;
00063               case DBA_WRITER:
00064                      gmode = O_RDWR;
00065                      break;
00066               case DBA_CREAT:
00067                      gmode = O_RDWR | O_CREAT;
00068                      break;
00069               case DBA_TRUNC:
00070                      gmode = O_RDWR | O_CREAT | O_TRUNC;
00071                      break;
00072               default:
00073                      return FAILURE; /* not possible */
00074        }
00075 
00076        db = dbopen((char *)info->path, gmode, filemode, DB_HASH, NULL);
00077 
00078        if (db == NULL) {
00079               return FAILURE;
00080        }
00081 
00082        dba = pemalloc(sizeof(*dba), info->flags&DBA_PERSISTENT);
00083        dba->dbp = db;
00084 
00085        info->dbf = dba;
00086 
00087        return SUCCESS;
00088 }
00089 
00090 DBA_CLOSE_FUNC(db1)
00091 {
00092        DB1_DATA;
00093        dba->dbp->close(dba->dbp);
00094        pefree(info->dbf, info->flags&DBA_PERSISTENT);
00095 }
00096 
00097 DBA_FETCH_FUNC(db1)
00098 {
00099        DBT gval;
00100        DB1_DATA;
00101        DB1_GKEY;
00102 
00103        memset(&gval, 0, sizeof(gval));
00104        if (dba->dbp->get(dba->dbp, &gkey, &gval, 0) == RET_SUCCESS) {
00105               if (newlen) *newlen = gval.size;
00106               return estrndup(gval.data, gval.size);
00107        }
00108        return NULL;
00109 }
00110 
00111 DBA_UPDATE_FUNC(db1)
00112 {
00113        DBT gval;
00114        DB1_DATA;
00115        DB1_GKEY;
00116 
00117        gval.data = (char *) val;
00118        gval.size = vallen;
00119 
00120        return dba->dbp->put(dba->dbp, &gkey, &gval, mode == 1 ? R_NOOVERWRITE : 0) != RET_SUCCESS ? FAILURE : SUCCESS;
00121 }
00122 
00123 DBA_EXISTS_FUNC(db1)
00124 {
00125        DBT gval;
00126        DB1_DATA;
00127        DB1_GKEY;
00128 
00129        return dba->dbp->get(dba->dbp, &gkey, &gval, 0) != RET_SUCCESS ? FAILURE : SUCCESS;
00130 }
00131 
00132 DBA_DELETE_FUNC(db1)
00133 {
00134        DB1_DATA;
00135        DB1_GKEY;
00136 
00137        return dba->dbp->del(dba->dbp, &gkey, 0) != RET_SUCCESS ? FAILURE : SUCCESS;
00138 }
00139 
00140 DBA_FIRSTKEY_FUNC(db1)
00141 {
00142        DBT gkey;
00143        DBT gval;
00144        DB1_DATA;
00145 
00146        memset(&gkey, 0, sizeof(gkey));
00147        memset(&gval, 0, sizeof(gval));
00148 
00149        if (dba->dbp->seq(dba->dbp, &gkey, &gval, R_FIRST) == RET_SUCCESS) {
00150               if (newlen) *newlen = gkey.size;
00151               return estrndup(gkey.data, gkey.size);
00152        }
00153        return NULL;
00154 }
00155 
00156 DBA_NEXTKEY_FUNC(db1)
00157 {
00158        DBT gkey;
00159        DBT gval;
00160        DB1_DATA;
00161 
00162        memset(&gkey, 0, sizeof(gkey));
00163        memset(&gval, 0, sizeof(gval));
00164 
00165        if (dba->dbp->seq(dba->dbp, &gkey, &gval, R_NEXT) == RET_SUCCESS) {
00166               if (newlen) *newlen = gkey.size;
00167               return estrndup(gkey.data, gkey.size);
00168        }
00169        return NULL;
00170 }
00171 
00172 DBA_OPTIMIZE_FUNC(db1)
00173 {
00174        /* dummy */
00175        return SUCCESS;
00176 }
00177 
00178 DBA_SYNC_FUNC(db1)
00179 {
00180        return SUCCESS;
00181 }
00182 
00183 DBA_INFO_FUNC(db1)
00184 {
00185        return estrdup(DB1_VERSION);
00186 }
00187 
00188 #endif
00189 
00190 /*
00191  * Local variables:
00192  * tab-width: 4
00193  * c-basic-offset: 4
00194  * End:
00195  * vim600: sw=4 ts=4 fdm=marker
00196  * vim<600: sw=4 ts=4
00197  */