From 10ecc84a3215ca02c11855e399a211f4e5cb60b1 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 16 Nov 2012 01:06:20 +0200 Subject: Improve DB field_data() for MySQL and CUBRID --- system/database/drivers/pdo/pdo_result.php | 55 +++++----------------- .../drivers/pdo/subdrivers/pdo_cubrid_driver.php | 38 ++++++++++++--- .../drivers/pdo/subdrivers/pdo_mysql_driver.php | 36 +++++++++++--- 3 files changed, 72 insertions(+), 57 deletions(-) (limited to 'system/database/drivers/pdo') diff --git a/system/database/drivers/pdo/pdo_result.php b/system/database/drivers/pdo/pdo_result.php index 32ab1c280..25cf87be8 100644 --- a/system/database/drivers/pdo/pdo_result.php +++ b/system/database/drivers/pdo/pdo_result.php @@ -109,55 +109,22 @@ class CI_DB_pdo_result extends CI_DB_result { */ public function field_data() { - $data = array(); - try { - if (strpos($this->result_id->queryString, 'PRAGMA') !== FALSE) - { - foreach ($this->result_array() as $field) - { - preg_match('/([a-zA-Z]+)(\(\d+\))?/', $field['type'], $matches); - - $F = new stdClass(); - $F->name = $field['name']; - $F->type = ( ! empty($matches[1])) ? $matches[1] : NULL; - $F->default = NULL; - $F->max_length = ( ! empty($matches[2])) ? preg_replace('/[^\d]/', '', $matches[2]) : NULL; - $F->primary_key = (int) $field['pk']; - $F->pdo_type = NULL; - - $data[] = $F; - } - } - else + $retval = array(); + + for ($i = 0, $c = $this->num_fields(); $i < $c; $i++) { - for($i = 0, $max = $this->num_fields(); $i < $max; $i++) - { - $field = $this->result_id->getColumnMeta($i); - - $F = new stdClass(); - $F->name = $field['name']; - $F->type = $field['native_type']; - $F->default = NULL; - $F->pdo_type = $field['pdo_type']; - - if ($field['precision'] < 0) - { - $F->max_length = NULL; - $F->primary_key = 0; - } - else - { - $F->max_length = ($field['len'] > 255) ? 0 : $field['len']; - $F->primary_key = (int) ( ! empty($field['flags']) && in_array('primary_key', $field['flags'])); - } - - $data[] = $F; - } + $field = $this->result_id->getColumnMeta($i); + + $retval[$i] = new stdClass(); + $retval[$i]->name = $field['name']; + $retval[$i]->type = $field['native_type']; + $retval[$i]->max_length = ($field['len'] > 0) ? $field['len'] : NULL; + $retval[$i]->primary_key = (int) ( ! empty($field['flags']) && in_array('primary_key', $field['flags'], TRUE)); } - return $data; + return $retval; } catch (Exception $e) { diff --git a/system/database/drivers/pdo/subdrivers/pdo_cubrid_driver.php b/system/database/drivers/pdo/subdrivers/pdo_cubrid_driver.php index 5a87cf0c7..c2112d68b 100644 --- a/system/database/drivers/pdo/subdrivers/pdo_cubrid_driver.php +++ b/system/database/drivers/pdo/subdrivers/pdo_cubrid_driver.php @@ -58,7 +58,7 @@ class CI_DB_pdo_cubrid_driver extends CI_DB_pdo_driver { /** * ORDER BY random keyword * - * @var array + * @var array */ protected $_random_keyword = array('RANDOM()', 'RANDOM(%d)'); @@ -126,16 +126,40 @@ class CI_DB_pdo_cubrid_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 1'; + if ($table === '') + { + return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE; + } + + if (($query = $this->query('SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE))) === FALSE) + { + return FALSE; + } + $query = $query->result_object(); + + $retval = array(); + for ($i = 0, $c = count($query); $i < $c; $i++) + { + $retval[$i] = new stdClass(); + $retval[$i]->name = $query[$i]->Field; + + sscanf($query[$i]->Type, '%[a-z](%d)', + $retval[$i]->type, + $retval[$i]->max_length + ); + + $retval[$i]->default = $query[$i]->Default; + $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI'); + } + + return $retval; } // -------------------------------------------------------------------- diff --git a/system/database/drivers/pdo/subdrivers/pdo_mysql_driver.php b/system/database/drivers/pdo/subdrivers/pdo_mysql_driver.php index 2d076f314..b54765d7f 100644 --- a/system/database/drivers/pdo/subdrivers/pdo_mysql_driver.php +++ b/system/database/drivers/pdo/subdrivers/pdo_mysql_driver.php @@ -162,16 +162,40 @@ class CI_DB_pdo_mysql_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 1'; + if ($table === '') + { + return ($this->db_debug) ? $this->display_error('db_field_param_missing') : FALSE; + } + + if (($query = $this->query('SHOW COLUMNS FROM '.$this->protect_identifiers($table, TRUE, NULL, FALSE))) === FALSE) + { + return FALSE; + } + $query = $query->result_object(); + + $retval = array(); + for ($i = 0, $c = count($query); $i < $c; $i++) + { + $retval[$i] = new stdClass(); + $retval[$i]->name = $query[$i]->Field; + + sscanf($query[$i]->Type, '%[a-z](%d)', + $retval[$i]->type, + $retval[$i]->max_length + ); + + $retval[$i]->default = $query[$i]->Default; + $retval[$i]->primary_key = (int) ($query[$i]->Key === 'PRI'); + } + + return $retval; } // -------------------------------------------------------------------- -- cgit v1.2.3-24-g4f1b