diff options
20 files changed, 211 insertions, 146 deletions
diff --git a/system/codeigniter/Common.php b/system/codeigniter/Common.php index 124ad9a38..d2342de42 100644 --- a/system/codeigniter/Common.php +++ b/system/codeigniter/Common.php @@ -72,7 +72,7 @@ function &_load_class($class, $instantiate = TRUE) // identically named file in the application/libraries folder. // We need to determine, however, if the class being requested is // a sub-class of an existing library or an independent instance - // since each needs to be handled slightly differently. + // since each needs to be handled slightly different. // To do this we'll open the requested class and read the top portion // of it. If the class extends a base class we will load the base first. // If it doesn't extend the base we'll only load the requested class. @@ -84,12 +84,12 @@ function &_load_class($class, $instantiate = TRUE) // replaced on-the-fly with nothing required for the user to do // except write the declaration. Fortunately PHP is ridiculously fast // at file reading operations so I'm not able to discern a performance - // hit based on my benchmarks, assuming a reasonable number of core + // hit based on my benchmarks, assuming only a small number of core // files are being extended, which will usually be the case. $fp = fopen(APPPATH.'libraries/'.$class.EXT, "rb"); - if (preg_match("/MY_".$class."\s+extends\s+CI_".$class."/", fread($fp, '6000'))) + if (preg_match("/MY_".$class."\s+extends\s+CI_".$class."/i", fread($fp, '6000'))) { require(BASEPATH.'libraries/'.$class.EXT); require(APPPATH.'libraries/'.$class.EXT); diff --git a/system/database/DB_driver.php b/system/database/DB_driver.php index 5102cc74c..81af466ef 100644 --- a/system/database/DB_driver.php +++ b/system/database/DB_driver.php @@ -555,37 +555,6 @@ class CI_DB_driver { // -------------------------------------------------------------------- - // -------------------------------------------------------------------- - - /** - * List databases - * - * @access public - * @return bool - */ - function list_databases() - { - // Is there a cached result? - if (isset($this->cache['db_names'])) - { - return $this->cache['db_names']; - } - - $query = $this->query($this->_list_database()); - $dbs = array(); - if ($query->num_rows() > 0) - { - foreach ($query->result_array() as $row) - { - $dbs[] = current($row); - } - } - - return $this->cache['db_names'] =& $dbs; - } - - // -------------------------------------------------------------------- - /** * Returns an array of table names * @@ -694,6 +663,20 @@ class CI_DB_driver { return $this->cache['field_names'][$table] =& $retval; } + + // -------------------------------------------------------------------- + + /** + * Determine if a particular field exists + * @access public + * @param string + * @param string + * @return boolean + */ + function field_exists($field_name, $table_name) + { + return ( ! in_array($field_name, $this->list_fields($table_name))) ? FALSE : TRUE; + } // -------------------------------------------------------------------- diff --git a/system/database/DB_utility.php b/system/database/DB_utility.php index dc56d6524..e568bce02 100644 --- a/system/database/DB_utility.php +++ b/system/database/DB_utility.php @@ -87,6 +87,35 @@ class CI_DB_utility { // -------------------------------------------------------------------- /** + * List databases + * + * @access public + * @return bool + */ + function list_databases() + { + // Is there a cached result? + if (isset($this->cache['db_names'])) + { + return $this->cache['db_names']; + } + + $query = $this->db->query($this->_list_database()); + $dbs = array(); + if ($query->num_rows() > 0) + { + foreach ($query->result_array() as $row) + { + $dbs[] = current($row); + } + } + + return $this->cache['db_names'] =& $dbs; + } + + // -------------------------------------------------------------------- + + /** * Optimize Table * * @access public diff --git a/system/database/drivers/mssql/mssql_driver.php b/system/database/drivers/mssql/mssql_driver.php index 8b82ee314..cb2f48dc3 100644 --- a/system/database/drivers/mssql/mssql_driver.php +++ b/system/database/drivers/mssql/mssql_driver.php @@ -258,19 +258,6 @@ class CI_DB_mssql_driver extends CI_DB { $row = $query->row(); return $row->numrows; } - - // -------------------------------------------------------------------- - - /** - * List databases - * - * @access private - * @return bool - */ - function _list_databases() - { - return "EXEC sp_helpdb"; // Can also be: EXEC sp_databases - } // -------------------------------------------------------------------- diff --git a/system/database/drivers/mssql/mssql_utility.php b/system/database/drivers/mssql/mssql_utility.php index ad13167cd..388dbe1c2 100644 --- a/system/database/drivers/mssql/mssql_utility.php +++ b/system/database/drivers/mssql/mssql_utility.php @@ -67,6 +67,19 @@ class CI_DB_mssql_utility extends CI_DB_utility { // -------------------------------------------------------------------- /** + * List databases + * + * @access private + * @return bool + */ + function _list_databases() + { + return "EXEC sp_helpdb"; // Can also be: EXEC sp_databases + } + + // -------------------------------------------------------------------- + + /** * Optimize table query * * Generates a platform-specific query so that a table can be optimized diff --git a/system/database/drivers/mysql/mysql_driver.php b/system/database/drivers/mysql/mysql_driver.php index ecab648d4..253627cd9 100644 --- a/system/database/drivers/mysql/mysql_driver.php +++ b/system/database/drivers/mysql/mysql_driver.php @@ -296,19 +296,6 @@ class CI_DB_mysql_driver extends CI_DB { // -------------------------------------------------------------------- /** - * List databases - * - * @access private - * @return bool - */ - function _list_databases() - { - return "SHOW DATABASES"; - } - - // -------------------------------------------------------------------- - - /** * List table query * * Generates a platform-specific query string so that the table names can be fetched diff --git a/system/database/drivers/mysql/mysql_utility.php b/system/database/drivers/mysql/mysql_utility.php index a81c915f6..b387ace9e 100644 --- a/system/database/drivers/mysql/mysql_utility.php +++ b/system/database/drivers/mysql/mysql_utility.php @@ -53,6 +53,19 @@ class CI_DB_mysql_utility extends CI_DB_utility { // -------------------------------------------------------------------- /** + * List databases + * + * @access private + * @return bool + */ + function _list_databases() + { + return "SHOW DATABASES"; + } + + // -------------------------------------------------------------------- + + /** * Drop Table * * @access private @@ -101,7 +114,7 @@ class CI_DB_mysql_utility extends CI_DB_utility { * MySQL Export * * @access private - * @param array Any preferences + * @param array Preferences * @return mixed */ function _backup($params = array()) @@ -217,7 +230,6 @@ class CI_DB_mysql_utility extends CI_DB_utility { // Build the INSERT string $output .= 'INSERT INTO '.$table.' ('.$field_str.') VALUES ('.$val_str.');'.$newline; - } $output .= $newline.$newline; diff --git a/system/database/drivers/mysqli/mysqli_driver.php b/system/database/drivers/mysqli/mysqli_driver.php index a2c380355..4fd9f3aaa 100644 --- a/system/database/drivers/mysqli/mysqli_driver.php +++ b/system/database/drivers/mysqli/mysqli_driver.php @@ -288,19 +288,6 @@ class CI_DB_mysqli_driver extends CI_DB { // -------------------------------------------------------------------- /** - * List databases - * - * @access private - * @return bool - */ - function _list_databases() - { - return "SHOW DATABASES"; - } - - // -------------------------------------------------------------------- - - /** * List table query * * Generates a platform-specific query string so that the table names can be fetched diff --git a/system/database/drivers/mysqli/mysqli_utility.php b/system/database/drivers/mysqli/mysqli_utility.php index f5b98b11f..a40441d20 100644 --- a/system/database/drivers/mysqli/mysqli_utility.php +++ b/system/database/drivers/mysqli/mysqli_utility.php @@ -62,6 +62,19 @@ class CI_DB_mysqli_utility extends CI_DB_utility { { return "DROP TABLE IF EXISTS ".$this->db->_escape_table($name); } + + // -------------------------------------------------------------------- + + /** + * List databases + * + * @access private + * @return bool + */ + function _list_databases() + { + return "SHOW DATABASES"; + } // -------------------------------------------------------------------- diff --git a/system/database/drivers/oci8/oci8_driver.php b/system/database/drivers/oci8/oci8_driver.php index b43b4c41f..e83c640bd 100644 --- a/system/database/drivers/oci8/oci8_driver.php +++ b/system/database/drivers/oci8/oci8_driver.php @@ -399,19 +399,6 @@ class CI_DB_oci8_driver extends CI_DB { return $row->NUMROWS; } - // -------------------------------------------------------------------- - - /** - * List databases - * - * @access private - * @return bool - */ - function _list_databases() - { - return FALSE; - } - // -------------------------------------------------------------------- /** diff --git a/system/database/drivers/oci8/oci8_utility.php b/system/database/drivers/oci8/oci8_utility.php index b6503de63..b1d539ad6 100644 --- a/system/database/drivers/oci8/oci8_utility.php +++ b/system/database/drivers/oci8/oci8_utility.php @@ -54,6 +54,19 @@ class CI_DB_oci8_utility extends CI_DB_utility { // -------------------------------------------------------------------- /** + * List databases + * + * @access private + * @return bool + */ + function _list_databases() + { + return FALSE; + } + + // -------------------------------------------------------------------- + + /** * Drop Table * * @access private diff --git a/system/database/drivers/odbc/odbc_driver.php b/system/database/drivers/odbc/odbc_driver.php index 0f002cc1b..4bd6e1106 100644 --- a/system/database/drivers/odbc/odbc_driver.php +++ b/system/database/drivers/odbc/odbc_driver.php @@ -263,24 +263,6 @@ class CI_DB_odbc_driver extends CI_DB { // -------------------------------------------------------------------- /** - * List databases - * - * @access private - * @return bool - */ - function _list_databases() - { - // Not sure if ODBC lets you list all databases... - if ($this->db_debug) - { - return $this->display_error('db_unsuported_feature'); - } - return FALSE; - } - - // -------------------------------------------------------------------- - - /** * Show table query * * Generates a platform-specific query string so that the table names can be fetched diff --git a/system/database/drivers/odbc/odbc_utility.php b/system/database/drivers/odbc/odbc_utility.php index 6d0fb79f1..5a0e365ef 100644 --- a/system/database/drivers/odbc/odbc_utility.php +++ b/system/database/drivers/odbc/odbc_utility.php @@ -66,6 +66,24 @@ class CI_DB_odbc_utility extends CI_DB_utility { // -------------------------------------------------------------------- /** + * List databases + * + * @access private + * @return bool + */ + function _list_databases() + { + // Not sure if ODBC lets you list all databases... + if ($this->db->db_debug) + { + return $this->db->display_error('db_unsuported_feature'); + } + return FALSE; + } + + // -------------------------------------------------------------------- + + /** * Drop Table * * @access private diff --git a/system/database/drivers/postgre/postgre_driver.php b/system/database/drivers/postgre/postgre_driver.php index f14395638..340d65046 100644 --- a/system/database/drivers/postgre/postgre_driver.php +++ b/system/database/drivers/postgre/postgre_driver.php @@ -290,19 +290,6 @@ class CI_DB_postgre_driver extends CI_DB { // -------------------------------------------------------------------- /** - * List databases - * - * @access private - * @return bool - */ - function _list_databases() - { - return "SELECT datname FROM pg_database"; - } - - // -------------------------------------------------------------------- - - /** * Show table query * * Generates a platform-specific query string so that the table names can be fetched diff --git a/system/database/drivers/postgre/postgre_utility.php b/system/database/drivers/postgre/postgre_utility.php index 46c98cc7d..038aa26cd 100644 --- a/system/database/drivers/postgre/postgre_utility.php +++ b/system/database/drivers/postgre/postgre_utility.php @@ -54,6 +54,19 @@ class CI_DB_postgre_utility extends CI_DB_utility { // -------------------------------------------------------------------- /** + * List databases + * + * @access private + * @return bool + */ + function _list_databases() + { + return "SELECT datname FROM pg_database"; + } + + // -------------------------------------------------------------------- + + /** * Drop Table * * @access private diff --git a/system/database/drivers/sqlite/sqlite_driver.php b/system/database/drivers/sqlite/sqlite_driver.php index f57c4b8c8..9da50b676 100644 --- a/system/database/drivers/sqlite/sqlite_driver.php +++ b/system/database/drivers/sqlite/sqlite_driver.php @@ -282,28 +282,6 @@ class CI_DB_sqlite_driver extends CI_DB { // -------------------------------------------------------------------- /** - * List databases - * - * I don't believe you can do a database listing with SQLite - * since each database is its own file. I suppose we could - * try reading a directory looking for SQLite files, but - * that doesn't seem like a terribly good idea - * - * @access private - * @return bool - */ - function _list_databases() - { - if ($this->db_debug) - { - return $this->display_error('db_unsuported_feature'); - } - return array(); - } - - // -------------------------------------------------------------------- - - /** * List table query * * Generates a platform-specific query string so that the table names can be fetched diff --git a/system/database/drivers/sqlite/sqlite_utility.php b/system/database/drivers/sqlite/sqlite_utility.php index 801660d94..19b06bd6d 100644 --- a/system/database/drivers/sqlite/sqlite_utility.php +++ b/system/database/drivers/sqlite/sqlite_utility.php @@ -64,6 +64,28 @@ class CI_DB_sqlite_utility extends CI_DB_utility { // -------------------------------------------------------------------- /** + * List databases + * + * I don't believe you can do a database listing with SQLite + * since each database is its own file. I suppose we could + * try reading a directory looking for SQLite files, but + * that doesn't seem like a terribly good idea + * + * @access private + * @return bool + */ + function _list_databases() + { + if ($this->db_debug) + { + return $this->display_error('db_unsuported_feature'); + } + return array(); + } + + // -------------------------------------------------------------------- + + /** * Drop Table * * Unsupported feature in SQLite diff --git a/user_guide/database/examples.html b/user_guide/database/examples.html index c58e9bb64..08051cadd 100644 --- a/user_guide/database/examples.html +++ b/user_guide/database/examples.html @@ -112,6 +112,28 @@ foreach ($query->result_array() as $row)<br /> <p>The above <dfn>result_array()</dfn> function returns an array of standard array indexes. Example: $row['title']</p>
+<h2>Testing for Results</h2>
+
+<p>If you run queries that might <strong>not</strong> produce a result, you are encouraged to test for a result first
+using the <dfn>num_rows()</dfn> function:</p>
+
+<code>
+$query = $this->db->query("YOUR QUERY");<br />
+<br />
+if ($query->num_rows() > 0)<br />
+{<br />
+ foreach ($query->result() as $row)<br />
+ {<br />
+ echo $row->title;<br />
+ echo $row->name;<br />
+ echo $row->body;<br />
+ }<br />
+}
+</code>
+
+
+
+
<h2>Standard Query With Single Result</h2>
<code>$query = $this->db->query('SELECT name FROM my_table LIMIT 1');<br />
@@ -120,6 +142,19 @@ $row = $query->row();<br /> echo $row->name;<br />
</code>
+<p>The above <dfn>row()</dfn> function returns an <strong>object</strong>. Example: $row->name</p>
+
+
+<h2>Standard Query With Single Result (Array version)</h2>
+
+<code>$query = $this->db->query('SELECT name FROM my_table LIMIT 1');<br />
+<br />
+$row = $query->row_array();<br />
+echo $row->['name'];<br />
+</code>
+
+<p>The above <dfn>row_array()</dfn> function returns an <strong>array</strong>. Example: $row->['name']</p>
+
<h2>Standard Insert</h2>
@@ -146,7 +181,11 @@ foreach ($query->result() as $row)<br /> {<br />
echo $row->title;<br />
}</code>
-
+
+<p>The above <dfn>get()</dfn> function retrieves all the results from the supplied table.
+The <a href="active_record.html">Active Record</a> class contains a full compliment of functions
+for working with data.</p>
+
<h2>Active Record Insert</h2>
diff --git a/user_guide/database/fields.html b/user_guide/database/fields.html index 260cd222f..83abe6327 100644 --- a/user_guide/database/fields.html +++ b/user_guide/database/fields.html @@ -94,6 +94,23 @@ foreach ($query->list_fields() as $field)<br /> </code>
+<h2>$this->db->field_exists();</h2>
+
+<p>Sometimes it's helpful to know whether a particular field exists befor performing an action.
+Returns a boolean TRUE/FALSE. Usage example:</p>
+
+<code>
+if ($this->db->field_exists('field_name', 'table_name'))<br />
+{<br />
+ // some code...<br />
+}
+</code>
+
+<p>Note: Replace <em>field_name</em> with the name of the column you are looking for, and replace
+<em>table_name</em> with the name of the table you are looking for.</p>
+
+
+
<h2>$this->db->field_data();</h2>
<p>Returns an array of objects containing field information.</p>
diff --git a/user_guide/database/results.html b/user_guide/database/results.html index 81abc50f8..75f4357b4 100644 --- a/user_guide/database/results.html +++ b/user_guide/database/results.html @@ -84,6 +84,8 @@ Query Results echo $row->name;<br />
echo $row->body;<br />
}</code>
+
+ <p>The above <dfn>function</dfn> is an alias of <dfn>result_object()</dfn>.</p>
<p>If you run queries that might <strong>not</strong> produce a result, you are encouraged to test the result first:</p>
@@ -113,10 +115,6 @@ Query Results echo $row['name'];<br />
echo $row['body'];<br />
}</code>
-
- <h2>result('array')</h2>
-
- <p>Identical to <dfn>$this->db->result_array()</dfn>.</p>
<h2>row()</h2>
|