Skip to content

Commit

Permalink
Cleaned up a few things in PDO driver
Browse files Browse the repository at this point in the history
  • Loading branch information
timw4mail committed Jan 24, 2012
1 parent d96f882 commit e943bc4
Showing 1 changed file with 39 additions and 32 deletions.
71 changes: 39 additions & 32 deletions system/database/drivers/pdo/pdo_driver.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
class CI_DB_pdo_driver extends CI_DB { class CI_DB_pdo_driver extends CI_DB {


var $dbdriver = 'pdo'; var $dbdriver = 'pdo';
var $pdo_driver = '';
var $dsn = '';


// the character used to excape - not necessary for PDO // the character used to excape - not necessary for PDO
var $_escape_char = ''; var $_escape_char = '';
Expand All @@ -64,39 +66,44 @@ function __construct($params)
{ {
parent::__construct($params); parent::__construct($params);


// clause and character used for LIKE escape sequences $host = explode(":", $this->hostname);
if (strpos($this->hostname, 'mysql') !== FALSE) $this->pdo_driver = $host[0];
{
$this->_like_escape_str = ''; $this->dsn = $this->hostname;
$this->_like_escape_chr = '';
switch($this->pdo_driver)
{
case "mysql":
$this->_like_escape_str = '';
$this->_like_escape_chr = '';

//Prior to this version, the charset can't be set in the dsn
if(is_php('5.3.6'))
{
$this->dsn .= ";charset={$this->char_set}";
}

//Set the charset with the connection options
$this->options['PDO::MYSQL_ATTR_INIT_COMMAND'] = "SET NAMES {$this->char_set}";
break;


//Prior to this version, the charset can't be set in the dsn case "odbc":
if(is_php('5.3.6')) $this->_like_escape_str = " {escape '%s'} ";
{ $this->_like_escape_chr = '!';
$this->hostname .= ";charset={$this->char_set}"; break;
}


//Set the charset with the connection options case "sqlite":
$this->options['PDO::MYSQL_ATTR_INIT_COMMAND'] = "SET NAMES {$this->char_set}";
} break;
else if (strpos($this->hostname, 'odbc') !== FALSE)
{ default:
$this->_like_escape_str = " {escape '%s'} "; $this->_like_escape_str = " ESCAPE '%s' ";
$this->_like_escape_chr = '!'; $this->_like_escape_chr = '!';
} break;
else
{
$this->_like_escape_str = " ESCAPE '%s' ";
$this->_like_escape_chr = '!';
}

if (strpos($this->hostname, 'sqlite') === FALSE)
{
$this->hostname .= ";dbname=".$this->database;
} }


$this->dsn .= ";dbname=".$this->database;
$this->trans_enabled = FALSE; $this->trans_enabled = FALSE;

$this->_random_keyword = ' RND('.time().')'; // database specific random keyword $this->_random_keyword = ' RND('.time().')'; // database specific random keyword
} }


Expand All @@ -110,7 +117,7 @@ function db_connect()
{ {
$this->options['PDO::ATTR_ERRMODE'] = PDO::ERRMODE_SILENT; $this->options['PDO::ATTR_ERRMODE'] = PDO::ERRMODE_SILENT;


return new PDO($this->hostname, $this->username, $this->password, $this->options); return new PDO($this->dsn, $this->username, $this->password, $this->options);
} }


// -------------------------------------------------------------------- // --------------------------------------------------------------------
Expand All @@ -126,7 +133,7 @@ function db_pconnect()
$this->options['PDO::ATTR_ERRMODE'] = PDO::ERRMODE_SILENT; $this->options['PDO::ATTR_ERRMODE'] = PDO::ERRMODE_SILENT;
$this->options['PDO::ATTR_PERSISTENT'] = TRUE; $this->options['PDO::ATTR_PERSISTENT'] = TRUE;


return new PDO($this->hostname, $this->username, $this->password, $this->options); return new PDO($this->dsn, $this->username, $this->password, $this->options);
} }


// -------------------------------------------------------------------- // --------------------------------------------------------------------
Expand Down Expand Up @@ -379,7 +386,7 @@ function affected_rows()
function insert_id($name=NULL) function insert_id($name=NULL)
{ {
//Convenience method for postgres insertid //Convenience method for postgres insertid
if (strpos($this->hostname, 'pgsql') !== FALSE) if ($this->pdo_driver === "pgsql")
{ {
$v = $this->_version(); $v = $this->_version();


Expand Down Expand Up @@ -769,7 +776,7 @@ function _delete($table, $where = array(), $like = array(), $limit = FALSE)
*/ */
function _limit($sql, $limit, $offset) function _limit($sql, $limit, $offset)
{ {
if (strpos($this->hostname, 'cubrid') !== FALSE || strpos($this->hostname, 'sqlite') !== FALSE) if ($this->pdo_driver === "cubrid" || $this->pdo_driver === "sqlite")
{ {
if ($offset == 0) if ($offset == 0)
{ {
Expand Down

0 comments on commit e943bc4

Please sign in to comment.