blob: 3a821fbdce018af97ccec8d6f77fd16486bf1507 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Timothy Warren80ab8162011-08-22 18:26:12 -04002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Timothy Warren80ab8162011-08-22 18:26:12 -04006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreevbe22c5b2012-03-20 23:01:53 +02008 *
Andrey Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, British Columbia Institute of Technology
Andrey Andreevbe22c5b2012-03-20 23:01:53 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreevcce6bd12018-01-09 11:32:02 +020032 * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 2.1.0
Timothy Warren80ab8162011-08-22 18:26:12 -040036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Timothy Warren80ab8162011-08-22 18:26:12 -040039
Timothy Warren80ab8162011-08-22 18:26:12 -040040/**
41 * PDO Result Class
42 *
43 * This class extends the parent result class: CI_DB_result
44 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020045 * @package CodeIgniter
46 * @subpackage Drivers
Timothy Warren80ab8162011-08-22 18:26:12 -040047 * @category Database
Derek Jonesf4a4bd82011-10-20 12:18:42 -050048 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020049 * @link https://codeigniter.com/user_guide/database/
Timothy Warren80ab8162011-08-22 18:26:12 -040050 */
51class CI_DB_pdo_result extends CI_DB_result {
52
53 /**
54 * Number of rows in the result set
55 *
Andrey Andreevbe22c5b2012-03-20 23:01:53 +020056 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -040057 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +020058 public function num_rows()
Timothy Warren80ab8162011-08-22 18:26:12 -040059 {
Andrey Andreevc7db6bb2012-07-05 15:11:20 +030060 if (is_int($this->num_rows))
Taufan Aditya18209332012-02-09 16:07:27 +070061 {
Andrey Andreevc7db6bb2012-07-05 15:11:20 +030062 return $this->num_rows;
Taufan Aditya18209332012-02-09 16:07:27 +070063 }
Andrey Andreevc7db6bb2012-07-05 15:11:20 +030064 elseif (count($this->result_array) > 0)
Taufan Aditya18209332012-02-09 16:07:27 +070065 {
Andrey Andreevc7db6bb2012-07-05 15:11:20 +030066 return $this->num_rows = count($this->result_array);
67 }
68 elseif (count($this->result_object) > 0)
69 {
70 return $this->num_rows = count($this->result_object);
71 }
Andrey Andreev7ca36132012-07-05 16:20:15 +030072 elseif (($num_rows = $this->result_id->rowCount()) > 0)
Andrey Andreevc7db6bb2012-07-05 15:11:20 +030073 {
Andrey Andreev7ca36132012-07-05 16:20:15 +030074 return $this->num_rows = $num_rows;
Taufan Aditya18209332012-02-09 16:07:27 +070075 }
76
Andrey Andreevc7db6bb2012-07-05 15:11:20 +030077 return $this->num_rows = count($this->result_array());
Timothy Warren80ab8162011-08-22 18:26:12 -040078 }
79
80 // --------------------------------------------------------------------
81
82 /**
83 * Number of fields in the result set
84 *
Andrey Andreevbe22c5b2012-03-20 23:01:53 +020085 * @return int
Timothy Warren80ab8162011-08-22 18:26:12 -040086 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +020087 public function num_fields()
Timothy Warren80ab8162011-08-22 18:26:12 -040088 {
Timothy Warrenab347582011-08-23 12:29:29 -040089 return $this->result_id->columnCount();
Timothy Warren80ab8162011-08-22 18:26:12 -040090 }
91
92 // --------------------------------------------------------------------
93
94 /**
95 * Fetch Field Names
96 *
97 * Generates an array of column names
98 *
Andrey Andreevbe22c5b2012-03-20 23:01:53 +020099 * @return bool
Timothy Warren80ab8162011-08-22 18:26:12 -0400100 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200101 public function list_fields()
Timothy Warren80ab8162011-08-22 18:26:12 -0400102 {
Andrey Andreev626f1a62012-07-04 22:38:52 +0300103 $field_names = array();
104 for ($i = 0, $c = $this->num_fields(); $i < $c; $i++)
Timothy Warren80ab8162011-08-22 18:26:12 -0400105 {
Andrey Andreev2bbbd1a2014-05-09 10:24:14 +0300106 // Might trigger an E_WARNING due to not all subdrivers
107 // supporting getColumnMeta()
Dionysis Arvanitis5df2f1b2014-07-05 12:14:56 +0300108 $field_names[$i] = @$this->result_id->getColumnMeta($i);
Andrey Andreev626f1a62012-07-04 22:38:52 +0300109 $field_names[$i] = $field_names[$i]['name'];
Timothy Warren80ab8162011-08-22 18:26:12 -0400110 }
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200111
Andrey Andreev626f1a62012-07-04 22:38:52 +0300112 return $field_names;
Timothy Warren80ab8162011-08-22 18:26:12 -0400113 }
114
115 // --------------------------------------------------------------------
116
117 /**
118 * Field data
119 *
120 * Generates an array of objects containing field meta-data
121 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400122 * @return array
123 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200124 public function field_data()
Timothy Warren80ab8162011-08-22 18:26:12 -0400125 {
Timothy Warrenab347582011-08-23 12:29:29 -0400126 try
Timothy Warren80ab8162011-08-22 18:26:12 -0400127 {
Andrey Andreev10ecc842012-11-16 01:06:20 +0200128 $retval = array();
129
130 for ($i = 0, $c = $this->num_fields(); $i < $c; $i++)
Timothy Warrenab347582011-08-23 12:29:29 -0400131 {
Andrey Andreev10ecc842012-11-16 01:06:20 +0200132 $field = $this->result_id->getColumnMeta($i);
Taufan Aditya4e44b342012-02-18 22:47:36 +0700133
Andrey Andreev10ecc842012-11-16 01:06:20 +0200134 $retval[$i] = new stdClass();
135 $retval[$i]->name = $field['name'];
136 $retval[$i]->type = $field['native_type'];
137 $retval[$i]->max_length = ($field['len'] > 0) ? $field['len'] : NULL;
138 $retval[$i]->primary_key = (int) ( ! empty($field['flags']) && in_array('primary_key', $field['flags'], TRUE));
Timothy Warrenab347582011-08-23 12:29:29 -0400139 }
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200140
Andrey Andreev10ecc842012-11-16 01:06:20 +0200141 return $retval;
Timothy Warren80ab8162011-08-22 18:26:12 -0400142 }
Timothy Warrenab347582011-08-23 12:29:29 -0400143 catch (Exception $e)
144 {
145 if ($this->db->db_debug)
146 {
Andrey Andreev8d3afde2012-11-06 12:53:47 +0200147 return $this->db->display_error('db_unsupported_feature');
Timothy Warrenab347582011-08-23 12:29:29 -0400148 }
Taufan Aditya18209332012-02-09 16:07:27 +0700149
Timothy Warrenab347582011-08-23 12:29:29 -0400150 return FALSE;
151 }
Timothy Warren80ab8162011-08-22 18:26:12 -0400152 }
153
154 // --------------------------------------------------------------------
155
156 /**
157 * Free the result
158 *
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200159 * @return void
Timothy Warren80ab8162011-08-22 18:26:12 -0400160 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200161 public function free_result()
Timothy Warren80ab8162011-08-22 18:26:12 -0400162 {
Timothy Warren6a450cf2011-08-23 12:46:11 -0400163 if (is_object($this->result_id))
Timothy Warren80ab8162011-08-22 18:26:12 -0400164 {
Timothy Warren80ab8162011-08-22 18:26:12 -0400165 $this->result_id = FALSE;
166 }
167 }
168
169 // --------------------------------------------------------------------
170
171 /**
Timothy Warren80ab8162011-08-22 18:26:12 -0400172 * Result - associative array
173 *
174 * Returns the result set as an array
175 *
Timothy Warren80ab8162011-08-22 18:26:12 -0400176 * @return array
177 */
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200178 protected function _fetch_assoc()
Timothy Warren80ab8162011-08-22 18:26:12 -0400179 {
Timothy Warrenab347582011-08-23 12:29:29 -0400180 return $this->result_id->fetch(PDO::FETCH_ASSOC);
Timothy Warren80ab8162011-08-22 18:26:12 -0400181 }
182
183 // --------------------------------------------------------------------
184
185 /**
186 * Result - object
187 *
188 * Returns the result set as an object
189 *
Andrey Andreev8463b912012-11-02 02:16:28 +0200190 * @param string $class_name
Timothy Warren80ab8162011-08-22 18:26:12 -0400191 * @return object
192 */
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300193 protected function _fetch_object($class_name = 'stdClass')
Andrey Andreevbe22c5b2012-03-20 23:01:53 +0200194 {
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300195 return $this->result_id->fetchObject($class_name);
Timothy Warren80ab8162011-08-22 18:26:12 -0400196 }
197
198}