xref: /PHP-7.3/ext/dba/dba_tcadb.c (revision 8d3f8ca1)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2018 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Author:  Michael Maclean <mgdm@php.net>                              |
16    +----------------------------------------------------------------------+
17  */
18 
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22 
23 #include "php.h"
24 
25 #if DBA_TCADB
26 #include "php_tcadb.h"
27 
28 #ifdef TCADB_INCLUDE_FILE
29 #include TCADB_INCLUDE_FILE
30 #endif
31 
32 #define TCADB_DATA dba_tcadb_data *dba = info->dbf
33 
34 typedef struct {
35 	TCADB *tcadb;
36 } dba_tcadb_data;
37 
DBA_OPEN_FUNC(tcadb)38 DBA_OPEN_FUNC(tcadb)
39 {
40 	char *path_string;
41 	TCADB *tcadb = tcadbnew();
42 
43 	if (tcadb) {
44 		switch(info->mode) {
45 			case DBA_READER:
46 				spprintf(&path_string, 0, "%s#mode=r", info->path);
47 				break;
48 			case DBA_WRITER:
49 				spprintf(&path_string, 0, "%s#mode=w", info->path);
50 				break;
51 			case DBA_CREAT:
52 				spprintf(&path_string, 0, "%s#mode=wc", info->path);
53 				break;
54 			case DBA_TRUNC:
55 				spprintf(&path_string, 0, "%s#mode=wct", info->path);
56 				break;
57 			default:
58 				tcadbdel(tcadb);
59 				return FAILURE;
60 		}
61 
62 		if (!tcadbopen(tcadb, path_string)) {
63 			efree(path_string);
64 			tcadbdel(tcadb);
65 			return FAILURE;
66 		}
67 
68 		efree(path_string);
69 
70 		info->dbf = pemalloc(sizeof(dba_tcadb_data), info->flags & DBA_PERSISTENT);
71 		memset(info->dbf, 0, sizeof(dba_tcadb_data));
72 		((dba_tcadb_data *) info->dbf)->tcadb = tcadb;
73 		return SUCCESS;
74 	}
75 
76 	return FAILURE;
77 }
78 
DBA_CLOSE_FUNC(tcadb)79 DBA_CLOSE_FUNC(tcadb)
80 {
81 	TCADB_DATA;
82 
83 	tcadbclose(dba->tcadb);
84 	pefree(dba, info->flags & DBA_PERSISTENT);
85 }
86 
DBA_FETCH_FUNC(tcadb)87 DBA_FETCH_FUNC(tcadb)
88 {
89 	TCADB_DATA;
90 	char *value, *new = NULL;
91 	int value_size;
92 
93 	value = tcadbget(dba->tcadb, key, keylen, &value_size);
94 	if (value) {
95 		if (newlen) {
96 			*newlen = value_size;
97 		}
98 		new = estrndup(value, value_size);
99 		tcfree(value);
100 	}
101 
102 	return new;
103 }
104 
DBA_UPDATE_FUNC(tcadb)105 DBA_UPDATE_FUNC(tcadb)
106 {
107 	TCADB_DATA;
108 	int result;
109 
110 	if (mode == 1) {
111 		/* Insert */
112 		if (tcadbvsiz(dba->tcadb, key, keylen) > -1) {
113 			return FAILURE;
114 		}
115 	}
116 
117 	result = tcadbput(dba->tcadb, key, keylen, val, vallen);
118 
119 	if (result) {
120 		return SUCCESS;
121 	}
122 
123 	php_error_docref2(NULL, key, val, E_WARNING, "Error updating data");
124 	return FAILURE;
125 }
126 
DBA_EXISTS_FUNC(tcadb)127 DBA_EXISTS_FUNC(tcadb)
128 {
129 	TCADB_DATA;
130 	char *value;
131 	int value_len;
132 
133 	value = tcadbget(dba->tcadb, key, keylen, &value_len);
134 	if (value) {
135 		tcfree(value);
136 		return SUCCESS;
137 	}
138 
139 	return FAILURE;
140 }
141 
DBA_DELETE_FUNC(tcadb)142 DBA_DELETE_FUNC(tcadb)
143 {
144 	TCADB_DATA;
145 
146 	return tcadbout(dba->tcadb, key, keylen) ? SUCCESS : FAILURE;
147 }
148 
DBA_FIRSTKEY_FUNC(tcadb)149 DBA_FIRSTKEY_FUNC(tcadb)
150 {
151 	TCADB_DATA;
152 	int value_size;
153 	char *value, *new = NULL;
154 
155 	tcadbiterinit(dba->tcadb);
156 
157 	value = tcadbiternext(dba->tcadb, &value_size);
158 	if (value) {
159 		if (newlen) {
160 			*newlen = value_size;
161 		}
162 		new = estrndup(value, value_size);
163 		tcfree(value);
164 	}
165 
166 	return new;
167 }
168 
DBA_NEXTKEY_FUNC(tcadb)169 DBA_NEXTKEY_FUNC(tcadb)
170 {
171 	TCADB_DATA;
172 	int value_size;
173 	char *value, *new = NULL;
174 
175 	value = tcadbiternext(dba->tcadb, &value_size);
176 	if (value) {
177 		if (newlen) {
178 			*newlen = value_size;
179 		}
180 		new = estrndup(value, value_size);
181 		tcfree(value);
182 	}
183 
184 	return new;
185 }
186 
DBA_OPTIMIZE_FUNC(tcadb)187 DBA_OPTIMIZE_FUNC(tcadb)
188 {
189 	TCADB_DATA;
190 
191 #if _TC_LIBVER >= 811
192 	return tcadboptimize(dba->tcadb, NULL) ? SUCCESS : FAILURE;
193 #else
194 	return FAILURE;
195 #endif
196 }
197 
DBA_SYNC_FUNC(tcadb)198 DBA_SYNC_FUNC(tcadb)
199 {
200 	TCADB_DATA;
201 
202 	return tcadbsync(dba->tcadb) ? SUCCESS : FAILURE;
203 }
204 
DBA_INFO_FUNC(tcadb)205 DBA_INFO_FUNC(tcadb)
206 {
207 	return estrdup(tcversion);
208 }
209 
210 #endif
211 
212 /*
213  * Local variables:
214  * tab-width: 4
215  * c-basic-offset: 4
216  * End:
217  * vim600: sw=4 ts=4 fdm=marker
218  * vim<600: sw=4 ts=4
219  */
220