Updated to work with KH 3.1

This commit is contained in:
Deon George
2011-05-23 20:01:27 +10:00
parent 4765770a1a
commit a244d693b5
28 changed files with 1027 additions and 553 deletions

View File

@@ -11,7 +11,6 @@
* @license http://phptsmadmin.sf.net/license.html
*/
class Database_TSM extends Database {
// Database in use by each connection
protected static $_current_databases = array();
@@ -39,8 +38,13 @@ class Database_TSM extends Database {
'ANR2034E', // SELECT: No match found using this criteria.
);
public function connect()
{
// Our required abstract methods
public function begin($mode = NULL) {}
public function commit() {}
public function rollback() {}
public function set_charset($charset) {}
public function connect() {
if ($this->_connection)
return;
@@ -55,7 +59,7 @@ class Database_TSM extends Database {
// Get user login details from user session - these are set by login
if (! $username)
$username = Session::instance()->get_once('username');
$username = Session::instance()->get_once(Kohana::config('auth.session_key'));
if (! $password)
$password = Session::instance()->get_once('password');
@@ -71,25 +75,22 @@ class Database_TSM extends Database {
}
if (! $username OR ! $password)
Request::instance()->redirect('/login?need_login=1');
Request::current()->redirect('/login?need_login=1');
try
{
if ($persistent)
{
try {
if ($persistent) {
// Create a persistent connection
throw new Kohana_Exception('Cant do persistant connections');
}
else
{
} else {
// Create a connection and force it to be a new link
$this->_connection = sprintf('%s %s -id=%s -password=%s -displ=list -dataonly=YES %s %s',
$this->_connection = sprintf('%s %s -id=%s -password=%s -displ=list -editor=no -dataonly=YES %s %s ',
Kohana::config('config.client'),
Kohana::config('config.stanza') ? '-server='.Kohana::config('config.stanza') : '',
$database ? '-server='.$database : '',
$username,
$password,
Kohana::config('config.client_errorlogname') ? sprintf('-errorlogname=%s',Kohana::config('config.client_errorlogname')) : '',
Kohana::config('config.client_stanza') ? sprintf('-se=%s',Kohana::config('config.client_stanza')) : ''
$database ? sprintf('-se=%s',$database) : ''
);
$result = $this->query(Database::SELECT,'SELECT server_name,platform,version,release,level,sublevel FROM status');
@@ -97,11 +98,10 @@ class Database_TSM extends Database {
if ($result)
return TSM::instance()->set($username,$password,$result);
else
Request::instance()->redirect(Request::instance()->uri());
Request::current()->redirect(Request::detect_uri());
}
}
catch (ErrorException $e)
{
} catch (ErrorException $e) {
// No connection exists
$this->_connection = NULL;
@@ -114,69 +114,24 @@ class Database_TSM extends Database {
// \xFF is a better delimiter, but the PHP driver uses underscore
$this->_connection_id = sha1($hostname.'_'.$username.'_'.$password);
if ( ! empty($this->_config['charset']))
{
if ( ! empty($this->_config['charset'])) {
// Set the character set
$this->set_charset($this->_config['charset']);
}
}
public function disconnect()
{
try
{
// Database is assumed disconnected
$status = TRUE;
if (is_resource($this->_connection))
{
if ($status = mysql_close($this->_connection))
{
// Clear the connection
$this->_connection = NULL;
}
}
}
catch (Exception $e)
{
// Database is probably not disconnected
$status = ! is_resource($this->_connection);
}
return $status;
}
public function set_charset($charset)
{
// Make sure the database is connected
$this->_connection or $this->connect();
// Nothing to do for TSM
$status = TRUE;
if ($status === FALSE)
{
throw new Database_Exception(':error',
array(':error' => mysql_error($this->_connection)),
mysql_errno($this->_connection));
}
}
private function clear() {
$this->_query_msg_codes = array();
}
public function query($type, $sql, $as_object = FALSE, array $params = NULL)
{
public function query($type, $sql, $as_object = FALSE, array $params = NULL) {
// Make sure the database is connected
$this->_connection or $this->connect();
$this->clear();
if ( ! empty($this->_config['profiling']))
{
// Benchmark this query for the current instance
$benchmark = Profiler::start("Database ({$this->_instance})", $sql);
}
// We need to escape any back slashes, since the exec will transpose them
// @todo Is there a better way of doing this?
@@ -211,98 +166,75 @@ class Database_TSM extends Database {
}
if (isset($benchmark))
{
Profiler::stop($benchmark);
}
// Set the last query
$this->last_query = $sql;
if ($type === Database::SELECT)
{
// Return an iterator of results
return new Database_TSM_Result($result, $sql, $as_object, $params);
}
elseif ($type === Database::INSERT)
{
throw new Kohana_Exception('Database INSERTS are not supported');
}
}
public function list_tables($like = NULL)
{
public function list_tables($like = NULL) {
if (is_string($like))
{
// Search for table names
$result = $this->query(Database::SELECT, 'SHOW TABLES LIKE '.$this->quote($like), FALSE);
}
else
{
// Find all table names
$result = $this->query(Database::SELECT, 'SHOW TABLES', FALSE);
}
$tables = array();
foreach ($result as $row)
{
$tables[] = reset($row);
}
return $tables;
}
public function list_columns($table, $like = NULL, $add_prefix = TRUE)
{
public function list_columns($table, $like = NULL, $add_prefix = TRUE) {
// Quote the table name
$table = ($add_prefix === TRUE) ? $this->quote_table($table) : $table;
if (is_string($like))
{
// Search for column names
$result = $this->query(Database::SELECT, 'SHOW FULL COLUMNS FROM '.$table.' LIKE '.$this->quote($like), FALSE);
}
else
{
// Find all column names
$result = $this->query(Database::SELECT, sprintf('SELECT * FROM SYSCAT.COLUMNS WHERE TABNAME=\'%s\'',$table), FALSE);
}
$count = 0;
$columns = array();
foreach ($result as $row)
{
foreach ($result as $row) {
list($type, $length) = $this->_parse_type($row['TYPENAME']);
$column = $this->datatype($type);
$column['column_name'] = $row['COLNAME'];
// $column['column_default'] = $row['Default'];
$column['data_type'] = $type;
$column['is_nullable'] = ($row['NULLS'] == 'TRUE');
$column['ordinal_position'] = ++$count;
switch (strtolower($column['data_type']))
{
switch (strtolower($column['data_type'])) {
case 'float':
if (isset($length))
{
list($column['numeric_precision'], $column['numeric_scale']) = explode(',', $length);
}
break;
break;
case 'int':
if (isset($length))
{
// MySQL attribute
$column['display'] = $length;
}
break;
break;
case 'string':
switch ($column['data_type'])
{
switch ($column['data_type']) {
case 'binary':
case 'varbinary':
$column['character_maximum_length'] = $length;
break;
break;
case 'char':
case 'varchar':
$column['character_maximum_length'] = $length;
@@ -311,27 +243,27 @@ class Database_TSM extends Database {
case 'mediumtext':
case 'longtext':
$column['collation_name'] = $row['Collation'];
break;
break;
case 'enum':
case 'set':
$column['collation_name'] = $row['Collation'];
$column['options'] = explode('\',\'', substr($length, 1, -1));
break;
break;
}
break;
break;
}
// TSM attributes
$column['comment'] = $row['REMARKS'];
$columns[$row['COLNAME']] = $column;
}
return $columns;
}
public function escape($value)
{
public function escape($value) {
// Make sure the database is connected
$this->_connection or $this->connect();

View File

@@ -73,7 +73,7 @@ class Database_TSM_Result extends Database_Result {
// Return an object of given class name
$o = new $this->_as_object;
return $o->values($this->_rows[$this->_current_row]);
return $o->load_object($this->_rows[$this->_current_row]);
}
else
{