blob: 5bbd85d7535baf7a91aeaab1a2ab0b630afb552c [file] [log] [blame]
Andrey Andreevbe22c5b2012-03-20 23:01:53 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Timothy Warren80ab8162011-08-22 18:26:12 -04002/**
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
Andrey Andreevbe22c5b2012-03-20 23:01:53 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevbe22c5b2012-03-20 23:01:53 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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
Timothy Warren80ab8162011-08-22 18:26:12 -040028/**
29 * PDO Result Class
30 *
31 * This class extends the parent result class: CI_DB_result
32 *
33 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Timothy Warren80ab8162011-08-22 18:26:12 -040035 * @link http://codeigniter.com/user_guide/database/
36 */
37class CI_DB_pdo_result extends CI_DB_result {
38
39 /**
Taufan Aditya18209332012-02-09 16:07:27 +070040 * @var bool Hold the flag whether a result handler already fetched before
41 */
42 protected $is_fetched = FALSE;
43
44 /**
45 * @var mixed Hold the fetched assoc array of a result handler
46 */
47 protected $result_assoc;
48
49 /**
Timothy Warren80ab8162011-08-22 18:26:12 -040050 * Number of rows in the result set
51 *
Andrey Andreevbe22c5b2012-03-20 23:01:53 +020052 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -040053 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +020054 public function num_rows()
Timothy Warren80ab8162011-08-22 18:26:12 -040055 {
Taufan Aditya18209332012-02-09 16:07:27 +070056 if (empty($this->result_id) OR ! is_object($this->result_id))
57 {
58 // invalid result handler
59 return 0;
60 }
61 elseif (($num_rows = $this->result_id->rowCount()) && $num_rows > 0)
62 {
63 // If rowCount return something, we're done.
64 return $num_rows;
65 }
66
67 // Fetch the result, instead perform another extra query
68 return ($this->is_fetched && is_array($this->result_assoc)) ? count($this->result_assoc) : count($this->result_assoc());
69 }
70
71 /**
72 * Fetch the result handler
73 *
Taufan Aditya18209332012-02-09 16:07:27 +070074 * @return mixed
75 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +020076 public function result_assoc()
Taufan Aditya18209332012-02-09 16:07:27 +070077 {
78 // If the result already fetched before, use that one
79 if (count($this->result_array) > 0 OR $this->is_fetched)
80 {
81 return $this->result_array();
82 }
83
84 // Define the output
85 $output = array('assoc', 'object');
86
87 // Fetch the result
88 foreach ($output as $type)
89 {
90 // Define the method and handler
91 $res_method = '_fetch_'.$type;
92 $res_handler = 'result_'.$type;
Andrey Andreevbe22c5b2012-03-20 23:01:53 +020093
Taufan Aditya18209332012-02-09 16:07:27 +070094 $this->$res_handler = array();
95 $this->_data_seek(0);
96
97 while ($row = $this->$res_method())
98 {
99 $this->{$res_handler}[] = $row;
100 }
101 }
102
103 // Save this as buffer and marked the fetch flag
104 $this->result_array = $this->result_assoc;
105 $this->is_fetched = TRUE;
106
107 return $this->result_assoc;
Timothy Warren80ab8162011-08-22 18:26:12 -0400108 }
109
110 // --------------------------------------------------------------------
111
112 /**
113 * Number of fields in the result set
114 *
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200115 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400116 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200117 public function num_fields()
Timothy Warren80ab8162011-08-22 18:26:12 -0400118 {
Timothy Warrenab347582011-08-23 12:29:29 -0400119 return $this->result_id->columnCount();
Timothy Warren80ab8162011-08-22 18:26:12 -0400120 }
121
122 // --------------------------------------------------------------------
123
124 /**
125 * Fetch Field Names
126 *
127 * Generates an array of column names
128 *
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200129 * @return bool
Timothy Warren80ab8162011-08-22 18:26:12 -0400130 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200131 public function list_fields()
Timothy Warren80ab8162011-08-22 18:26:12 -0400132 {
Timothy Warrenab347582011-08-23 12:29:29 -0400133 if ($this->db->db_debug)
Timothy Warren80ab8162011-08-22 18:26:12 -0400134 {
Timothy Warrenab347582011-08-23 12:29:29 -0400135 return $this->db->display_error('db_unsuported_feature');
Timothy Warren80ab8162011-08-22 18:26:12 -0400136 }
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200137
Timothy Warrenab347582011-08-23 12:29:29 -0400138 return FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400139 }
140
141 // --------------------------------------------------------------------
142
143 /**
144 * Field data
145 *
146 * Generates an array of objects containing field meta-data
147 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400148 * @return array
149 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200150 public function field_data()
Timothy Warren80ab8162011-08-22 18:26:12 -0400151 {
Timothy Warrenab347582011-08-23 12:29:29 -0400152 $data = array();
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200153
Timothy Warrenab347582011-08-23 12:29:29 -0400154 try
Timothy Warren80ab8162011-08-22 18:26:12 -0400155 {
Taufan Aditya4e44b342012-02-18 22:47:36 +0700156 if (strpos($this->result_id->queryString, 'PRAGMA') !== FALSE)
Timothy Warrenab347582011-08-23 12:29:29 -0400157 {
Taufan Aditya865ce1e2012-03-17 02:19:42 +0700158 foreach ($this->result_array() as $field)
Taufan Aditya4e44b342012-02-18 22:47:36 +0700159 {
160 preg_match('/([a-zA-Z]+)(\(\d+\))?/', $field['type'], $matches);
161
162 $F = new stdClass();
163 $F->name = $field['name'];
164 $F->type = ( ! empty($matches[1])) ? $matches[1] : NULL;
165 $F->default = NULL;
166 $F->max_length = ( ! empty($matches[2])) ? preg_replace('/[^\d]/', '', $matches[2]) : NULL;
167 $F->primary_key = (int) $field['pk'];
168 $F->pdo_type = NULL;
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200169
Taufan Aditya4e44b342012-02-18 22:47:36 +0700170 $data[] = $F;
171 }
172 }
173 else
174 {
175 for($i = 0, $max = $this->num_fields(); $i < $max; $i++)
176 {
177 $field = $this->result_id->getColumnMeta($i);
178
179 $F = new stdClass();
180 $F->name = $field['name'];
181 $F->type = $field['native_type'];
182 $F->default = NULL;
183 $F->pdo_type = $field['pdo_type'];
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200184
Taufan Aditya4e44b342012-02-18 22:47:36 +0700185 if ($field['precision'] < 0)
186 {
187 $F->max_length = NULL;
188 $F->primary_key = 0;
189 }
190 else
191 {
Taufan Aditya4a801542012-03-18 01:00:36 +0700192 $F->max_length = ($field['len'] > 255) ? 0 : $field['len'];
Taufan Aditya865ce1e2012-03-17 02:19:42 +0700193 $F->primary_key = (int) ( ! empty($field['flags']) && in_array('primary_key', $field['flags']));
Taufan Aditya4e44b342012-02-18 22:47:36 +0700194 }
195
196 $data[] = $F;
197 }
Timothy Warrenab347582011-08-23 12:29:29 -0400198 }
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200199
Timothy Warrenab347582011-08-23 12:29:29 -0400200 return $data;
Timothy Warren80ab8162011-08-22 18:26:12 -0400201 }
Timothy Warrenab347582011-08-23 12:29:29 -0400202 catch (Exception $e)
203 {
204 if ($this->db->db_debug)
205 {
206 return $this->db->display_error('db_unsuported_feature');
207 }
Taufan Aditya18209332012-02-09 16:07:27 +0700208
Timothy Warrenab347582011-08-23 12:29:29 -0400209 return FALSE;
210 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400211 }
212
213 // --------------------------------------------------------------------
214
215 /**
216 * Free the result
217 *
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200218 * @return void
Timothy Warren80ab8162011-08-22 18:26:12 -0400219 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200220 public function free_result()
Timothy Warren80ab8162011-08-22 18:26:12 -0400221 {
Timothy Warren6a450cf2011-08-23 12:46:11 -0400222 if (is_object($this->result_id))
Timothy Warren80ab8162011-08-22 18:26:12 -0400223 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400224 $this->result_id = FALSE;
225 }
226 }
227
228 // --------------------------------------------------------------------
229
230 /**
231 * Data Seek
232 *
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200233 * Moves the internal pointer to the desired offset. We call
Timothy Warren80ab8162011-08-22 18:26:12 -0400234 * this internally before fetching results to make sure the
235 * result set starts at zero
236 *
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200237 * @return bool
Timothy Warren80ab8162011-08-22 18:26:12 -0400238 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200239 protected function _data_seek($n = 0)
Timothy Warren80ab8162011-08-22 18:26:12 -0400240 {
241 return FALSE;
242 }
243
244 // --------------------------------------------------------------------
245
246 /**
247 * Result - associative array
248 *
249 * Returns the result set as an array
250 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400251 * @return array
252 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200253 protected function _fetch_assoc()
Timothy Warren80ab8162011-08-22 18:26:12 -0400254 {
Timothy Warrenab347582011-08-23 12:29:29 -0400255 return $this->result_id->fetch(PDO::FETCH_ASSOC);
Timothy Warren80ab8162011-08-22 18:26:12 -0400256 }
257
258 // --------------------------------------------------------------------
259
260 /**
261 * Result - object
262 *
263 * Returns the result set as an object
264 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400265 * @return object
266 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200267 protected function _fetch_object()
268 {
Timothy Warrenab347582011-08-23 12:29:29 -0400269 return $this->result_id->fetchObject();
Timothy Warren80ab8162011-08-22 18:26:12 -0400270 }
271
272}
273
Timothy Warren80ab8162011-08-22 18:26:12 -0400274/* End of file pdo_result.php */
275/* Location: ./system/database/drivers/pdo/pdo_result.php */