InsertMultiple.class.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * @author Ironpilot
  4. *
  5. *
  6. */
  7. class Staple_Query_InsertMultiple extends Staple_Query_Insert
  8. {
  9. /**
  10. * The data to insert. May be a Select Statement Object or an array of DataSets
  11. * @var array[Staple_Query_DataSet]
  12. */
  13. protected $data = array();
  14. /**
  15. * The columns to insert the data into
  16. * @var array[string]
  17. */
  18. protected $columns = array();
  19. /**
  20. * Query to insert multiple rows
  21. * @param string $table
  22. * @param array $columns
  23. * @param mysqli $db
  24. * @param unknown_type $priority
  25. * @throws Exception
  26. */
  27. public function __construct($table = NULL, array $columns = NULL, $db = NULL, $priority = NULL)
  28. {
  29. //Process Database connection
  30. if($db instanceof mysqli)
  31. {
  32. $this->setDb($db);
  33. }
  34. else
  35. {
  36. try {
  37. $this->setDb(Staple_DB::get());
  38. }
  39. catch (Exception $e)
  40. {
  41. $this->setDb(new mysqli());
  42. }
  43. }
  44. //No DB = Bad
  45. if(!($this->db instanceof mysqli))
  46. {
  47. throw new Exception('Unable to create database object', Staple_Error::DB_ERROR);
  48. }
  49. //Set Table
  50. if(isset($table))
  51. {
  52. $this->setTable($table);
  53. }
  54. //Set Data
  55. if(isset($columns))
  56. {
  57. $this->setColumns($columns);
  58. }
  59. }
  60. /**
  61. * (non-PHPdoc)
  62. * @see Staple_Query_Insert::build()
  63. */
  64. public function build()
  65. {
  66. //Statement start
  67. $stmt = "INSERT ";
  68. //Flags
  69. if(isset($this->priority))
  70. {
  71. $stmt .= $this->priority.' ';
  72. }
  73. if($this->ignore === TRUE)
  74. {
  75. $stmt .= 'IGNORE ';
  76. }
  77. //DB Table
  78. $stmt .= "\nINTO ".$this->table.' ';
  79. //Columns
  80. $stmt .= '('.implode(',', $this->columns).') ';
  81. //Data
  82. $stmt .= 'VALUES ';
  83. $rows = array();
  84. foreach($this->data as $dataset)
  85. {
  86. if($dataset instanceof Staple_Query_DataSet)
  87. {
  88. $rows[] = trim($dataset->getInsertMultipleString());
  89. }
  90. else
  91. {
  92. //Return null string since we can't throw exceptions on __toString().
  93. return '';
  94. }
  95. }
  96. $stmt .= implode(', ', $rows);
  97. //Duplicate Updates
  98. if($this->updateOnDuplicate === true)
  99. {
  100. $first = true;
  101. $stmt .= "\nON DUPLICATE KEY UPDATE ";
  102. foreach($this->updateColumns as $ucol)
  103. {
  104. if($first === true)
  105. {
  106. $first = false;
  107. }
  108. else
  109. {
  110. $stmt .= ',';
  111. }
  112. $stmt .= " $ucol=VALUES($ucol)";
  113. }
  114. }
  115. return $stmt;
  116. }
  117. /**
  118. * Adds a dataset to the object
  119. * @param Staple_Query_DataSet $row
  120. */
  121. public function addRow(Staple_Query_DataSet $row)
  122. {
  123. if(count($row->getColumns()) != count($this->columns))
  124. {
  125. throw new Exception('DataSet row count does not match the count for this object');
  126. }
  127. else
  128. {
  129. $this->data[] = $row;
  130. return $this;
  131. }
  132. }
  133. //------------------------------------------------GETTERS AND SETTERS------------------------------------------------//
  134. /**
  135. * @return the $columns
  136. */
  137. public function getColumns()
  138. {
  139. return $this->columns;
  140. }
  141. /**
  142. * @param array[string] $columns
  143. */
  144. public function setColumns(array $columns)
  145. {
  146. $this->columns = $columns;
  147. return $this;
  148. }
  149. /**
  150. * Overides the original setter to verify that the dataset is inserted with proper specifications.
  151. * @param array[Staple_Query_DataSet] $data
  152. * @return Staple_Query_InsertMultiple
  153. */
  154. public function setData(array $data)
  155. {
  156. //Check all the array values
  157. foreach ($data as $row)
  158. {
  159. if(!($row instanceof Staple_Query_DataSet))
  160. {
  161. throw new Exception('To set the data for this object, the submission must be an array of Staple_Query_DataSet objects.', Staple_Error::APPLICATION_ERROR);
  162. }
  163. }
  164. $this->data = $data;
  165. return $this;
  166. }
  167. }
  168. ?>