blob: 1b8fbc9d49e58e335c060ae460f36630d18abe35 [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
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, 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 Andreev2bbbd1a2014-05-09 10:24:14 +030094 // Might trigger an E_WARNING due to not all subdrivers
95 // supporting getColumnMeta()
Andrey Andreev626f1a62012-07-04 22:38:52 +030096 $field_names[$i] = @$this->result_id->getColumnMeta();
97 $field_names[$i] = $field_names[$i]['name'];
Timothy Warren80ab8162011-08-22 18:26:12 -040098 }
Andrey Andreevbe22c5b2012-03-20 23:01:53 +020099
Andrey Andreev626f1a62012-07-04 22:38:52 +0300100 return $field_names;
Timothy Warren80ab8162011-08-22 18:26:12 -0400101 }
102
103 // --------------------------------------------------------------------
104
105 /**
106 * Field data
107 *
108 * Generates an array of objects containing field meta-data
109 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400110 * @return array
111 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200112 public function field_data()
Timothy Warren80ab8162011-08-22 18:26:12 -0400113 {
Timothy Warrenab347582011-08-23 12:29:29 -0400114 try
Timothy Warren80ab8162011-08-22 18:26:12 -0400115 {
Andrey Andreev10ecc842012-11-16 01:06:20 +0200116 $retval = array();
117
118 for ($i = 0, $c = $this->num_fields(); $i < $c; $i++)
Timothy Warrenab347582011-08-23 12:29:29 -0400119 {
Andrey Andreev10ecc842012-11-16 01:06:20 +0200120 $field = $this->result_id->getColumnMeta($i);
Taufan Aditya4e44b342012-02-18 22:47:36 +0700121
Andrey Andreev10ecc842012-11-16 01:06:20 +0200122 $retval[$i] = new stdClass();
123 $retval[$i]->name = $field['name'];
124 $retval[$i]->type = $field['native_type'];
125 $retval[$i]->max_length = ($field['len'] > 0) ? $field['len'] : NULL;
126 $retval[$i]->primary_key = (int) ( ! empty($field['flags']) && in_array('primary_key', $field['flags'], TRUE));
Timothy Warrenab347582011-08-23 12:29:29 -0400127 }
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200128
Andrey Andreev10ecc842012-11-16 01:06:20 +0200129 return $retval;
Timothy Warren80ab8162011-08-22 18:26:12 -0400130 }
Timothy Warrenab347582011-08-23 12:29:29 -0400131 catch (Exception $e)
132 {
133 if ($this->db->db_debug)
134 {
Andrey Andreev8d3afde2012-11-06 12:53:47 +0200135 return $this->db->display_error('db_unsupported_feature');
Timothy Warrenab347582011-08-23 12:29:29 -0400136 }
Taufan Aditya18209332012-02-09 16:07:27 +0700137
Timothy Warrenab347582011-08-23 12:29:29 -0400138 return FALSE;
139 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400140 }
141
142 // --------------------------------------------------------------------
143
144 /**
145 * Free the result
146 *
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200147 * @return void
Timothy Warren80ab8162011-08-22 18:26:12 -0400148 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200149 public function free_result()
Timothy Warren80ab8162011-08-22 18:26:12 -0400150 {
Timothy Warren6a450cf2011-08-23 12:46:11 -0400151 if (is_object($this->result_id))
Timothy Warren80ab8162011-08-22 18:26:12 -0400152 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400153 $this->result_id = FALSE;
154 }
155 }
156
157 // --------------------------------------------------------------------
158
159 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400160 * Result - associative array
161 *
162 * Returns the result set as an array
163 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400164 * @return array
165 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200166 protected function _fetch_assoc()
Timothy Warren80ab8162011-08-22 18:26:12 -0400167 {
Timothy Warrenab347582011-08-23 12:29:29 -0400168 return $this->result_id->fetch(PDO::FETCH_ASSOC);
Timothy Warren80ab8162011-08-22 18:26:12 -0400169 }
170
171 // --------------------------------------------------------------------
172
173 /**
174 * Result - object
175 *
176 * Returns the result set as an object
177 *
Andrey Andreev8463b912012-11-02 02:16:28 +0200178 * @param string $class_name
Timothy Warren80ab8162011-08-22 18:26:12 -0400179 * @return object
180 */
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300181 protected function _fetch_object($class_name = 'stdClass')
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200182 {
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300183 return $this->result_id->fetchObject($class_name);
Timothy Warren80ab8162011-08-22 18:26:12 -0400184 }
185
186}
187
Timothy Warren80ab8162011-08-22 18:26:12 -0400188/* End of file pdo_result.php */
189/* Location: ./system/database/drivers/pdo/pdo_result.php */