Simplify and shorten methods to increase code quality
This commit is contained in:
parent
a04b8b44ff
commit
14a4449f3f
@ -69,25 +69,15 @@ abstract class Abstract_Driver extends PDO implements Driver_Interface {
|
|||||||
*/
|
*/
|
||||||
public function __construct($dsn, $username=NULL, $password=NULL, array $driver_options=array())
|
public function __construct($dsn, $username=NULL, $password=NULL, array $driver_options=array())
|
||||||
{
|
{
|
||||||
// Set PDO to display errors as exceptions
|
// Set PDO to display errors as exceptions, and apply driver options
|
||||||
$driver_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
|
$driver_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
|
||||||
parent::__construct($dsn, $username, $password, $driver_options);
|
parent::__construct($dsn, $username, $password, $driver_options);
|
||||||
|
|
||||||
// Load the sql class for the driver
|
// Load the sql and util class for the driver
|
||||||
$class = get_class($this)."_sql";
|
foreach(array('sql', 'util') as $sub)
|
||||||
$this->sql = new $class();
|
|
||||||
|
|
||||||
// Load the util class for the driver
|
|
||||||
$class = get_class($this)."_util";
|
|
||||||
$this->util = new $class($this);
|
|
||||||
|
|
||||||
// Set additional driver options, if they exist
|
|
||||||
if ( ! empty($driver_options) && is_array($driver_options))
|
|
||||||
{
|
{
|
||||||
foreach($driver_options as $key => $val)
|
$class = get_class($this) . "_{$sub}";
|
||||||
{
|
$this->$sub = new $class($this);
|
||||||
$this->setAttribute($key, $val);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,11 +95,8 @@ abstract class Abstract_Driver extends PDO implements Driver_Interface {
|
|||||||
*/
|
*/
|
||||||
public function prepare_query($sql, $data)
|
public function prepare_query($sql, $data)
|
||||||
{
|
{
|
||||||
// Prepare the sql
|
// Prepare the sql, save the statement for easy access later
|
||||||
$query = $this->prepare($sql);
|
$this->statement = $this->prepare($sql);
|
||||||
|
|
||||||
// Set the statement in the class variable for easy later access
|
|
||||||
$this->statement = $query;
|
|
||||||
|
|
||||||
if( ! (is_array($data) || is_object($data)))
|
if( ! (is_array($data) || is_object($data)))
|
||||||
{
|
{
|
||||||
@ -119,15 +106,13 @@ abstract class Abstract_Driver extends PDO implements Driver_Interface {
|
|||||||
// Bind the parameters
|
// Bind the parameters
|
||||||
foreach($data as $k => $value)
|
foreach($data as $k => $value)
|
||||||
{
|
{
|
||||||
if(is_numeric($k))
|
// Parameters are 1-based, the data is 0-based
|
||||||
{
|
// So, if the key is numeric, add 1
|
||||||
$k++;
|
if(is_numeric($k)) $k++;
|
||||||
|
$this->statement->bindValue($k, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
$query->bindValue($k, $value);
|
return $this->statement;
|
||||||
}
|
|
||||||
|
|
||||||
return $query;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------------------------------------------------------------------------
|
// -------------------------------------------------------------------------
|
||||||
@ -170,25 +155,24 @@ abstract class Abstract_Driver extends PDO implements Driver_Interface {
|
|||||||
*/
|
*/
|
||||||
public function quote_table($table)
|
public function quote_table($table)
|
||||||
{
|
{
|
||||||
// If there isn't a prefix set, just quote the table name
|
// Add the prefix to the table name
|
||||||
if (empty($this->table_prefix))
|
// before quoting it
|
||||||
|
if ( ! empty($this->table_prefix))
|
||||||
{
|
{
|
||||||
return $this->quote_ident($table);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Split indentifier by period, will split into:
|
// Split indentifier by period, will split into:
|
||||||
// database.schema.table OR
|
// database.schema.table OR
|
||||||
// schema.table OR
|
// schema.table OR
|
||||||
// database.table OR
|
// database.table OR
|
||||||
// table
|
// table
|
||||||
$idents = (array) explode('.', $table);
|
$idents = explode('.', $table);
|
||||||
$segments = count($idents);
|
$segments = count($idents);
|
||||||
|
|
||||||
// Quote the last item
|
// Quote the last item, and add the database prefix
|
||||||
$idents[$segments - 1] = $this->_prefix(end($idents));
|
$idents[$segments - 1] = $this->_prefix(end($idents));
|
||||||
|
|
||||||
// Rejoin
|
// Rejoin
|
||||||
$table = implode('.', $idents);
|
$table = implode('.', $idents);
|
||||||
|
}
|
||||||
|
|
||||||
// Finally, quote the table
|
// Finally, quote the table
|
||||||
return $this->quote_ident($table);
|
return $this->quote_ident($table);
|
||||||
@ -270,16 +254,18 @@ abstract class Abstract_Driver extends PDO implements Driver_Interface {
|
|||||||
*/
|
*/
|
||||||
public function _quote($str)
|
public function _quote($str)
|
||||||
{
|
{
|
||||||
// Don't add additional quotes, or quote numbers
|
// Check that the current value is a string,
|
||||||
if (strpos($str, $this->escape_char) === 0 ||
|
// and is not already quoted before quoting
|
||||||
strrpos($str, $this->escape_char) === 0 ||
|
// that value, otherwise, return the original value
|
||||||
( ! is_string($str) && is_numeric($str))
|
return (
|
||||||
|
strpos($str, $this->escape_char) !== 0
|
||||||
|
&& strrpos($str, $this->escape_char) !== 0
|
||||||
|
&& is_string($str)
|
||||||
|
&& ! is_numeric($str)
|
||||||
)
|
)
|
||||||
{
|
? "{$this->escape_char}{$str}{$this->escape_char}"
|
||||||
return $str;
|
: $str;
|
||||||
}
|
|
||||||
|
|
||||||
return "{$this->escape_char}{$str}{$this->escape_char}";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------------------------------------------------------------------------
|
// -------------------------------------------------------------------------
|
||||||
@ -423,22 +409,17 @@ abstract class Abstract_Driver extends PDO implements Driver_Interface {
|
|||||||
*
|
*
|
||||||
* @param string $sql
|
* @param string $sql
|
||||||
* @param bool $filtered_index
|
* @param bool $filtered_index
|
||||||
* @return mixed
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function driver_query($sql, $filtered_index=TRUE)
|
public function driver_query($sql, $filtered_index=TRUE)
|
||||||
{
|
{
|
||||||
// Return if the values are returned instead of a query
|
// Return if the values are returned instead of a query,
|
||||||
if (is_array($sql))
|
// or if the query doesn't apply to the driver
|
||||||
|
if (is_array($sql) || is_null($sql))
|
||||||
{
|
{
|
||||||
return $sql;
|
return $sql;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return if the query doesn't apply to the driver
|
|
||||||
if ($sql === NULL)
|
|
||||||
{
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
$res = $this->query($sql);
|
$res = $this->query($sql);
|
||||||
|
|
||||||
$flag = ($filtered_index) ? PDO::FETCH_NUM : PDO::FETCH_ASSOC;
|
$flag = ($filtered_index) ? PDO::FETCH_NUM : PDO::FETCH_ASSOC;
|
||||||
@ -492,30 +473,30 @@ abstract class Abstract_Driver extends PDO implements Driver_Interface {
|
|||||||
*/
|
*/
|
||||||
public function insert_batch($table, $data=array())
|
public function insert_batch($table, $data=array())
|
||||||
{
|
{
|
||||||
if ( ! is_array($data[0])) return NULL;
|
if ( ! is_array(current($data))) return NULL;
|
||||||
|
|
||||||
|
$vals = array(); // Values for insertion
|
||||||
$table = $this->quote_table($table);
|
$table = $this->quote_table($table);
|
||||||
$fields = array_keys($data[0]);
|
$fields = array_keys(current($data));
|
||||||
|
|
||||||
$sql = "INSERT INTO {$table} (";
|
$sql = "INSERT INTO {$table} ("
|
||||||
$sql .= implode(',', $this->quote_ident($fields));
|
. implode(',', $this->quote_ident($fields))
|
||||||
$sql .= ") VALUES ";
|
. ") VALUES ";
|
||||||
|
|
||||||
|
// Create the placholder groups
|
||||||
$params = array_fill(0, count($fields), '?');
|
$params = array_fill(0, count($fields), '?');
|
||||||
$param_string = implode(',', $params);
|
$param_string = "(" . implode(',', $params) . ")";
|
||||||
|
$param_list = array_fill(0, count($data), $param_string);
|
||||||
// Remove the first array after use, as it is a special case
|
|
||||||
$sql .= "({$param_string})";
|
|
||||||
$vals = array_values($data[0]);
|
|
||||||
array_shift($data);
|
|
||||||
|
|
||||||
// Add another grouping for each
|
// Add another grouping for each
|
||||||
foreach($data as $group)
|
foreach($data as $group)
|
||||||
{
|
{
|
||||||
$sql .= ",({$param_string})";
|
|
||||||
$vals = array_merge($vals, array_values($group));
|
$vals = array_merge($vals, array_values($group));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add the param groups
|
||||||
|
$sql .= implode(',', $param_list);
|
||||||
|
|
||||||
return array($sql, $vals);
|
return array($sql, $vals);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -217,15 +217,12 @@ final class Connection_Manager {
|
|||||||
*/
|
*/
|
||||||
private function create_dsn($dbtype, $params)
|
private function create_dsn($dbtype, $params)
|
||||||
{
|
{
|
||||||
// Add the driver type to the dsn
|
|
||||||
$dsn = ($dbtype !== 'firebird' && $dbtype !== 'sqlite')
|
|
||||||
? strtolower($dbtype).':'
|
|
||||||
: '';
|
|
||||||
|
|
||||||
if ($dbtype === 'firebird') $dsn = "{$params->host}:{$params->file}";
|
if ($dbtype === 'firebird') $dsn = "{$params->host}:{$params->file}";
|
||||||
elseif ($dbtype === 'sqlite') $dsn = $params->file;
|
elseif ($dbtype === 'sqlite') $dsn = $params->file;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
$dsn = strtolower($dbtype) . ':';
|
||||||
|
|
||||||
if ( ! empty($params->database))
|
if ( ! empty($params->database))
|
||||||
{
|
{
|
||||||
$dsn .= "dbname={$params->database}";
|
$dsn .= "dbname={$params->database}";
|
||||||
|
Loading…
Reference in New Issue
Block a user