blob: 8dc16b1df71bf8d2c7d07d8db4d77aac70980824 [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
Andrey Andreevc7db6bb2012-07-05 15:11:20 +030024 * @since Version 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/
Andrey Andreevc7db6bb2012-07-05 15:11:20 +030036 * @since 2.1
Timothy Warren80ab8162011-08-22 18:26:12 -040037 */
38class CI_DB_pdo_result extends CI_DB_result {
39
40 /**
41 * Number of rows in the result set
42 *
Andrey Andreevbe22c5b2012-03-20 23:01:53 +020043 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -040044 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +020045 public function num_rows()
Timothy Warren80ab8162011-08-22 18:26:12 -040046 {
Andrey Andreevc7db6bb2012-07-05 15:11:20 +030047 if (is_int($this->num_rows))
Taufan Aditya18209332012-02-09 16:07:27 +070048 {
Andrey Andreevc7db6bb2012-07-05 15:11:20 +030049 return $this->num_rows;
Taufan Aditya18209332012-02-09 16:07:27 +070050 }
Andrey Andreevc7db6bb2012-07-05 15:11:20 +030051 elseif (count($this->result_array) > 0)
Taufan Aditya18209332012-02-09 16:07:27 +070052 {
Andrey Andreevc7db6bb2012-07-05 15:11:20 +030053 return $this->num_rows = count($this->result_array);
54 }
55 elseif (count($this->result_object) > 0)
56 {
57 return $this->num_rows = count($this->result_object);
58 }
59 elseif (($this->num_rows = $this->restul_id->rowCount()) > 0)
60 {
61 return $this->num_rows;
Taufan Aditya18209332012-02-09 16:07:27 +070062 }
63
Andrey Andreevc7db6bb2012-07-05 15:11:20 +030064 return $this->num_rows = count($this->result_array());
Timothy Warren80ab8162011-08-22 18:26:12 -040065 }
66
67 // --------------------------------------------------------------------
68
69 /**
70 * Number of fields in the result set
71 *
Andrey Andreevbe22c5b2012-03-20 23:01:53 +020072 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -040073 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +020074 public function num_fields()
Timothy Warren80ab8162011-08-22 18:26:12 -040075 {
Timothy Warrenab347582011-08-23 12:29:29 -040076 return $this->result_id->columnCount();
Timothy Warren80ab8162011-08-22 18:26:12 -040077 }
78
79 // --------------------------------------------------------------------
80
81 /**
82 * Fetch Field Names
83 *
84 * Generates an array of column names
85 *
Andrey Andreevbe22c5b2012-03-20 23:01:53 +020086 * @return bool
Timothy Warren80ab8162011-08-22 18:26:12 -040087 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +020088 public function list_fields()
Timothy Warren80ab8162011-08-22 18:26:12 -040089 {
Andrey Andreev626f1a62012-07-04 22:38:52 +030090 $field_names = array();
91 for ($i = 0, $c = $this->num_fields(); $i < $c; $i++)
Timothy Warren80ab8162011-08-22 18:26:12 -040092 {
Andrey Andreev626f1a62012-07-04 22:38:52 +030093 $field_names[$i] = @$this->result_id->getColumnMeta();
94 $field_names[$i] = $field_names[$i]['name'];
Timothy Warren80ab8162011-08-22 18:26:12 -040095 }
Andrey Andreevbe22c5b2012-03-20 23:01:53 +020096
Andrey Andreev626f1a62012-07-04 22:38:52 +030097 return $field_names;
Timothy Warren80ab8162011-08-22 18:26:12 -040098 }
99
100 // --------------------------------------------------------------------
101
102 /**
103 * Field data
104 *
105 * Generates an array of objects containing field meta-data
106 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400107 * @return array
108 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200109 public function field_data()
Timothy Warren80ab8162011-08-22 18:26:12 -0400110 {
Timothy Warrenab347582011-08-23 12:29:29 -0400111 $data = array();
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200112
Timothy Warrenab347582011-08-23 12:29:29 -0400113 try
Timothy Warren80ab8162011-08-22 18:26:12 -0400114 {
Taufan Aditya4e44b342012-02-18 22:47:36 +0700115 if (strpos($this->result_id->queryString, 'PRAGMA') !== FALSE)
Timothy Warrenab347582011-08-23 12:29:29 -0400116 {
Taufan Aditya865ce1e2012-03-17 02:19:42 +0700117 foreach ($this->result_array() as $field)
Taufan Aditya4e44b342012-02-18 22:47:36 +0700118 {
119 preg_match('/([a-zA-Z]+)(\(\d+\))?/', $field['type'], $matches);
120
121 $F = new stdClass();
122 $F->name = $field['name'];
123 $F->type = ( ! empty($matches[1])) ? $matches[1] : NULL;
124 $F->default = NULL;
125 $F->max_length = ( ! empty($matches[2])) ? preg_replace('/[^\d]/', '', $matches[2]) : NULL;
126 $F->primary_key = (int) $field['pk'];
127 $F->pdo_type = NULL;
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200128
Taufan Aditya4e44b342012-02-18 22:47:36 +0700129 $data[] = $F;
130 }
131 }
132 else
133 {
134 for($i = 0, $max = $this->num_fields(); $i < $max; $i++)
135 {
136 $field = $this->result_id->getColumnMeta($i);
137
138 $F = new stdClass();
139 $F->name = $field['name'];
140 $F->type = $field['native_type'];
141 $F->default = NULL;
142 $F->pdo_type = $field['pdo_type'];
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200143
Taufan Aditya4e44b342012-02-18 22:47:36 +0700144 if ($field['precision'] < 0)
145 {
146 $F->max_length = NULL;
147 $F->primary_key = 0;
148 }
149 else
150 {
Taufan Aditya4a801542012-03-18 01:00:36 +0700151 $F->max_length = ($field['len'] > 255) ? 0 : $field['len'];
Taufan Aditya865ce1e2012-03-17 02:19:42 +0700152 $F->primary_key = (int) ( ! empty($field['flags']) && in_array('primary_key', $field['flags']));
Taufan Aditya4e44b342012-02-18 22:47:36 +0700153 }
154
155 $data[] = $F;
156 }
Timothy Warrenab347582011-08-23 12:29:29 -0400157 }
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200158
Timothy Warrenab347582011-08-23 12:29:29 -0400159 return $data;
Timothy Warren80ab8162011-08-22 18:26:12 -0400160 }
Timothy Warrenab347582011-08-23 12:29:29 -0400161 catch (Exception $e)
162 {
163 if ($this->db->db_debug)
164 {
165 return $this->db->display_error('db_unsuported_feature');
166 }
Taufan Aditya18209332012-02-09 16:07:27 +0700167
Timothy Warrenab347582011-08-23 12:29:29 -0400168 return FALSE;
169 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400170 }
171
172 // --------------------------------------------------------------------
173
174 /**
175 * Free the result
176 *
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200177 * @return void
Timothy Warren80ab8162011-08-22 18:26:12 -0400178 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200179 public function free_result()
Timothy Warren80ab8162011-08-22 18:26:12 -0400180 {
Timothy Warren6a450cf2011-08-23 12:46:11 -0400181 if (is_object($this->result_id))
Timothy Warren80ab8162011-08-22 18:26:12 -0400182 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400183 $this->result_id = FALSE;
184 }
185 }
186
187 // --------------------------------------------------------------------
188
189 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400190 * Result - associative array
191 *
192 * Returns the result set as an array
193 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400194 * @return array
195 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200196 protected function _fetch_assoc()
Timothy Warren80ab8162011-08-22 18:26:12 -0400197 {
Timothy Warrenab347582011-08-23 12:29:29 -0400198 return $this->result_id->fetch(PDO::FETCH_ASSOC);
Timothy Warren80ab8162011-08-22 18:26:12 -0400199 }
200
201 // --------------------------------------------------------------------
202
203 /**
204 * Result - object
205 *
206 * Returns the result set as an object
207 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400208 * @return object
209 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200210 protected function _fetch_object()
211 {
Taufan Aditya8ca31f32012-05-04 23:57:46 +0700212 return $this->result_id->fetch(PDO::FETCH_OBJ);
Timothy Warren80ab8162011-08-22 18:26:12 -0400213 }
214
215}
216
Timothy Warren80ab8162011-08-22 18:26:12 -0400217/* End of file pdo_result.php */
218/* Location: ./system/database/drivers/pdo/pdo_result.php */