Subversion-Projekte lars-tiefland.ci

Revision

Revision 2257 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
68 lars 1
<?php
2
/**
3
 * CodeIgniter
4
 *
5
 * An open source application development framework for PHP
6
 *
7
 * This content is released under the MIT License (MIT)
8
 *
2414 lars 9
 * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
68 lars 10
 *
11
 * Permission is hereby granted, free of charge, to any person obtaining a copy
12
 * of this software and associated documentation files (the "Software"), to deal
13
 * in the Software without restriction, including without limitation the rights
14
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
 * copies of the Software, and to permit persons to whom the Software is
16
 * furnished to do so, subject to the following conditions:
17
 *
18
 * The above copyright notice and this permission notice shall be included in
19
 * all copies or substantial portions of the Software.
20
 *
21
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
 * THE SOFTWARE.
28
 *
29
 * @package	CodeIgniter
30
 * @author	EllisLab Dev Team
31
 * @copyright	Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
2414 lars 32
 * @copyright	Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
33
 * @license	https://opensource.org/licenses/MIT	MIT License
68 lars 34
 * @link	https://codeigniter.com
35
 * @since	Version 1.0.0
36
 * @filesource
37
 */
38
defined('BASEPATH') OR exit('No direct script access allowed');
39
 
40
/**
41
 * Database Result Class
42
 *
43
 * This is the platform-independent result class.
44
 * This class will not be called directly. Rather, the adapter
45
 * class for the specific database will extend and instantiate it.
46
 *
47
 * @category	Database
48
 * @author		EllisLab Dev Team
49
 * @link		https://codeigniter.com/user_guide/database/
50
 */
51
class CI_DB_result {
52
 
53
	/**
54
	 * Connection ID
55
	 *
56
	 * @var	resource|object
57
	 */
58
	public $conn_id;
59
 
60
	/**
61
	 * Result ID
62
	 *
63
	 * @var	resource|object
64
	 */
65
	public $result_id;
66
 
67
	/**
68
	 * Result Array
69
	 *
70
	 * @var	array[]
71
	 */
72
	public $result_array			= array();
73
 
74
	/**
75
	 * Result Object
76
	 *
77
	 * @var	object[]
78
	 */
79
	public $result_object			= array();
80
 
81
	/**
82
	 * Custom Result Object
83
	 *
84
	 * @var	object[]
85
	 */
86
	public $custom_result_object		= array();
87
 
88
	/**
89
	 * Current Row index
90
	 *
91
	 * @var	int
92
	 */
93
	public $current_row			= 0;
94
 
95
	/**
96
	 * Number of rows
97
	 *
98
	 * @var	int
99
	 */
100
	public $num_rows;
101
 
102
	/**
103
	 * Row data
104
	 *
105
	 * @var	array
106
	 */
107
	public $row_data;
108
 
109
	// --------------------------------------------------------------------
110
 
111
	/**
112
	 * Constructor
113
	 *
114
	 * @param	object	$driver_object
115
	 * @return	void
116
	 */
117
	public function __construct(&$driver_object)
118
	{
119
		$this->conn_id = $driver_object->conn_id;
120
		$this->result_id = $driver_object->result_id;
121
	}
122
 
123
	// --------------------------------------------------------------------
124
 
125
	/**
126
	 * Number of rows in the result set
127
	 *
128
	 * @return	int
129
	 */
130
	public function num_rows()
131
	{
132
		if (is_int($this->num_rows))
133
		{
134
			return $this->num_rows;
135
		}
136
		elseif (count($this->result_array) > 0)
137
		{
138
			return $this->num_rows = count($this->result_array);
139
		}
140
		elseif (count($this->result_object) > 0)
141
		{
142
			return $this->num_rows = count($this->result_object);
143
		}
144
 
145
		return $this->num_rows = count($this->result_array());
146
	}
147
 
148
	// --------------------------------------------------------------------
149
 
150
	/**
151
	 * Query result. Acts as a wrapper function for the following functions.
152
	 *
153
	 * @param	string	$type	'object', 'array' or a custom class name
154
	 * @return	array
155
	 */
156
	public function result($type = 'object')
157
	{
158
		if ($type === 'array')
159
		{
160
			return $this->result_array();
161
		}
162
		elseif ($type === 'object')
163
		{
164
			return $this->result_object();
165
		}
2257 lars 166
 
167
		return $this->custom_result_object($type);
68 lars 168
	}
169
 
170
	// --------------------------------------------------------------------
171
 
172
	/**
173
	 * Custom query result.
174
	 *
175
	 * @param	string	$class_name
176
	 * @return	array
177
	 */
178
	public function custom_result_object($class_name)
179
	{
180
		if (isset($this->custom_result_object[$class_name]))
181
		{
182
			return $this->custom_result_object[$class_name];
183
		}
184
		elseif ( ! $this->result_id OR $this->num_rows === 0)
185
		{
186
			return array();
187
		}
188
 
189
		// Don't fetch the result set again if we already have it
190
		$_data = NULL;
191
		if (($c = count($this->result_array)) > 0)
192
		{
193
			$_data = 'result_array';
194
		}
195
		elseif (($c = count($this->result_object)) > 0)
196
		{
197
			$_data = 'result_object';
198
		}
199
 
200
		if ($_data !== NULL)
201
		{
202
			for ($i = 0; $i < $c; $i++)
203
			{
204
				$this->custom_result_object[$class_name][$i] = new $class_name();
205
 
206
				foreach ($this->{$_data}[$i] as $key => $value)
207
				{
208
					$this->custom_result_object[$class_name][$i]->$key = $value;
209
				}
210
			}
211
 
212
			return $this->custom_result_object[$class_name];
213
		}
214
 
215
		is_null($this->row_data) OR $this->data_seek(0);
216
		$this->custom_result_object[$class_name] = array();
217
 
218
		while ($row = $this->_fetch_object($class_name))
219
		{
220
			$this->custom_result_object[$class_name][] = $row;
221
		}
222
 
223
		return $this->custom_result_object[$class_name];
224
	}
225
 
226
	// --------------------------------------------------------------------
227
 
228
	/**
229
	 * Query result. "object" version.
230
	 *
231
	 * @return	array
232
	 */
233
	public function result_object()
234
	{
235
		if (count($this->result_object) > 0)
236
		{
237
			return $this->result_object;
238
		}
239
 
240
		// In the event that query caching is on, the result_id variable
241
		// will not be a valid resource so we'll simply return an empty
242
		// array.
243
		if ( ! $this->result_id OR $this->num_rows === 0)
244
		{
245
			return array();
246
		}
247
 
248
		if (($c = count($this->result_array)) > 0)
249
		{
250
			for ($i = 0; $i < $c; $i++)
251
			{
252
				$this->result_object[$i] = (object) $this->result_array[$i];
253
			}
254
 
255
			return $this->result_object;
256
		}
257
 
258
		is_null($this->row_data) OR $this->data_seek(0);
259
		while ($row = $this->_fetch_object())
260
		{
261
			$this->result_object[] = $row;
262
		}
263
 
264
		return $this->result_object;
265
	}
266
 
267
	// --------------------------------------------------------------------
268
 
269
	/**
270
	 * Query result. "array" version.
271
	 *
272
	 * @return	array
273
	 */
274
	public function result_array()
275
	{
276
		if (count($this->result_array) > 0)
277
		{
278
			return $this->result_array;
279
		}
280
 
281
		// In the event that query caching is on, the result_id variable
282
		// will not be a valid resource so we'll simply return an empty
283
		// array.
284
		if ( ! $this->result_id OR $this->num_rows === 0)
285
		{
286
			return array();
287
		}
288
 
289
		if (($c = count($this->result_object)) > 0)
290
		{
291
			for ($i = 0; $i < $c; $i++)
292
			{
293
				$this->result_array[$i] = (array) $this->result_object[$i];
294
			}
295
 
296
			return $this->result_array;
297
		}
298
 
299
		is_null($this->row_data) OR $this->data_seek(0);
300
		while ($row = $this->_fetch_assoc())
301
		{
302
			$this->result_array[] = $row;
303
		}
304
 
305
		return $this->result_array;
306
	}
307
 
308
	// --------------------------------------------------------------------
309
 
310
	/**
311
	 * Row
312
	 *
313
	 * A wrapper method.
314
	 *
315
	 * @param	mixed	$n
316
	 * @param	string	$type	'object' or 'array'
317
	 * @return	mixed
318
	 */
319
	public function row($n = 0, $type = 'object')
320
	{
321
		if ( ! is_numeric($n))
322
		{
323
			// We cache the row data for subsequent uses
324
			is_array($this->row_data) OR $this->row_data = $this->row_array(0);
325
 
326
			// array_key_exists() instead of isset() to allow for NULL values
327
			if (empty($this->row_data) OR ! array_key_exists($n, $this->row_data))
328
			{
329
				return NULL;
330
			}
331
 
332
			return $this->row_data[$n];
333
		}
334
 
335
		if ($type === 'object') return $this->row_object($n);
336
		elseif ($type === 'array') return $this->row_array($n);
2257 lars 337
 
338
		return $this->custom_row_object($n, $type);
68 lars 339
	}
340
 
341
	// --------------------------------------------------------------------
342
 
343
	/**
344
	 * Assigns an item into a particular column slot
345
	 *
346
	 * @param	mixed	$key
347
	 * @param	mixed	$value
348
	 * @return	void
349
	 */
350
	public function set_row($key, $value = NULL)
351
	{
352
		// We cache the row data for subsequent uses
353
		if ( ! is_array($this->row_data))
354
		{
355
			$this->row_data = $this->row_array(0);
356
		}
357
 
358
		if (is_array($key))
359
		{
360
			foreach ($key as $k => $v)
361
			{
362
				$this->row_data[$k] = $v;
363
			}
364
			return;
365
		}
366
 
367
		if ($key !== '' && $value !== NULL)
368
		{
369
			$this->row_data[$key] = $value;
370
		}
371
	}
372
 
373
	// --------------------------------------------------------------------
374
 
375
	/**
376
	 * Returns a single result row - custom object version
377
	 *
378
	 * @param	int	$n
379
	 * @param	string	$type
380
	 * @return	object
381
	 */
382
	public function custom_row_object($n, $type)
383
	{
2414 lars 384
		isset($this->custom_result_object[$type]) OR $this->custom_result_object[$type] = $this->custom_result_object($type);
68 lars 385
 
386
		if (count($this->custom_result_object[$type]) === 0)
387
		{
388
			return NULL;
389
		}
390
 
391
		if ($n !== $this->current_row && isset($this->custom_result_object[$type][$n]))
392
		{
393
			$this->current_row = $n;
394
		}
395
 
396
		return $this->custom_result_object[$type][$this->current_row];
397
	}
398
 
399
	// --------------------------------------------------------------------
400
 
401
	/**
402
	 * Returns a single result row - object version
403
	 *
404
	 * @param	int	$n
405
	 * @return	object
406
	 */
407
	public function row_object($n = 0)
408
	{
409
		$result = $this->result_object();
410
		if (count($result) === 0)
411
		{
412
			return NULL;
413
		}
414
 
415
		if ($n !== $this->current_row && isset($result[$n]))
416
		{
417
			$this->current_row = $n;
418
		}
419
 
420
		return $result[$this->current_row];
421
	}
422
 
423
	// --------------------------------------------------------------------
424
 
425
	/**
426
	 * Returns a single result row - array version
427
	 *
428
	 * @param	int	$n
429
	 * @return	array
430
	 */
431
	public function row_array($n = 0)
432
	{
433
		$result = $this->result_array();
434
		if (count($result) === 0)
435
		{
436
			return NULL;
437
		}
438
 
439
		if ($n !== $this->current_row && isset($result[$n]))
440
		{
441
			$this->current_row = $n;
442
		}
443
 
444
		return $result[$this->current_row];
445
	}
446
 
447
	// --------------------------------------------------------------------
448
 
449
	/**
450
	 * Returns the "first" row
451
	 *
452
	 * @param	string	$type
453
	 * @return	mixed
454
	 */
455
	public function first_row($type = 'object')
456
	{
457
		$result = $this->result($type);
458
		return (count($result) === 0) ? NULL : $result[0];
459
	}
460
 
461
	// --------------------------------------------------------------------
462
 
463
	/**
464
	 * Returns the "last" row
465
	 *
466
	 * @param	string	$type
467
	 * @return	mixed
468
	 */
469
	public function last_row($type = 'object')
470
	{
471
		$result = $this->result($type);
472
		return (count($result) === 0) ? NULL : $result[count($result) - 1];
473
	}
474
 
475
	// --------------------------------------------------------------------
476
 
477
	/**
478
	 * Returns the "next" row
479
	 *
480
	 * @param	string	$type
481
	 * @return	mixed
482
	 */
483
	public function next_row($type = 'object')
484
	{
485
		$result = $this->result($type);
486
		if (count($result) === 0)
487
		{
488
			return NULL;
489
		}
490
 
491
		return isset($result[$this->current_row + 1])
492
			? $result[++$this->current_row]
493
			: NULL;
494
	}
495
 
496
	// --------------------------------------------------------------------
497
 
498
	/**
499
	 * Returns the "previous" row
500
	 *
501
	 * @param	string	$type
502
	 * @return	mixed
503
	 */
504
	public function previous_row($type = 'object')
505
	{
506
		$result = $this->result($type);
507
		if (count($result) === 0)
508
		{
509
			return NULL;
510
		}
511
 
512
		if (isset($result[$this->current_row - 1]))
513
		{
514
			--$this->current_row;
515
		}
516
		return $result[$this->current_row];
517
	}
518
 
519
	// --------------------------------------------------------------------
520
 
521
	/**
522
	 * Returns an unbuffered row and move pointer to next row
523
	 *
524
	 * @param	string	$type	'array', 'object' or a custom class name
525
	 * @return	mixed
526
	 */
527
	public function unbuffered_row($type = 'object')
528
	{
529
		if ($type === 'array')
530
		{
531
			return $this->_fetch_assoc();
532
		}
533
		elseif ($type === 'object')
534
		{
535
			return $this->_fetch_object();
536
		}
537
 
538
		return $this->_fetch_object($type);
539
	}
540
 
541
	// --------------------------------------------------------------------
542
 
543
	/**
544
	 * The following methods are normally overloaded by the identically named
545
	 * methods in the platform-specific driver -- except when query caching
546
	 * is used. When caching is enabled we do not load the other driver.
547
	 * These functions are primarily here to prevent undefined function errors
548
	 * when a cached result object is in use. They are not otherwise fully
549
	 * operational due to the unavailability of the database resource IDs with
550
	 * cached results.
551
	 */
552
 
553
	// --------------------------------------------------------------------
554
 
555
	/**
556
	 * Number of fields in the result set
557
	 *
558
	 * Overridden by driver result classes.
559
	 *
560
	 * @return	int
561
	 */
562
	public function num_fields()
563
	{
564
		return 0;
565
	}
566
 
567
	// --------------------------------------------------------------------
568
 
569
	/**
570
	 * Fetch Field Names
571
	 *
572
	 * Generates an array of column names.
573
	 *
574
	 * Overridden by driver result classes.
575
	 *
576
	 * @return	array
577
	 */
578
	public function list_fields()
579
	{
580
		return array();
581
	}
582
 
583
	// --------------------------------------------------------------------
584
 
585
	/**
586
	 * Field data
587
	 *
588
	 * Generates an array of objects containing field meta-data.
589
	 *
590
	 * Overridden by driver result classes.
591
	 *
592
	 * @return	array
593
	 */
594
	public function field_data()
595
	{
596
		return array();
597
	}
598
 
599
	// --------------------------------------------------------------------
600
 
601
	/**
602
	 * Free the result
603
	 *
604
	 * Overridden by driver result classes.
605
	 *
606
	 * @return	void
607
	 */
608
	public function free_result()
609
	{
610
		$this->result_id = FALSE;
611
	}
612
 
613
	// --------------------------------------------------------------------
614
 
615
	/**
616
	 * Data Seek
617
	 *
618
	 * Moves the internal pointer to the desired offset. We call
619
	 * this internally before fetching results to make sure the
620
	 * result set starts at zero.
621
	 *
622
	 * Overridden by driver result classes.
623
	 *
624
	 * @param	int	$n
625
	 * @return	bool
626
	 */
627
	public function data_seek($n = 0)
628
	{
629
		return FALSE;
630
	}
631
 
632
	// --------------------------------------------------------------------
633
 
634
	/**
635
	 * Result - associative array
636
	 *
637
	 * Returns the result set as an array.
638
	 *
639
	 * Overridden by driver result classes.
640
	 *
641
	 * @return	array
642
	 */
643
	protected function _fetch_assoc()
644
	{
645
		return array();
646
	}
647
 
648
	// --------------------------------------------------------------------
649
 
650
	/**
651
	 * Result - object
652
	 *
653
	 * Returns the result set as an object.
654
	 *
655
	 * Overridden by driver result classes.
656
	 *
657
	 * @param	string	$class_name
658
	 * @return	object
659
	 */
660
	protected function _fetch_object($class_name = 'stdClass')
661
	{
1257 lars 662
		return new $class_name();
68 lars 663
	}
664
 
665
}