diff options
author | Pascal Kriete <pascal.kriete@ellislab.com> | 2011-01-07 21:05:40 +0100 |
---|---|---|
committer | Pascal Kriete <pascal.kriete@ellislab.com> | 2011-01-07 21:05:40 +0100 |
commit | 43ded2351b2703bf6fc6d9d0b2311cb08194d4bf (patch) | |
tree | 689bb2e3a2f7afb4449a54c07bf65348eea21c07 /system/database/drivers | |
parent | 790ebf3a77677dd6d7473bb14f8e9c5594ddcb46 (diff) |
Adding batch insert and batch update to the mysqli driver.
Diffstat (limited to 'system/database/drivers')
-rw-r--r-- | system/database/drivers/mysqli/mysqli_driver.php | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/system/database/drivers/mysqli/mysqli_driver.php b/system/database/drivers/mysqli/mysqli_driver.php index b50fe5f77..e9ec873b7 100644 --- a/system/database/drivers/mysqli/mysqli_driver.php +++ b/system/database/drivers/mysqli/mysqli_driver.php @@ -539,6 +539,24 @@ class CI_DB_mysqli_driver extends CI_DB { // -------------------------------------------------------------------- /** + * Insert_batch statement + * + * Generates a platform-specific insert string from the supplied data + * + * @access public + * @param string the table name + * @param array the insert keys + * @param array the insert values + * @return string + */ + function _insert_batch($table, $keys, $values) + { + return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES ".implode(', ', $values); + } + + // -------------------------------------------------------------------- + + /** * Update statement * * Generates a platform-specific update string from the supplied data @@ -571,6 +589,57 @@ class CI_DB_mysqli_driver extends CI_DB { return $sql; } + // -------------------------------------------------------------------- + + /** + * Update_Batch statement + * + * Generates a platform-specific batch update string from the supplied data + * + * @access public + * @param string the table name + * @param array the update data + * @param array the where clause + * @return string + */ + function _update_batch($table, $values, $index, $where = NULL) + { + $ids = array(); + $where = ($where != '' AND count($where) >=1) ? implode(" ", $where).' AND ' : ''; + + foreach($values as $key => $val) + { + $ids[] = $val[$index]; + + foreach(array_keys($val) as $field) + { + if ($field != $index) + { + $final[$field][] = 'WHEN '.$index.' = '.$val[$index].' THEN '.$val[$field]; + } + } + } + + $sql = "UPDATE ".$table." SET "; + $cases = ''; + + foreach($final as $k => $v) + { + $cases .= $k.' = CASE '."\n"; + foreach ($v as $row) + { + $cases .= $row."\n"; + } + + $cases .= 'ELSE '.$k.' END, '; + } + + $sql .= substr($cases, 0, -2); + + $sql .= ' WHERE '.$where.$index.' IN ('.implode(',', $ids).')'; + + return $sql; + } // -------------------------------------------------------------------- |