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.
lndb2/classes/Kohana/Database/DB2.php
2015-10-13 13:17:16 +11:00

377 lines
9.7 KiB
PHP

<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* DB2 database connection.
*
* @package Kohana/Database
* @category Drivers
* @author Deon George
* @copyright (c) 2010-2014 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Kohana_Database_DB2 extends Database {
// Database in use by each connection
protected static $_current_databases = array();
// Identifier for this connection within the PHP driver
protected $_connection_id;
// DB2 doesnt use identifies identifiers
protected $_identifier = '';
// Required Abstract Classes
public function begin($mode = NULL) {}
public function commit() {}
public function rollback() {}
public function list_tables($like = NULL) {}
public function connect() {
if ($this->_connection)
return;
// Extract the connection parameters, adding required variabels
extract($this->_config['connection'] + array(
'database' => '',
'hostname' => '',
'username' => '',
'port' => '',
'password' => '',
'persistent' => FALSE,
));
// Get user login details from user session - these are set by login
if (! $username)
$username = Session::instance()->get_once(Kohana::$config->load('auth')->session_key);
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 (! $username OR ! $password)
HTTP::redirect('login');
try {
if ($persistent) {
// Create a persistent connection
throw new Kohana_Exception('Cant do persistant connections');
} else {
// Create a connection and force it to be a new link
$constring = sprintf('DATABASE=%s;HOSTNAME=%s;PORT=%s;PROTOCOL=%s;UID=%s;PWD=%s',
$database,$hostname,$port,'TCPIP',$username,$password);
$this->_connection = db2_connect($constring,NULL,NULL);
}
} catch (ErrorException $e) {
// 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());
}
if (! $this->_connection)
throw new Database_Exception('Connection failed (:error)',array(':error'=>db2_conn_errormsg()),db2_conn_error());
// \xFF is a better delimiter, but the PHP driver uses underscore
$this->_connection_id = sha1($hostname.'_'.$username.'_'.$password);
if ( ! empty($this->_config['charset'])) {
// Set the character set
$this->set_charset($this->_config['charset']);
}
}
// @todo
public function disconnect() {}
public function escape($value)
{
// SQL standard is to use single-quotes for all values
return "'$value'";
}
public function set_charset($charset) {}
public function query($type, $sql, $as_object = FALSE, array $params = NULL)
{
// Make sure the database is connected
$this->_connection OR $this->connect();
if (Kohana::$profiling)
{
// Benchmark this query for the current instance
$benchmark = Profiler::start("Database ({$this->_instance})", $sql);
}
if ( ! empty($this->_config['connection']['persistent']) AND $this->_config['connection']['database'] !== Database_DB2::$_current_databases[$this->_connection_id])
{
// Select database on persistent connections
$this->_select_db($this->_config['connection']['database']);
}
// Execute the query
try
{
$result = db2_exec($this->_connection,$sql,array('cursor'=>DB2_SCROLLABLE));
}
catch (Exception $e)
{
if (isset($benchmark))
{
// This benchmark is worthless
Profiler::delete($benchmark);
}
throw new Database_Exception(':error [ :query ]',array(':error' => db2_stmt_errormsg(), ':query' => $sql),db2_stmt_error());
}
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_DB2_Result($result, $sql, $as_object, $params);
}
elseif ($type === Database::INSERT)
{
throw new Kohana_Exception('Not Implemented (:type).',array(':type'=>$type));
}
else
{
throw new Kohana_Exception('Not Implemented (:type).',array(':type'=>$type));
}
}
public function list_columns($table, $like = NULL, $add_prefix = FALSE) {
// If caching is on, cache the tables
if ($this->_config['caching']) {
$cache_key = __METHOD__.':'.$table.':'.$like.':'.$add_prefix;
$columns = Kohana::cache($cache_key);
if ($columns) {
return $columns;
}
}
// Make sure the database is connected
$this->_connection or $this->connect();
$table = strtoupper($table);
// Quote the table name
$table = ($add_prefix === TRUE) ? $this->quote_table($table) : $table;
// Find all column names
try {
$result = db2_columns($this->_connection,NULL,$this->_config['connection']['schema'] ? $this->_config['connection']['schema'] : '%',$table,$like);
} catch (Exception $e) {
throw new Database_Exception('Unable to obtain Columns for :table [:error] (:like)',array(':like'=>$like,':table'=>$table,':error' => db2_stmt_errormsg()),db2_stmt_error());
}
$count = 0;
$columns = array();
while ($row = db2_fetch_assoc($result)) {
list($type, $length) = $this->_parse_type($row['TYPE_NAME']);
$column = $this->datatype($type);
$column['column_name'] = $row['COLUMN_NAME'];
$column['data_type'] = $type;
$column['is_nullable'] = ($row['IS_NULLABLE'] == 'YES');
$column['ordinal_position'] = ++$count;
switch (strtolower($column['data_type'])) {
case 'float':
if (isset($length))
list($column['numeric_precision'], $column['numeric_scale']) = explode(',', $length);
break;
case 'int':
if (isset($length))
// MySQL attribute
$column['display'] = $length;
break;
case 'string':
switch ($column['data_type']) {
case 'binary':
case 'varbinary':
$column['character_maximum_length'] = $length;
break;
case 'char':
case 'varchar':
$column['character_maximum_length'] = $length;
case 'text':
case 'tinytext':
case 'mediumtext':
case 'longtext':
$column['collation_name'] = $row['Collation'];
break;
case 'enum':
case 'set':
$column['collation_name'] = $row['Collation'];
$column['options'] = explode('\',\'', substr($length, 1, -1));
break;
}
break;
}
$columns[$row['COLUMN_NAME']] = $column;
}
if (! $columns)
throw new Database_Exception('Unable to obtain Columns for :table [:error] (:like)',
array(':like'=>$like,':table'=>$table,':error' => db2_conn_errormsg()),db2_conn_error());
// Save our cache data for next call
if ($this->_config['caching']) {
Kohana::cache($cache_key,$columns);
}
return $columns;
}
public function quote_column($column)
{
// Identifiers are escaped by repeating them
$escaped_identifier = $this->_identifier.$this->_identifier;
if (is_array($column)) {
list($column, $alias) = $column;
$alias = str_replace($this->_identifier, $escaped_identifier, $alias);
}
if ($column instanceof Database_Query) {
// Create a sub-query
$column = '('.$column->compile($this).')';
} elseif ($column instanceof Database_Expression) {
// Compile the expression
$column = $column->compile($this);
} else {
// Convert to a string
$column = (string) $column;
$column = str_replace($this->_identifier, $escaped_identifier, $column);
if ($column === '*') {
return $column;
} elseif (strpos($column, '.') !== FALSE) {
$parts = explode('.', $column);
if ($prefix = $this->table_prefix()) {
// Get the offset of the table name, 2nd-to-last part
$offset = count($parts) - 2;
// Add the table prefix to the table name
$parts[$offset] = $prefix.$parts[$offset];
}
foreach ($parts as & $part) {
if ($part !== '*') {
// Quote each of the parts
$part = $this->_identifier.$part.$this->_identifier;
}
}
$column = implode('.', $parts);
} else {
$column = $this->_identifier.$column.$this->_identifier;
}
}
if (isset($alias))
$column .= ' AS '.$this->_identifier.$alias.$this->_identifier;
return $column;
}
public function quote_table($table)
{
// Identifiers are escaped by repeating them
$escaped_identifier = $this->_identifier.$this->_identifier;
if (is_array($table))
{
list($table, $alias) = $table;
$alias = str_replace($this->_identifier, $escaped_identifier, $alias);
}
if ($table instanceof Database_Query)
{
// Create a sub-query
$table = '('.$table->compile($this).')';
}
elseif ($table instanceof Database_Expression)
{
// Compile the expression
$table = $table->compile($this);
}
else
{
// Convert to a string
$table = (string) $table;
$table = str_replace($this->_identifier, $escaped_identifier, $table);
if (strpos($table, '.') !== FALSE)
{
$parts = explode('.', $table);
if ($prefix = $this->table_prefix())
{
// Get the offset of the table name, last part
$offset = count($parts) - 1;
// Add the table prefix to the table name
$parts[$offset] = $prefix.$parts[$offset];
}
foreach ($parts as & $part)
{
// Quote each of the parts
$part = $this->_identifier.$part.$this->_identifier;
}
$table = implode('.', $parts);
}
else
{
// Add the table prefix
$table = $this->table_prefix(TRUE).$table;
}
}
if (isset($alias))
{
// Attach table prefix to alias
$table .= ' AS '.$this->table_prefix().$alias;
}
return $table;
}
public function table_prefix($schema=FALSE)
{
return ($schema === TRUE ? $this->_config['connection']['schema'].'.' : '').$this->_config['table_prefix'];
}
}
?>