blob: 19aee1dfc9c2bbe4158b20b257b1af6619c47702 [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();
Taufan Aditya18209332012-02-09 16:07:27 +070095
96 while ($row = $this->$res_method())
97 {
98 $this->{$res_handler}[] = $row;
99 }
100 }
101
102 // Save this as buffer and marked the fetch flag
103 $this->result_array = $this->result_assoc;
104 $this->is_fetched = TRUE;
105
106 return $this->result_assoc;
Timothy Warren80ab8162011-08-22 18:26:12 -0400107 }
108
109 // --------------------------------------------------------------------
110
111 /**
112 * Number of fields in the result set
113 *
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200114 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -0400115 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200116 public function num_fields()
Timothy Warren80ab8162011-08-22 18:26:12 -0400117 {
Timothy Warrenab347582011-08-23 12:29:29 -0400118 return $this->result_id->columnCount();
Timothy Warren80ab8162011-08-22 18:26:12 -0400119 }
120
121 // --------------------------------------------------------------------
122
123 /**
124 * Fetch Field Names
125 *
126 * Generates an array of column names
127 *
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200128 * @return bool
Timothy Warren80ab8162011-08-22 18:26:12 -0400129 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200130 public function list_fields()
Timothy Warren80ab8162011-08-22 18:26:12 -0400131 {
Timothy Warrenab347582011-08-23 12:29:29 -0400132 if ($this->db->db_debug)
Timothy Warren80ab8162011-08-22 18:26:12 -0400133 {
Timothy Warrenab347582011-08-23 12:29:29 -0400134 return $this->db->display_error('db_unsuported_feature');
Timothy Warren80ab8162011-08-22 18:26:12 -0400135 }
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200136
Timothy Warrenab347582011-08-23 12:29:29 -0400137 return FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400138 }
139
140 // --------------------------------------------------------------------
141
142 /**
143 * Field data
144 *
145 * Generates an array of objects containing field meta-data
146 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400147 * @return array
148 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200149 public function field_data()
Timothy Warren80ab8162011-08-22 18:26:12 -0400150 {
Timothy Warrenab347582011-08-23 12:29:29 -0400151 $data = array();
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200152
Timothy Warrenab347582011-08-23 12:29:29 -0400153 try
Timothy Warren80ab8162011-08-22 18:26:12 -0400154 {
Taufan Aditya4e44b342012-02-18 22:47:36 +0700155 if (strpos($this->result_id->queryString, 'PRAGMA') !== FALSE)
Timothy Warrenab347582011-08-23 12:29:29 -0400156 {
Taufan Aditya865ce1e2012-03-17 02:19:42 +0700157 foreach ($this->result_array() as $field)
Taufan Aditya4e44b342012-02-18 22:47:36 +0700158 {
159 preg_match('/([a-zA-Z]+)(\(\d+\))?/', $field['type'], $matches);
160
161 $F = new stdClass();
162 $F->name = $field['name'];
163 $F->type = ( ! empty($matches[1])) ? $matches[1] : NULL;
164 $F->default = NULL;
165 $F->max_length = ( ! empty($matches[2])) ? preg_replace('/[^\d]/', '', $matches[2]) : NULL;
166 $F->primary_key = (int) $field['pk'];
167 $F->pdo_type = NULL;
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200168
Taufan Aditya4e44b342012-02-18 22:47:36 +0700169 $data[] = $F;
170 }
171 }
172 else
173 {
174 for($i = 0, $max = $this->num_fields(); $i < $max; $i++)
175 {
176 $field = $this->result_id->getColumnMeta($i);
177
178 $F = new stdClass();
179 $F->name = $field['name'];
180 $F->type = $field['native_type'];
181 $F->default = NULL;
182 $F->pdo_type = $field['pdo_type'];
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200183
Taufan Aditya4e44b342012-02-18 22:47:36 +0700184 if ($field['precision'] < 0)
185 {
186 $F->max_length = NULL;
187 $F->primary_key = 0;
188 }
189 else
190 {
Taufan Aditya4a801542012-03-18 01:00:36 +0700191 $F->max_length = ($field['len'] > 255) ? 0 : $field['len'];
Taufan Aditya865ce1e2012-03-17 02:19:42 +0700192 $F->primary_key = (int) ( ! empty($field['flags']) && in_array('primary_key', $field['flags']));
Taufan Aditya4e44b342012-02-18 22:47:36 +0700193 }
194
195 $data[] = $F;
196 }
Timothy Warrenab347582011-08-23 12:29:29 -0400197 }
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200198
Timothy Warrenab347582011-08-23 12:29:29 -0400199 return $data;
Timothy Warren80ab8162011-08-22 18:26:12 -0400200 }
Timothy Warrenab347582011-08-23 12:29:29 -0400201 catch (Exception $e)
202 {
203 if ($this->db->db_debug)
204 {
205 return $this->db->display_error('db_unsuported_feature');
206 }
Taufan Aditya18209332012-02-09 16:07:27 +0700207
Timothy Warrenab347582011-08-23 12:29:29 -0400208 return FALSE;
209 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400210 }
211
212 // --------------------------------------------------------------------
213
214 /**
215 * Free the result
216 *
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200217 * @return void
Timothy Warren80ab8162011-08-22 18:26:12 -0400218 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200219 public function free_result()
Timothy Warren80ab8162011-08-22 18:26:12 -0400220 {
Timothy Warren6a450cf2011-08-23 12:46:11 -0400221 if (is_object($this->result_id))
Timothy Warren80ab8162011-08-22 18:26:12 -0400222 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400223 $this->result_id = FALSE;
224 }
225 }
226
227 // --------------------------------------------------------------------
228
229 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400230 * Result - associative array
231 *
232 * Returns the result set as an array
233 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400234 * @return array
235 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200236 protected function _fetch_assoc()
Timothy Warren80ab8162011-08-22 18:26:12 -0400237 {
Timothy Warrenab347582011-08-23 12:29:29 -0400238 return $this->result_id->fetch(PDO::FETCH_ASSOC);
Timothy Warren80ab8162011-08-22 18:26:12 -0400239 }
240
241 // --------------------------------------------------------------------
242
243 /**
244 * Result - object
245 *
246 * Returns the result set as an object
247 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400248 * @return object
249 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200250 protected function _fetch_object()
251 {
Timothy Warrenab347582011-08-23 12:29:29 -0400252 return $this->result_id->fetchObject();
Timothy Warren80ab8162011-08-22 18:26:12 -0400253 }
254
255}
256
Timothy Warren80ab8162011-08-22 18:26:12 -0400257/* End of file pdo_result.php */
258/* Location: ./system/database/drivers/pdo/pdo_result.php */