Result   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 266
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 1

Importance

Changes 5
Bugs 3 Features 0
Metric Value
wmc 21
c 5
b 3
f 0
lcom 3
cbo 1
dl 0
loc 266
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 25 5
A bindColumn() 0 4 1
A bindParam() 0 4 1
A bindValue() 0 4 1
A execute() 0 12 1
B fetch() 0 34 5
A fetchAll() 0 13 2
A fetchColumn() 0 5 1
A fetchObject() 0 4 1
A rowCount() 0 4 1
A errorCode() 0 4 1
A errorInfo() 0 4 1
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 result class to emulate PDOStatement Class - only implements
20
 * data-fetching methods
21
 *
22
 * @package Query
23
 * @subpackage Drivers
24
 */
25
class Result extends \PDOStatement {
26
27
	/**
28
	 * Reference to fbird resource
29
	 *
30
	 * @var resource
31
	 */
32
	private $statement;
33
34
	/**
35
	 * Current row in result array
36
	 *
37
	 * @var int
38
	 */
39
	private $row;
40
41
	/**
42
	 * Data pulled from query
43
	 *
44
	 * @param mixed
45
	 */
46
	private $result = array();
47
48
	/**
49
	 * Reference to the db drive to de-duplicate error functions
50
	 *
51
	 * @var Driver
52
	 */
53
	private $db;
54
55
	/**
56
	 * Create the object by passing the resource for
57
	 * the query
58
	 *
59
	 * @param resource $link
60
	 * @param Driver|null $db
61
	 */
62
	public function __construct($link, Driver $db = NULL)
63
	{
64
		if ( ! is_null($db))
65
		{
66
			$this->db = $db;
67
		}
68
		$this->statement = $link;
69
		$this->setFetchMode(\PDO::FETCH_ASSOC);
70
		$this->row = -1;
71
		$this->result = array();
72
73
		// Create the result array, so that we can get row counts
74
		// Check the resource type, because prepared statements are "interbase query"
75
		// but we only want "interbase result" types when attempting to fetch data
76
		if (\is_resource($link) && \get_resource_type($link) === "interbase result")
77
		{
78
			while($row = \fbird_fetch_assoc($link, \IBASE_FETCH_BLOBS))
79
			{
80
				$this->result[] = $row;
81
			}
82
83
			// Free the result resource
84
			\fbird_free_result($link);
85
		}
86
	}
87
88
	// --------------------------------------------------------------------------
89
90
	/**
91
	 * Invalidate method for data consistency
92
	 *
93
	 * @param mixed $column
94
	 * @param mixed $param
95
	 * @param int $type
96
	 * @param mixed $maxlen
97
	 * @param array $driverdata
98
	 * @return NULL
99
	 */
100
	public function bindColumn($column, &$param, $type=NULL, $maxlen=NULL, $driverdata=NULL)
101
	{
102
		return NULL;
103
	}
104
105
	// --------------------------------------------------------------------------
106
107
	/**
108
	 * Invalidate method for data consistency
109
	 *
110
	 * @param mixed $parameter
111
	 * @param mixed $variable
112
	 * @param int $data_type
113
	 * @param mixed $maxlen
114
	 * @param array $driverdata
115
	 * @return NULL
116
	 */
117
	public function bindParam($parameter, &$variable, $data_type=NULL, $maxlen=NULL, $driverdata=NULL)
118
	{
119
		return NULL;
120
	}
121
122
	// --------------------------------------------------------------------------
123
124
	/**
125
	 * Invalidate method for data consistency
126
	 *
127
	 * @param mixed $parameter
128
	 * @param mixed $variable
129
	 * @param int $data_type
130
	 * @return NULL
131
	 */
132
	public function bindValue($parameter, $variable, $data_type=NULL)
133
	{
134
		return NULL;
135
	}
136
137
	// --------------------------------------------------------------------------
138
139
	/**
140
	 * Run a prepared statement query
141
	 *
142
	 * @param  array $args
143
	 * @return Result
144
	 */
145
	public function execute($args = NULL)
146
	{
147
		//Add the prepared statement as the first parameter
148
		\array_unshift($args, $this->statement);
149
150
		// Let php do all the hard stuff in converting
151
		// the array of arguments into a list of arguments
152
		// Then pass the resource to the constructor
153
		$this->__construct(\call_user_func_array('fbird_execute', $args));
154
155
		return $this;
156
	}
157
158
	// --------------------------------------------------------------------------
159
160
	/**
161
	 * Emulate PDO fetch public function
162
	 *
163
	 * @param int $fetch_style
164
	 * @param mixed $cursor_orientation
165
	 * @param mixed $cursor_offset
166
	 * @return mixed
167
	 */
168
	public function fetch($fetch_style=\PDO::FETCH_ASSOC, $cursor_orientation = \PDO::FETCH_ORI_NEXT, $cursor_offset=NULL)
169
	{
170
		// If there is no result, continue
171
		if (empty($this->result))
172
		{
173
			return NULL;
174
		}
175
176
		// Keep track of the current row being fetched
177
		++$this->row;
178
179
		// return NULL if the next row doesn't exist
180
		if ( ! isset($this->result[$this->row]))
181
		{
182
			return NULL;
183
		}
184
185
		switch($fetch_style)
186
		{
187
			case \PDO::FETCH_OBJ:
188
				$row = (object) $this->result[$this->row];
189
			break;
190
191
			case \PDO::FETCH_NUM:
192
				$row = \array_values($this->result[$this->row]);
193
			break;
194
195
			default:
196
				$row = $this->result[$this->row];
197
			break;
198
		}
199
200
		return $row;
201
	}
202
203
	// --------------------------------------------------------------------------
204
205
	/**
206
	 * Emulate PDO fetchAll public function
207
	 *
208
	 * @param int  $fetch_style
209
	 * @param mixed $statement
210
	 * @param mixed $ctor_args
211
	 * @return mixed
212
	 */
213
	public function fetchAll($fetch_style=\PDO::FETCH_ASSOC, $statement=NULL, $ctor_args=NULL)
214
	{
215
		$all = array();
216
217
		while($row = $this->fetch($fetch_style, $statement))
218
		{
219
			$all[] = $row;
220
		}
221
222
		$this->result = $all;
223
224
		return $all;
225
	}
226
227
	// --------------------------------------------------------------------------
228
229
	/**
230
	 * Emulate PDOStatement::fetchColumn
231
	 *
232
	 * @param int $column_num
233
	 * @return mixed
234
	 */
235
	public function fetchColumn($column_num=0)
236
	{
237
		$row = $this->fetch(\PDO::FETCH_NUM);
238
		return $row[$column_num];
239
	}
240
241
	// --------------------------------------------------------------------------
242
243
	/**
244
	 * Emulate PDOStatement::fetchObject, but only for the default use
245
	 *
246
	 * @param string $class_name
247
	 * @param array $ctor_args
248
	 * @return stdClass
249
	 */
250
	public function fetchObject($class_name='stdClass', $ctor_args=array())
251
	{
252
		return $this->fetch(\PDO::FETCH_OBJ);
253
	}
254
255
	// --------------------------------------------------------------------------
256
257
	/**
258
	 * Return the number of rows affected by the previous query
259
	 *
260
	 * @return int
261
	 */
262
	public function rowCount()
263
	{
264
		return \fbird_affected_rows();
265
	}
266
267
	// --------------------------------------------------------------------------
268
269
	/**
270
	 * Method to emulate PDOStatement->errorCode
271
	 *
272
	 * @return string
273
	 */
274
	public function errorCode()
275
	{
276
		return $this->db->errorCode();
277
	}
278
279
	// --------------------------------------------------------------------------
280
281
	/**
282
	 * Method to emulate PDO->errorInfo / PDOStatement->errorInfo
283
	 *
284
	 * @return array
285
	 */
286
	public function errorInfo()
287
	{
288
		return $this->db->errorInfo();
289
	}
290
}
291
// End of firebird_result.php