MY_DB_mysqli_driver.php 2.1KB

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