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