1 #ifndef    PHP_TARANTOOL_H
2 #define    PHP_TARANTOOL_H
3 
4 #include <zend.h>
5 #include <zend_API.h>
6 #include <zend_compile.h>
7 #include <zend_exceptions.h>
8 
9 #include <php.h>
10 #include <php_ini.h>
11 #include <php_network.h>
12 
13 #include <ext/standard/info.h>
14 #include <ext/standard/base64.h>
15 
16 #ifdef HAVE_CONFIG_H
17 #  include "config.h"
18 #endif
19 
20 #if PHP_VERSION_ID >= 70200
21 #  include <Zend/zend_smart_string.h>
22 #  define smart_string_alloc4(d, n, what, newlen) newlen = smart_string_alloc(d, n, what)
23 #elif PHP_VERSION_ID >= 70000
24 #  include <ext/standard/php_smart_string.h>
25 #else
26 #  include <ext/standard/php_smart_str.h>
27    typedef smart_str smart_string;
28 #  define smart_string_alloc4(...) smart_str_alloc4(__VA_ARGS__)
29 #  define smart_string_free_ex(...) smart_str_free_ex(__VA_ARGS__)
30 #endif
31 
32 #if PHP_VERSION_ID < 70300
33 #  define GC_SET_REFCOUNT(p, rc) do { \
34 	GC_REFCOUNT((p)) = (rc);      \
35 } while(0)
36 #endif /* PHP_VERSION_ID < 70300 */
37 
38 #if PHP_VERSION_ID < 70300
39 
40 #  define ARRAY_PROTECT_RECURSION(data) do {                \
41 	if (Z_TYPE_P((data)) == IS_ARRAY &&                 \
42 	    ZEND_HASH_APPLY_PROTECTION(Z_ARRVAL_P((data)))) \
43 	{                                                   \
44 		Z_ARRVAL_P((data))->u.v.nApplyCount++;      \
45 	}                                                   \
46 } while(0)
47 
48 #  define ARRAY_UNPROTECT_RECURSION(data) do {              \
49 	if (Z_TYPE_P((data)) == IS_ARRAY &&                 \
50 	    ZEND_HASH_APPLY_PROTECTION(Z_ARRVAL_P((data)))) \
51 	{                                                   \
52 		Z_ARRVAL_P((data))->u.v.nApplyCount--;      \
53 	}                                                   \
54 } while(0)
55 
56 #  define ARRAY_IS_RECURSIVE(data) (                      \
57 	Z_TYPE_P(data) == IS_ARRAY &&                     \
58 	ZEND_HASH_APPLY_PROTECTION(Z_ARRVAL_P((data))) && \
59 	Z_ARRVAL_P((data))->u.v.nApplyCount > 1           \
60 )
61 
62 #else /* PHP_VERSION_ID < 70300 */
63 
64 #  define ARRAY_PROTECT_RECURSION(data) do {                \
65 	if (Z_TYPE_P((data)) == IS_ARRAY &&                 \
66 	    !(GC_FLAGS(Z_ARRVAL_P((data))) & GC_IMMUTABLE)) \
67 	{                                                   \
68 		GC_PROTECT_RECURSION(Z_ARRVAL_P((data)));   \
69 	}                                                   \
70 } while(0)
71 
72 #  define ARRAY_UNPROTECT_RECURSION(data)  do {             \
73 	if (Z_TYPE_P((data)) == IS_ARRAY &&                 \
74 	    !(GC_FLAGS(Z_ARRVAL_P((data))) & GC_IMMUTABLE)) \
75 	{                                                   \
76 		GC_UNPROTECT_RECURSION(Z_ARRVAL_P((data))); \
77 	}                                                   \
78 } while(0)
79 
80 #  define ARRAY_IS_RECURSIVE(data) (                      \
81 	Z_TYPE_P((data)) == IS_ARRAY &&                   \
82 	!(GC_FLAGS(Z_ARRVAL_P((data))) & GC_IMMUTABLE) && \
83 	GC_IS_RECURSIVE(Z_ARRVAL_P((data)))               \
84 )
85 
86 #endif /* !(PHP_VERSION_ID < 70300)) */
87 
88 extern zend_module_entry tarantool_module_entry;
89 #define phpext_tarantool_ptr &tarantool_module_entry
90 
91 #define PHP_TARANTOOL_VERSION "0.3.3"
92 #define PHP_TARANTOOL_EXTNAME "tarantool"
93 
94 #ifdef PHP_WIN32
95 #  define PHP_TARANTOOL_API __declspec(__dllexport)
96 #elif defined(__GNUC__) && __GNUC__ >= 4
97 #  define PHP_TARANTOOL_API __attribute__ ((visibility("default")))
98 #else
99 #  define PHP_TARANTOOL_API
100 #endif
101 
102 #define TARANTOOL_TIMEOUT_SEC 10
103 #define TARANTOOL_TIMEOUT_USEC 0
104 
105 #ifdef ZTS
106 #include "TSRM.h"
107 #endif
108 
109 struct tarantool_schema;
110 
111 #define SSTR_BEG(str) (str->c)
112 #define SSTR_END(str) (str->c + str->a)
113 #define SSTR_AWA(str) (str->a)
114 #define SSTR_LEN(str) (str->len)
115 #define SSTR_POS(str) (str->c + str->len)
116 #define SSTR_DIF(str, end) (end - str->c)
117 
118 PHP_MINIT_FUNCTION(tarantool);
119 PHP_RINIT_FUNCTION(tarantool);
120 PHP_MSHUTDOWN_FUNCTION(tarantool);
121 PHP_MINFO_FUNCTION(tarantool);
122 
123 PHP_METHOD(Tarantool, __construct);
124 PHP_METHOD(Tarantool, connect);
125 PHP_METHOD(Tarantool, reconnect);
126 PHP_METHOD(Tarantool, close);
127 PHP_METHOD(Tarantool, authenticate);
128 PHP_METHOD(Tarantool, ping);
129 PHP_METHOD(Tarantool, select);
130 PHP_METHOD(Tarantool, insert);
131 PHP_METHOD(Tarantool, replace);
132 PHP_METHOD(Tarantool, call);
133 PHP_METHOD(Tarantool, eval);
134 PHP_METHOD(Tarantool, delete);
135 PHP_METHOD(Tarantool, update);
136 PHP_METHOD(Tarantool, upsert);
137 PHP_METHOD(Tarantool, flush_schema);
138 
139 ZEND_BEGIN_MODULE_GLOBALS(tarantool)
140 	zend_bool persistent;
141 	zend_bool use_namespace;
142 	zend_bool connection_alias;
143 	long sync_counter;
144 	long retry_count;
145 	double retry_sleep;
146 	double timeout;
147 	double request_timeout;
148 ZEND_END_MODULE_GLOBALS(tarantool)
149 
150 ZEND_EXTERN_MODULE_GLOBALS(tarantool);
151 
152 typedef struct tarantool_object {
153 	struct tarantool_connection  {
154 		char                    *host;
155 		int                      port;
156 		char                    *login;
157 		char                    *passwd;
158 		php_stream              *stream;
159 		struct tarantool_schema *schema;
160 		smart_string            *value;
161 		struct tp               *tps;
162 		char                    *greeting;
163 		char                    *salt;
164 		/* Only for persistent connections */
165 		char                    *orig_login;
166 		char                    *suffix;
167 		int                     suffix_len;
168 		zend_string             *persistent_id;
169 	}            *obj;
170 
171 	zend_bool     is_persistent;
172 
173 	zend_object   zo;
174 } tarantool_object;
175 
176 typedef struct tarantool_connection tarantool_connection;
177 
178 PHP_TARANTOOL_API zend_class_entry *php_tarantool_get_ce(void);
179 PHP_TARANTOOL_API zend_class_entry *php_tarantool_get_exception(void);
180 PHP_TARANTOOL_API zend_class_entry *php_tarantool_get_ioexception(void);
181 PHP_TARANTOOL_API zend_class_entry *php_tarantool_get_clienterror(void);
182 PHP_TARANTOOL_API zend_class_entry *php_tarantool_get_parsingexception(void);
183 #if PHP_MAJOR_VERSION >= 8
184 PHP_TARANTOOL_API zend_class_entry *php_tarantool_get_exception_base(int root);
185 #else
186 PHP_TARANTOOL_API zend_class_entry *php_tarantool_get_exception_base(int root TSRMLS_DC);
187 #endif
188 
189 #ifdef ZTS
190 #  define TARANTOOL_G(v) TSRMG(tarantool_globals_id, zend_tarantool_globals *, v)
191 #else
192 #  define TARANTOOL_G(v) (tarantool_globals.v)
193 #endif
194 
195 #endif  /* PHP_TARANTOOL_H */
196