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 | https://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: Michael Maclean <mgdm@php.net> |
14 +----------------------------------------------------------------------+
15 */
16
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include "php.h"
22
23 #if DBA_TCADB
24 #include "php_tcadb.h"
25
26 #ifdef TCADB_INCLUDE_FILE
27 #include TCADB_INCLUDE_FILE
28 #endif
29
30 #define TCADB_DATA dba_tcadb_data *dba = info->dbf
31
32 typedef struct {
33 TCADB *tcadb;
34 } dba_tcadb_data;
35
DBA_OPEN_FUNC(tcadb)36 DBA_OPEN_FUNC(tcadb)
37 {
38 char *path_string;
39 TCADB *tcadb = tcadbnew();
40
41 if (tcadb) {
42 switch(info->mode) {
43 case DBA_READER:
44 spprintf(&path_string, 0, "%s#mode=r", info->path);
45 break;
46 case DBA_WRITER:
47 spprintf(&path_string, 0, "%s#mode=w", info->path);
48 break;
49 case DBA_CREAT:
50 spprintf(&path_string, 0, "%s#mode=wc", info->path);
51 break;
52 case DBA_TRUNC:
53 spprintf(&path_string, 0, "%s#mode=wct", info->path);
54 break;
55 default:
56 tcadbdel(tcadb);
57 return FAILURE;
58 }
59
60 if (!tcadbopen(tcadb, path_string)) {
61 efree(path_string);
62 tcadbdel(tcadb);
63 return FAILURE;
64 }
65
66 efree(path_string);
67
68 info->dbf = pemalloc(sizeof(dba_tcadb_data), info->flags & DBA_PERSISTENT);
69 memset(info->dbf, 0, sizeof(dba_tcadb_data));
70 ((dba_tcadb_data *) info->dbf)->tcadb = tcadb;
71 return SUCCESS;
72 }
73
74 return FAILURE;
75 }
76
DBA_CLOSE_FUNC(tcadb)77 DBA_CLOSE_FUNC(tcadb)
78 {
79 TCADB_DATA;
80
81 tcadbclose(dba->tcadb);
82 tcadbdel(dba->tcadb);
83 pefree(dba, info->flags & DBA_PERSISTENT);
84 }
85
DBA_FETCH_FUNC(tcadb)86 DBA_FETCH_FUNC(tcadb)
87 {
88 TCADB_DATA;
89 char *value, *new = NULL;
90 int value_size;
91
92 value = tcadbget(dba->tcadb, key, keylen, &value_size);
93 if (value) {
94 if (newlen) {
95 *newlen = value_size;
96 }
97 new = estrndup(value, value_size);
98 tcfree(value);
99 }
100
101 return new;
102 }
103
DBA_UPDATE_FUNC(tcadb)104 DBA_UPDATE_FUNC(tcadb)
105 {
106 TCADB_DATA;
107 int result;
108
109 if (mode == 1) {
110 /* Insert */
111 if (tcadbvsiz(dba->tcadb, key, keylen) > -1) {
112 return FAILURE;
113 }
114 }
115
116 result = tcadbput(dba->tcadb, key, keylen, val, vallen);
117
118 if (result) {
119 return SUCCESS;
120 }
121
122 php_error_docref2(NULL, key, val, E_WARNING, "Error updating data");
123 return FAILURE;
124 }
125
DBA_EXISTS_FUNC(tcadb)126 DBA_EXISTS_FUNC(tcadb)
127 {
128 TCADB_DATA;
129 char *value;
130 int value_len;
131
132 value = tcadbget(dba->tcadb, key, keylen, &value_len);
133 if (value) {
134 tcfree(value);
135 return SUCCESS;
136 }
137
138 return FAILURE;
139 }
140
DBA_DELETE_FUNC(tcadb)141 DBA_DELETE_FUNC(tcadb)
142 {
143 TCADB_DATA;
144
145 return tcadbout(dba->tcadb, key, keylen) ? SUCCESS : FAILURE;
146 }
147
DBA_FIRSTKEY_FUNC(tcadb)148 DBA_FIRSTKEY_FUNC(tcadb)
149 {
150 TCADB_DATA;
151 int value_size;
152 char *value, *new = NULL;
153
154 tcadbiterinit(dba->tcadb);
155
156 value = tcadbiternext(dba->tcadb, &value_size);
157 if (value) {
158 if (newlen) {
159 *newlen = value_size;
160 }
161 new = estrndup(value, value_size);
162 tcfree(value);
163 }
164
165 return new;
166 }
167
DBA_NEXTKEY_FUNC(tcadb)168 DBA_NEXTKEY_FUNC(tcadb)
169 {
170 TCADB_DATA;
171 int value_size;
172 char *value, *new = NULL;
173
174 value = tcadbiternext(dba->tcadb, &value_size);
175 if (value) {
176 if (newlen) {
177 *newlen = value_size;
178 }
179 new = estrndup(value, value_size);
180 tcfree(value);
181 }
182
183 return new;
184 }
185
DBA_OPTIMIZE_FUNC(tcadb)186 DBA_OPTIMIZE_FUNC(tcadb)
187 {
188 TCADB_DATA;
189
190 #if _TC_LIBVER >= 811
191 return tcadboptimize(dba->tcadb, NULL) ? SUCCESS : FAILURE;
192 #else
193 return FAILURE;
194 #endif
195 }
196
DBA_SYNC_FUNC(tcadb)197 DBA_SYNC_FUNC(tcadb)
198 {
199 TCADB_DATA;
200
201 return tcadbsync(dba->tcadb) ? SUCCESS : FAILURE;
202 }
203
DBA_INFO_FUNC(tcadb)204 DBA_INFO_FUNC(tcadb)
205 {
206 return estrdup(tcversion);
207 }
208
209 #endif
210