blob: 1c72216b1479fdf4f2305ed2fca31375ad237bbd [file] [log] [blame]
Timothy Warren80ab8162011-08-22 18:26:12 -04001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 5.1.6 or newer
6 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Timothy Warren80ab8162011-08-22 18:26:12 -040019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Timothy Warren80ab8162011-08-22 18:26:12 -040023 * @link http://codeigniter.com
Timothy Warren018af7a2011-09-07 12:07:35 -040024 * @since Version 2.1.0
Timothy Warren80ab8162011-08-22 18:26:12 -040025 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * PDO Result Class
32 *
33 * This class extends the parent result class: CI_DB_result
34 *
35 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Timothy Warren80ab8162011-08-22 18:26:12 -040037 * @link http://codeigniter.com/user_guide/database/
38 */
39class CI_DB_pdo_result extends CI_DB_result {
40
41 /**
Taufan Aditya18209332012-02-09 16:07:27 +070042 * @var bool Hold the flag whether a result handler already fetched before
43 */
44 protected $is_fetched = FALSE;
45
46 /**
47 * @var mixed Hold the fetched assoc array of a result handler
48 */
49 protected $result_assoc;
50
51 /**
Timothy Warren80ab8162011-08-22 18:26:12 -040052 * Number of rows in the result set
53 *
54 * @access public
55 * @return integer
56 */
57 function num_rows()
58 {
Taufan Aditya18209332012-02-09 16:07:27 +070059 if (empty($this->result_id) OR ! is_object($this->result_id))
60 {
61 // invalid result handler
62 return 0;
63 }
64 elseif (($num_rows = $this->result_id->rowCount()) && $num_rows > 0)
65 {
66 // If rowCount return something, we're done.
67 return $num_rows;
68 }
69
70 // Fetch the result, instead perform another extra query
71 return ($this->is_fetched && is_array($this->result_assoc)) ? count($this->result_assoc) : count($this->result_assoc());
72 }
73
74 /**
75 * Fetch the result handler
76 *
77 * @access public
78 * @return mixed
79 */
80 function result_assoc()
81 {
82 // If the result already fetched before, use that one
83 if (count($this->result_array) > 0 OR $this->is_fetched)
84 {
85 return $this->result_array();
86 }
87
88 // Define the output
89 $output = array('assoc', 'object');
90
91 // Fetch the result
92 foreach ($output as $type)
93 {
94 // Define the method and handler
95 $res_method = '_fetch_'.$type;
96 $res_handler = 'result_'.$type;
97
98 $this->$res_handler = array();
99 $this->_data_seek(0);
100
101 while ($row = $this->$res_method())
102 {
103 $this->{$res_handler}[] = $row;
104 }
105 }
106
107 // Save this as buffer and marked the fetch flag
108 $this->result_array = $this->result_assoc;
109 $this->is_fetched = TRUE;
110
111 return $this->result_assoc;
Timothy Warren80ab8162011-08-22 18:26:12 -0400112 }
113
114 // --------------------------------------------------------------------
115
116 /**
117 * Number of fields in the result set
118 *
119 * @access public
120 * @return integer
121 */
122 function num_fields()
123 {
Timothy Warrenab347582011-08-23 12:29:29 -0400124 return $this->result_id->columnCount();
Timothy Warren80ab8162011-08-22 18:26:12 -0400125 }
126
127 // --------------------------------------------------------------------
128
129 /**
130 * Fetch Field Names
131 *
132 * Generates an array of column names
133 *
134 * @access public
135 * @return array
136 */
137 function list_fields()
138 {
Timothy Warrenab347582011-08-23 12:29:29 -0400139 if ($this->db->db_debug)
Timothy Warren80ab8162011-08-22 18:26:12 -0400140 {
Timothy Warrenab347582011-08-23 12:29:29 -0400141 return $this->db->display_error('db_unsuported_feature');
Timothy Warren80ab8162011-08-22 18:26:12 -0400142 }
Taufan Aditya18209332012-02-09 16:07:27 +0700143
Timothy Warrenab347582011-08-23 12:29:29 -0400144 return FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400145 }
146
147 // --------------------------------------------------------------------
148
149 /**
150 * Field data
151 *
152 * Generates an array of objects containing field meta-data
153 *
154 * @access public
155 * @return array
156 */
157 function field_data()
158 {
Timothy Warrenab347582011-08-23 12:29:29 -0400159 $data = array();
160
161 try
Timothy Warren80ab8162011-08-22 18:26:12 -0400162 {
Taufan Aditya4e44b342012-02-18 22:47:36 +0700163 if (strpos($this->result_id->queryString, 'PRAGMA') !== FALSE)
Timothy Warrenab347582011-08-23 12:29:29 -0400164 {
Taufan Aditya865ce1e2012-03-17 02:19:42 +0700165 foreach ($this->result_array() as $field)
Taufan Aditya4e44b342012-02-18 22:47:36 +0700166 {
167 preg_match('/([a-zA-Z]+)(\(\d+\))?/', $field['type'], $matches);
168
169 $F = new stdClass();
170 $F->name = $field['name'];
171 $F->type = ( ! empty($matches[1])) ? $matches[1] : NULL;
172 $F->default = NULL;
173 $F->max_length = ( ! empty($matches[2])) ? preg_replace('/[^\d]/', '', $matches[2]) : NULL;
174 $F->primary_key = (int) $field['pk'];
175 $F->pdo_type = NULL;
176
177 $data[] = $F;
178 }
179 }
180 else
181 {
182 for($i = 0, $max = $this->num_fields(); $i < $max; $i++)
183 {
184 $field = $this->result_id->getColumnMeta($i);
185
186 $F = new stdClass();
187 $F->name = $field['name'];
188 $F->type = $field['native_type'];
189 $F->default = NULL;
190 $F->pdo_type = $field['pdo_type'];
191
192 if ($field['precision'] < 0)
193 {
194 $F->max_length = NULL;
195 $F->primary_key = 0;
196 }
197 else
198 {
Taufan Aditya4a801542012-03-18 01:00:36 +0700199 $F->max_length = ($field['len'] > 255) ? 0 : $field['len'];
Taufan Aditya865ce1e2012-03-17 02:19:42 +0700200 $F->primary_key = (int) ( ! empty($field['flags']) && in_array('primary_key', $field['flags']));
Taufan Aditya4e44b342012-02-18 22:47:36 +0700201 }
202
203 $data[] = $F;
204 }
Timothy Warrenab347582011-08-23 12:29:29 -0400205 }
206
207 return $data;
Timothy Warren80ab8162011-08-22 18:26:12 -0400208 }
Timothy Warrenab347582011-08-23 12:29:29 -0400209 catch (Exception $e)
210 {
211 if ($this->db->db_debug)
212 {
213 return $this->db->display_error('db_unsuported_feature');
214 }
Taufan Aditya18209332012-02-09 16:07:27 +0700215
Timothy Warrenab347582011-08-23 12:29:29 -0400216 return FALSE;
217 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400218 }
219
220 // --------------------------------------------------------------------
221
222 /**
223 * Free the result
224 *
225 * @return null
226 */
227 function free_result()
228 {
Timothy Warren6a450cf2011-08-23 12:46:11 -0400229 if (is_object($this->result_id))
Timothy Warren80ab8162011-08-22 18:26:12 -0400230 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400231 $this->result_id = FALSE;
232 }
233 }
234
235 // --------------------------------------------------------------------
236
237 /**
238 * Data Seek
239 *
240 * Moves the internal pointer to the desired offset. We call
241 * this internally before fetching results to make sure the
242 * result set starts at zero
243 *
244 * @access private
245 * @return array
246 */
247 function _data_seek($n = 0)
248 {
249 return FALSE;
250 }
251
252 // --------------------------------------------------------------------
253
254 /**
255 * Result - associative array
256 *
257 * Returns the result set as an array
258 *
259 * @access private
260 * @return array
261 */
262 function _fetch_assoc()
263 {
Timothy Warrenab347582011-08-23 12:29:29 -0400264 return $this->result_id->fetch(PDO::FETCH_ASSOC);
Timothy Warren80ab8162011-08-22 18:26:12 -0400265 }
266
267 // --------------------------------------------------------------------
268
269 /**
270 * Result - object
271 *
272 * Returns the result set as an object
273 *
274 * @access private
275 * @return object
276 */
277 function _fetch_object()
Timothy Warrenab347582011-08-23 12:29:29 -0400278 {
279 return $this->result_id->fetchObject();
Timothy Warren80ab8162011-08-22 18:26:12 -0400280 }
281
282}
283
Timothy Warren80ab8162011-08-22 18:26:12 -0400284/* End of file pdo_result.php */
285/* Location: ./system/database/drivers/pdo/pdo_result.php */