blob: 1b29d29b7537ec3a87efe731eafade4d9baf5bee [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Timothy Warren80ab8162011-08-22 18:26:12 -040028
Timothy Warren80ab8162011-08-22 18:26:12 -040029/**
30 * PDO Result Class
31 *
32 * This class extends the parent result class: CI_DB_result
33 *
34 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Timothy Warren80ab8162011-08-22 18:26:12 -040036 * @link http://codeigniter.com/user_guide/database/
Andrey Andreevc7db6bb2012-07-05 15:11:20 +030037 * @since 2.1
Timothy Warren80ab8162011-08-22 18:26:12 -040038 */
39class CI_DB_pdo_result extends CI_DB_result {
40
41 /**
42 * Number of rows in the result set
43 *
Andrey Andreevbe22c5b2012-03-20 23:01:53 +020044 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -040045 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +020046 public function num_rows()
Timothy Warren80ab8162011-08-22 18:26:12 -040047 {
Andrey Andreevc7db6bb2012-07-05 15:11:20 +030048 if (is_int($this->num_rows))
Taufan Aditya18209332012-02-09 16:07:27 +070049 {
Andrey Andreevc7db6bb2012-07-05 15:11:20 +030050 return $this->num_rows;
Taufan Aditya18209332012-02-09 16:07:27 +070051 }
Andrey Andreevc7db6bb2012-07-05 15:11:20 +030052 elseif (count($this->result_array) > 0)
Taufan Aditya18209332012-02-09 16:07:27 +070053 {
Andrey Andreevc7db6bb2012-07-05 15:11:20 +030054 return $this->num_rows = count($this->result_array);
55 }
56 elseif (count($this->result_object) > 0)
57 {
58 return $this->num_rows = count($this->result_object);
59 }
Andrey Andreev7ca36132012-07-05 16:20:15 +030060 elseif (($num_rows = $this->result_id->rowCount()) > 0)
Andrey Andreevc7db6bb2012-07-05 15:11:20 +030061 {
Andrey Andreev7ca36132012-07-05 16:20:15 +030062 return $this->num_rows = $num_rows;
Taufan Aditya18209332012-02-09 16:07:27 +070063 }
64
Andrey Andreevc7db6bb2012-07-05 15:11:20 +030065 return $this->num_rows = count($this->result_array());
Timothy Warren80ab8162011-08-22 18:26:12 -040066 }
67
68 // --------------------------------------------------------------------
69
70 /**
71 * Number of fields in the result set
72 *
Andrey Andreevbe22c5b2012-03-20 23:01:53 +020073 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -040074 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +020075 public function num_fields()
Timothy Warren80ab8162011-08-22 18:26:12 -040076 {
Timothy Warrenab347582011-08-23 12:29:29 -040077 return $this->result_id->columnCount();
Timothy Warren80ab8162011-08-22 18:26:12 -040078 }
79
80 // --------------------------------------------------------------------
81
82 /**
83 * Fetch Field Names
84 *
85 * Generates an array of column names
86 *
Andrey Andreevbe22c5b2012-03-20 23:01:53 +020087 * @return bool
Timothy Warren80ab8162011-08-22 18:26:12 -040088 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +020089 public function list_fields()
Timothy Warren80ab8162011-08-22 18:26:12 -040090 {
Andrey Andreev626f1a62012-07-04 22:38:52 +030091 $field_names = array();
92 for ($i = 0, $c = $this->num_fields(); $i < $c; $i++)
Timothy Warren80ab8162011-08-22 18:26:12 -040093 {
Andrey Andreev626f1a62012-07-04 22:38:52 +030094 $field_names[$i] = @$this->result_id->getColumnMeta();
95 $field_names[$i] = $field_names[$i]['name'];
Timothy Warren80ab8162011-08-22 18:26:12 -040096 }
Andrey Andreevbe22c5b2012-03-20 23:01:53 +020097
Andrey Andreev626f1a62012-07-04 22:38:52 +030098 return $field_names;
Timothy Warren80ab8162011-08-22 18:26:12 -040099 }
100
101 // --------------------------------------------------------------------
102
103 /**
104 * Field data
105 *
106 * Generates an array of objects containing field meta-data
107 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400108 * @return array
109 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200110 public function field_data()
Timothy Warren80ab8162011-08-22 18:26:12 -0400111 {
Timothy Warrenab347582011-08-23 12:29:29 -0400112 $data = array();
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200113
Timothy Warrenab347582011-08-23 12:29:29 -0400114 try
Timothy Warren80ab8162011-08-22 18:26:12 -0400115 {
Taufan Aditya4e44b342012-02-18 22:47:36 +0700116 if (strpos($this->result_id->queryString, 'PRAGMA') !== FALSE)
Timothy Warrenab347582011-08-23 12:29:29 -0400117 {
Taufan Aditya865ce1e2012-03-17 02:19:42 +0700118 foreach ($this->result_array() as $field)
Taufan Aditya4e44b342012-02-18 22:47:36 +0700119 {
120 preg_match('/([a-zA-Z]+)(\(\d+\))?/', $field['type'], $matches);
121
122 $F = new stdClass();
123 $F->name = $field['name'];
124 $F->type = ( ! empty($matches[1])) ? $matches[1] : NULL;
125 $F->default = NULL;
126 $F->max_length = ( ! empty($matches[2])) ? preg_replace('/[^\d]/', '', $matches[2]) : NULL;
127 $F->primary_key = (int) $field['pk'];
128 $F->pdo_type = NULL;
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200129
Taufan Aditya4e44b342012-02-18 22:47:36 +0700130 $data[] = $F;
131 }
132 }
133 else
134 {
135 for($i = 0, $max = $this->num_fields(); $i < $max; $i++)
136 {
137 $field = $this->result_id->getColumnMeta($i);
138
139 $F = new stdClass();
140 $F->name = $field['name'];
141 $F->type = $field['native_type'];
142 $F->default = NULL;
143 $F->pdo_type = $field['pdo_type'];
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200144
Taufan Aditya4e44b342012-02-18 22:47:36 +0700145 if ($field['precision'] < 0)
146 {
147 $F->max_length = NULL;
148 $F->primary_key = 0;
149 }
150 else
151 {
Taufan Aditya4a801542012-03-18 01:00:36 +0700152 $F->max_length = ($field['len'] > 255) ? 0 : $field['len'];
Taufan Aditya865ce1e2012-03-17 02:19:42 +0700153 $F->primary_key = (int) ( ! empty($field['flags']) && in_array('primary_key', $field['flags']));
Taufan Aditya4e44b342012-02-18 22:47:36 +0700154 }
155
156 $data[] = $F;
157 }
Timothy Warrenab347582011-08-23 12:29:29 -0400158 }
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200159
Timothy Warrenab347582011-08-23 12:29:29 -0400160 return $data;
Timothy Warren80ab8162011-08-22 18:26:12 -0400161 }
Timothy Warrenab347582011-08-23 12:29:29 -0400162 catch (Exception $e)
163 {
164 if ($this->db->db_debug)
165 {
166 return $this->db->display_error('db_unsuported_feature');
167 }
Taufan Aditya18209332012-02-09 16:07:27 +0700168
Timothy Warrenab347582011-08-23 12:29:29 -0400169 return FALSE;
170 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400171 }
172
173 // --------------------------------------------------------------------
174
175 /**
176 * Free the result
177 *
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200178 * @return void
Timothy Warren80ab8162011-08-22 18:26:12 -0400179 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200180 public function free_result()
Timothy Warren80ab8162011-08-22 18:26:12 -0400181 {
Timothy Warren6a450cf2011-08-23 12:46:11 -0400182 if (is_object($this->result_id))
Timothy Warren80ab8162011-08-22 18:26:12 -0400183 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400184 $this->result_id = FALSE;
185 }
186 }
187
188 // --------------------------------------------------------------------
189
190 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400191 * Result - associative array
192 *
193 * Returns the result set as an array
194 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400195 * @return array
196 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200197 protected function _fetch_assoc()
Timothy Warren80ab8162011-08-22 18:26:12 -0400198 {
Timothy Warrenab347582011-08-23 12:29:29 -0400199 return $this->result_id->fetch(PDO::FETCH_ASSOC);
Timothy Warren80ab8162011-08-22 18:26:12 -0400200 }
201
202 // --------------------------------------------------------------------
203
204 /**
205 * Result - object
206 *
207 * Returns the result set as an object
208 *
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300209 * @param string
Timothy Warren80ab8162011-08-22 18:26:12 -0400210 * @return object
211 */
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300212 protected function _fetch_object($class_name = 'stdClass')
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200213 {
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300214 return $this->result_id->fetchObject($class_name);
Timothy Warren80ab8162011-08-22 18:26:12 -0400215 }
216
217}
218
Timothy Warren80ab8162011-08-22 18:26:12 -0400219/* End of file pdo_result.php */
220/* Location: ./system/database/drivers/pdo/pdo_result.php */