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