blob: ba38f74545112e4f9fc140ef00b98ff0a38cddec [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Alex Bilbie84445d02011-03-10 16:43:39 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Alex Bilbie84445d02011-03-10 16:43:39 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev0e8968a2012-01-26 02:04:37 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev0e8968a2012-01-26 02:04:37 +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 *
Alex Bilbie84445d02011-03-10 16:43:39 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, 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)
Alex Bilbie84445d02011-03-10 16:43:39 +000023 * @link http://codeigniter.com
Andrey Andreevc7db6bb2012-07-05 15:11:20 +030024 * @since Version 1.0
Alex Bilbie84445d02011-03-10 16:43:39 +000025 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Alex Bilbie84445d02011-03-10 16:43:39 +000028
Alex Bilbie84445d02011-03-10 16:43:39 +000029/**
Alex Bilbie01ab0042011-03-18 19:24:30 +000030 * SQLSRV Result Class
Alex Bilbie84445d02011-03-10 16:43:39 +000031 *
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
Alex Bilbie84445d02011-03-10 16:43:39 +000036 * @link http://codeigniter.com/user_guide/database/
Andrey Andreevc7db6bb2012-07-05 15:11:20 +030037 * @since 2.0.3
Alex Bilbie84445d02011-03-10 16:43:39 +000038 */
Alex Bilbie01ab0042011-03-18 19:24:30 +000039class CI_DB_sqlsrv_result extends CI_DB_result {
Alex Bilbie84445d02011-03-10 16:43:39 +000040
41 /**
Andrey Andreevba8bf562014-01-21 19:04:18 +020042 * Scrollable flag
43 *
44 * @var mixed
45 */
46 public $scrollable;
47
48 // --------------------------------------------------------------------
49
50 /**
51 * Constructor
52 *
53 * @param object $driver_object
54 * @return void
55 */
56 public function __construct(&$driver_object)
57 {
58 parent::__construct($driver_object);
59
60 $this->scrollable = $driver_object->scrollable;
61 }
62
63 // --------------------------------------------------------------------
64
65 /**
Alex Bilbie84445d02011-03-10 16:43:39 +000066 * Number of rows in the result set
67 *
Andrey Andreev0e8968a2012-01-26 02:04:37 +020068 * @return int
Alex Bilbie84445d02011-03-10 16:43:39 +000069 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +020070 public function num_rows()
Alex Bilbie84445d02011-03-10 16:43:39 +000071 {
Andrey Andreevba8bf562014-01-21 19:04:18 +020072 // sqlsrv_num_rows() doesn't work with the FORWARD and DYNAMIC cursors (FALSE is the same as FORWARD)
73 if ( ! in_array($this->scrollable, array(FALSE, SQLSRV_CURSOR_FORWARD, SQLSRV_CURSOR_DYNAMIC), TRUE))
74 {
75 return parent::num_rows();
76 }
77
Andrey Andreevc7db6bb2012-07-05 15:11:20 +030078 return is_int($this->num_rows)
79 ? $this->num_rows
Andrey Andreevba8bf562014-01-21 19:04:18 +020080 : $this->num_rows = sqlsrv_num_rows($this->result_id);
Alex Bilbie84445d02011-03-10 16:43:39 +000081 }
82
83 // --------------------------------------------------------------------
84
85 /**
86 * Number of fields in the result set
87 *
Andrey Andreev0e8968a2012-01-26 02:04:37 +020088 * @return int
Alex Bilbie84445d02011-03-10 16:43:39 +000089 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +020090 public function num_fields()
Alex Bilbie84445d02011-03-10 16:43:39 +000091 {
Alex Bilbie079069c2011-03-10 19:36:58 +000092 return @sqlsrv_num_fields($this->result_id);
Alex Bilbie84445d02011-03-10 16:43:39 +000093 }
94
95 // --------------------------------------------------------------------
96
97 /**
98 * Fetch Field Names
99 *
100 * Generates an array of column names
101 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000102 * @return array
103 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200104 public function list_fields()
Alex Bilbie84445d02011-03-10 16:43:39 +0000105 {
106 $field_names = array();
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200107 foreach (sqlsrv_field_metadata($this->result_id) as $offset => $field)
Alex Bilbie84445d02011-03-10 16:43:39 +0000108 {
Alex Bilbie3a43c7a2011-03-18 19:48:04 +0000109 $field_names[] = $field['Name'];
Alex Bilbie84445d02011-03-10 16:43:39 +0000110 }
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200111
Alex Bilbie84445d02011-03-10 16:43:39 +0000112 return $field_names;
113 }
114
115 // --------------------------------------------------------------------
116
117 /**
118 * Field data
119 *
120 * Generates an array of objects containing field meta-data
121 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000122 * @return array
123 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200124 public function field_data()
Alex Bilbie84445d02011-03-10 16:43:39 +0000125 {
126 $retval = array();
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200127 foreach (sqlsrv_field_metadata($this->result_id) as $i => $field)
Alex Bilbie84445d02011-03-10 16:43:39 +0000128 {
Andrey Andreevf1e1b772012-11-16 01:27:00 +0200129 $retval[$i] = new stdClass();
130 $retval[$i]->name = $field['Name'];
131 $retval[$i]->type = $field['Type'];
132 $retval[$i]->max_length = $field['Size'];
Alex Bilbie84445d02011-03-10 16:43:39 +0000133 }
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200134
Alex Bilbie84445d02011-03-10 16:43:39 +0000135 return $retval;
136 }
137
138 // --------------------------------------------------------------------
139
140 /**
141 * Free the result
142 *
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200143 * @return void
Alex Bilbie84445d02011-03-10 16:43:39 +0000144 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200145 public function free_result()
Alex Bilbie84445d02011-03-10 16:43:39 +0000146 {
147 if (is_resource($this->result_id))
148 {
Alex Bilbie079069c2011-03-10 19:36:58 +0000149 sqlsrv_free_stmt($this->result_id);
Alex Bilbie84445d02011-03-10 16:43:39 +0000150 $this->result_id = FALSE;
151 }
152 }
153
154 // --------------------------------------------------------------------
155
156 /**
Alex Bilbie84445d02011-03-10 16:43:39 +0000157 * Result - associative array
158 *
159 * Returns the result set as an array
160 *
Alex Bilbie84445d02011-03-10 16:43:39 +0000161 * @return array
162 */
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200163 protected function _fetch_assoc()
Alex Bilbie84445d02011-03-10 16:43:39 +0000164 {
Alex Bilbie079069c2011-03-10 19:36:58 +0000165 return sqlsrv_fetch_array($this->result_id, SQLSRV_FETCH_ASSOC);
Alex Bilbie84445d02011-03-10 16:43:39 +0000166 }
167
168 // --------------------------------------------------------------------
169
170 /**
171 * Result - object
172 *
173 * Returns the result set as an object
174 *
Andrey Andreev8463b912012-11-02 02:16:28 +0200175 * @param string $class_name
Alex Bilbie84445d02011-03-10 16:43:39 +0000176 * @return object
177 */
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300178 protected function _fetch_object($class_name = 'stdClass')
Alex Bilbie84445d02011-03-10 16:43:39 +0000179 {
Andrey Andreev9a4f3562012-07-06 11:57:37 +0300180 return sqlsrv_fetch_object($this->result_id, $class_name);
Alex Bilbie84445d02011-03-10 16:43:39 +0000181 }
182
183}
184
Andrey Andreev0e8968a2012-01-26 02:04:37 +0200185/* End of file sqlsrv_result.php */
Andrey Andreev9659bd52012-03-20 16:27:47 +0200186/* Location: ./system/database/drivers/sqlsrv/sqlsrv_result.php */