note 96173 deleted from class.pdo by thiago
| From: | thiago@php.net | Date: | Fri, 12 Feb 2010 11:47:27 +0000 |
| Subject: | note 96173 deleted from class.pdo by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-165902@lists.php.net to get a copy of this message | ||
Note Submitter: Dsiembab01 at google dot com
----
so would this be outrageous
<?php
class Db_Pdo extends PDO
{
public $dsnStruct = array(
'mysql' => array(
'dsnIndexes' => array('unix_socket')
),
'pgsql' => array(
'dsnIndexes' => array('user', 'password'),
'dsnOptions' => array('dsnDelimeter' => ' '),
),
'sqlite' => array()
);
public $dsnIndexes = array('host'=>'', 'port'=>
'', 'dbname'=>'');
public $dsnDelimeter = ';';
public $type = 'mysql';
public $required = array('user', 'pass');
public function __construct($params)
{
if(is_array($params))
{
$this->setOptions($params['type']);
$dsn = $this->makeDsn($params);
}
}
public function setOptions($type)
{
if(array_key_exists($type, $this->dsnStruct))
{
$this->type = $type;
$options = $this->dsnStruct[$type];
if(isset($options['dsnOptions'])){
foreach($options as $k => $v){
$property = 'set' . ucfirst($k);
$this->set{$property}($v);
}
}
if(isset($options['dsnIndexes']))
{
foreach ($options['dsnIndexes'] as $k => $v)
{
if(is_int($k))
{
$k = $v;
$v = null;
}
$this->dsnIndexes[$k] = $v;
}
}else{
$this->dsnIndexes = array('file' => '');
}
}
}
public function makeDsn($array)
{
$dsnCheck = array_intersect_key($array, $this->dsnIndexes);
$dsnString = $this->type .':';
if(isset($dsnCheck['file']))
{
$dsnString .= $dsnCheck['file'];
$array = null;
}else{
foreach($dsnCheck as $key => $value)
{
$dsnString .= $key . '=' . $value . $this->dsnDelimeter;
}
}
$dsn = rtrim($dsnString, $this->dsnDelimeter);
parent::__construct($dsn, $array['user'], $array['pass']);
}
public function setDsnDelimeter($value)
{
$this->dsnDelimeter = $value;
}
}
?>