blob: 0b8937cc5a064f1a3c59d7e420837c835500fd76 [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 {
Timothy Warrenab347582011-08-23 12:29:29 -0400127 if ($this->db->db_debug)
Timothy Warren80ab8162011-08-22 18:26:12 -0400128 {
Timothy Warrenab347582011-08-23 12:29:29 -0400129 return $this->db->display_error('db_unsuported_feature');
Timothy Warren80ab8162011-08-22 18:26:12 -0400130 }
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200131
Timothy Warrenab347582011-08-23 12:29:29 -0400132 return FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400133 }
134
135 // --------------------------------------------------------------------
136
137 /**
138 * Field data
139 *
140 * Generates an array of objects containing field meta-data
141 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400142 * @return array
143 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200144 public function field_data()
Timothy Warren80ab8162011-08-22 18:26:12 -0400145 {
Timothy Warrenab347582011-08-23 12:29:29 -0400146 $data = array();
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200147
Timothy Warrenab347582011-08-23 12:29:29 -0400148 try
Timothy Warren80ab8162011-08-22 18:26:12 -0400149 {
Taufan Aditya4e44b342012-02-18 22:47:36 +0700150 if (strpos($this->result_id->queryString, 'PRAGMA') !== FALSE)
Timothy Warrenab347582011-08-23 12:29:29 -0400151 {
Taufan Aditya865ce1e2012-03-17 02:19:42 +0700152 foreach ($this->result_array() as $field)
Taufan Aditya4e44b342012-02-18 22:47:36 +0700153 {
154 preg_match('/([a-zA-Z]+)(\(\d+\))?/', $field['type'], $matches);
155
156 $F = new stdClass();
157 $F->name = $field['name'];
158 $F->type = ( ! empty($matches[1])) ? $matches[1] : NULL;
159 $F->default = NULL;
160 $F->max_length = ( ! empty($matches[2])) ? preg_replace('/[^\d]/', '', $matches[2]) : NULL;
161 $F->primary_key = (int) $field['pk'];
162 $F->pdo_type = NULL;
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200163
Taufan Aditya4e44b342012-02-18 22:47:36 +0700164 $data[] = $F;
165 }
166 }
167 else
168 {
169 for($i = 0, $max = $this->num_fields(); $i < $max; $i++)
170 {
171 $field = $this->result_id->getColumnMeta($i);
172
173 $F = new stdClass();
174 $F->name = $field['name'];
175 $F->type = $field['native_type'];
176 $F->default = NULL;
177 $F->pdo_type = $field['pdo_type'];
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200178
Taufan Aditya4e44b342012-02-18 22:47:36 +0700179 if ($field['precision'] < 0)
180 {
181 $F->max_length = NULL;
182 $F->primary_key = 0;
183 }
184 else
185 {
Taufan Aditya4a801542012-03-18 01:00:36 +0700186 $F->max_length = ($field['len'] > 255) ? 0 : $field['len'];
Taufan Aditya865ce1e2012-03-17 02:19:42 +0700187 $F->primary_key = (int) ( ! empty($field['flags']) && in_array('primary_key', $field['flags']));
Taufan Aditya4e44b342012-02-18 22:47:36 +0700188 }
189
190 $data[] = $F;
191 }
Timothy Warrenab347582011-08-23 12:29:29 -0400192 }
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200193
Timothy Warrenab347582011-08-23 12:29:29 -0400194 return $data;
Timothy Warren80ab8162011-08-22 18:26:12 -0400195 }
Timothy Warrenab347582011-08-23 12:29:29 -0400196 catch (Exception $e)
197 {
198 if ($this->db->db_debug)
199 {
200 return $this->db->display_error('db_unsuported_feature');
201 }
Taufan Aditya18209332012-02-09 16:07:27 +0700202
Timothy Warrenab347582011-08-23 12:29:29 -0400203 return FALSE;
204 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400205 }
206
207 // --------------------------------------------------------------------
208
209 /**
210 * Free the result
211 *
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200212 * @return void
Timothy Warren80ab8162011-08-22 18:26:12 -0400213 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200214 public function free_result()
Timothy Warren80ab8162011-08-22 18:26:12 -0400215 {
Timothy Warren6a450cf2011-08-23 12:46:11 -0400216 if (is_object($this->result_id))
Timothy Warren80ab8162011-08-22 18:26:12 -0400217 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400218 $this->result_id = FALSE;
219 }
220 }
221
222 // --------------------------------------------------------------------
223
224 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400225 * Result - associative array
226 *
227 * Returns the result set as an array
228 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400229 * @return array
230 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200231 protected function _fetch_assoc()
Timothy Warren80ab8162011-08-22 18:26:12 -0400232 {
Timothy Warrenab347582011-08-23 12:29:29 -0400233 return $this->result_id->fetch(PDO::FETCH_ASSOC);
Timothy Warren80ab8162011-08-22 18:26:12 -0400234 }
235
236 // --------------------------------------------------------------------
237
238 /**
239 * Result - object
240 *
241 * Returns the result set as an object
242 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400243 * @return object
244 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200245 protected function _fetch_object()
246 {
Taufan Aditya8ca31f32012-05-04 23:57:46 +0700247 return $this->result_id->fetch(PDO::FETCH_OBJ);
Timothy Warren80ab8162011-08-22 18:26:12 -0400248 }
249
250}
251
Timothy Warren80ab8162011-08-22 18:26:12 -0400252/* End of file pdo_result.php */
253/* Location: ./system/database/drivers/pdo/pdo_result.php */