Driver::errorInfo()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
/**
3
 * Query
4
 *
5
 * Free Query Builder / Database Abstraction Layer
6
 *
7
 * @package		Query
8
 * @author		Timothy J. Warren
9
 * @copyright	Copyright (c) 2012 - 2015
10
 * @link 		https://github.com/aviat4ion/Query
11
 * @license		http://philsturgeon.co.uk/code/dbad-license
12
 */
13
14
// --------------------------------------------------------------------------
15
16
namespace Query\Drivers\Firebird;
17
18
/**
19
 * Firebird Database class
20
 *
21
 * PDO-firebird isn't stable, so this is a wrapper of the fbird_ public functions.
22
 *
23
 * @package Query
24
 * @subpackage Drivers
25
 */
26
class Driver extends \Query\AbstractDriver {
27
28
	/**
29
	 * Reference to the last query executed
30
	 *
31
	 * @var object
32
	 */
33
	protected $statement = NULL;
34
35
	/**
36
	 * Reference to the resource returned by
37
	 * the last query executed
38
	 *
39
	 * @var resource
40
	 */
41
	protected $statement_link = NULL;
42
43
	/**
44
	 * Reference to the current transaction
45
	 *
46
	 * @var resource
47
	 */
48
	protected $trans = NULL;
49
50
	/**
51
	 * Reference to the connection resource
52
	 *
53
	 * @var resource
54
	 */
55
	protected $conn = NULL;
56
57
	/**
58
	 * Reference to the service resource
59
	 *
60
	 * @var resource
61
	 */
62
	protected $service = NULL;
63
64
	/**
65
	 * Firebird doesn't have the truncate keyword
66
	 *
67
	 * @var bool
68
	 */
69
	protected $has_truncate = FALSE;
70
71
	/**
72
	 * Open the link to the database
73
	 *
74
	 * @param string $dbpath
75
	 * @param string $user
76
	 * @param string $pass
77
	 * @param array $options
78
	 * @throws \PDOException
79
	 */
80
	public function __construct($dbpath, $user='SYSDBA', $pass='masterkey', array $options = array())
81
	{
82
		$connect_function = (isset($options[\PDO::ATTR_PERSISTENT]) && $options[\PDO::ATTR_PERSISTENT])
83
			? '\\fbird_pconnect'
84
			: '\\fbird_connect';
85
86
		$this->conn = $connect_function($dbpath, $user, $pass, 'utf-8', 0);
87
		$this->service = \fbird_service_attach('localhost', $user, $pass);
88
89
		// Throw an exception to make this match other pdo classes
90
		if ( ! \is_resource($this->conn))
91
		{
92
			throw new \PDOException(\fbird_errmsg(), \fbird_errcode(), NULL);
93
		}
94
95
		// Load these classes here because this
96
		// driver does not call the constructor
97
		// of DB_PDO, which defines these
98
		// class variables for the other drivers
99
		$this->_load_sub_classes();
100
	}
101
102
	// --------------------------------------------------------------------------
103
104
	/**
105
	 * Cleanup some loose ends
106
	 * @codeCoverageIgnore
107
	 */
108
	public function __destruct()
109
	{
110
		\fbird_service_detach($this->service);
111
	}
112
113
	// --------------------------------------------------------------------------
114
115
	/**
116
	 * Return service handle
117
	 *
118
	 * @return resource
119
	 */
120
	public function get_service()
121
	{
122
		return $this->service;
123
	}
124
125
126
	// --------------------------------------------------------------------------
127
128
	/**
129
	 * Execute an sql statement and return number of affected rows
130
	 *
131
	 * @param string $sql
132
	 * @return int
133
	 */
134
	public function exec($sql)
135
	{
136
		return NULL;
137
	}
138
139
	// --------------------------------------------------------------------------
140
141
	/**
142
	 * Implement for compatibility with PDO
143
	 *
144
	 * @param int $attribute
145
	 * @return mixed
146
	 */
147
	public function getAttribute($attribute)
148
	{
149
		return NULL;
150
	}
151
152
	// --------------------------------------------------------------------------
153
154
	/**
155
	 * Return whether the current statement is in a transaction
156
	 *
157
	 * @return bool
158
	 */
159
	public function inTransaction()
160
	{
161
		return ! is_null($this->trans);
162
	}
163
164
	// --------------------------------------------------------------------------
165
166
	/**
167
	 * Returns the last value of the specified generator
168
	 *
169
	 * @param string $name
170
	 * @return mixed
171
	 */
172
	public function lastInsertId($name = NULL)
173
	{
174
		return \fbird_gen_id($name, 0, $this->conn);
175
	}
176
177
	// --------------------------------------------------------------------------
178
179
	/**
180
	 * Wrapper public function to better match PDO
181
	 *
182
	 * @param string $sql
183
	 * @return Result
184
	 * @throws PDOException
185
	 */
186
	public function query($sql = '')
187
	{
188
189
		if (empty($sql))
190
		{
191
			throw new \PDOException("Query method requires an sql query!", 0, NULL);
192
		}
193
194
		$this->statement_link = (isset($this->trans))
195
			? \fbird_query($this->trans, $sql)
196
			: \fbird_query($this->conn, $sql);
197
198
		// Throw the error as a exception
199
		$err_string = \fbird_errmsg() . "Last query:" . $this->get_last_query();
200
		if ($this->statement_link === FALSE)
201
		{
202
			throw new \PDOException($err_string, \fbird_errcode(), NULL);
203
		}
204
205
		$this->statement = new Result($this->statement_link, $this);
206
207
		return $this->statement;
208
	}
209
210
	// --------------------------------------------------------------------------
211
212
	/**
213
	 * Emulate PDO prepare
214
	 *
215
	 * @param string $query
216
	 * @param array $options
217
	 * @return Result
218
	 * @throws \PDOException
219
	 */
220
	public function prepare($query, $options=array())
221
	{
222
		$this->statement_link = \fbird_prepare($this->conn, $query);
223
224
		// Throw the error as an exception
225
		if ($this->statement_link === FALSE)
226
		{
227
			throw new \PDOException(\fbird_errmsg(), \fbird_errcode(), NULL);
228
		}
229
230
		$this->statement = new Result($this->statement_link, $this);
231
232
		return $this->statement;
233
	}
234
235
	// --------------------------------------------------------------------------
236
237
	/**
238
	 * Start a database transaction
239
	 *
240
	 * @return boolean|null
241
	 */
242
	public function beginTransaction()
243
	{
244
		return (($this->trans = \fbird_trans($this->conn)) !== NULL) ? TRUE : NULL;
245
	}
246
247
	// --------------------------------------------------------------------------
248
249
	/**
250
	 * Commit a database transaction
251
	 *
252
	 * @return bool
253
	 */
254
	public function commit()
255
	{
256
		$res = \fbird_commit($this->trans);
257
		$this->trans = NULL;
258
		return $res;
259
	}
260
261
	// --------------------------------------------------------------------------
262
263
	/**
264
	 * Rollback a transaction
265
	 *
266
	 * @return bool
267
	 */
268
	public function rollBack()
269
	{
270
		$res = \fbird_rollback($this->trans);
271
		$this->trans = NULL;
272
		return $res;
273
	}
274
275
	// --------------------------------------------------------------------------
276
277
	/**
278
	 * Set a connection attribute
279
	 * @param int $attribute
280
	 * @param mixed $value
281
	 * @return bool
282
	 */
283
	public function setAttribute($attribute, $value)
284
	{
285
		return FALSE;
286
	}
287
288
	// --------------------------------------------------------------------------
289
290
	/**
291
	 * Prepare and execute a query
292
	 *
293
	 * @param string $sql
294
	 * @param array $args
295
	 * @return Result
296
	 */
297
	public function prepare_execute($sql, $args)
298
	{
299
		$query = $this->prepare($sql);
300
301
		// Set the statement in the class variable for easy later access
302
		$this->statement_link =& $query;
303
304
		return $query->execute($args);
305
	}
306
307
	// --------------------------------------------------------------------------
308
309
	/**
310
	 * Method to emulate PDO->quote
311
	 *
312
	 * @param string $str
313
	 * @param int $param_type
314
	 * @return string
315
	 */
316
	public function quote($str, $param_type = \PDO::PARAM_STR)
317
	{
318
		if(is_numeric($str))
319
		{
320
			return $str;
321
		}
322
323
		return "'".str_replace("'", "''", $str)."'";
324
	}
325
326
	// --------------------------------------------------------------------------
327
328
	/**
329
	 * Method to emulate PDO->errorInfo / PDOStatement->errorInfo
330
	 *
331
	 * @return array
332
	 */
333
	public function errorInfo()
334
	{
335
		$code = \fbird_errcode();
336
		$msg = \fbird_errmsg();
337
338
		return array(0, $code, $msg);
339
	}
340
341
	// --------------------------------------------------------------------------
342
343
	/**
344
	 * Method to emulate PDO->errorCode
345
	 *
346
	 * @return array
347
	 */
348
	public function errorCode()
349
	{
350
		return \fbird_errcode();
351
	}
352
353
	// --------------------------------------------------------------------------
354
355
	/**
356
	 * Bind a prepared query with arguments for executing
357
	 *
358
	 * @param string $sql
359
	 * @param array $params
360
	 * @return NULL
361
	 */
362
	public function prepare_query($sql, $params)
363
	{
364
		// You can't bind query statements before execution with
365
		// the firebird database
366
		return NULL;
367
	}
368
369
	// --------------------------------------------------------------------------
370
371
	/**
372
	 * Create sql for batch insert
373
	 *
374
	 * @param string $table
375
	 * @param array $data
376
	 * @return array
377
	 */
378
	public function insert_batch($table, $data=array())
379
	{
380
		// Each member of the data array needs to be an array
381
		if ( ! is_array(current($data)))
382
		{
383
			return NULL;
384
		}
385
386
		// Start the block of sql statements
387
		$sql = "EXECUTE BLOCK AS BEGIN\n";
388
389
		$table = $this->quote_table($table);
390
		$fields = \array_keys(\current($data));
391
392
		$insert_template = "INSERT INTO {$table} ("
393
			. implode(',', $this->quote_ident($fields))
394
			. ") VALUES (";
395
396 View Code Duplication
		foreach($data as $item)
397
		{
398
			// Quote string values
399
			$vals = array_map(array($this, 'quote'), $item);
400
401
			// Add the values in the sql
402
			$sql .= $insert_template . implode(', ', $vals) . ");\n";
403
		}
404
405
		// End the block of SQL statements
406
		$sql .= "END";
407
408
		// Return a null array value so the query is run as it is,
409
		// not as a prepared statement, because a prepared statement
410
		// doesn't work for this type of query in Firebird.
411
		return array($sql, NULL);
412
	}
413
}
414
// End of firebird_driver.php