Back to index

php5  5.3.10
dba_db2.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: Sascha Schumann <sascha@schumann.cx>                         |
00016    +----------------------------------------------------------------------+
00017  */
00018 
00019 /* $Id: dba_db2.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_DB2
00028 #include "php_db2.h"
00029 #include <sys/stat.h>
00030 
00031 #include <string.h>
00032 #ifdef DB2_INCLUDE_FILE
00033 #include DB2_INCLUDE_FILE
00034 #endif
00035 
00036 #define DB2_DATA dba_db2_data *dba = info->dbf
00037 #define DB2_GKEY \
00038        DBT gkey = {0}; \
00039        gkey.data = (char *) key; \
00040        gkey.size = keylen
00041 
00042 typedef struct {
00043        DB *dbp;
00044        DBC *cursor;
00045 } dba_db2_data;
00046 
00047 DBA_OPEN_FUNC(db2)
00048 {
00049        DB *dbp;
00050        DBTYPE type;
00051        int gmode = 0;
00052        int filemode = 0644;
00053        struct stat check_stat;
00054        int s = VCWD_STAT(info->path, &check_stat);
00055 
00056        if (!s && !check_stat.st_size) {
00057               info->mode = DBA_TRUNC; /* force truncate */
00058        }
00059 
00060        type = info->mode == DBA_READER ? DB_UNKNOWN :
00061               info->mode == DBA_TRUNC ? DB_BTREE :
00062               s ? DB_BTREE : DB_UNKNOWN;
00063          
00064        gmode = info->mode == DBA_READER ? DB_RDONLY :
00065               (info->mode == DBA_CREAT && s) ? DB_CREATE : 
00066               (info->mode == DBA_CREAT && !s) ? 0 :
00067               info->mode == DBA_WRITER ? 0         : 
00068               info->mode == DBA_TRUNC ? DB_CREATE | DB_TRUNCATE : -1;
00069 
00070        if (gmode == -1) {
00071               return FAILURE;/* not possible */
00072        }
00073 
00074        if (info->argc > 0) {
00075               convert_to_long_ex(info->argv[0]);
00076               filemode = Z_LVAL_PP(info->argv[0]);
00077        }
00078 
00079        if (db_open(info->path, type, gmode, filemode, NULL, NULL, &dbp)) {
00080               return FAILURE;
00081        }
00082 
00083        info->dbf = pemalloc(sizeof(dba_db2_data), info->flags&DBA_PERSISTENT);
00084        memset(info->dbf, 0, sizeof(dba_db2_data));
00085        ((dba_db2_data *) info->dbf)->dbp = dbp;
00086        return SUCCESS;
00087 }
00088 
00089 DBA_CLOSE_FUNC(db2)
00090 {
00091        DB2_DATA;
00092        
00093        if (dba->cursor) 
00094               dba->cursor->c_close(dba->cursor);
00095        dba->dbp->close(dba->dbp, 0);
00096        pefree(dba, info->flags&DBA_PERSISTENT);
00097 }
00098 
00099 DBA_FETCH_FUNC(db2)
00100 {
00101        DBT gval = {0};
00102        DB2_DATA;
00103        DB2_GKEY;
00104        
00105        if (dba->dbp->get(dba->dbp, NULL, &gkey, &gval, 0)) {
00106               return NULL;
00107        }
00108 
00109        if (newlen) *newlen = gval.size;
00110        return estrndup(gval.data, gval.size);
00111 }
00112 
00113 DBA_UPDATE_FUNC(db2)
00114 {
00115        DBT gval = {0};
00116        DB2_DATA;
00117        DB2_GKEY;
00118        
00119        gval.data = (char *) val;
00120        gval.size = vallen;
00121 
00122        if (dba->dbp->put(dba->dbp, NULL, &gkey, &gval, 
00123                             mode == 1 ? DB_NOOVERWRITE : 0)) {
00124               return FAILURE;
00125        }
00126        return SUCCESS;
00127 }
00128 
00129 DBA_EXISTS_FUNC(db2)
00130 {
00131        DBT gval = {0};
00132        DB2_DATA;
00133        DB2_GKEY;
00134        
00135        if (dba->dbp->get(dba->dbp, NULL, &gkey, &gval, 0)) {
00136               return FAILURE;
00137        }
00138        return SUCCESS;
00139 }
00140 
00141 DBA_DELETE_FUNC(db2)
00142 {
00143        DB2_DATA;
00144        DB2_GKEY;
00145 
00146        return dba->dbp->del(dba->dbp, NULL, &gkey, 0) ? FAILURE : SUCCESS;
00147 }
00148 
00149 DBA_FIRSTKEY_FUNC(db2)
00150 {
00151        DB2_DATA;
00152 
00153        if (dba->cursor) {
00154               dba->cursor->c_close(dba->cursor);
00155               dba->cursor = NULL;
00156        }
00157 
00158 #if (DB_VERSION_MAJOR > 2) || (DB_VERSION_MAJOR == 2 && DB_VERSION_MINOR > 6) || (DB_VERSION_MAJOR == 2 && DB_VERSION_MINOR == 6 && DB_VERSION_PATCH >= 4)
00159        if (dba->dbp->cursor(dba->dbp, NULL, &dba->cursor, 0)) {
00160 #else
00161        if (dba->dbp->cursor(dba->dbp, NULL, &dba->cursor)) {
00162 #endif
00163               return NULL;
00164        }
00165 
00166        /* we should introduce something like PARAM_PASSTHRU... */
00167        return dba_nextkey_db2(info, newlen TSRMLS_CC);
00168 }
00169 
00170 DBA_NEXTKEY_FUNC(db2)
00171 {
00172        DB2_DATA;
00173        DBT gkey = {0}, gval = {0};
00174 
00175        if (dba->cursor->c_get(dba->cursor, &gkey, &gval, DB_NEXT)
00176                      || !gkey.data)
00177               return NULL;
00178 
00179        if (newlen) *newlen = gkey.size;
00180        return estrndup(gkey.data, gkey.size);
00181 }
00182 
00183 DBA_OPTIMIZE_FUNC(db2)
00184 {
00185        return SUCCESS;
00186 }
00187 
00188 DBA_SYNC_FUNC(db2)
00189 {
00190        DB2_DATA;
00191 
00192        return dba->dbp->sync(dba->dbp, 0) ? FAILURE : SUCCESS;
00193 }
00194 
00195 DBA_INFO_FUNC(db2)
00196 {
00197        return estrdup(DB_VERSION_STRING);
00198 }
00199 
00200 #endif
00201 
00202 /*
00203  * Local variables:
00204  * tab-width: 4
00205  * c-basic-offset: 4
00206  * End:
00207  * vim600: sw=4 ts=4 fdm=marker
00208  * vim<600: sw=4 ts=4
00209  */