blob: c333abc40aed02d54df4c330100a8bfb5ce95128 [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 *
5 * An open source application development framework for PHP 5.1.6 or newer
6 *
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 *
54 * @access public
55 * @return integer
56 */
57 function num_rows()
58 {
Taufan Aditya18209332012-02-09 16:07:27 +070059 if (empty($this->result_id) OR ! is_object($this->result_id))
60 {
61 // invalid result handler
62 return 0;
63 }
64 elseif (($num_rows = $this->result_id->rowCount()) && $num_rows > 0)
65 {
66 // If rowCount return something, we're done.
67 return $num_rows;
68 }
69
70 // Fetch the result, instead perform another extra query
71 return ($this->is_fetched && is_array($this->result_assoc)) ? count($this->result_assoc) : count($this->result_assoc());
72 }
73
74 /**
75 * Fetch the result handler
76 *
77 * @access public
78 * @return mixed
79 */
80 function result_assoc()
81 {
82 // If the result already fetched before, use that one
83 if (count($this->result_array) > 0 OR $this->is_fetched)
84 {
85 return $this->result_array();
86 }
87
88 // Define the output
89 $output = array('assoc', 'object');
90
91 // Fetch the result
92 foreach ($output as $type)
93 {
94 // Define the method and handler
95 $res_method = '_fetch_'.$type;
96 $res_handler = 'result_'.$type;
97
98 $this->$res_handler = array();
99 $this->_data_seek(0);
100
101 while ($row = $this->$res_method())
102 {
103 $this->{$res_handler}[] = $row;
104 }
105 }
106
107 // Save this as buffer and marked the fetch flag
108 $this->result_array = $this->result_assoc;
109 $this->is_fetched = TRUE;
110
111 return $this->result_assoc;
Timothy Warren80ab8162011-08-22 18:26:12 -0400112 }
113
114 // --------------------------------------------------------------------
115
116 /**
117 * Number of fields in the result set
118 *
119 * @access public
120 * @return integer
121 */
122 function num_fields()
123 {
Timothy Warrenab347582011-08-23 12:29:29 -0400124 return $this->result_id->columnCount();
Timothy Warren80ab8162011-08-22 18:26:12 -0400125 }
126
127 // --------------------------------------------------------------------
128
129 /**
130 * Fetch Field Names
131 *
132 * Generates an array of column names
133 *
134 * @access public
135 * @return array
136 */
137 function list_fields()
138 {
Timothy Warrenab347582011-08-23 12:29:29 -0400139 if ($this->db->db_debug)
Timothy Warren80ab8162011-08-22 18:26:12 -0400140 {
Timothy Warrenab347582011-08-23 12:29:29 -0400141 return $this->db->display_error('db_unsuported_feature');
Timothy Warren80ab8162011-08-22 18:26:12 -0400142 }
Taufan Aditya18209332012-02-09 16:07:27 +0700143
Timothy Warrenab347582011-08-23 12:29:29 -0400144 return FALSE;
Timothy Warren80ab8162011-08-22 18:26:12 -0400145 }
146
147 // --------------------------------------------------------------------
148
149 /**
150 * Field data
151 *
152 * Generates an array of objects containing field meta-data
153 *
154 * @access public
155 * @return array
156 */
157 function field_data()
158 {
Timothy Warrenab347582011-08-23 12:29:29 -0400159 $data = array();
160
161 try
Timothy Warren80ab8162011-08-22 18:26:12 -0400162 {
Timothy Warrenab347582011-08-23 12:29:29 -0400163 for($i = 0; $i < $this->num_fields(); $i++)
164 {
165 $data[] = $this->result_id->getColumnMeta($i);
166 }
167
168 return $data;
Timothy Warren80ab8162011-08-22 18:26:12 -0400169 }
Timothy Warrenab347582011-08-23 12:29:29 -0400170 catch (Exception $e)
171 {
172 if ($this->db->db_debug)
173 {
174 return $this->db->display_error('db_unsuported_feature');
175 }
Taufan Aditya18209332012-02-09 16:07:27 +0700176
Timothy Warrenab347582011-08-23 12:29:29 -0400177 return FALSE;
178 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400179 }
180
181 // --------------------------------------------------------------------
182
183 /**
184 * Free the result
185 *
186 * @return null
187 */
188 function free_result()
189 {
Timothy Warren6a450cf2011-08-23 12:46:11 -0400190 if (is_object($this->result_id))
Timothy Warren80ab8162011-08-22 18:26:12 -0400191 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400192 $this->result_id = FALSE;
193 }
194 }
195
196 // --------------------------------------------------------------------
197
198 /**
199 * Data Seek
200 *
201 * Moves the internal pointer to the desired offset. We call
202 * this internally before fetching results to make sure the
203 * result set starts at zero
204 *
205 * @access private
206 * @return array
207 */
208 function _data_seek($n = 0)
209 {
210 return FALSE;
211 }
212
213 // --------------------------------------------------------------------
214
215 /**
216 * Result - associative array
217 *
218 * Returns the result set as an array
219 *
220 * @access private
221 * @return array
222 */
223 function _fetch_assoc()
224 {
Timothy Warrenab347582011-08-23 12:29:29 -0400225 return $this->result_id->fetch(PDO::FETCH_ASSOC);
Timothy Warren80ab8162011-08-22 18:26:12 -0400226 }
227
228 // --------------------------------------------------------------------
229
230 /**
231 * Result - object
232 *
233 * Returns the result set as an object
234 *
235 * @access private
236 * @return object
237 */
238 function _fetch_object()
Timothy Warrenab347582011-08-23 12:29:29 -0400239 {
240 return $this->result_id->fetchObject();
Timothy Warren80ab8162011-08-22 18:26:12 -0400241 }
242
243}
244
Timothy Warren80ab8162011-08-22 18:26:12 -0400245/* End of file pdo_result.php */
246/* Location: ./system/database/drivers/pdo/pdo_result.php */