Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 5.1.6 or newer |
| 6 | * |
| 7 | * NOTICE OF LICENSE |
| 8 | * |
| 9 | * Licensed under the Open Software License version 3.0 |
| 10 | * |
| 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
Andrey Andreev | f20fb98 | 2012-01-24 15:26:01 +0200 | [diff] [blame] | 12 | * bundled with this package in the files license.txt / license.rst. It is |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 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 | * |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 19 | * @package CodeIgniter |
| 20 | * @author EllisLab Dev Team |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Andrey Andreev | d947eba | 2012-04-09 14:58:28 +0300 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
| 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 25 | * @filesource |
| 26 | */ |
| 27 | |
| 28 | /** |
Andrey Andreev | 9a4f356 | 2012-07-06 11:57:37 +0300 | [diff] [blame^] | 29 | * SQLite3 Result Class |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 30 | * |
| 31 | * This class extends the parent result class: CI_DB_result |
| 32 | * |
| 33 | * @category Database |
Andrey Andreev | 9a4f356 | 2012-07-06 11:57:37 +0300 | [diff] [blame^] | 34 | * @author Andrey Andreev |
| 35 | * @link http://codeigniter.com/user_guide/database/ |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 36 | * @since 3.0 |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 37 | */ |
| 38 | class CI_DB_sqlite3_result extends CI_DB_result { |
| 39 | |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 40 | // num_fields() might be called multiple times, so we'll use this one to cache it's result |
| 41 | protected $_num_fields; |
| 42 | |
| 43 | /** |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 44 | * Number of fields in the result set |
| 45 | * |
| 46 | * @return int |
| 47 | */ |
| 48 | public function num_fields() |
| 49 | { |
| 50 | return ( ! is_int($this->_num_fields)) |
| 51 | ? $this->_num_fields = $this->result_id->numColumns() |
| 52 | : $this->_num_fields; |
| 53 | } |
| 54 | |
| 55 | // -------------------------------------------------------------------- |
| 56 | |
| 57 | /** |
| 58 | * Fetch Field Names |
| 59 | * |
| 60 | * Generates an array of column names |
| 61 | * |
| 62 | * @return array |
| 63 | */ |
| 64 | public function list_fields() |
| 65 | { |
| 66 | $field_names = array(); |
Andrey Andreev | 53921ca | 2012-01-16 14:35:22 +0200 | [diff] [blame] | 67 | for ($i = 0, $c = $this->num_fields(); $i < $c; $i++) |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 68 | { |
| 69 | $field_names[] = $this->result_id->columnName($i); |
| 70 | } |
| 71 | |
| 72 | return $field_names; |
| 73 | } |
| 74 | |
| 75 | // -------------------------------------------------------------------- |
| 76 | |
| 77 | /** |
| 78 | * Field data |
| 79 | * |
| 80 | * Generates an array of objects containing field meta-data |
| 81 | * |
| 82 | * @return array |
| 83 | */ |
| 84 | public function field_data() |
| 85 | { |
| 86 | $retval = array(); |
| 87 | for ($i = 0, $c = $this->num_fields(); $i < $this->num_fields(); $i++) |
| 88 | { |
| 89 | $retval[$i] = new stdClass(); |
| 90 | $retval[$i]->name = $this->result_id->columnName($i); |
| 91 | $retval[$i]->type = 'varchar'; |
| 92 | $retval[$i]->max_length = 0; |
| 93 | $retval[$i]->primary_key = 0; |
| 94 | $retval[$i]->default = ''; |
| 95 | } |
| 96 | |
| 97 | return $retval; |
| 98 | } |
| 99 | |
| 100 | // -------------------------------------------------------------------- |
| 101 | |
| 102 | /** |
| 103 | * Free the result |
| 104 | * |
| 105 | * @return void |
| 106 | */ |
| 107 | public function free_result() |
| 108 | { |
| 109 | if (is_object($this->result_id)) |
| 110 | { |
| 111 | $this->result_id->finalize(); |
| 112 | $this->result_id = NULL; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // -------------------------------------------------------------------- |
| 117 | |
| 118 | /** |
| 119 | * Result - associative array |
| 120 | * |
| 121 | * Returns the result set as an array |
| 122 | * |
| 123 | * @return array |
| 124 | */ |
| 125 | protected function _fetch_assoc() |
| 126 | { |
| 127 | return $this->result_id->fetchArray(SQLITE3_ASSOC); |
| 128 | } |
| 129 | |
| 130 | // -------------------------------------------------------------------- |
| 131 | |
| 132 | /** |
| 133 | * Result - object |
| 134 | * |
| 135 | * Returns the result set as an object |
| 136 | * |
Andrey Andreev | 9a4f356 | 2012-07-06 11:57:37 +0300 | [diff] [blame^] | 137 | * @param string |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 138 | * @return object |
| 139 | */ |
Andrey Andreev | 9a4f356 | 2012-07-06 11:57:37 +0300 | [diff] [blame^] | 140 | protected function _fetch_object($class_name = 'stdClass') |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 141 | { |
Andrey Andreev | 9a4f356 | 2012-07-06 11:57:37 +0300 | [diff] [blame^] | 142 | // No native support for fetching rows as objects |
| 143 | if (($row = $this->result_id->fetchArray(SQLITE3_ASSOC)) === FALSE) |
| 144 | { |
| 145 | return FALSE; |
| 146 | } |
| 147 | elseif ($class_name === 'stdClass') |
| 148 | { |
| 149 | return (object) $row; |
| 150 | } |
| 151 | |
| 152 | $class_name = new $class_name(); |
| 153 | foreach (array_keys($row) as $key) |
| 154 | { |
| 155 | $class_name->$key = $row[$key]; |
| 156 | } |
| 157 | |
| 158 | return $class_name; |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | // -------------------------------------------------------------------- |
| 162 | |
| 163 | /** |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 164 | * Data Seek |
| 165 | * |
| 166 | * Moves the internal pointer to the desired offset. We call |
| 167 | * this internally before fetching results to make sure the |
| 168 | * result set starts at zero |
| 169 | * |
| 170 | * @return array |
| 171 | */ |
Andrey Andreev | f944d3b | 2012-03-20 22:12:55 +0200 | [diff] [blame] | 172 | protected function _data_seek($n = 0) |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 173 | { |
| 174 | // Only resetting to the start of the result set is supported |
| 175 | return $this->result_id->reset(); |
| 176 | } |
| 177 | |
| 178 | } |
| 179 | |
| 180 | /* End of file sqlite3_result.php */ |
Andrey Andreev | f944d3b | 2012-03-20 22:12:55 +0200 | [diff] [blame] | 181 | /* Location: ./system/database/drivers/sqlite3/sqlite3_result.php */ |