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: Ard Biesheuvel <abies@php.net>                               |
14   +----------------------------------------------------------------------+
15 */
16 
17 /* internal header; not supposed to be installed */
18 
19 #ifndef PHP_PDO_FIREBIRD_INT_H
20 #define PHP_PDO_FIREBIRD_INT_H
21 
22 #include <ibase.h>
23 
24 #ifdef SQLDA_VERSION
25 #define PDO_FB_SQLDA_VERSION SQLDA_VERSION
26 #else
27 #define PDO_FB_SQLDA_VERSION 1
28 #endif
29 
30 #define PDO_FB_DIALECT 3
31 
32 #define PDO_FB_DEF_DATE_FMT "%Y-%m-%d"
33 #define PDO_FB_DEF_TIME_FMT "%H:%M:%S"
34 #define PDO_FB_DEF_TIMESTAMP_FMT PDO_FB_DEF_DATE_FMT " " PDO_FB_DEF_TIME_FMT
35 
36 #define SHORT_MAX (1 << (8*sizeof(short)-1))
37 
38 #if SIZEOF_ZEND_LONG == 8 && !defined(PHP_WIN32)
39 # define LL_LIT(lit) lit ## L
40 #else
41 # define LL_LIT(lit) lit ## LL
42 #endif
43 #define LL_MASK "ll"
44 
45 /* Firebird API has a couple of missing const decls in its API */
46 #define const_cast(s) ((char*)(s))
47 
48 #ifdef PHP_WIN32
49 typedef void (__stdcall *info_func_t)(char*);
50 #else
51 typedef void (*info_func_t)(char*);
52 #endif
53 
54 #ifndef min
55 #define min(a,b) ((a)<(b)?(a):(b))
56 #endif
57 
58 #if defined(_LP64) || defined(__LP64__) || defined(__arch64__) || defined(_WIN64)
59 # define PDO_FIREBIRD_HANDLE_INITIALIZER 0U
60 #else
61 # define PDO_FIREBIRD_HANDLE_INITIALIZER NULL
62 #endif
63 
64 typedef struct {
65 	int sqlcode;
66 	char *errmsg;
67 	size_t errmsg_length;
68 } pdo_firebird_error_info;
69 
70 typedef struct {
71 	/* the result of the last API call */
72 	ISC_STATUS isc_status[20];
73 
74 	/* the connection handle */
75 	isc_db_handle db;
76 
77 	/* the transaction handle */
78 	isc_tr_handle tr;
79 	bool in_manually_txn;
80 	bool is_writable_txn;
81 	zend_ulong txn_isolation_level;
82 
83 	/* date and time format strings, can be set by the set_attribute method */
84 	char *date_format;
85 	char *time_format;
86 	char *timestamp_format;
87 
88 	unsigned sql_dialect:2;
89 
90 	/* prepend table names on column names in fetch */
91 	unsigned fetch_table_names:1;
92 
93 	unsigned _reserved:29;
94 
95 	pdo_firebird_error_info einfo;
96 } pdo_firebird_db_handle;
97 
98 
99 typedef struct {
100 	/* the link that owns this statement */
101 	pdo_firebird_db_handle *H;
102 
103 	/* the statement handle */
104 	isc_stmt_handle stmt;
105 
106 	/* the name of the cursor (if it has one) */
107 	char name[32];
108 
109 	/* the type of statement that was issued */
110 	char statement_type:8;
111 
112 	/* whether EOF was reached for this statement */
113 	unsigned exhausted:1;
114 
115 	/* successful isc_dsql_execute opens a cursor */
116 	unsigned cursor_open:1;
117 
118 	unsigned _reserved:22;
119 
120 	/* the named params that were converted to ?'s by the driver */
121 	HashTable *named_params;
122 
123 	/* the input SQLDA */
124 	XSQLDA *in_sqlda;
125 
126 	/* the output SQLDA */
127 	XSQLDA out_sqlda; /* last member */
128 } pdo_firebird_stmt;
129 
130 extern const pdo_driver_t pdo_firebird_driver;
131 
132 extern const struct pdo_stmt_methods firebird_stmt_methods;
133 
134 extern void php_firebird_set_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, const char *state, const size_t state_len,
135 	const char *msg, const size_t msg_len);
136 #define php_firebird_error(d) php_firebird_set_error(d, NULL, NULL, 0, NULL, 0)
137 #define php_firebird_error_stmt(s) php_firebird_set_error(s->dbh, s, NULL, 0, NULL, 0)
138 #define php_firebird_error_with_info(d,e,el,m,ml) php_firebird_set_error(d, NULL, e, el, m, ml)
139 #define php_firebird_error_stmt_with_info(s,e,el,m,ml) php_firebird_set_error(s->dbh, s, e, el, m, ml)
140 
141 extern bool php_firebird_commit_transaction(pdo_dbh_t *dbh, bool retain);
142 
143 enum {
144 	PDO_FB_ATTR_DATE_FORMAT = PDO_ATTR_DRIVER_SPECIFIC,
145 	PDO_FB_ATTR_TIME_FORMAT,
146 	PDO_FB_ATTR_TIMESTAMP_FORMAT,
147 
148 	/*
149 	 * transaction isolation level
150 	 * firebird does not have a level equivalent to read uncommited.
151 	 */
152 	PDO_FB_TRANSACTION_ISOLATION_LEVEL,
153 	PDO_FB_READ_COMMITTED,
154 	PDO_FB_REPEATABLE_READ,
155 	PDO_FB_SERIALIZABLE,
156 
157 	/* transaction access mode */
158 	PDO_FB_WRITABLE_TRANSACTION,
159 };
160 
161 #endif	/* PHP_PDO_FIREBIRD_INT_H */
162