|
| 1 | +/* |
| 2 | + +----------------------------------------------------------------------+ |
| 3 | + | PHP HTML Embedded Scripting Language Version 3.0 | |
| 4 | + +----------------------------------------------------------------------+ |
| 5 | + | Copyright (c) 1999 PHP Development Team (See Credits file) | |
| 6 | + +----------------------------------------------------------------------+ |
| 7 | + | This program is free software; you can redistribute it and/or modify | |
| 8 | + | it under the terms of one of the following licenses: | |
| 9 | + | | |
| 10 | + | A) the GNU General Public License as published by the Free Software | |
| 11 | + | Foundation; either version 2 of the License, or (at your option) | |
| 12 | + | any later version. | |
| 13 | + | | |
| 14 | + | B) the PHP License as published by the PHP Development Team and | |
| 15 | + | included in the distribution in the file: LICENSE | |
| 16 | + | | |
| 17 | + | This program is distributed in the hope that it will be useful, | |
| 18 | + | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 19 | + | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 20 | + | GNU General Public License for more details. | |
| 21 | + | | |
| 22 | + | You should have received a copy of both licenses referred to here. | |
| 23 | + | If you did not, or have any questions about PHP licensing, please | |
| 24 | + | contact core@php.net. | |
| 25 | + +----------------------------------------------------------------------+ |
| 26 | + | Authors: Sascha Schumann <sas@schell.de> | |
| 27 | + +----------------------------------------------------------------------+ |
| 28 | + */ |
| 29 | + |
| 30 | +/* $Id$ */ |
| 31 | + |
| 32 | +#include "php.h" |
| 33 | + |
| 34 | +#if DBA_DB3 |
| 35 | +#include "php3_db3.h" |
| 36 | +#include <sys/stat.h> |
| 37 | + |
| 38 | +#include <string.h> |
| 39 | +#if DB3_DB3_DB_H |
| 40 | +#include <db3/db.h> |
| 41 | +#elif DB3_DB_DB3_H |
| 42 | +#include <db/db3.h> |
| 43 | +#elif DB3_DB3_H |
| 44 | +#include <db3.h> |
| 45 | +#elif DB3_DB_H |
| 46 | +#include <db.h> |
| 47 | +#endif |
| 48 | + |
| 49 | +#define DB3_DATA dba_db3_data *dba = info->dbf |
| 50 | +#define DB3_GKEY \ |
| 51 | + DBT gkey; \ |
| 52 | + memset(&gkey, 0, sizeof(gkey)); \ |
| 53 | + gkey.data = (char *) key; gkey.size = keylen |
| 54 | + |
| 55 | +typedef struct { |
| 56 | + DB *dbp; |
| 57 | + DBC *cursor; |
| 58 | +} dba_db3_data; |
| 59 | + |
| 60 | +DBA_OPEN_FUNC(db3) |
| 61 | +{ |
| 62 | + DB *dbp = NULL; |
| 63 | + DBTYPE type; |
| 64 | + int gmode = 0; |
| 65 | + int filemode = 0644; |
| 66 | + struct stat check_stat; |
| 67 | + |
| 68 | + type = info->mode == DBA_READER ? DB_UNKNOWN : |
| 69 | + info->mode == DBA_TRUNC ? DB_BTREE : |
| 70 | + stat(info->path, &check_stat) ? DB_BTREE : DB_UNKNOWN; |
| 71 | + |
| 72 | + gmode = info->mode == DBA_READER ? DB_RDONLY : |
| 73 | + info->mode == DBA_CREAT ? DB_CREATE : |
| 74 | + info->mode == DBA_WRITER ? 0 : |
| 75 | + info->mode == DBA_TRUNC ? DB_CREATE | DB_TRUNCATE : -1; |
| 76 | + |
| 77 | + if (gmode == -1) |
| 78 | + return FAILURE; |
| 79 | + |
| 80 | + if (info->argc > 0) { |
| 81 | + convert_to_long_ex(info->argv[0]); |
| 82 | + filemode = (*info->argv[0])->value.lval; |
| 83 | + } |
| 84 | + |
| 85 | + if (db_create(&dbp, NULL, 0) == 0 && |
| 86 | + dbp->open(dbp, info->path, NULL, type, gmode, filemode) == 0) { |
| 87 | + dba_db3_data *data; |
| 88 | + |
| 89 | + data = malloc(sizeof(*data)); |
| 90 | + data->dbp = dbp; |
| 91 | + data->cursor = NULL; |
| 92 | + info->dbf = data; |
| 93 | + |
| 94 | + return SUCCESS; |
| 95 | + } else if (dbp != NULL) { |
| 96 | + dbp->close(dbp, 0); |
| 97 | + } |
| 98 | + |
| 99 | + return FAILURE; |
| 100 | +} |
| 101 | + |
| 102 | +DBA_CLOSE_FUNC(db3) |
| 103 | +{ |
| 104 | + DB3_DATA; |
| 105 | + |
| 106 | + if (dba->cursor) dba->cursor->c_close(dba->cursor); |
| 107 | + dba->dbp->close(dba->dbp, 0); |
| 108 | + free(dba); |
| 109 | +} |
| 110 | + |
| 111 | +DBA_FETCH_FUNC(db3) |
| 112 | +{ |
| 113 | + DBT gval; |
| 114 | + char *new = NULL; |
| 115 | + DB3_DATA; |
| 116 | + DB3_GKEY; |
| 117 | + |
| 118 | + memset(&gval, 0, sizeof(gval)); |
| 119 | + if (!dba->dbp->get(dba->dbp, NULL, &gkey, &gval, 0)) { |
| 120 | + if (newlen) *newlen = gval.size; |
| 121 | + new = estrndup(gval.data, gval.size); |
| 122 | + } |
| 123 | + return new; |
| 124 | +} |
| 125 | + |
| 126 | +DBA_UPDATE_FUNC(db3) |
| 127 | +{ |
| 128 | + DBT gval; |
| 129 | + DB3_DATA; |
| 130 | + DB3_GKEY; |
| 131 | + |
| 132 | + memset(&gval, 0, sizeof(gval)); |
| 133 | + gval.data = (char *) val; |
| 134 | + gval.size = vallen; |
| 135 | + |
| 136 | + if (!dba->dbp->put(dba->dbp, NULL, &gkey, &gval, |
| 137 | + mode == 1 ? DB_NOOVERWRITE : 0)) { |
| 138 | + return SUCCESS; |
| 139 | + } |
| 140 | + return FAILURE; |
| 141 | +} |
| 142 | + |
| 143 | +DBA_EXISTS_FUNC(db3) |
| 144 | +{ |
| 145 | + DBT gval; |
| 146 | + DB3_DATA; |
| 147 | + DB3_GKEY; |
| 148 | + |
| 149 | + memset(&gval, 0, sizeof(gval)); |
| 150 | + if (!dba->dbp->get(dba->dbp, NULL, &gkey, &gval, 0)) { |
| 151 | + return SUCCESS; |
| 152 | + } |
| 153 | + return FAILURE; |
| 154 | +} |
| 155 | + |
| 156 | +DBA_DELETE_FUNC(db3) |
| 157 | +{ |
| 158 | + DB3_DATA; |
| 159 | + DB3_GKEY; |
| 160 | + |
| 161 | + return dba->dbp->del(dba->dbp, NULL, &gkey, 0) ? FAILURE : SUCCESS; |
| 162 | +} |
| 163 | + |
| 164 | +DBA_FIRSTKEY_FUNC(db3) |
| 165 | +{ |
| 166 | + DB3_DATA; |
| 167 | + |
| 168 | + if (dba->cursor) { |
| 169 | + dba->cursor->c_close(dba->cursor); |
| 170 | + } |
| 171 | + |
| 172 | + dba->cursor = NULL; |
| 173 | + if (dba->dbp->cursor(dba->dbp, NULL, &dba->cursor, 0) != 0) { |
| 174 | + return NULL; |
| 175 | + } |
| 176 | + |
| 177 | + /* we should introduce something like PARAM_PASSTHRU... */ |
| 178 | + return dba_nextkey_db3(info, newlen); |
| 179 | +} |
| 180 | + |
| 181 | +DBA_NEXTKEY_FUNC(db3) |
| 182 | +{ |
| 183 | + DB3_DATA; |
| 184 | + DBT gkey, gval; |
| 185 | + char *nkey = NULL; |
| 186 | + |
| 187 | + memset(&gkey, 0, sizeof(gkey)); |
| 188 | + memset(&gval, 0, sizeof(gval)); |
| 189 | + |
| 190 | + if (dba->cursor->c_get(dba->cursor, &gkey, &gval, DB_NEXT) == 0) { |
| 191 | + if (gkey.data) { |
| 192 | + nkey = estrndup(gkey.data, gkey.size); |
| 193 | + if (newlen) *newlen = gkey.size; |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + return nkey; |
| 198 | +} |
| 199 | + |
| 200 | +DBA_OPTIMIZE_FUNC(db3) |
| 201 | +{ |
| 202 | + return SUCCESS; |
| 203 | +} |
| 204 | + |
| 205 | +DBA_SYNC_FUNC(db3) |
| 206 | +{ |
| 207 | + DB3_DATA; |
| 208 | + |
| 209 | + return dba->dbp->sync(dba->dbp, 0) ? FAILURE : SUCCESS; |
| 210 | +} |
| 211 | + |
| 212 | +#endif |
0 commit comments