xref: /PHP-8.0/ext/dba/dba_db2.c (revision 5d6e923d)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | http://www.php.net/license/3_01.txt                                  |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Author: Sascha Schumann <sascha@schumann.cx>                         |
14    +----------------------------------------------------------------------+
15  */
16 
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20 
21 #include "php.h"
22 
23 #if DBA_DB2
24 #include "php_db2.h"
25 #include <sys/stat.h>
26 
27 #include <string.h>
28 #ifdef DB2_INCLUDE_FILE
29 #include DB2_INCLUDE_FILE
30 #endif
31 
32 #define DB2_DATA dba_db2_data *dba = info->dbf
33 #define DB2_GKEY \
34 	DBT gkey = {0}; \
35 	gkey.data = (char *) key; \
36 	gkey.size = keylen
37 
38 typedef struct {
39 	DB *dbp;
40 	DBC *cursor;
41 } dba_db2_data;
42 
DBA_OPEN_FUNC(db2)43 DBA_OPEN_FUNC(db2)
44 {
45 	DB *dbp;
46 	DBTYPE type;
47 	int gmode = 0;
48 	int filemode = 0644;
49 	struct stat check_stat;
50 	int s = VCWD_STAT(info->path, &check_stat);
51 
52 	if (!s && !check_stat.st_size) {
53 		info->mode = DBA_TRUNC; /* force truncate */
54 	}
55 
56 	type = info->mode == DBA_READER ? DB_UNKNOWN :
57 		info->mode == DBA_TRUNC ? DB_BTREE :
58 		s ? DB_BTREE : DB_UNKNOWN;
59 
60 	gmode = info->mode == DBA_READER ? DB_RDONLY :
61 		(info->mode == DBA_CREAT && s) ? DB_CREATE :
62 		(info->mode == DBA_CREAT && !s) ? 0 :
63 		info->mode == DBA_WRITER ? 0         :
64 		info->mode == DBA_TRUNC ? DB_CREATE | DB_TRUNCATE : -1;
65 
66 	if (gmode == -1) {
67 		return FAILURE;/* not possible */
68 	}
69 
70 	if (info->argc > 0) {
71 		filemode = zval_get_long(&info->argv[0]);
72 	}
73 
74 	if (db_open(info->path, type, gmode, filemode, NULL, NULL, &dbp)) {
75 		return FAILURE;
76 	}
77 
78 	info->dbf = pemalloc(sizeof(dba_db2_data), info->flags&DBA_PERSISTENT);
79 	memset(info->dbf, 0, sizeof(dba_db2_data));
80 	((dba_db2_data *) info->dbf)->dbp = dbp;
81 	return SUCCESS;
82 }
83 
DBA_CLOSE_FUNC(db2)84 DBA_CLOSE_FUNC(db2)
85 {
86 	DB2_DATA;
87 
88 	if (dba->cursor)
89 		dba->cursor->c_close(dba->cursor);
90 	dba->dbp->close(dba->dbp, 0);
91 	pefree(dba, info->flags&DBA_PERSISTENT);
92 }
93 
DBA_FETCH_FUNC(db2)94 DBA_FETCH_FUNC(db2)
95 {
96 	DBT gval = {0};
97 	DB2_DATA;
98 	DB2_GKEY;
99 
100 	if (dba->dbp->get(dba->dbp, NULL, &gkey, &gval, 0)) {
101 		return NULL;
102 	}
103 
104 	if (newlen) *newlen = gval.size;
105 	return estrndup(gval.data, gval.size);
106 }
107 
DBA_UPDATE_FUNC(db2)108 DBA_UPDATE_FUNC(db2)
109 {
110 	DBT gval = {0};
111 	DB2_DATA;
112 	DB2_GKEY;
113 
114 	gval.data = (char *) val;
115 	gval.size = vallen;
116 
117 	if (dba->dbp->put(dba->dbp, NULL, &gkey, &gval,
118 				mode == 1 ? DB_NOOVERWRITE : 0)) {
119 		return FAILURE;
120 	}
121 	return SUCCESS;
122 }
123 
DBA_EXISTS_FUNC(db2)124 DBA_EXISTS_FUNC(db2)
125 {
126 	DBT gval = {0};
127 	DB2_DATA;
128 	DB2_GKEY;
129 
130 	if (dba->dbp->get(dba->dbp, NULL, &gkey, &gval, 0)) {
131 		return FAILURE;
132 	}
133 	return SUCCESS;
134 }
135 
DBA_DELETE_FUNC(db2)136 DBA_DELETE_FUNC(db2)
137 {
138 	DB2_DATA;
139 	DB2_GKEY;
140 
141 	return dba->dbp->del(dba->dbp, NULL, &gkey, 0) ? FAILURE : SUCCESS;
142 }
143 
DBA_FIRSTKEY_FUNC(db2)144 DBA_FIRSTKEY_FUNC(db2)
145 {
146 	DB2_DATA;
147 
148 	if (dba->cursor) {
149 		dba->cursor->c_close(dba->cursor);
150 		dba->cursor = NULL;
151 	}
152 
153 #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)
154 	if (dba->dbp->cursor(dba->dbp, NULL, &dba->cursor, 0)) {
155 #else
156 	if (dba->dbp->cursor(dba->dbp, NULL, &dba->cursor)) {
157 #endif
158 		return NULL;
159 	}
160 
161 	/* we should introduce something like PARAM_PASSTHRU... */
162 	return dba_nextkey_db2(info, newlen);
163 }
164 
165 DBA_NEXTKEY_FUNC(db2)
166 {
167 	DB2_DATA;
168 	DBT gkey = {0}, gval = {0};
169 
170 	if (dba->cursor->c_get(dba->cursor, &gkey, &gval, DB_NEXT)
171 			|| !gkey.data)
172 		return NULL;
173 
174 	if (newlen) *newlen = gkey.size;
175 	return estrndup(gkey.data, gkey.size);
176 }
177 
178 DBA_OPTIMIZE_FUNC(db2)
179 {
180 	return SUCCESS;
181 }
182 
183 DBA_SYNC_FUNC(db2)
184 {
185 	DB2_DATA;
186 
187 	return dba->dbp->sync(dba->dbp, 0) ? FAILURE : SUCCESS;
188 }
189 
190 DBA_INFO_FUNC(db2)
191 {
192 	return estrdup(DB_VERSION_STRING);
193 }
194 
195 #endif
196