Remove some duplication

This commit is contained in:
Timothy Warren 2014-04-23 17:03:46 -04:00
parent fddd92a375
commit 6b814c06a0
3 changed files with 16 additions and 43 deletions

View File

@ -577,11 +577,17 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface {
*/
public function insert_batch($table, $data=array())
{
if ( ! is_array(current($data))) return NULL;
$first_row = current($data);
if ( ! is_array($first_row)) return NULL;
$vals = array(); // Values for insertion
// Values for insertion
$vals = array();
foreach($data as $group)
{
$vals = array_merge($vals, array_values($group));
}
$table = $this->quote_table($table);
$fields = array_keys(current($data));
$fields = array_keys($first_row);
$sql = "INSERT INTO {$table} ("
. implode(',', $this->quote_ident($fields))
@ -592,13 +598,7 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface {
$param_string = "(" . implode(',', $params) . ")";
$param_list = array_fill(0, count($data), $param_string);
// For each grouping, add the values
foreach($data as $group)
{
$vals = array_merge($vals, array_values($group));
}
// Add the param groups
// Append the placeholder groups to the query
$sql .= implode(',', $param_list);
return array($sql, $vals);

View File

@ -65,12 +65,15 @@ abstract class Abstract_Util {
* @param string $name
* @param array $fields
* @param array $constraints
* @param bool $if_not_exists
* @return string
*/
public function create_table($name, $fields, array $constraints=array())
public function create_table($name, $fields, array $constraints=array(), $if_not_exists=TRUE)
{
$column_array = array();
$exists_str = ($if_not_exists) ? ' IF NOT EXISTS ' : ' ';
// Reorganize into an array indexed with column information
// Eg $column_array[$colname] = array(
// 'type' => ...,
@ -103,7 +106,7 @@ abstract class Abstract_Util {
}
// Generate the sql for the creation of the table
$sql = 'CREATE TABLE IF NOT EXISTS '.$this->quote_table($name).' (';
$sql = 'CREATE TABLE'.$exists_str.$this->quote_table($name).' (';
$sql .= implode(', ', $columns);
$sql .= ')';

View File

@ -38,37 +38,7 @@ class Firebird_Util extends Abstract_Util {
*/
public function create_table($name, $fields, array $constraints=array())
{
$column_array = array();
// Reorganize into an array indexed with column information
// Eg $column_array[$colname] = array(
// 'type' => ...,
// 'constraint' => ...,
// 'index' => ...,
// )
foreach($fields as $colname => $type)
{
$column_array[$colname] = array();
$column_array[$colname]['type'] = ($type !== $colname) ? $type : '';
}
// Join column definitons together
$columns = array();
foreach($column_array as $n => $props)
{
$str = $this->quote_ident($n);
$str .= (isset($props['type'])) ? " {$props['type']}" : "";
$str .= (isset($props['constraint'])) ? " {$props['constraint']}" : "";
$columns[] = $str;
}
// Generate the sql for the creation of the table
$sql = 'CREATE TABLE '.$this->quote_table($name).' (';
$sql .= implode(', ', $columns);
$sql .= ')';
return $sql;
return parent::create_table($name, $fields, $constraints, FALSE);
}
/**