1
0
mirror of https://github.com/dg/dibi.git synced 2025-02-25 03:12:50 +01:00
php-dibi/dibi/drivers/odbc.php

309 lines
7.9 KiB
PHP
Raw Normal View History

2006-06-04 23:06:33 +00:00
<?php
/**
2007-04-11 18:30:30 +00:00
* This file is part of the "dibi" project (http://dibi.texy.info/)
2006-06-04 23:06:33 +00:00
*
2007-06-24 23:49:57 +00:00
* @author David Grudl
* @copyright Copyright (c) 2005-2007 David Grudl aka -dgx- (http://www.dgx.cz)
* @license New BSD License
* @version $Revision$ $Date$
* @category Database
* @package Dibi
2006-06-04 23:06:33 +00:00
*/
/**
* The dibi driver interacting with databases via ODBC connections
*
*/
class DibiOdbcDriver extends DibiDriver
{
public $formats = array(
'TRUE' => "-1",
'FALSE' => "0",
'date' => "#m/d/Y#",
'datetime' => "#m/d/Y H:i:s#",
);
/**
* Affected rows
* @var mixed
*/
private $affectedRows = FALSE;
2006-06-04 23:06:33 +00:00
/**
* @param array connect configuration
2007-06-24 23:49:57 +00:00
* @throws DibiException
*/
public function __construct($config)
2006-06-04 23:06:33 +00:00
{
// default values
if (empty($config['username'])) $config['username'] = ini_get('odbc.default_user');
if (empty($config['password'])) $config['password'] = ini_get('odbc.default_pw');
if (empty($config['database'])) $config['database'] = ini_get('odbc.default_db');
if (empty($config['username'])) {
throw new DibiException("Username must be specified (driver odbc)");
}
2006-08-25 18:10:30 +00:00
if (empty($config['password'])) {
throw new DibiException("Password must be specified (driver odbc)");
}
2006-08-25 18:10:30 +00:00
if (empty($config['database'])) {
throw new DibiException("Database must be specified (driver odbc)");
}
parent::__construct($config);
}
protected function connect()
{
if (!extension_loaded('odbc')) {
throw new DibiException("PHP extension 'odbc' is not loaded");
}
$config = $this->getConfig();
if (empty($config['persistent'])) {
2007-06-25 17:02:12 +00:00
$connection = @odbc_connect($config['database'], $config['username'], $config['password']);
} else {
2007-06-25 17:02:12 +00:00
$connection = @odbc_pconnect($config['database'], $config['username'], $config['password']);
}
2006-06-04 23:06:33 +00:00
if (!is_resource($connection)) {
throw new DibiDatabaseException(odbc_errormsg(), odbc_error());
}
2006-06-04 23:06:33 +00:00
dibi::notify('connected', $this);
2007-06-25 17:02:12 +00:00
return $connection;
2006-06-04 23:06:33 +00:00
}
public function nativeQuery($sql)
2006-06-04 23:06:33 +00:00
{
2006-06-04 23:09:53 +00:00
$this->affectedRows = FALSE;
$res = parent::nativeQuery($sql);
if ($res instanceof DibiResult) {
$this->affectedRows = odbc_num_rows($res->getResource());
if ($this->affectedRows < 0) $this->affectedRows = FALSE;
}
return $res;
}
protected function doQuery($sql)
{
$connection = $this->getConnection();
$res = @odbc_exec($connection, $sql);
2006-06-04 23:09:53 +00:00
if ($res === FALSE) {
throw new DibiDatabaseException(odbc_errormsg($connection), odbc_error($connection), $sql);
} elseif (is_resource($res)) {
2007-03-26 06:22:53 +00:00
return new DibiOdbcResult($res);
}
2006-06-04 23:06:33 +00:00
}
2007-08-23 00:57:28 +00:00
2006-06-04 23:06:33 +00:00
public function affectedRows()
{
2006-06-04 23:09:53 +00:00
return $this->affectedRows;
2006-06-04 23:06:33 +00:00
}
2007-08-23 00:57:28 +00:00
2006-06-04 23:06:33 +00:00
public function insertId()
{
throw new DibiException(__METHOD__ . ' is not implemented');
2006-06-04 23:06:33 +00:00
}
2007-08-23 00:57:28 +00:00
2006-06-04 23:06:33 +00:00
public function begin()
{
$connection = $this->getConnection();
if (!odbc_autocommit($connection, FALSE)) {
throw new DibiDatabaseException(odbc_errormsg($connection), odbc_error($connection));
}
dibi::notify('begin', $this);
2006-06-04 23:06:33 +00:00
}
2007-08-23 00:57:28 +00:00
2006-06-04 23:06:33 +00:00
public function commit()
{
2007-06-25 17:02:12 +00:00
$connection = $this->getConnection();
if (!odbc_commit($connection)) {
throw new DibiDatabaseException(odbc_errormsg($connection), odbc_error($connection));
}
2007-06-25 17:02:12 +00:00
odbc_autocommit($connection, TRUE);
dibi::notify('commit', $this);
2006-06-04 23:06:33 +00:00
}
2007-08-23 00:57:28 +00:00
2006-06-04 23:06:33 +00:00
public function rollback()
{
2007-06-25 17:02:12 +00:00
$connection = $this->getConnection();
if (!odbc_rollback($connection)) {
throw new DibiDatabaseException(odbc_errormsg($connection), odbc_error($connection));
}
2007-06-25 17:02:12 +00:00
odbc_autocommit($connection, TRUE);
dibi::notify('rollback', $this);
2006-06-04 23:06:33 +00:00
}
2007-08-23 00:57:28 +00:00
public function errorInfo()
2006-06-04 23:06:33 +00:00
{
2007-06-25 17:02:12 +00:00
$connection = $this->getConnection();
2006-06-04 23:06:33 +00:00
return array(
2007-06-25 17:02:12 +00:00
'message' => odbc_errormsg($connection),
'code' => odbc_error($connection),
2006-06-04 23:06:33 +00:00
);
}
public function escape($value, $appendQuotes = TRUE)
2006-06-04 23:06:33 +00:00
{
$value = str_replace("'", "''", $value);
return $appendQuotes
? "'" . $value . "'"
: $value;
}
2007-08-23 00:57:28 +00:00
public function delimite($value)
2006-06-04 23:06:33 +00:00
{
return '[' . str_replace('.', '].[', $value) . ']';
2006-06-04 23:06:33 +00:00
}
2007-08-23 00:57:28 +00:00
2006-06-04 23:06:33 +00:00
public function getMetaData()
{
throw new DibiException(__METHOD__ . ' is not implemented');
2006-06-04 23:06:33 +00:00
}
2006-09-23 07:55:11 +00:00
2007-08-23 00:57:28 +00:00
2006-09-23 07:55:11 +00:00
/**
* @see DibiDriver::applyLimit()
*/
public function applyLimit(&$sql, $limit, $offset = 0)
{
// offset suppot is missing...
if ($limit >= 0) {
2006-09-23 07:55:11 +00:00
$sql = 'SELECT TOP ' . (int) $limit . ' * FROM (' . $sql . ')';
}
2007-03-26 06:22:53 +00:00
if ($offset) throw new DibiException('Offset is not implemented in driver odbc');
2006-09-23 07:55:11 +00:00
}
2006-06-04 23:06:33 +00:00
} // class DibiOdbcDriver
class DibiOdbcResult extends DibiResult
{
private $row = 0;
2006-06-04 23:06:33 +00:00
2007-08-23 00:57:28 +00:00
2006-06-04 23:06:33 +00:00
public function rowCount()
{
// will return -1 with many drivers :-(
return odbc_num_rows($this->resource);
}
2007-08-23 00:57:28 +00:00
2006-06-04 23:06:33 +00:00
protected function doFetch()
{
return odbc_fetch_array($this->resource, $this->row++);
}
2007-08-23 00:57:28 +00:00
2006-06-04 23:06:33 +00:00
public function seek($row)
{
$this->row = $row;
}
2007-08-23 00:57:28 +00:00
2006-06-04 23:06:33 +00:00
protected function free()
{
odbc_free_result($this->resource);
}
2007-08-23 00:57:28 +00:00
2006-06-04 23:06:33 +00:00
/** this is experimental */
protected function buildMeta()
2006-06-04 23:06:33 +00:00
{
// cache
if ($this->meta !== NULL) {
2006-06-04 23:06:33 +00:00
return $this->meta;
}
2006-06-04 23:06:33 +00:00
static $types = array(
2006-06-08 01:35:44 +00:00
'CHAR' => dibi::FIELD_TEXT,
'COUNTER' => dibi::FIELD_COUNTER,
'VARCHAR' => dibi::FIELD_TEXT,
'LONGCHAR' => dibi::FIELD_TEXT,
'INTEGER' => dibi::FIELD_INTEGER,
'DATETIME' => dibi::FIELD_DATETIME,
'CURRENCY' => dibi::FIELD_FLOAT,
'BIT' => dibi::FIELD_BOOL,
'LONGBINARY'=> dibi::FIELD_BINARY,
'SMALLINT' => dibi::FIELD_INTEGER,
'BYTE' => dibi::FIELD_INTEGER,
'BIGINT' => dibi::FIELD_INTEGER,
'INT' => dibi::FIELD_INTEGER,
'TINYINT' => dibi::FIELD_INTEGER,
'REAL' => dibi::FIELD_FLOAT,
'DOUBLE' => dibi::FIELD_FLOAT,
'DECIMAL' => dibi::FIELD_FLOAT,
'NUMERIC' => dibi::FIELD_FLOAT,
'MONEY' => dibi::FIELD_FLOAT,
'SMALLMONEY'=> dibi::FIELD_FLOAT,
'FLOAT' => dibi::FIELD_FLOAT,
'YESNO' => dibi::FIELD_BOOL,
2006-06-04 23:06:33 +00:00
// and many others?
);
$count = odbc_num_fields($this->resource);
$this->meta = $this->convert = array();
for ($index = 1; $index <= $count; $index++) {
$native = strtoupper(odbc_field_type($this->resource, $index));
$name = odbc_field_name($this->resource, $index);
$this->meta[$name] = array(
2006-06-08 01:35:44 +00:00
'type' => isset($types[$native]) ? $types[$native] : dibi::FIELD_UNKNOWN,
2006-06-04 23:06:33 +00:00
'native' => $native,
'length' => odbc_field_len($this->resource, $index),
'scale' => odbc_field_scale($this->resource, $index),
'precision' => odbc_field_precision($this->resource, $index),
);
$this->convert[$name] = $this->meta[$name]['type'];
}
}
} // class DibiOdbcResult