blob: 25cf87be88d6194cda7840c2739627d845b96e3f [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 try
Timothy Warren80ab8162011-08-22 18:26:12 -0400113 {
Andrey Andreev10ecc842012-11-16 01:06:20 +0200114 $retval = array();
115
116 for ($i = 0, $c = $this->num_fields(); $i < $c; $i++)
Timothy Warrenab347582011-08-23 12:29:29 -0400117 {
Andrey Andreev10ecc842012-11-16 01:06:20 +0200118 $field = $this->result_id->getColumnMeta($i);
Taufan Aditya4e44b342012-02-18 22:47:36 +0700119
Andrey Andreev10ecc842012-11-16 01:06:20 +0200120 $retval[$i] = new stdClass();
121 $retval[$i]->name = $field['name'];
122 $retval[$i]->type = $field['native_type'];
123 $retval[$i]->max_length = ($field['len'] > 0) ? $field['len'] : NULL;
124 $retval[$i]->primary_key = (int) ( ! empty($field['flags']) && in_array('primary_key', $field['flags'], TRUE));
Timothy Warrenab347582011-08-23 12:29:29 -0400125 }
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200126
Andrey Andreev10ecc842012-11-16 01:06:20 +0200127 return $retval;
Timothy Warren80ab8162011-08-22 18:26:12 -0400128 }
Timothy Warrenab347582011-08-23 12:29:29 -0400129 catch (Exception $e)
130 {
131 if ($this->db->db_debug)
132 {
Andrey Andreev8d3afde2012-11-06 12:53:47 +0200133 return $this->db->display_error('db_unsupported_feature');
Timothy Warrenab347582011-08-23 12:29:29 -0400134 }
Taufan Aditya18209332012-02-09 16:07:27 +0700135
Timothy Warrenab347582011-08-23 12:29:29 -0400136 return FALSE;
137 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400138 }
139
140 // --------------------------------------------------------------------
141
142 /**
143 * Free the result
144 *
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200145 * @return void
Timothy Warren80ab8162011-08-22 18:26:12 -0400146 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200147 public function free_result()
Timothy Warren80ab8162011-08-22 18:26:12 -0400148 {
Timothy Warren6a450cf2011-08-23 12:46:11 -0400149 if (is_object($this->result_id))
Timothy Warren80ab8162011-08-22 18:26:12 -0400150 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400151 $this->result_id = FALSE;
152 }
153 }
154
155 // --------------------------------------------------------------------
156
157 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400158 * Result - associative array
159 *
160 * Returns the result set as an array
161 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400162 * @return array
163 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200164 protected function _fetch_assoc()
Timothy Warren80ab8162011-08-22 18:26:12 -0400165 {
Timothy Warrenab347582011-08-23 12:29:29 -0400166 return $this->result_id->fetch(PDO::FETCH_ASSOC);
Timothy Warren80ab8162011-08-22 18:26:12 -0400167 }
168
169 // --------------------------------------------------------------------
170
171 /**
172 * Result - object
173 *
174 * Returns the result set as an object
175 *
Andrey Andreev8463b912012-11-02 02:16:28 +0200176 * @param string $class_name
Timothy Warren80ab8162011-08-22 18:26:12 -0400177 * @return object
178 */
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300179 protected function _fetch_object($class_name = 'stdClass')
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200180 {
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300181 return $this->result_id->fetchObject($class_name);
Timothy Warren80ab8162011-08-22 18:26:12 -0400182 }
183
184}
185
Timothy Warren80ab8162011-08-22 18:26:12 -0400186/* End of file pdo_result.php */
187/* Location: ./system/database/drivers/pdo/pdo_result.php */