From 822e74ee39fffd2c35477be2bedfb37e78eb3bc1 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 16 Nov 2012 02:33:30 +0200 Subject: Improve DB field_data() for SQLite --- .../drivers/pdo/subdrivers/pdo_sqlite_driver.php | 37 ++++++++++++++++++---- system/database/drivers/sqlite/sqlite_driver.php | 37 ++++++++++++++++++---- system/database/drivers/sqlite/sqlite_result.php | 6 ++-- system/database/drivers/sqlite3/sqlite3_driver.php | 37 ++++++++++++++++++---- system/database/drivers/sqlite3/sqlite3_result.php | 17 +++++++--- 5 files changed, 108 insertions(+), 26 deletions(-) (limited to 'system/database') diff --git a/system/database/drivers/pdo/subdrivers/pdo_sqlite_driver.php b/system/database/drivers/pdo/subdrivers/pdo_sqlite_driver.php index f9d186f6c..cd6965036 100644 --- a/system/database/drivers/pdo/subdrivers/pdo_sqlite_driver.php +++ b/system/database/drivers/pdo/subdrivers/pdo_sqlite_driver.php @@ -126,16 +126,41 @@ class CI_DB_pdo_sqlite_driver extends CI_DB_pdo_driver { // -------------------------------------------------------------------- /** - * Field data query - * - * Generates a platform-specific query so that the column data can be retrieved + * Returns an object with field data * * @param string $table - * @return string + * @return array */ - protected function _field_data($table) + public function field_data($table = '') { - return 'SELECT * FROM '.$this->protect_identifiers($table).' LIMIT 0,1'; + if ($table === '') + { + return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE; + } + + if (($query = $this->query('PRAGMA TABLE_INFO('.$this->protect_identifiers($table, TRUE, NULL, FALSE).')')) === FALSE) + { + return FALSE; + } + + $query = $query->result_array(); + if (empty($query)) + { + return FALSE; + } + + $retval = array(); + for ($i = 0, $c = count($query); $i < $c; $i++) + { + $retval[$i] = new stdClass(); + $retval[$i]->name = $query[$i]['name']; + $retval[$i]->type = $query[$i]['type']; + $retval[$i]->max_length = NULL; + $retval[$i]->default = $query[$i]['dflt_value']; + $retval[$i]->primary_key = isset($query[$i]['pk']) ? (int) $query[$i]['pk'] : 0; + } + + return $retval; } // -------------------------------------------------------------------- diff --git a/system/database/drivers/sqlite/sqlite_driver.php b/system/database/drivers/sqlite/sqlite_driver.php index 23f287b7e..8c706c242 100644 --- a/system/database/drivers/sqlite/sqlite_driver.php +++ b/system/database/drivers/sqlite/sqlite_driver.php @@ -296,16 +296,41 @@ class CI_DB_sqlite_driver extends CI_DB { // -------------------------------------------------------------------- /** - * Field data query - * - * Generates a platform-specific query so that the column data can be retrieved + * Returns an object with field data * * @param string $table - * @return string + * @return array */ - protected function _field_data($table) + public function field_data($table = '') { - return 'SELECT * FROM '.$this->escape_identifiers($table).' LIMIT 1'; + if ($table === '') + { + return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE; + } + + if (($query = $this->query('PRAGMA TABLE_INFO('.$this->protect_identifiers($table, TRUE, NULL, FALSE).')')) === FALSE) + { + return FALSE; + } + + $query = $query->result_array(); + if (empty($query)) + { + return FALSE; + } + + $retval = array(); + for ($i = 0, $c = count($query); $i < $c; $i++) + { + $retval[$i] = new stdClass(); + $retval[$i]->name = $query[$i]['name']; + $retval[$i]->type = $query[$i]['type']; + $retval[$i]->max_length = NULL; + $retval[$i]->default = $query[$i]['dflt_value']; + $retval[$i]->primary_key = isset($query[$i]['pk']) ? (int) $query[$i]['pk'] : 0; + } + + return $retval; } // -------------------------------------------------------------------- diff --git a/system/database/drivers/sqlite/sqlite_result.php b/system/database/drivers/sqlite/sqlite_result.php index 0e6335f73..24f02a8b4 100644 --- a/system/database/drivers/sqlite/sqlite_result.php +++ b/system/database/drivers/sqlite/sqlite_result.php @@ -98,10 +98,8 @@ class CI_DB_sqlite_result extends CI_DB_result { { $retval[$i] = new stdClass(); $retval[$i]->name = sqlite_field_name($this->result_id, $i); - $retval[$i]->type = 'varchar'; - $retval[$i]->max_length = 0; - $retval[$i]->primary_key = 0; - $retval[$i]->default = ''; + $retval[$i]->type = NULL; + $retval[$i]->max_length = NULL; } return $retval; diff --git a/system/database/drivers/sqlite3/sqlite3_driver.php b/system/database/drivers/sqlite3/sqlite3_driver.php index f0818aea1..e37268aca 100644 --- a/system/database/drivers/sqlite3/sqlite3_driver.php +++ b/system/database/drivers/sqlite3/sqlite3_driver.php @@ -281,16 +281,41 @@ class CI_DB_sqlite3_driver extends CI_DB { // -------------------------------------------------------------------- /** - * Field data query - * - * Generates a platform-specific query so that the column data can be retrieved + * Returns an object with field data * * @param string $table - * @return string + * @return array */ - protected function _field_data($table) + public function field_data($table = '') { - return 'SELECT * FROM '.$table.' LIMIT 0,1'; + if ($table === '') + { + return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE; + } + + if (($query = $this->query('PRAGMA TABLE_INFO('.$this->protect_identifiers($table, TRUE, NULL, FALSE).')')) === FALSE) + { + return FALSE; + } + + $query = $query->result_array(); + if (empty($query)) + { + return FALSE; + } + + $retval = array(); + for ($i = 0, $c = count($query); $i < $c; $i++) + { + $retval[$i] = new stdClass(); + $retval[$i]->name = $query[$i]['name']; + $retval[$i]->type = $query[$i]['type']; + $retval[$i]->max_length = NULL; + $retval[$i]->default = $query[$i]['dflt_value']; + $retval[$i]->primary_key = isset($query[$i]['pk']) ? (int) $query[$i]['pk'] : 0; + } + + return $retval; } // -------------------------------------------------------------------- diff --git a/system/database/drivers/sqlite3/sqlite3_result.php b/system/database/drivers/sqlite3/sqlite3_result.php index 153e3480a..44fef8909 100644 --- a/system/database/drivers/sqlite3/sqlite3_result.php +++ b/system/database/drivers/sqlite3/sqlite3_result.php @@ -79,15 +79,24 @@ class CI_DB_sqlite3_result extends CI_DB_result { */ public function field_data() { + static $data_types = array( + SQLITE3_INTEGER => 'integer', + SQLITE3_FLOAT => 'float', + SQLITE3_TEXT => 'text', + SQLITE3_BLOB => 'blob', + SQLITE3_NULL => 'null' + ); + $retval = array(); for ($i = 0, $c = $this->num_fields(); $i < $this->num_fields(); $i++) { $retval[$i] = new stdClass(); $retval[$i]->name = $this->result_id->columnName($i); - $retval[$i]->type = 'varchar'; - $retval[$i]->max_length = 0; - $retval[$i]->primary_key = 0; - $retval[$i]->default = ''; + + $type = $this->result_id->columnType($i); + $retval[$i]->type = isset($data_types[$type]) ? $data_types[$type] : $type; + + $retval[$i]->max_length = NULL; } return $retval; -- cgit v1.2.3-24-g4f1b