From ab4f61bacfa022c0c7238e9661ad9cb2ac440d95 Mon Sep 17 00:00:00 2001 From: admin Date: Mon, 25 Sep 2006 22:12:32 +0000 Subject: --- system/database/DB_utility.php | 114 +++++++++++++-------- system/database/drivers/mssql/mssql_result.php | 23 ++++- system/database/drivers/mssql/mssql_utility.php | 31 ++++++ system/database/drivers/mysql/mysql_result.php | 21 ++++ system/database/drivers/mysql/mysql_utility.php | 33 ++++++ system/database/drivers/mysqli/mysqli_result.php | 23 ++++- system/database/drivers/mysqli/mysqli_utility.php | 31 ++++++ system/database/drivers/oci8/oci8_result.php | 21 ++++ system/database/drivers/oci8/oci8_utility.php | 32 ++++++ system/database/drivers/odbc/odbc_result.php | 23 ++++- system/database/drivers/odbc/odbc_utility.php | 41 ++++++++ system/database/drivers/postgre/postgre_result.php | 23 ++++- .../database/drivers/postgre/postgre_utility.php | 32 ++++++ system/database/drivers/sqlite/sqlite_result.php | 23 ++++- system/database/drivers/sqlite/sqlite_utility.php | 31 ++++++ user_guide/database/fields.html | 22 +++- 16 files changed, 474 insertions(+), 50 deletions(-) diff --git a/system/database/DB_utility.php b/system/database/DB_utility.php index 36d74c5b3..950db7dfd 100644 --- a/system/database/DB_utility.php +++ b/system/database/DB_utility.php @@ -64,6 +64,30 @@ class CI_DB_utility { // -------------------------------------------------------------------- + /** + * Primary + * + * Retrieves the primary key. It assumes that the row in the first + * position is the primary key + * + * @access public + * @param string the table name + * @return string + */ + function primary($table = '') + { + $fields = $this->field_names($table); + + if ( ! is_array($fields)) + { + return FALSE; + } + + return current($fields); + } + + // -------------------------------------------------------------------- + /** * Returns an array of table names * @@ -187,30 +211,6 @@ class CI_DB_utility { // -------------------------------------------------------------------- - /** - * Primary - * - * Retrieves the primary key. It assumes that the row in the first - * position is the primary key - * - * @access public - * @param string the table name - * @return string - */ - function primary($table = '') - { - $fields = $this->field_names($table); - - if ( ! is_array($fields)) - { - return FALSE; - } - - return current($fields); - } - - // -------------------------------------------------------------------- - /** * Create database * @@ -218,9 +218,9 @@ class CI_DB_utility { * @param string the database name * @return bool */ - function create_database($name) + function create_database($db_name) { - $sql = $this->_create_database($name); + $sql = $this->_create_database($db_name); if (is_bool($sql)) { @@ -239,9 +239,9 @@ class CI_DB_utility { * @param string the database name * @return bool */ - function drop_database($name) + function drop_database($db_name) { - $sql = $this->_drop_database($name); + $sql = $this->_drop_database($db_name); if (is_bool($sql)) { @@ -273,48 +273,76 @@ class CI_DB_utility { return $dbs; } - + // -------------------------------------------------------------------- /** - * Drop Table + * Optimize Table * * @access public * @param string the table name * @return bool */ - function drop_table($name) + function optimize_table($table_name) { - $sql = $this->_drop_table($name); + $sql = $this->_optimize_table($table_name); if (is_bool($sql)) { return $sql; } - return $this->db->query($sql); + $query = $this->db->query($sql); + return current($query->result_array()); } + // -------------------------------------------------------------------- - - function alter_table() + /** + * Optimize Table + * + * @access public + * @param string the table name + * @return bool + */ + + function repair_table($table_name) { - } + $sql = $this->_repair_table($table_name); + + if (is_bool($sql)) + { + return $sql; + } - function create_index() - { + $query = $this->db->query($sql); + return current($query->result_array()); } - - function drop_index() + + // -------------------------------------------------------------------- + + /** + * Drop Table + * + * @access public + * @param string the table name + * @return bool + */ + function drop_table($table_name) { - } + $sql = $this->_drop_table($table_name); + + if (is_bool($sql)) + { + return $sql; + } - function optimize() - { + return $this->db->query($sql); } + } ?> \ No newline at end of file diff --git a/system/database/drivers/mssql/mssql_result.php b/system/database/drivers/mssql/mssql_result.php index a8a8e2006..53b7832d9 100644 --- a/system/database/drivers/mssql/mssql_result.php +++ b/system/database/drivers/mssql/mssql_result.php @@ -49,7 +49,28 @@ class CI_DB_mssql_result extends CI_DB_result { { return @mssql_num_fields($this->result_id); } - + + // -------------------------------------------------------------------- + + /** + * Fetch Field Names + * + * Generates an array of column names + * + * @access public + * @return array + */ + function field_names() + { + $field_names = array(); + while ($field = mssql_fetch_field($this->result_id)) + { + $field_names[] = $field->name; + } + + return $field_names; + } + // -------------------------------------------------------------------- /** diff --git a/system/database/drivers/mssql/mssql_utility.php b/system/database/drivers/mssql/mssql_utility.php index b85a420e7..cf15104c6 100644 --- a/system/database/drivers/mssql/mssql_utility.php +++ b/system/database/drivers/mssql/mssql_utility.php @@ -138,6 +138,37 @@ class CI_DB_mssql_utility extends CI_DB_utility { } + // -------------------------------------------------------------------- + + /** + * Optimize table query + * + * Generates a platform-specific query so that a table can be optimized + * + * @access private + * @param string the table name + * @return object + */ + function _optimize_table($table) + { + return FALSE; // Is this supported in MS SQL? + } + + // -------------------------------------------------------------------- + + /** + * Repair table query + * + * Generates a platform-specific query so that a table can be repaired + * + * @access private + * @param string the table name + * @return object + */ + function _repair_table($table) + { + return return FALSE; // Is this supported in MS SQL? + } } diff --git a/system/database/drivers/mysql/mysql_result.php b/system/database/drivers/mysql/mysql_result.php index e540489bb..91c4af12c 100644 --- a/system/database/drivers/mysql/mysql_result.php +++ b/system/database/drivers/mysql/mysql_result.php @@ -52,6 +52,27 @@ class CI_DB_mysql_result extends CI_DB_result { // -------------------------------------------------------------------- + /** + * Fetch Field Names + * + * Generates an array of column names + * + * @access public + * @return array + */ + function field_names() + { + $field_names = array(); + while ($field = mysql_fetch_field($this->result_id)) + { + $field_names[] = $field->name; + } + + return $field_names; + } + + // -------------------------------------------------------------------- + /** * Field data * diff --git a/system/database/drivers/mysql/mysql_utility.php b/system/database/drivers/mysql/mysql_utility.php index a0c746207..800a90c7a 100644 --- a/system/database/drivers/mysql/mysql_utility.php +++ b/system/database/drivers/mysql/mysql_utility.php @@ -136,6 +136,39 @@ class CI_DB_mysql_utility extends CI_DB_utility { return "SELECT * FROM ".$this->db->_escape_table($table)." LIMIT 1"; } + // -------------------------------------------------------------------- + + /** + * Optimize table query + * + * Generates a platform-specific query so that a table can be optimized + * + * @access private + * @param string the table name + * @return object + */ + function _optimize_table($table) + { + return "OPTIMIZE TABLE ".$this->db->_escape_table($table); + } + + // -------------------------------------------------------------------- + + /** + * Repair table query + * + * Generates a platform-specific query so that a table can be repaired + * + * @access private + * @param string the table name + * @return object + */ + function _repair_table($table) + { + return "REPAIR TABLE ".$this->db->_escape_table($table); + } + + } diff --git a/system/database/drivers/mysqli/mysqli_result.php b/system/database/drivers/mysqli/mysqli_result.php index b0db6c106..2eca68ff5 100644 --- a/system/database/drivers/mysqli/mysqli_result.php +++ b/system/database/drivers/mysqli/mysqli_result.php @@ -49,7 +49,28 @@ class CI_DB_mysqli_result extends CI_DB_result { { return @mysqli_num_fields($this->result_id); } - + + // -------------------------------------------------------------------- + + /** + * Fetch Field Names + * + * Generates an array of column names + * + * @access public + * @return array + */ + function field_names() + { + $field_names = array(); + while ($field = mysql_fetch_field($this->result_id)) + { + $field_names[] = $field->name; + } + + return $field_names; + } + // -------------------------------------------------------------------- /** diff --git a/system/database/drivers/mysqli/mysqli_utility.php b/system/database/drivers/mysqli/mysqli_utility.php index 328661866..8c9e1e960 100644 --- a/system/database/drivers/mysqli/mysqli_utility.php +++ b/system/database/drivers/mysqli/mysqli_utility.php @@ -136,6 +136,37 @@ class CI_DB_mysqli_utility extends CI_DB_utility { return "SELECT * FROM ".$this->db->_escape_table($table)." LIMIT 1"; } + // -------------------------------------------------------------------- + + /** + * Optimize table query + * + * Generates a platform-specific query so that a table can be optimized + * + * @access private + * @param string the table name + * @return object + */ + function _optimize_table($table) + { + return "OPTIMIZE TABLE ".$this->db->_escape_table($table); + } + + // -------------------------------------------------------------------- + + /** + * Repair table query + * + * Generates a platform-specific query so that a table can be repaired + * + * @access private + * @param string the table name + * @return object + */ + function _repair_table($table) + { + return "REPAIR TABLE ".$this->db->_escape_table($table); + } } diff --git a/system/database/drivers/oci8/oci8_result.php b/system/database/drivers/oci8/oci8_result.php index d2354a1c6..59adda76b 100644 --- a/system/database/drivers/oci8/oci8_result.php +++ b/system/database/drivers/oci8/oci8_result.php @@ -74,6 +74,27 @@ class CI_DB_oci8_result extends CI_DB_result { return $count; } + // -------------------------------------------------------------------- + + /** + * Fetch Field Names + * + * Generates an array of column names + * + * @access public + * @return array + */ + function field_names() + { + $field_names = array(); + $fieldCount = $this->num_fields(); + for ($c = 1; $c <= $fieldCount; $c++) + { + $field_names[] = ocicolumnname($this->stmt_id, $c); + } + return $field_names; + } + // -------------------------------------------------------------------- /** diff --git a/system/database/drivers/oci8/oci8_utility.php b/system/database/drivers/oci8/oci8_utility.php index 9c3059f41..011e971d9 100644 --- a/system/database/drivers/oci8/oci8_utility.php +++ b/system/database/drivers/oci8/oci8_utility.php @@ -138,6 +138,38 @@ class CI_DB_oci8_utility extends CI_DB_utility { } + // -------------------------------------------------------------------- + + /** + * Optimize table query + * + * Generates a platform-specific query so that a table can be optimized + * + * @access private + * @param string the table name + * @return object + */ + function _optimize_table($table) + { + return FALSE; // Is this supported in Oracle? + } + + // -------------------------------------------------------------------- + + /** + * Repair table query + * + * Generates a platform-specific query so that a table can be repaired + * + * @access private + * @param string the table name + * @return object + */ + function _repair_table($table) + { + return return FALSE; // Is this supported in Oracle? + } + } ?> \ No newline at end of file diff --git a/system/database/drivers/odbc/odbc_result.php b/system/database/drivers/odbc/odbc_result.php index 49e5e9012..385209c56 100644 --- a/system/database/drivers/odbc/odbc_result.php +++ b/system/database/drivers/odbc/odbc_result.php @@ -49,7 +49,28 @@ class CI_DB_odbc_result extends CI_DB_result { { return @odbc_num_fields($this->result_id); } - + + // -------------------------------------------------------------------- + + /** + * Fetch Field Names + * + * Generates an array of column names + * + * @access public + * @return array + */ + function field_names() + { + $field_names = array(); + for ($i = 0; $i < $this->num_fields(); $i++) + { + $field_names[] = odbc_field_name($this->result_id, $i); + } + + return $field_names; + } + // -------------------------------------------------------------------- /** diff --git a/system/database/drivers/odbc/odbc_utility.php b/system/database/drivers/odbc/odbc_utility.php index 5b4558f68..2932da874 100644 --- a/system/database/drivers/odbc/odbc_utility.php +++ b/system/database/drivers/odbc/odbc_utility.php @@ -159,6 +159,47 @@ class CI_DB_odbc_utility extends CI_DB_utility { return "SELECT TOP 1 FROM ".$this->db->_escape_table($table); } + // -------------------------------------------------------------------- + + /** + * Optimize table query + * + * Generates a platform-specific query so that a table can be optimized + * + * @access private + * @param string the table name + * @return object + */ + function _optimize_table($table) + { + // Not a supported ODBC feature + if ($this->db_debug) + { + return $this->display_error('db_unsuported_feature'); + } + return FALSE; + } + + // -------------------------------------------------------------------- + + /** + * Repair table query + * + * Generates a platform-specific query so that a table can be repaired + * + * @access private + * @param string the table name + * @return object + */ + function _repair_table($table) + { + // Not a supported ODBC feature + if ($this->db_debug) + { + return $this->display_error('db_unsuported_feature'); + } + return FALSE; + } } diff --git a/system/database/drivers/postgre/postgre_result.php b/system/database/drivers/postgre/postgre_result.php index 6af7f94f2..ee838b450 100644 --- a/system/database/drivers/postgre/postgre_result.php +++ b/system/database/drivers/postgre/postgre_result.php @@ -49,7 +49,28 @@ class CI_DB_postgre_result extends CI_DB_result { { return @pg_num_fields($this->result_id); } - + + // -------------------------------------------------------------------- + + /** + * Fetch Field Names + * + * Generates an array of column names + * + * @access public + * @return array + */ + function field_names() + { + $field_names = array(); + for ($i = 0; $i < $this->num_fields(); $i++) + { + $Ffield_names[] = pg_field_name($this->result_id, $i); + } + + return $field_names; + } + // -------------------------------------------------------------------- /** diff --git a/system/database/drivers/postgre/postgre_utility.php b/system/database/drivers/postgre/postgre_utility.php index b31609aea..b2fdd5fd4 100644 --- a/system/database/drivers/postgre/postgre_utility.php +++ b/system/database/drivers/postgre/postgre_utility.php @@ -137,6 +137,38 @@ class CI_DB_postgre_utility extends CI_DB_utility { return "SELECT * FROM ".$this->db->_escape_table($table)." LIMIT 1"; } + // -------------------------------------------------------------------- + + /** + * Optimize table query + * + * Generates a platform-specific query so that a table can be optimized + * + * @access private + * @param string the table name + * @return object + */ + function _optimize_table($table) + { + return FALSE; // Is this supported in Postgre? + } + + // -------------------------------------------------------------------- + + /** + * Repair table query + * + * Generates a platform-specific query so that a table can be repaired + * + * @access private + * @param string the table name + * @return object + */ + function _repair_table($table) + { + return return FALSE; // Is this supported in Postgre? + } + } diff --git a/system/database/drivers/sqlite/sqlite_result.php b/system/database/drivers/sqlite/sqlite_result.php index f30a8cfdf..7f48ce8aa 100644 --- a/system/database/drivers/sqlite/sqlite_result.php +++ b/system/database/drivers/sqlite/sqlite_result.php @@ -49,7 +49,28 @@ class CI_DB_sqlite_result extends CI_DB_result { { return @sqlite_num_fields($this->result_id); } - + + // -------------------------------------------------------------------- + + /** + * Fetch Field Names + * + * Generates an array of column names + * + * @access public + * @return array + */ + function field_names() + { + $field_names = array(); + for ($i = 0; $i < $this->num_fields(); $i++) + { + $Ffield_names[] = sqlite_field_name($this->result_id, $i); + } + + return $field_names; + } + // -------------------------------------------------------------------- /** diff --git a/system/database/drivers/sqlite/sqlite_utility.php b/system/database/drivers/sqlite/sqlite_utility.php index 754a755e4..e9f4eb64e 100644 --- a/system/database/drivers/sqlite/sqlite_utility.php +++ b/system/database/drivers/sqlite/sqlite_utility.php @@ -155,6 +155,37 @@ class CI_DB_sqlite_utility extends CI_DB_utility { return "SELECT * FROM ".$this->db->_escape_table($table)." LIMIT 1"; } + // -------------------------------------------------------------------- + + /** + * Optimize table query + * + * Generates a platform-specific query so that a table can be optimized + * + * @access private + * @param string the table name + * @return object + */ + function _optimize_table($table) + { + return FALSE; // Is this supported SQLite? + } + + // -------------------------------------------------------------------- + + /** + * Repair table query + * + * Generates a platform-specific query so that a table can be repaired + * + * @access private + * @param string the table name + * @return object + */ + function _repair_table($table) + { + return return FALSE; // Is this supported in SQLite? + } } diff --git a/user_guide/database/fields.html b/user_guide/database/fields.html index 7cf1f9c4e..ecb6fcb45 100644 --- a/user_guide/database/fields.html +++ b/user_guide/database/fields.html @@ -66,10 +66,13 @@ Field Names

Retrieving Field Names

-

Sometimes it's helpful to gather the field names.

+

Sometimes it's helpful to gather the field names for a particular table or with a paritcular query result.

$this->db->field_names();

-

Returns an array containing the field names. You must supply the table name to the function:

+

Returns an array containing the field names. This query can be called two ways:

+ + +

1. You can supply the table name and call it from the $this->db-> object:

$fields = $this->db->field_names('table_name');

@@ -80,6 +83,21 @@ foreach ($fields as $field)
}
+

2. You can gather the feild names associated with any query you run by calling the function +from your query result object:

+ + +$query = $this->db->query('SELECT * FROM some_table'); +

+ +foreach ($query->field_names() as $field)
+{
+   echo $field;
+} +
+ + +

Retrieving Field MetaData

-- cgit v1.2.3-24-g4f1b