This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
phptsmadmin/application/classes/database/tsm.php

288 lines
8.2 KiB
PHP
Raw Normal View History

2011-03-03 23:06:18 +00:00
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* TSM database connection.
*
* @package PTA
* @subpackage TSM
* @category Drivers
* @author Deon George
* @copyright (c) 2010 phpTSMadmin Development Team
* @license http://phptsmadmin.sf.net/license.html
*/
class Database_TSM extends Database {
// Database in use by each connection
protected static $_current_databases = array();
// Use SET NAMES to set the character set
protected static $_set_names;
// Identifier for this connection within the PHP driver
protected $_connection_id;
// TSM does not use a backtick for identifiers
protected $_identifier = '';
// Our TSM Message Format
private $msg_format = '[A-Z]{3}[0-9]{4}[I|E|W|S]';
// Our Message Codes stored in the last query
private $_query_msg_codes = array();
// Our return code error messages we can ignore
private $ignore_codes = array(
'ANS8001I', // Return code %s.
);
// Our return code error messages we can ignore
private $nodata_codes = array(
'ANR2034E', // SELECT: No match found using this criteria.
);
2011-05-23 10:01:27 +00:00
// Our required abstract methods
public function begin($mode = NULL) {}
public function commit() {}
public function rollback() {}
public function set_charset($charset) {}
2011-05-28 09:46:46 +00:00
/**
* Return the caching defined in the current configuration.
*
* $cache_time = $db->caching("table");
*
* @return string
*/
public function caching($table) {
return ($this->_config['caching'] AND isset($this->_config['cache'][$table])) ? $this->_config['cache'][$table] : FALSE;
}
2011-05-23 10:01:27 +00:00
public function connect() {
2011-03-03 23:06:18 +00:00
if ($this->_connection)
return;
// Extract the connection parameters, adding required variabels
extract($this->_config['connection'] + array(
'database' => '',
'hostname' => '',
'username' => '',
'password' => '',
'persistent' => FALSE,
));
// Get user login details from user session - these are set by login
if (! $username)
2011-05-23 10:01:27 +00:00
$username = Session::instance()->get_once(Kohana::config('auth.session_key'));
2011-03-03 23:06:18 +00:00
if (! $password)
$password = Session::instance()->get_once('password');
// Prevent this information from showing up in traces
unset($this->_config['connection']['username'], $this->_config['connection']['password']);
if (! file_exists(Kohana::config('config.client'))) {
system_message(array('title'=>'Cant find DSMADMC',
'body'=>sprintf('Unable to find the dsmadmc at <b>%s</b>',Kohana::config('client.config')),
'type'=>'error'));
return FALSE;
}
if (! $username OR ! $password)
2011-05-23 10:01:27 +00:00
Request::current()->redirect('/login?need_login=1');
2011-03-03 23:06:18 +00:00
2011-05-23 10:01:27 +00:00
try {
if ($persistent) {
2011-03-03 23:06:18 +00:00
// Create a persistent connection
throw new Kohana_Exception('Cant do persistant connections');
2011-05-23 10:01:27 +00:00
} else {
2011-03-03 23:06:18 +00:00
// Create a connection and force it to be a new link
2011-05-23 10:01:27 +00:00
$this->_connection = sprintf('%s %s -id=%s -password=%s -displ=list -editor=no -dataonly=YES %s %s ',
2011-03-03 23:06:18 +00:00
Kohana::config('config.client'),
2011-05-23 10:01:27 +00:00
$database ? '-server='.$database : '',
2011-03-03 23:06:18 +00:00
$username,
$password,
Kohana::config('config.client_errorlogname') ? sprintf('-errorlogname=%s',Kohana::config('config.client_errorlogname')) : '',
2011-05-23 10:01:27 +00:00
$database ? sprintf('-se=%s',$database) : ''
2011-03-03 23:06:18 +00:00
);
$result = $this->query(Database::SELECT,'SELECT server_name,platform,version,release,level,sublevel FROM status');
if ($result)
return TSM::instance()->set($username,$password,$result);
else
2011-05-23 10:01:27 +00:00
Request::current()->redirect(Request::detect_uri());
2011-03-03 23:06:18 +00:00
}
2011-05-23 10:01:27 +00:00
} catch (ErrorException $e) {
2011-03-03 23:06:18 +00:00
// No connection exists
$this->_connection = NULL;
throw new Database_Exception(':error', array(
':error' => sprintf('%s error in %s (%s)',$e->getMessage(),$e->getFile(),$e->getLine()),
),
$e->getCode());
}
// \xFF is a better delimiter, but the PHP driver uses underscore
$this->_connection_id = sha1($hostname.'_'.$username.'_'.$password);
2011-05-23 10:01:27 +00:00
if ( ! empty($this->_config['charset'])) {
2011-03-03 23:06:18 +00:00
// Set the character set
$this->set_charset($this->_config['charset']);
}
}
2011-04-08 05:34:04 +00:00
private function clear() {
$this->_query_msg_codes = array();
}
2011-05-23 10:01:27 +00:00
public function query($type, $sql, $as_object = FALSE, array $params = NULL) {
2011-03-03 23:06:18 +00:00
// Make sure the database is connected
$this->_connection or $this->connect();
2011-04-08 05:34:04 +00:00
$this->clear();
2011-03-03 23:06:18 +00:00
if ( ! empty($this->_config['profiling']))
// Benchmark this query for the current instance
$benchmark = Profiler::start("Database ({$this->_instance})", $sql);
2011-04-08 05:34:04 +00:00
// We need to escape any back slashes, since the exec will transpose them
// @todo Is there a better way of doing this?
$sql = str_replace('\\','\\\\',$sql);
2011-03-03 23:06:18 +00:00
// Execute the query
$stderr = exec($this->_connection.'"'.$sql.'"',$stdout,$rc);
// Work out our message codes
$result = array();
foreach ($stdout as $line)
if (! preg_match('/^('.$this->msg_format.')\s+/',$line,$matches))
array_push($result,$line);
elseif (! in_array($matches[1],$this->ignore_codes))
array_push($this->_query_msg_codes,$matches[1]);
// If we got a no data code
if (array_intersect($this->_query_msg_codes,$this->nodata_codes)) {
2011-04-08 05:34:04 +00:00
$result = array();
2011-03-03 23:06:18 +00:00
$rc = 0;
}
if ($stderr AND $rc) {
if (isset($benchmark))
{
// This benchmark is worthless
Profiler::delete($benchmark);
}
SystemMessage::TSM_Error($stdout,$sql);
2011-05-30 09:27:08 +00:00
Request::current()->redirect('welcome');
2011-03-03 23:06:18 +00:00
}
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');
2011-06-24 01:27:21 +00:00
elseif ($type === Database::SHOW)
return new Database_TSM_Show($result, $sql, $as_object, $params);
2011-03-03 23:06:18 +00:00
}
2011-05-23 10:01:27 +00:00
public function list_tables($like = NULL) {
2011-03-03 23:06:18 +00:00
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;
}
2011-05-23 10:01:27 +00:00
public function list_columns($table, $like = NULL, $add_prefix = TRUE) {
2011-03-03 23:06:18 +00:00
// 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();
2011-05-23 10:01:27 +00:00
foreach ($result as $row) {
2011-03-03 23:06:18 +00:00
list($type, $length) = $this->_parse_type($row['TYPENAME']);
$column = $this->datatype($type);
$column['column_name'] = $row['COLNAME'];
$column['data_type'] = $type;
$column['is_nullable'] = ($row['NULLS'] == 'TRUE');
$column['ordinal_position'] = ++$count;
2011-05-23 10:01:27 +00:00
switch (strtolower($column['data_type'])) {
2011-03-03 23:06:18 +00:00
case 'float':
if (isset($length))
list($column['numeric_precision'], $column['numeric_scale']) = explode(',', $length);
2011-05-23 10:01:27 +00:00
break;
2011-03-03 23:06:18 +00:00
case 'int':
if (isset($length))
// MySQL attribute
$column['display'] = $length;
2011-05-23 10:01:27 +00:00
break;
2011-03-03 23:06:18 +00:00
case 'string':
2011-05-23 10:01:27 +00:00
switch ($column['data_type']) {
2011-03-03 23:06:18 +00:00
case 'binary':
case 'varbinary':
$column['character_maximum_length'] = $length;
2011-05-23 10:01:27 +00:00
break;
2011-03-03 23:06:18 +00:00
case 'char':
case 'varchar':
$column['character_maximum_length'] = $length;
case 'text':
case 'tinytext':
case 'mediumtext':
case 'longtext':
$column['collation_name'] = $row['Collation'];
2011-05-23 10:01:27 +00:00
break;
2011-03-03 23:06:18 +00:00
case 'enum':
case 'set':
$column['collation_name'] = $row['Collation'];
$column['options'] = explode('\',\'', substr($length, 1, -1));
2011-05-23 10:01:27 +00:00
break;
2011-03-03 23:06:18 +00:00
}
2011-05-23 10:01:27 +00:00
break;
2011-03-03 23:06:18 +00:00
}
// TSM attributes
2011-05-28 09:46:46 +00:00
$column['comment'] = $row['REMARKS'];
2011-03-03 23:06:18 +00:00
$columns[$row['COLNAME']] = $column;
}
return $columns;
}
2011-05-23 10:01:27 +00:00
public function escape($value) {
2011-03-03 23:06:18 +00:00
// Make sure the database is connected
$this->_connection or $this->connect();
// SQL standard is to use single-quotes for all values
return "'$value'";
}
} // End Database_TSM