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