MY_DB_mysqli_driver.php 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. class MY_DB_mysqli_driver extends CI_DB_mysqli_driver
  3. {
  4. final public function __construct($params)
  5. {
  6. parent::__construct($params);
  7. }
  8. /**
  9. * Insert_On_Duplicate_Update_Batch
  10. *
  11. * Compiles batch insert strings and runs the queries
  12. * MODIFIED to do a MySQL 'ON DUPLICATE KEY UPDATE'
  13. *
  14. * @access public
  15. * @param string the table to retrieve the results from
  16. * @param array an associative array of insert values
  17. * @return object
  18. */
  19. public function insert_on_duplicate_update_batch($table = '', $set = null)
  20. {
  21. if (!is_null($set)) {
  22. $this->set_insert_batch($set);
  23. }
  24. if (count($this->qb_set) == 0) {
  25. if ($this->db_debug) {
  26. //No valid data array. Folds in cases where keys and values did not match up
  27. return $this->display_error('db_must_use_set');
  28. }
  29. return false;
  30. }
  31. if ($table == '') {
  32. if (!isset($this->qb_from[0])) {
  33. if ($this->db_debug) {
  34. return $this->display_error('db_must_set_table');
  35. }
  36. return false;
  37. }
  38. $table = $this->qb_from[0];
  39. }
  40. // Batch this baby
  41. for ($i = 0, $total = count($this->qb_set); $i < $total; $i = $i + 5000) {
  42. $sql = $this->_insert_on_duplicate_update_batch($this->protect_identifiers($table, true, null, false), $this->qb_keys, array_slice($this->qb_set, $i, 5000));
  43. // echo $sql;
  44. $this->query($sql);
  45. }
  46. $this->_reset_write();
  47. return true;
  48. }
  49. /**
  50. * Insert_on_duplicate_update_batch statement
  51. *
  52. * Generates a platform-specific insert string from the supplied data
  53. * MODIFIED to include ON DUPLICATE UPDATE
  54. *
  55. * @access public
  56. * @param string the table name
  57. * @param array the insert keys
  58. * @param array the insert values
  59. * @return string
  60. */
  61. private function _insert_on_duplicate_update_batch($table, $keys, $values)
  62. {
  63. foreach ($keys as $key) {
  64. $update_fields[] = $key . '=VALUES(' . $key . ')';
  65. }
  66. return "INSERT INTO " . $table . " (" . implode(', ', $keys) . ") VALUES " . implode(', ', $values) . " ON DUPLICATE KEY UPDATE " . implode(', ', $update_fields);
  67. }
  68. }