diff --git a/classes/db_pdo.php b/classes/db_pdo.php index f8a1aaa..037419a 100644 --- a/classes/db_pdo.php +++ b/classes/db_pdo.php @@ -538,5 +538,44 @@ abstract class DB_PDO extends PDO { * @return void */ abstract public function truncate($table); + + // -------------------------------------------------------------------------- + + /** + * Create sql for batch insert + * + * @param string $table + * @param array $data + * @return string + */ + public function insert_batch($table, $data=array()) + { + if ( ! is_array($data[0])) return NULL; + + $table = $this->quote_table($table); + $fields = array_keys($data[0]); + $vals = array(); + + $sql = "INSERT INTO {$table} ("; + $sql .= implode(',', $this->quote_ident($fields)); + $sql .= ") VALUES "; + + $params = array_fill(0, count($fields), '?'); + $param_string = implode(',', $params); + + // 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 + foreach($data as $group) + { + $sql .= ",({$param_string})"; + $vals = array_merge($vals, array_values($group)); + } + + return array($sql, $vals); + } } // End of db_pdo.php \ No newline at end of file diff --git a/classes/query_builder.php b/classes/query_builder.php index 735d0da..ee352fc 100644 --- a/classes/query_builder.php +++ b/classes/query_builder.php @@ -1016,6 +1016,28 @@ class Query_Builder implements iQuery_Builder { return $this->_run("insert", $table); } + + // -------------------------------------------------------------------------- + + /** + * Create sql for batch insert + * + * @param string $table + * @param array $data + * @return string + */ + public function insert_batch($table, $data=array()) + { + // Get the generated values and sql string + list($sql, $data) = $this->db->insert_batch($table, $data); + + if ( ! is_null($sql)) + { + return $this->_run('', $table, FALSE, $sql, $data); + } + + return NULL; + } // -------------------------------------------------------------------------- @@ -1185,13 +1207,22 @@ class Query_Builder implements iQuery_Builder { * @param string $type * @param string $table * @param bool $simple + * @param string $sql + * @param mixed $vals * @return mixed */ - protected function _run($type, $table, $simple=FALSE) + protected function _run($type, $table, $simple=FALSE, $sql=NULL, $vals=NULL) { - $sql = $this->_compile($type, $table); - $vals = array_merge($this->values, (array) $this->where_values); - + if (is_null($sql)) + { + $sql = $this->_compile($type, $table); + } + + if (is_null($vals)) + { + $vals = array_merge($this->values, (array) $this->where_values); + } + // Add quotes to 'string' values foreach($vals as &$v) { diff --git a/docs/classes/BadConnectionException.html b/docs/classes/BadConnectionException.html index b7abc69..1fc90b7 100644 --- a/docs/classes/BadConnectionException.html +++ b/docs/classes/BadConnectionException.html @@ -219,7 +219,7 @@
+ generated on 2013-05-03T13:07:08-04:00.