1
0
mirror of https://github.com/dg/dibi.git synced 2025-02-24 19:02:36 +01:00
php-dibi/dibi/drivers/odbc.php

266 lines
6.7 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-04-11 18:30:30 +00:00
* Copyright (c) 2005-2007 David Grudl aka -dgx- <dave@dgx.cz>
2006-06-04 23:06:33 +00:00
*
2007-04-11 18:30:30 +00:00
* @version $Revision$ $Date$
* @package dibi
2006-06-04 23:06:33 +00:00
*/
// security - include dibi.php, not this file
2006-06-07 11:48:20 +00:00
if (!defined('DIBI')) die();
2006-06-04 23:06:33 +00:00
/**
* The dibi driver interacting with databases via ODBC connections
*
*/
class DibiOdbcDriver extends DibiDriver
{
2006-06-04 23:06:33 +00:00
private
2006-06-04 23:09:53 +00:00
$conn,
$affectedRows = FALSE;
2006-06-04 23:06:33 +00:00
public
$formats = array(
'TRUE' => "-1",
'FALSE' => "0",
'date' => "#m/d/Y#",
'datetime' => "#m/d/Y H:i:s#",
);
public static function connect($config)
{
if (!extension_loaded('odbc'))
2006-11-13 06:32:16 +00:00
throw new DibiException("PHP extension 'odbc' is not loaded");
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']))
2006-11-13 06:32:16 +00:00
throw new DibiException("Username must be specified");
2006-08-25 18:10:30 +00:00
if (empty($config['password']))
2006-11-13 06:32:16 +00:00
throw new DibiException("Password must be specified");
2006-08-25 18:10:30 +00:00
if (empty($config['database']))
throw new DibiException("Database must be specified");
2006-08-25 18:10:30 +00:00
if (empty($config['persistent']))
2006-06-04 23:06:33 +00:00
$conn = @odbc_connect($config['database'], $config['username'], $config['password']);
2006-08-25 18:10:30 +00:00
else
$conn = @odbc_pconnect($config['database'], $config['username'], $config['password']);
2006-06-04 23:06:33 +00:00
if (!is_resource($conn))
2006-11-13 06:32:16 +00:00
throw new DibiException("Connecting error", array(
2006-06-04 23:06:33 +00:00
'message' => odbc_errormsg(),
'code' => odbc_error(),
));
$obj = new self($config);
$obj->conn = $conn;
return $obj;
}
public function nativeQuery($sql)
2006-06-04 23:06:33 +00:00
{
2006-06-04 23:09:53 +00:00
$this->affectedRows = FALSE;
2006-06-04 23:06:33 +00:00
$res = @odbc_exec($this->conn, $sql);
if ($res === FALSE) return FALSE;
2006-06-04 23:09:53 +00:00
$this->affectedRows = odbc_num_rows($this->conn);
if ($this->affectedRows < 0) $this->affectedRows = FALSE;
2007-03-26 06:22:53 +00:00
if (is_resource($res))
return new DibiOdbcResult($res);
2006-06-04 23:06:33 +00:00
return TRUE;
}
public function affectedRows()
{
2006-06-04 23:09:53 +00:00
return $this->affectedRows;
2006-06-04 23:06:33 +00:00
}
public function insertId()
{
return FALSE;
}
public function begin()
{
return odbc_autocommit($this->conn, FALSE);
}
public function commit()
{
$ok = odbc_commit($this->conn);
odbc_autocommit($this->conn, TRUE);
return $ok;
}
public function rollback()
{
$ok = odbc_rollback($this->conn);
odbc_autocommit($this->conn, TRUE);
return $ok;
}
public function errorInfo()
2006-06-04 23:06:33 +00:00
{
return array(
'message' => odbc_errormsg($this->conn),
'code' => odbc_error($this->conn),
);
}
public function escape($value, $appendQuotes=TRUE)
2006-06-04 23:06:33 +00:00
{
$value = str_replace("'", "''", $value);
return $appendQuotes
? "'" . $value . "'"
: $value;
}
public function delimite($value)
2006-06-04 23:06:33 +00:00
{
return '[' . str_replace('.', '].[', $value) . ']';
2006-06-04 23:06:33 +00:00
}
public function getMetaData()
{
trigger_error('Meta is not implemented yet.', E_USER_WARNING);
}
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)
$sql = 'SELECT TOP ' . (int) $limit . ' * FROM (' . $sql . ')';
2007-03-26 06:22:53 +00:00
if ($offset) throw new DibiException('Offset is not implemented.');
2006-09-23 07:55:11 +00:00
}
2006-06-04 23:06:33 +00:00
} // class DibiOdbcDriver
class DibiOdbcResult extends DibiResult
{
private $resource;
private $row = 0;
2006-06-04 23:06:33 +00:00
public function __construct($resource)
{
$this->resource = $resource;
}
public function rowCount()
{
// will return -1 with many drivers :-(
return odbc_num_rows($this->resource);
}
protected function doFetch()
{
return odbc_fetch_array($this->resource, $this->row++);
}
public function seek($row)
{
$this->row = $row;
}
protected function free()
{
odbc_free_result($this->resource);
}
/** this is experimental */
protected function buildMeta()
2006-06-04 23:06:33 +00:00
{
// cache
if ($this->meta !== NULL)
return $this->meta;
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