xref: /PHP-7.4/ext/dba/dba_tcadb.c (revision 96dd310c)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 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 	tcadbdel(dba->tcadb);
85 	pefree(dba, info->flags & DBA_PERSISTENT);
86 }
87 
DBA_FETCH_FUNC(tcadb)88 DBA_FETCH_FUNC(tcadb)
89 {
90 	TCADB_DATA;
91 	char *value, *new = NULL;
92 	int value_size;
93 
94 	value = tcadbget(dba->tcadb, key, keylen, &value_size);
95 	if (value) {
96 		if (newlen) {
97 			*newlen = value_size;
98 		}
99 		new = estrndup(value, value_size);
100 		tcfree(value);
101 	}
102 
103 	return new;
104 }
105 
DBA_UPDATE_FUNC(tcadb)106 DBA_UPDATE_FUNC(tcadb)
107 {
108 	TCADB_DATA;
109 	int result;
110 
111 	if (mode == 1) {
112 		/* Insert */
113 		if (tcadbvsiz(dba->tcadb, key, keylen) > -1) {
114 			return FAILURE;
115 		}
116 	}
117 
118 	result = tcadbput(dba->tcadb, key, keylen, val, vallen);
119 
120 	if (result) {
121 		return SUCCESS;
122 	}
123 
124 	php_error_docref2(NULL, key, val, E_WARNING, "Error updating data");
125 	return FAILURE;
126 }
127 
DBA_EXISTS_FUNC(tcadb)128 DBA_EXISTS_FUNC(tcadb)
129 {
130 	TCADB_DATA;
131 	char *value;
132 	int value_len;
133 
134 	value = tcadbget(dba->tcadb, key, keylen, &value_len);
135 	if (value) {
136 		tcfree(value);
137 		return SUCCESS;
138 	}
139 
140 	return FAILURE;
141 }
142 
DBA_DELETE_FUNC(tcadb)143 DBA_DELETE_FUNC(tcadb)
144 {
145 	TCADB_DATA;
146 
147 	return tcadbout(dba->tcadb, key, keylen) ? SUCCESS : FAILURE;
148 }
149 
DBA_FIRSTKEY_FUNC(tcadb)150 DBA_FIRSTKEY_FUNC(tcadb)
151 {
152 	TCADB_DATA;
153 	int value_size;
154 	char *value, *new = NULL;
155 
156 	tcadbiterinit(dba->tcadb);
157 
158 	value = tcadbiternext(dba->tcadb, &value_size);
159 	if (value) {
160 		if (newlen) {
161 			*newlen = value_size;
162 		}
163 		new = estrndup(value, value_size);
164 		tcfree(value);
165 	}
166 
167 	return new;
168 }
169 
DBA_NEXTKEY_FUNC(tcadb)170 DBA_NEXTKEY_FUNC(tcadb)
171 {
172 	TCADB_DATA;
173 	int value_size;
174 	char *value, *new = NULL;
175 
176 	value = tcadbiternext(dba->tcadb, &value_size);
177 	if (value) {
178 		if (newlen) {
179 			*newlen = value_size;
180 		}
181 		new = estrndup(value, value_size);
182 		tcfree(value);
183 	}
184 
185 	return new;
186 }
187 
DBA_OPTIMIZE_FUNC(tcadb)188 DBA_OPTIMIZE_FUNC(tcadb)
189 {
190 	TCADB_DATA;
191 
192 #if _TC_LIBVER >= 811
193 	return tcadboptimize(dba->tcadb, NULL) ? SUCCESS : FAILURE;
194 #else
195 	return FAILURE;
196 #endif
197 }
198 
DBA_SYNC_FUNC(tcadb)199 DBA_SYNC_FUNC(tcadb)
200 {
201 	TCADB_DATA;
202 
203 	return tcadbsync(dba->tcadb) ? SUCCESS : FAILURE;
204 }
205 
DBA_INFO_FUNC(tcadb)206 DBA_INFO_FUNC(tcadb)
207 {
208 	return estrdup(tcversion);
209 }
210 
211 #endif
212