Skip to content

Commit 9a04765

Browse files
author
Sascha Schumann
committed
add Berkeley DB3 support
@- Added Berkeley DB3 support in DBA (Sascha)
1 parent 90ecf41 commit 9a04765

File tree

6 files changed

+260
-2
lines changed

6 files changed

+260
-2
lines changed

ext/dba/Makefile.am

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
noinst_LTLIBRARIES=libphpext_dba.la
44
libphpext_dba_la_SOURCES=dba.c dba_cdb.c dba_db2.c dba_dbm.c dba_gdbm.c \
5-
dba_ndbm.c
5+
dba_ndbm.c dba_db3.c
66

ext/dba/config.h.stub

+2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
#define DB2_DB_DB2_H 0
77
#define DB2_DB_H 0
88
#define DB2_DB2_H 0
9+
#define DB3_DB_H 0
910
#define HAVE_DBA 0
1011
#define DBA_GDBM 0
1112
#define DBA_NDBM 0
1213
#define DBA_DBOPEN 0
1314
#define DBA_DB2 0
15+
#define DBA_DB3 0
1416
#define DBA_DBM 0
1517
#define DBA_CDB 0
1618

ext/dba/config.m4

+29-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ AC_ARG_WITH(db2,
134134
DB2_EXTRA="DB2_DB_H"
135135
fi
136136
137-
if test "$DB2_EXTRA" != ""; then
137+
if test -n "$DB2_EXTRA"; then
138138
eval "AC_DEFINE($DB2_EXTRA, 1)"
139139
fi
140140
@@ -152,6 +152,34 @@ AC_ARG_WITH(db2,
152152
AC_MSG_CHECKING(for Berkeley DB2 support)
153153
AC_DBA_STD_RESULT
154154

155+
AC_ARG_WITH(db3,
156+
[ --with-db3[=DIR] Include Berkeley DB3 support],[
157+
if test "$withval" != "no"; then
158+
for i in /usr/local /usr $withval; do
159+
if test -f "$i/include/db.h" ; then
160+
THIS_PREFIX="$i"
161+
DB3_EXTRA="DB3_DB_H"
162+
fi
163+
done
164+
165+
if test -n "$DB3_EXTRA"; then
166+
eval "AC_DEFINE($DB3_EXTRA, 1)"
167+
fi
168+
169+
for LIB in db; do
170+
AC_TEMP_LDFLAGS(-L$THIS_PREFIX/lib,[
171+
AC_CHECK_LIB($LIB, db_create, [AC_DEFINE(DBA_DB3,1) THIS_LIBS="$LIB"])
172+
])
173+
done
174+
175+
AC_DBA_STD_ASSIGN
176+
AC_DBA_STD_CHECK
177+
AC_DBA_STD_ATTACH
178+
fi
179+
])
180+
AC_MSG_CHECKING(for Berkeley DB3 support)
181+
AC_DBA_STD_RESULT
182+
155183
AC_ARG_WITH(dbm,
156184
[ --with-dbm[=DIR] Include DBM support],[
157185
if test "$withval" != "no"; then

ext/dba/dba.c

+4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "php3_dbm.h"
4141
#include "php3_cdb.h"
4242
#include "php3_db2.h"
43+
#include "php3_db3.h"
4344

4445
function_entry dba_functions[] = {
4546
PHP_FE(dba_open, NULL)
@@ -157,6 +158,9 @@ static dba_handler handler[] = {
157158
#endif
158159
#if DBA_DB2
159160
DBA_HND(db2)
161+
#endif
162+
#if DBA_DB3
163+
DBA_HND(db3)
160164
#endif
161165
{ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
162166
};

ext/dba/dba_db3.c

+212
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
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

ext/dba/php3_db3.h

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef _PHP3_DB3_H
2+
#define _PHP3_DB3_H
3+
4+
#if DBA_DB3
5+
6+
#include "php3_dba.h"
7+
8+
DBA_FUNCS(db3);
9+
10+
#endif
11+
12+
#endif

0 commit comments

Comments
 (0)