Updated database module, changed Query::set() to Query::value(), added list_tables() and list_columns() to MySQL class

This commit is contained in:
Woody Gilk 2009-04-09 13:05:02 -05:00
parent 15b9ca1785
commit ab1d950deb
3 changed files with 56 additions and 14 deletions

View file

@ -19,7 +19,7 @@ abstract class Database_Core {
public static $instances = array();
public static function instance($name, $cached = TRUE)
public static function instance($name = 'default', $cached = TRUE)
{
if ( ! isset(Database::$instances[$name]))
{
@ -84,6 +84,10 @@ abstract class Database_Core {
abstract public function query($type, $sql);
abstract public function list_tables();
abstract public function list_columns($table);
abstract public function escape($value);
public function quote($value)
@ -104,16 +108,4 @@ abstract class Database_Core {
return '"'.$this->escape($value).'"';
}
public function list_tables()
{
throw new Database_Exception('The :method is not implemented in :class',
array(':method' => __FUNCTION__, ':class' => get_class($this)));
}
public function list_columns($table)
{
throw new Database_Exception('The :method is not implemented in :class',
array(':method' => __FUNCTION__, ':class' => get_class($this)));
}
} // End Database_Connection

View file

@ -11,6 +11,9 @@ class Database_MySQL_Core extends Database {
protected $_config_required = array('hostname', 'username', 'database');
// Database name
protected $_database;
public function connect()
{
if ($this->_connection)
@ -46,6 +49,9 @@ class Database_MySQL_Core extends Database {
mysql_errno($this->_connection));
}
// Store the database name for use by meta functions
$this->_database = $database;
if (isset($charset))
{
// Set the character set
@ -122,6 +128,50 @@ class Database_MySQL_Core extends Database {
}
}
public function list_tables($like = NULL)
{
// Make sure the database is connected
$this->_connection or $this->connect();
if (is_string($like))
{
// Search for table names
$result = $this->query(Database::SELECT, 'SHOW TABLES LIKE '.$this->quote($like))->as_array();
}
else
{
// Find all table names
$result = $this->query(Database::SELECT, 'SHOW TABLES')->as_array();
}
$tables = array();
foreach ($result as $row)
{
// Get the table name from the results
$tables[] = current($row);
}
return $tables;
}
public function list_columns($table)
{
// Make sure the database is connected
$this->_connection or $this->connect();
// Find all table names
$result = $this->query(Database::SELECT, 'SHOW COLUMNS FROM '.$table)->as_array();
$columns = array();
foreach ($result as $row)
{
// Get the column name from the results
$columns[] = $row['Field'];
}
return $columns;
}
public function escape($value)
{
// Make sure the database is connected

View file

@ -25,7 +25,7 @@ class Database_Query_Core {
return $this->_sql;
}
public function set($key, $value)
public function value($key, $value)
{
$this->_params[$key] = $value;