blob: 330a2e677f9f360bc30dc72bf4f51609e0cff3bb [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 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Timothy Warren80ab8162011-08-22 18:26:12 -04006 *
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 *
Timothy Warren80ab8162011-08-22 18:26:12 -040054 * @return integer
55 */
Timothy Warren8c332e72012-03-19 18:09:13 -040056 public function num_rows()
Timothy Warren80ab8162011-08-22 18:26:12 -040057 {
Taufan Aditya18209332012-02-09 16:07:27 +070058 if (empty($this->result_id) OR ! is_object($this->result_id))
59 {
60 // invalid result handler
61 return 0;
62 }
63 elseif (($num_rows = $this->result_id->rowCount()) && $num_rows > 0)
64 {
65 // If rowCount return something, we're done.
66 return $num_rows;
67 }
68
69 // Fetch the result, instead perform another extra query
70 return ($this->is_fetched && is_array($this->result_assoc)) ? count($this->result_assoc) : count($this->result_assoc());
71 }
72
73 /**
74 * Fetch the result handler
75 *
Taufan Aditya18209332012-02-09 16:07:27 +070076 * @return mixed
77 */
Timothy Warren8c332e72012-03-19 18:09:13 -040078 public function result_assoc()
Taufan Aditya18209332012-02-09 16:07:27 +070079 {
80 // If the result already fetched before, use that one
81 if (count($this->result_array) > 0 OR $this->is_fetched)
82 {
83 return $this->result_array();
84 }
85
86 // Define the output
87 $output = array('assoc', 'object');
88
89 // Fetch the result
90 foreach ($output as $type)
91 {
92 // Define the method and handler
93 $res_method = '_fetch_'.$type;
94 $res_handler = 'result_'.$type;
95
96 $this->$res_handler = array();
97 $this->_data_seek(0);
98
99 while ($row = $this->$res_method())
100 {
101 $this->{$res_handler}[] = $row;
102 }
103 }
104
105 // Save this as buffer and marked the fetch flag
106 $this->result_array = $this->result_assoc;
107 $this->is_fetched = TRUE;
108
109 return $this->result_assoc;
Timothy Warren80ab8162011-08-22 18:26:12 -0400110 }
111
112 // --------------------------------------------------------------------
113
114 /**
115 * Number of fields in the result set
116 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400117 * @return integer
118 */
Timothy Warren8c332e72012-03-19 18:09:13 -0400119 public function num_fields()
Timothy Warren80ab8162011-08-22 18:26:12 -0400120 {
Timothy Warrenab347582011-08-23 12:29:29 -0400121 return $this->result_id->columnCount();
Timothy Warren80ab8162011-08-22 18:26:12 -0400122 }
123
124 // --------------------------------------------------------------------
125
126 /**
127 * Fetch Field Names
128 *
129 * Generates an array of column names
130 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400131 * @return array
132 */
Timothy Warren8c332e72012-03-19 18:09:13 -0400133 public function list_fields()
Timothy Warren80ab8162011-08-22 18:26:12 -0400134 {
Timothy Warrenab347582011-08-23 12:29:29 -0400135 if ($this->db->db_debug)
Timothy Warren80ab8162011-08-22 18:26:12 -0400136 {
Timothy Warrenab347582011-08-23 12:29:29 -0400137 return $this->db->display_error('db_unsuported_feature');
Timothy Warren80ab8162011-08-22 18:26:12 -0400138 }
Taufan Aditya18209332012-02-09 16:07:27 +0700139
Timothy Warrenab347582011-08-23 12:29:29 -0400140 return FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400141 }
142
143 // --------------------------------------------------------------------
144
145 /**
146 * Field data
147 *
148 * Generates an array of objects containing field meta-data
149 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400150 * @return array
151 */
Timothy Warren8c332e72012-03-19 18:09:13 -0400152 public function field_data()
Timothy Warren80ab8162011-08-22 18:26:12 -0400153 {
Timothy Warrenab347582011-08-23 12:29:29 -0400154 $data = array();
155
156 try
Timothy Warren80ab8162011-08-22 18:26:12 -0400157 {
Taufan Aditya4e44b342012-02-18 22:47:36 +0700158 if (strpos($this->result_id->queryString, 'PRAGMA') !== FALSE)
Timothy Warrenab347582011-08-23 12:29:29 -0400159 {
Taufan Aditya865ce1e2012-03-17 02:19:42 +0700160 foreach ($this->result_array() as $field)
Taufan Aditya4e44b342012-02-18 22:47:36 +0700161 {
162 preg_match('/([a-zA-Z]+)(\(\d+\))?/', $field['type'], $matches);
163
164 $F = new stdClass();
165 $F->name = $field['name'];
166 $F->type = ( ! empty($matches[1])) ? $matches[1] : NULL;
167 $F->default = NULL;
168 $F->max_length = ( ! empty($matches[2])) ? preg_replace('/[^\d]/', '', $matches[2]) : NULL;
169 $F->primary_key = (int) $field['pk'];
170 $F->pdo_type = NULL;
171
172 $data[] = $F;
173 }
174 }
175 else
176 {
177 for($i = 0, $max = $this->num_fields(); $i < $max; $i++)
178 {
179 $field = $this->result_id->getColumnMeta($i);
180
181 $F = new stdClass();
182 $F->name = $field['name'];
183 $F->type = $field['native_type'];
184 $F->default = NULL;
185 $F->pdo_type = $field['pdo_type'];
186
187 if ($field['precision'] < 0)
188 {
189 $F->max_length = NULL;
190 $F->primary_key = 0;
191 }
192 else
193 {
Taufan Aditya4a801542012-03-18 01:00:36 +0700194 $F->max_length = ($field['len'] > 255) ? 0 : $field['len'];
Taufan Aditya865ce1e2012-03-17 02:19:42 +0700195 $F->primary_key = (int) ( ! empty($field['flags']) && in_array('primary_key', $field['flags']));
Taufan Aditya4e44b342012-02-18 22:47:36 +0700196 }
197
198 $data[] = $F;
199 }
Timothy Warrenab347582011-08-23 12:29:29 -0400200 }
201
202 return $data;
Timothy Warren80ab8162011-08-22 18:26:12 -0400203 }
Timothy Warrenab347582011-08-23 12:29:29 -0400204 catch (Exception $e)
205 {
206 if ($this->db->db_debug)
207 {
208 return $this->db->display_error('db_unsuported_feature');
209 }
Taufan Aditya18209332012-02-09 16:07:27 +0700210
Timothy Warrenab347582011-08-23 12:29:29 -0400211 return FALSE;
212 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400213 }
214
215 // --------------------------------------------------------------------
216
217 /**
218 * Free the result
219 *
220 * @return null
221 */
Timothy Warren8c332e72012-03-19 18:09:13 -0400222 public function free_result()
Timothy Warren80ab8162011-08-22 18:26:12 -0400223 {
Timothy Warren6a450cf2011-08-23 12:46:11 -0400224 if (is_object($this->result_id))
Timothy Warren80ab8162011-08-22 18:26:12 -0400225 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400226 $this->result_id = FALSE;
227 }
228 }
229
230 // --------------------------------------------------------------------
231
232 /**
233 * Data Seek
234 *
235 * Moves the internal pointer to the desired offset. We call
236 * this internally before fetching results to make sure the
237 * result set starts at zero
238 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400239 * @return array
240 */
Timothy Warren8c332e72012-03-19 18:09:13 -0400241 protected function _data_seek($n = 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400242 {
243 return FALSE;
244 }
245
246 // --------------------------------------------------------------------
247
248 /**
249 * Result - associative array
250 *
251 * Returns the result set as an array
252 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400253 * @return array
254 */
Timothy Warren8c332e72012-03-19 18:09:13 -0400255 protected function _fetch_assoc()
Timothy Warren80ab8162011-08-22 18:26:12 -0400256 {
Timothy Warrenab347582011-08-23 12:29:29 -0400257 return $this->result_id->fetch(PDO::FETCH_ASSOC);
Timothy Warren80ab8162011-08-22 18:26:12 -0400258 }
259
260 // --------------------------------------------------------------------
261
262 /**
263 * Result - object
264 *
265 * Returns the result set as an object
266 *
267 * @access private
268 * @return object
269 */
Timothy Warren8c332e72012-03-19 18:09:13 -0400270 protected function _fetch_object()
Timothy Warrenab347582011-08-23 12:29:29 -0400271 {
272 return $this->result_id->fetchObject();
Timothy Warren80ab8162011-08-22 18:26:12 -0400273 }
274
275}
276
Timothy Warren80ab8162011-08-22 18:26:12 -0400277/* End of file pdo_result.php */
278/* Location: ./system/database/drivers/pdo/pdo_result.php */