Skip to content

Instantly share code, notes, and snippets.

@riaf
Created January 29, 2009 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riaf/54504 to your computer and use it in GitHub Desktop.
Save riaf/54504 to your computer and use it in GitHub Desktop.
<?php
class Test_DB extends Ethna_DB_ADOdb
{
var $type;
var $dsninfo;
var $sql;
function Test_DB(&$controller, $dsn, $persistent){
parent::Ethna_DB_ADOdb($controller, $dsn, $persistent);
$this->db = null;
$this->sql =& $controller->getSQL();
$this->dsninfo = $this->parseDSN($dsn);
$this->dsninfo['new_link'] = true;
$this->type = $this->dsninfo['phptype'];
}
function connect(){
if($this->type == 'sqlite'){
$path = $this->dsninfo['database'];
$this->db = ADONewConnection('sqlite');
$this->db->Connect($path);
} else {
$this->db = ADONewConnection($this->dsn);
}
if($this->db){
$this->db->SetFetchMode(ADODB_FETCH_ASSOC);
return 0;
} else {
$this->db = null;
return Ethna::raiseError('Cannot connect DB.', E_DB_CONNECT);
}
}
function getType(){
return $this->type;
}
function getInsertId(){
if($this->isValid() == false){
return null;
} else {
return $this->db->Insert_Id();
}
}
function getNextId($table_name, $field_name){
if($this->isValid() == false){
return null;
} else if($this->type == 'pgsql'){
$seq_name = sprintf('%s_%s', $table_name, $field_name);
$ret = $this->db->GenID($seq_name);
return $ret;
}
return null;
}
function quoteIdentifier($identifier)
{
if (is_array($identifier)) {
foreach (array_keys($identifier) as $key) {
$identifier[$key] = $this->quoteIdentifier($identifier[$key]);
}
return $identifier;
}
switch ($this->type) {
case 'mysql':
$ret = '`' . $identifier . '`';
break;
case 'pgsql':
case 'sqlite':
default:
$ret = '"' . $identifier . '"';
break;
}
return $ret;
}
function &sqlquery($sqlid)
{
$args = func_get_args();
array_shift($args);
$query = $this->sql->get($sqlid, $args);
return $this->_query($query);
}
function sql($sqlid)
{
$args = func_get_args();
array_shift($args);
$query = $this->sql->get($sqlid, $args);
return $query;
}
function &fetchRow(&$res, $fetchmode=null, $rownum=null){
return $res;
}
function getMetaData($table){
$def = $this->db->MetaColumns($table);
foreach(array_keys($def) as $i){
$def[$i] = (array) $def[$i];
// type
$type_map = array(
'int' => array(
'int', 'integer', '^int\(?[0-9]\+', '^serial', '[a-z]+int$',
),
'boolean' => array(
'bit', 'bool', 'boolean',
),
'datetime' => array(
'timestamp', 'datetime',
),
);
foreach ($type_map as $convert_to => $regex) {
foreach ($regex as $r) {
if (preg_match('/'.$r.'/', $def[$i]['type'])) {
$def[$i]['type'] = $convert_to;
break 2;
}
}
}
if($def[$i]['primary_key'] == true){
$def[$i]['flags'][] = 'primary_key';
}
if($def[$i]['not_null'] == true){
$def[$i]['flags'][] = 'not_null';
}
if($def[$i]['max_length'] > 0){
$def[$i]['len'] = $def[$i]['max_length'];
}
switch($this->type){
case 'mysql':
default:
if($def[$i]['auto_increment'] == true){
$def[$i]['flags'][] = 'sequence';
}
break;
}
}
return $def;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment