Skip to content

Instantly share code, notes, and snippets.

@xrado
Created June 9, 2010 07:13
Show Gist options
  • Save xrado/431158 to your computer and use it in GitHub Desktop.
Save xrado/431158 to your computer and use it in GitHub Desktop.
<?php
class Kohana_Database_MsSQL extends Database_PDO {
public function query($type, $sql, $as_object)
{
// Make sure the database is connected
$this->_connection or $this->connect();
if ( ! empty($this->_config['profiling']))
{
// Benchmark this query for the current instance
$benchmark = Profiler::start("Database ({$this->_instance})", $sql);
}
try
{
$result = $this->_connection->query($sql);
}
catch (Exception $e)
{
if (isset($benchmark))
{
// This benchmark is worthless
Profiler::delete($benchmark);
}
// Rethrow the exception
throw $e;
}
if (isset($benchmark))
{
Profiler::stop($benchmark);
}
// Set the last query
$this->last_query = $sql;
if ($type === Database::SELECT)
{
// Convert the result into an array, as PDOStatement::rowCount is not reliable
if ($as_object === FALSE)
{
$result->setFetchMode(PDO::FETCH_ASSOC);
}
elseif (is_string($as_object))
{
$result->setFetchMode(PDO::FETCH_CLASS, $as_object);
}
else
{
$result->setFetchMode(PDO::FETCH_CLASS, 'stdClass');
}
$result = $result->fetchAll();
// Return an iterator of results
return new Database_Result_Cached($result, $sql, $as_object);
}
elseif ($type === Database::INSERT)
{
// Return a list of insert id and rows created
return array(
$this->insert_id(),
$result->rowCount(),
);
}
else
{
// Return the number of rows affected
return $result->rowCount();
}
}
public function insert_id()
{
$data = $this->query(Database::SELECT,'SELECT @@IDENTITY as id',FALSE)->current();
return Arr::get($data,'id');
}
public function datatype($type)
{
static $types = array
(
'nvarchar' => array('type' => 'string'),
'ntext' => array('type' => 'string'),
);
if (isset($types[$type]))
return $types[$type];
return parent::datatype($type);
}
public function list_tables($like = NULL)
{
if (is_string($like))
{
// Search for table names
$result = $this->query(Database::SELECT, 'SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE '.$this->quote($like), false)->as_array();
}
else
{
// Find all table names
$result = $this->query(Database::SELECT, 'SELECT * FROM INFORMATION_SCHEMA.TABLES', false)->as_array();
}
$tables = array();
foreach ($result as $row)
{
// Get the table name from the results
$tables[] = $row['TABLE_NAME'];
}
return $tables;
}
//@todo make this work
public function list_columns($table, $like = NULL)
{
if (is_string($like))
{
$results = $this->query(Database::SELECT,'SELECT COLUMN_NAME AS Field, DATA_TYPE as Type, IS_NULLABLE as is_nullable FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME LIKE '.$this->quote($table), FALSE);
}
else
{
$results = $this->query(Database::SELECT,'SELECT COLUMN_NAME AS Field, DATA_TYPE as Type, IS_NULLABLE as is_nullable FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '.$this->quote($table), FALSE);
}
$result = array();
foreach ($results as $column)
{
// Make an associative array
$result[$column['Field']] = $this->datatype($column['Type']);
}
return $result;
}
public function set_charset($charset){}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment