Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
vlakoff | fe83249 | 2012-07-13 20:40:06 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 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 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 28 | |
| 29 | /** |
Andrey Andreev | 9a4f356 | 2012-07-06 11:57:37 +0300 | [diff] [blame] | 30 | * SQLite3 Result Class |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 31 | * |
| 32 | * This class extends the parent result class: CI_DB_result |
| 33 | * |
| 34 | * @category Database |
Andrey Andreev | 9a4f356 | 2012-07-06 11:57:37 +0300 | [diff] [blame] | 35 | * @author Andrey Andreev |
| 36 | * @link http://codeigniter.com/user_guide/database/ |
Andrey Andreev | 5ca0513 | 2012-07-05 12:06:34 +0300 | [diff] [blame] | 37 | * @since 3.0 |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 38 | */ |
| 39 | class CI_DB_sqlite3_result extends CI_DB_result { |
| 40 | |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 41 | /** |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 42 | * Number of fields in the result set |
| 43 | * |
| 44 | * @return int |
| 45 | */ |
| 46 | public function num_fields() |
| 47 | { |
Andrey Andreev | 8463b91 | 2012-11-02 02:16:28 +0200 | [diff] [blame] | 48 | return $this->result_id->numColumns(); |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | // -------------------------------------------------------------------- |
| 52 | |
| 53 | /** |
| 54 | * Fetch Field Names |
| 55 | * |
| 56 | * Generates an array of column names |
| 57 | * |
| 58 | * @return array |
| 59 | */ |
| 60 | public function list_fields() |
| 61 | { |
| 62 | $field_names = array(); |
Andrey Andreev | 53921ca | 2012-01-16 14:35:22 +0200 | [diff] [blame] | 63 | for ($i = 0, $c = $this->num_fields(); $i < $c; $i++) |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 64 | { |
| 65 | $field_names[] = $this->result_id->columnName($i); |
| 66 | } |
| 67 | |
| 68 | return $field_names; |
| 69 | } |
| 70 | |
| 71 | // -------------------------------------------------------------------- |
| 72 | |
| 73 | /** |
| 74 | * Field data |
| 75 | * |
| 76 | * Generates an array of objects containing field meta-data |
| 77 | * |
| 78 | * @return array |
| 79 | */ |
| 80 | public function field_data() |
| 81 | { |
Andrey Andreev | 822e74e | 2012-11-16 02:33:30 +0200 | [diff] [blame^] | 82 | static $data_types = array( |
| 83 | SQLITE3_INTEGER => 'integer', |
| 84 | SQLITE3_FLOAT => 'float', |
| 85 | SQLITE3_TEXT => 'text', |
| 86 | SQLITE3_BLOB => 'blob', |
| 87 | SQLITE3_NULL => 'null' |
| 88 | ); |
| 89 | |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 90 | $retval = array(); |
| 91 | for ($i = 0, $c = $this->num_fields(); $i < $this->num_fields(); $i++) |
| 92 | { |
| 93 | $retval[$i] = new stdClass(); |
| 94 | $retval[$i]->name = $this->result_id->columnName($i); |
Andrey Andreev | 822e74e | 2012-11-16 02:33:30 +0200 | [diff] [blame^] | 95 | |
| 96 | $type = $this->result_id->columnType($i); |
| 97 | $retval[$i]->type = isset($data_types[$type]) ? $data_types[$type] : $type; |
| 98 | |
| 99 | $retval[$i]->max_length = NULL; |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | return $retval; |
| 103 | } |
| 104 | |
| 105 | // -------------------------------------------------------------------- |
| 106 | |
| 107 | /** |
| 108 | * Free the result |
| 109 | * |
| 110 | * @return void |
| 111 | */ |
| 112 | public function free_result() |
| 113 | { |
| 114 | if (is_object($this->result_id)) |
| 115 | { |
| 116 | $this->result_id->finalize(); |
| 117 | $this->result_id = NULL; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // -------------------------------------------------------------------- |
| 122 | |
| 123 | /** |
| 124 | * Result - associative array |
| 125 | * |
| 126 | * Returns the result set as an array |
| 127 | * |
| 128 | * @return array |
| 129 | */ |
| 130 | protected function _fetch_assoc() |
| 131 | { |
| 132 | return $this->result_id->fetchArray(SQLITE3_ASSOC); |
| 133 | } |
| 134 | |
| 135 | // -------------------------------------------------------------------- |
| 136 | |
| 137 | /** |
| 138 | * Result - object |
| 139 | * |
| 140 | * Returns the result set as an object |
| 141 | * |
Andrey Andreev | 8463b91 | 2012-11-02 02:16:28 +0200 | [diff] [blame] | 142 | * @param string $class_name |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 143 | * @return object |
| 144 | */ |
Andrey Andreev | 9a4f356 | 2012-07-06 11:57:37 +0300 | [diff] [blame] | 145 | protected function _fetch_object($class_name = 'stdClass') |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 146 | { |
Andrey Andreev | 9a4f356 | 2012-07-06 11:57:37 +0300 | [diff] [blame] | 147 | // No native support for fetching rows as objects |
| 148 | if (($row = $this->result_id->fetchArray(SQLITE3_ASSOC)) === FALSE) |
| 149 | { |
| 150 | return FALSE; |
| 151 | } |
| 152 | elseif ($class_name === 'stdClass') |
| 153 | { |
| 154 | return (object) $row; |
| 155 | } |
| 156 | |
| 157 | $class_name = new $class_name(); |
| 158 | foreach (array_keys($row) as $key) |
| 159 | { |
| 160 | $class_name->$key = $row[$key]; |
| 161 | } |
| 162 | |
| 163 | return $class_name; |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | // -------------------------------------------------------------------- |
| 167 | |
| 168 | /** |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 169 | * Data Seek |
| 170 | * |
| 171 | * Moves the internal pointer to the desired offset. We call |
| 172 | * this internally before fetching results to make sure the |
Andrey Andreev | 8463b91 | 2012-11-02 02:16:28 +0200 | [diff] [blame] | 173 | * result set starts at zero. |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 174 | * |
Andrey Andreev | 8463b91 | 2012-11-02 02:16:28 +0200 | [diff] [blame] | 175 | * @param int $n (ignored) |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 176 | * @return array |
| 177 | */ |
Andrey Andreev | f944d3b | 2012-03-20 22:12:55 +0200 | [diff] [blame] | 178 | protected function _data_seek($n = 0) |
Andrey Andreev | 8ae24c5 | 2012-01-16 13:05:23 +0200 | [diff] [blame] | 179 | { |
| 180 | // Only resetting to the start of the result set is supported |
| 181 | return $this->result_id->reset(); |
| 182 | } |
| 183 | |
| 184 | } |
| 185 | |
| 186 | /* End of file sqlite3_result.php */ |
Andrey Andreev | f944d3b | 2012-03-20 22:12:55 +0200 | [diff] [blame] | 187 | /* Location: ./system/database/drivers/sqlite3/sqlite3_result.php */ |