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