1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2015 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    | Authors: Jouni Ahto <jouni.ahto@exdec.fi>                            |
16    |          Andrew Avdeev <andy@simgts.mv.ru>                           |
17    |          Ard Biesheuvel <a.k.biesheuvel@ewi.tudelft.nl>              |
18    +----------------------------------------------------------------------+
19  */
20 
21 #ifndef PHP_IBASE_INCLUDES_H
22 #define PHP_IBASE_INCLUDES_H
23 
24 #include <ibase.h>
25 
26 #ifndef SQLDA_CURRENT_VERSION
27 #define SQLDA_CURRENT_VERSION SQLDA_VERSION1
28 #endif
29 
30 #ifndef METADATALENGTH
31 #define METADATALENGTH 68
32 #endif
33 
34 #define RESET_ERRMSG do { IBG(errmsg)[0] = '\0'; IBG(sql_code) = 0; } while (0)
35 
36 #define IB_STATUS (IBG(status))
37 
38 #ifdef ZEND_DEBUG_
39 #define IBDEBUG(a) php_printf("::: %s (%d)\n", a, __LINE__);
40 #endif
41 
42 #ifndef IBDEBUG
43 #define IBDEBUG(a)
44 #endif
45 
46 extern int le_link, le_plink, le_trans;
47 
48 #define LE_LINK "Firebird/InterBase link"
49 #define LE_PLINK "Firebird/InterBase persistent link"
50 #define LE_TRANS "Firebird/InterBase transaction"
51 
52 #define IBASE_MSGSIZE 512
53 #define MAX_ERRMSG (IBASE_MSGSIZE*2)
54 
55 #define IB_DEF_DATE_FMT "%Y-%m-%d"
56 #define IB_DEF_TIME_FMT "%H:%M:%S"
57 
58 /* this value should never be > USHRT_MAX */
59 #define IBASE_BLOB_SEG 4096
60 
61 ZEND_BEGIN_MODULE_GLOBALS(ibase)
62 	ISC_STATUS status[20];
63 	long default_link;
64 	long num_links, num_persistent;
65 	char errmsg[MAX_ERRMSG];
66 	long sql_code;
67 ZEND_END_MODULE_GLOBALS(ibase)
68 
69 ZEND_EXTERN_MODULE_GLOBALS(ibase)
70 
71 typedef struct {
72 	isc_db_handle handle;
73 	struct tr_list *tr_list;
74 	unsigned short dialect;
75 	struct event *event_head;
76 } ibase_db_link;
77 
78 typedef struct {
79 	isc_tr_handle handle;
80 	unsigned short link_cnt;
81 	unsigned long affected_rows;
82 	ibase_db_link *db_link[1]; /* last member */
83 } ibase_trans;
84 
85 typedef struct tr_list {
86 	ibase_trans *trans;
87 	struct tr_list *next;
88 } ibase_tr_list;
89 
90 typedef struct {
91 	isc_blob_handle bl_handle;
92 	unsigned short type;
93 	ISC_QUAD bl_qd;
94 } ibase_blob;
95 
96 typedef struct event {
97 	ibase_db_link *link;
98 	long link_res_id;
99 	ISC_LONG event_id;
100 	unsigned short event_count;
101 	char **events;
102 	char *event_buffer, *result_buffer;
103 	zval *callback;
104 	void **thread_ctx;
105 	struct event *event_next;
106 	enum event_state { NEW, ACTIVE, DEAD } state;
107 } ibase_event;
108 
109 enum php_interbase_option {
110 	PHP_IBASE_DEFAULT 			= 0,
111 	PHP_IBASE_CREATE            = 0,
112 	/* fetch flags */
113 	PHP_IBASE_FETCH_BLOBS		= 1,
114 	PHP_IBASE_FETCH_ARRAYS      = 2,
115 	PHP_IBASE_UNIXTIME 			= 4,
116 	/* transaction access mode */
117 	PHP_IBASE_WRITE 			= 1,
118 	PHP_IBASE_READ 				= 2,
119 	/* transaction isolation level */
120 	PHP_IBASE_CONCURRENCY 		= 4,
121 	PHP_IBASE_COMMITTED 		= 8,
122 	  PHP_IBASE_REC_NO_VERSION 	= 32,
123 	  PHP_IBASE_REC_VERSION 	= 64,
124 	PHP_IBASE_CONSISTENCY 		= 16,
125 	/* transaction lock resolution */
126 	PHP_IBASE_WAIT 				= 128,
127 	PHP_IBASE_NOWAIT 			= 256
128 };
129 
130 #ifdef ZTS
131 #define IBG(v) TSRMG(ibase_globals_id, zend_ibase_globals *, v)
132 #else
133 #define IBG(v) (ibase_globals.v)
134 #endif
135 
136 #define BLOB_ID_LEN		18
137 #define BLOB_ID_MASK	"0x%" LL_MASK "x"
138 
139 #define BLOB_INPUT		1
140 #define BLOB_OUTPUT		2
141 
142 #ifdef PHP_WIN32
143 #define LL_MASK "I64"
144 #define LL_LIT(lit) lit ## I64
145 typedef void (__stdcall *info_func_t)(char*);
146 #else
147 #define LL_MASK "ll"
148 #define LL_LIT(lit) lit ## ll
149 typedef void (*info_func_t)(char*);
150 #endif
151 
152 void _php_ibase_error(TSRMLS_D);
153 void _php_ibase_module_error(char * TSRMLS_DC, ...)
154 	PHP_ATTRIBUTE_FORMAT(printf,1,PHP_ATTR_FMT_OFFSET +2);
155 
156 /* determine if a resource is a link or transaction handle */
157 #define PHP_IBASE_LINK_TRANS(pzval, lh, th)													\
158 	do { if (!pzval) {																		\
159 			ZEND_FETCH_RESOURCE2(lh, ibase_db_link *, NULL, IBG(default_link),				\
160 				"InterBase link", le_link, le_plink) }										\
161 		else																				\
162 			_php_ibase_get_link_trans(INTERNAL_FUNCTION_PARAM_PASSTHRU, &pzval, &lh, &th);	\
163 		if (SUCCESS != _php_ibase_def_trans(lh, &th TSRMLS_CC)) { RETURN_FALSE; }			\
164 	} while (0)
165 
166 int _php_ibase_def_trans(ibase_db_link *ib_link, ibase_trans **trans TSRMLS_DC);
167 void _php_ibase_get_link_trans(INTERNAL_FUNCTION_PARAMETERS, zval **link_id,
168 	ibase_db_link **ib_link, ibase_trans **trans);
169 
170 /* provided by ibase_query.c */
171 void php_ibase_query_minit(INIT_FUNC_ARGS);
172 
173 /* provided by ibase_blobs.c */
174 void php_ibase_blobs_minit(INIT_FUNC_ARGS);
175 int _php_ibase_string_to_quad(char const *id, ISC_QUAD *qd);
176 char *_php_ibase_quad_to_string(ISC_QUAD const qd);
177 int _php_ibase_blob_get(zval *return_value, ibase_blob *ib_blob, unsigned long max_len TSRMLS_DC);
178 int _php_ibase_blob_add(zval **string_arg, ibase_blob *ib_blob TSRMLS_DC);
179 
180 /* provided by ibase_events.c */
181 void php_ibase_events_minit(INIT_FUNC_ARGS);
182 void _php_ibase_free_event(ibase_event *event TSRMLS_DC);
183 
184 /* provided by ibase_service.c */
185 void php_ibase_service_minit(INIT_FUNC_ARGS);
186 
187 #ifndef max
188 #define max(a,b) ((a)>(b)?(a):(b))
189 #endif
190 
191 #endif /* PHP_IBASE_INCLUDES_H */
192 
193 /*
194  * Local variables:
195  * tab-width: 4
196  * c-basic-offset: 4
197  * End:
198  */
199