blob: 867105605c465c549feeb71b9d2c9ea6eafb2065 [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
Taufan Aditya8ca31f32012-05-04 23:57:46 +070087 // Initial value
88 $this->result_assoc = array() and $this->result_object = array();
89
Taufan Aditya18209332012-02-09 16:07:27 +070090 // Fetch the result
Taufan Aditya8ca31f32012-05-04 23:57:46 +070091 while ($row = $this->_fetch_assoc())
Taufan Aditya18209332012-02-09 16:07:27 +070092 {
Taufan Aditya8ca31f32012-05-04 23:57:46 +070093 $this->result_assoc[] = $row;
94 $this->result_object[] = (object) $row;
Taufan Aditya18209332012-02-09 16:07:27 +070095 }
96
97 // Save this as buffer and marked the fetch flag
98 $this->result_array = $this->result_assoc;
99 $this->is_fetched = TRUE;
100
101 return $this->result_assoc;
Timothy Warren80ab8162011-08-22 18:26:12 -0400102 }
103
104 // --------------------------------------------------------------------
105
106 /**
107 * Number of fields in the result set
108 *
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200109 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400110 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200111 public function num_fields()
Timothy Warren80ab8162011-08-22 18:26:12 -0400112 {
Timothy Warrenab347582011-08-23 12:29:29 -0400113 return $this->result_id->columnCount();
Timothy Warren80ab8162011-08-22 18:26:12 -0400114 }
115
116 // --------------------------------------------------------------------
117
118 /**
119 * Fetch Field Names
120 *
121 * Generates an array of column names
122 *
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200123 * @return bool
Timothy Warren80ab8162011-08-22 18:26:12 -0400124 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200125 public function list_fields()
Timothy Warren80ab8162011-08-22 18:26:12 -0400126 {
Andrey Andreev626f1a62012-07-04 22:38:52 +0300127 $field_names = array();
128 for ($i = 0, $c = $this->num_fields(); $i < $c; $i++)
Timothy Warren80ab8162011-08-22 18:26:12 -0400129 {
Andrey Andreev626f1a62012-07-04 22:38:52 +0300130 $field_names[$i] = @$this->result_id->getColumnMeta();
131 $field_names[$i] = $field_names[$i]['name'];
Timothy Warren80ab8162011-08-22 18:26:12 -0400132 }
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200133
Andrey Andreev626f1a62012-07-04 22:38:52 +0300134 return $field_names;
Timothy Warren80ab8162011-08-22 18:26:12 -0400135 }
136
137 // --------------------------------------------------------------------
138
139 /**
140 * Field data
141 *
142 * Generates an array of objects containing field meta-data
143 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400144 * @return array
145 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200146 public function field_data()
Timothy Warren80ab8162011-08-22 18:26:12 -0400147 {
Timothy Warrenab347582011-08-23 12:29:29 -0400148 $data = array();
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200149
Timothy Warrenab347582011-08-23 12:29:29 -0400150 try
Timothy Warren80ab8162011-08-22 18:26:12 -0400151 {
Taufan Aditya4e44b342012-02-18 22:47:36 +0700152 if (strpos($this->result_id->queryString, 'PRAGMA') !== FALSE)
Timothy Warrenab347582011-08-23 12:29:29 -0400153 {
Taufan Aditya865ce1e2012-03-17 02:19:42 +0700154 foreach ($this->result_array() as $field)
Taufan Aditya4e44b342012-02-18 22:47:36 +0700155 {
156 preg_match('/([a-zA-Z]+)(\(\d+\))?/', $field['type'], $matches);
157
158 $F = new stdClass();
159 $F->name = $field['name'];
160 $F->type = ( ! empty($matches[1])) ? $matches[1] : NULL;
161 $F->default = NULL;
162 $F->max_length = ( ! empty($matches[2])) ? preg_replace('/[^\d]/', '', $matches[2]) : NULL;
163 $F->primary_key = (int) $field['pk'];
164 $F->pdo_type = NULL;
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200165
Taufan Aditya4e44b342012-02-18 22:47:36 +0700166 $data[] = $F;
167 }
168 }
169 else
170 {
171 for($i = 0, $max = $this->num_fields(); $i < $max; $i++)
172 {
173 $field = $this->result_id->getColumnMeta($i);
174
175 $F = new stdClass();
176 $F->name = $field['name'];
177 $F->type = $field['native_type'];
178 $F->default = NULL;
179 $F->pdo_type = $field['pdo_type'];
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200180
Taufan Aditya4e44b342012-02-18 22:47:36 +0700181 if ($field['precision'] < 0)
182 {
183 $F->max_length = NULL;
184 $F->primary_key = 0;
185 }
186 else
187 {
Taufan Aditya4a801542012-03-18 01:00:36 +0700188 $F->max_length = ($field['len'] > 255) ? 0 : $field['len'];
Taufan Aditya865ce1e2012-03-17 02:19:42 +0700189 $F->primary_key = (int) ( ! empty($field['flags']) && in_array('primary_key', $field['flags']));
Taufan Aditya4e44b342012-02-18 22:47:36 +0700190 }
191
192 $data[] = $F;
193 }
Timothy Warrenab347582011-08-23 12:29:29 -0400194 }
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200195
Timothy Warrenab347582011-08-23 12:29:29 -0400196 return $data;
Timothy Warren80ab8162011-08-22 18:26:12 -0400197 }
Timothy Warrenab347582011-08-23 12:29:29 -0400198 catch (Exception $e)
199 {
200 if ($this->db->db_debug)
201 {
202 return $this->db->display_error('db_unsuported_feature');
203 }
Taufan Aditya18209332012-02-09 16:07:27 +0700204
Timothy Warrenab347582011-08-23 12:29:29 -0400205 return FALSE;
206 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400207 }
208
209 // --------------------------------------------------------------------
210
211 /**
212 * Free the result
213 *
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200214 * @return void
Timothy Warren80ab8162011-08-22 18:26:12 -0400215 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200216 public function free_result()
Timothy Warren80ab8162011-08-22 18:26:12 -0400217 {
Timothy Warren6a450cf2011-08-23 12:46:11 -0400218 if (is_object($this->result_id))
Timothy Warren80ab8162011-08-22 18:26:12 -0400219 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400220 $this->result_id = FALSE;
221 }
222 }
223
224 // --------------------------------------------------------------------
225
226 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400227 * Result - associative array
228 *
229 * Returns the result set as an array
230 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400231 * @return array
232 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200233 protected function _fetch_assoc()
Timothy Warren80ab8162011-08-22 18:26:12 -0400234 {
Timothy Warrenab347582011-08-23 12:29:29 -0400235 return $this->result_id->fetch(PDO::FETCH_ASSOC);
Timothy Warren80ab8162011-08-22 18:26:12 -0400236 }
237
238 // --------------------------------------------------------------------
239
240 /**
241 * Result - object
242 *
243 * Returns the result set as an object
244 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400245 * @return object
246 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200247 protected function _fetch_object()
248 {
Taufan Aditya8ca31f32012-05-04 23:57:46 +0700249 return $this->result_id->fetch(PDO::FETCH_OBJ);
Timothy Warren80ab8162011-08-22 18:26:12 -0400250 }
251
252}
253
Timothy Warren80ab8162011-08-22 18:26:12 -0400254/* End of file pdo_result.php */
255/* Location: ./system/database/drivers/pdo/pdo_result.php */