Source for file InsertQuery.class.php

Documentation is available at InsertQuery.class.php

  1. <?php
  2. /**
  3. * @package harmoni.dbc
  4. *
  5. * @copyright Copyright &copy; 2005, Middlebury College
  6. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  7. *
  8. * @version $Id: InsertQuery.class.php,v 1.9 2007/09/05 21:38:59 adamfranco Exp $
  9. */
  10. require_once(HARMONI."DBHandler/InsertQuery.interface.php");
  11.  
  12. /**
  13. * An InsertQuery object provides the tools to build an INSERT query.
  14. *
  15. * This is an abstract class that simply provides all the accessor methods,
  16. * initialization steps, etc. What is left to be implemented is the
  17. * generateSQLQuery() method.
  18. *
  19. *
  20. * @package harmoni.dbc
  21. *
  22. * @copyright Copyright &copy; 2005, Middlebury College
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  24. *
  25. * @version $Id: InsertQuery.class.php,v 1.9 2007/09/05 21:38:59 adamfranco Exp $
  26. */
  27.  
  28. class InsertQuery
  29. extends QueryAbstract
  30. implements InsertQueryInterface
  31. {
  32.  
  33. /**
  34. * @var string $_table The name of the table to insert into.
  35. * @access private
  36. */
  37. var $_table;
  38.  
  39. /**
  40. * @var array $_columns The list of columns we will be inserting.
  41. * @access private
  42. */
  43. var $_columns;
  44.  
  45. /**
  46. * @var array $_values This variable is an array of arrays. The outer array
  47. * stores all the rows that will be inserted. The inner array stores the field
  48. * values of the current row.
  49. * @access private
  50. */
  51. var $_values;
  52. /**
  53. * The autoincrement column.
  54. * @var string _autoIncrementColumn
  55. * @access private
  56. */
  57. var $_autoIncrementColumn;
  58.  
  59. /**
  60. * The sequence to use for generating new ids for the autoincrement column.
  61. * @var string _sequence
  62. * @access private
  63. */
  64. var $_sequence;
  65. /**
  66. * This is the constructor for a MySQL INSERT query.
  67. * @access public
  68. */
  69. function InsertQuery() {
  70. $this->reset();
  71. }
  72.  
  73.  
  74.  
  75. /**
  76. * Sets the table to insert into.
  77. * @param string $table The table to insert into.
  78. * @access public
  79. */
  80. function setTable($table) {
  81. // ** parameter validation
  82. $stringRule = StringValidatorRule::getRule();
  83. ArgumentValidator::validate($table, $stringRule, true);
  84. // ** end of parameter validation
  85.  
  86. $this->_table = $table;
  87. }
  88.  
  89. /**
  90. * Sets the columns to insert into the table.
  91. * @param array $table The columns to insert into the table.
  92. * @access public
  93. */
  94. function setColumns($columns) {
  95. // ** parameter validation
  96. $arrayRule = ArrayValidatorRule::getRule();
  97. ArgumentValidator::validate($columns, $arrayRule, true);
  98. // ** end of parameter validation
  99.  
  100. $this->_columns = $columns;
  101. }
  102. /**
  103. * Create a new, empty row. This is used when adding values via the addValue() or
  104. * addRawValue() method rather than the setColumns.
  105. *
  106. * @return void
  107. * @access public
  108. * @since 3/8/07
  109. */
  110. function createRow () {
  111. $this->_values[] = array();
  112. $index = count($this->_values) - 1;
  113. // Ensure that rows of values at least have a null value for the column
  114. for ($i = 0; $i < count($this->_columns); $i++) {
  115. $this->_values[$index][] = 'NULL';
  116. }
  117. }
  118. /**
  119. * Add a new column and populate all rows of values with a null value. Return
  120. * the array index of the new column.
  121. *
  122. * @param string $column
  123. * @return integer
  124. * @access public
  125. * @since 3/8/07
  126. */
  127. function addColumn ( $column ) {
  128. $this->_columns[] = $this->cleanColumn($column);
  129. $index = count($this->_columns) - 1;
  130. // Ensure that rows of values at least have a null value for the column
  131. for ($i = 0; $i < count($this->_values); $i++) {
  132. $this->_values[$i][] = 'NULL';
  133. }
  134. return $index;
  135. }
  136. /**
  137. * Add a column/value pair to the latest row, if a value for the column exists,
  138. * it will be overwritten. The value will not have any new escaping or quotes
  139. * added to it. All rows of values MUST have the same number and order of
  140. * columns.
  141. *
  142. * @param string $column
  143. * @param string $value
  144. * @return void
  145. * @access public
  146. * @since 3/8/07
  147. */
  148. function addRawValue ( $column, $value ) {
  149. ArgumentValidator::validate($column, NonzeroLengthStringValidatorRule::getRule());
  150. ArgumentValidator::validate($value, NonzeroLengthStringValidatorRule::getRule());
  151. // Make sure that we have a row
  152. if (!count($this->_values))
  153. $this->createRow();
  154. $key = array_search($column, $this->_columns);
  155. if ($key === FALSE || !is_int($key)) {
  156. $key = $this->addColumn($column);
  157. }
  158. $this->_values[count($this->_values) - 1][$key] = $value;
  159. }
  160. /**
  161. * Add a value to the latest row, escaping it and surrounding it with quotes.
  162. *
  163. * @param string $column
  164. * @param string $value
  165. * @return void
  166. * @access public
  167. * @since 3/8/07
  168. */
  169. function addValue ( $column, $value ) {
  170. ArgumentValidator::validate($column, NonzeroLengthStringValidatorRule::getRule());
  171. ArgumentValidator::validate($value, StringValidatorRule::getRule());
  172. $this->addRawValue($column, "'".addslashes($value)."'");
  173. }
  174.  
  175. /**
  176. * Adds one row of values to insert into the table.
  177. *
  178. * By calling this method multiple times, you can insert many rows of
  179. * information using just one query.
  180. * @param array $values One row of values to insert into the table. Must
  181. * match the order of columns specified with the setColumns() method.
  182. * @access public
  183. */
  184. function addRowOfValues($values) {
  185. // ** parameter validation
  186. $arrayRule = ArrayValidatorRule::getRule();
  187. ArgumentValidator::validate($values, $arrayRule, true);
  188. // ** end of parameter validation
  189.  
  190. $this->_values[] = $values;
  191. }
  192.  
  193. /**
  194. * This is an alias for addRowOfValues for compatability with the UpdateQuery class.
  195. * Adds one row of values to insert into the table.
  196. * By calling this method multiple times, you can insert many rows of
  197. * information using just one query.
  198. * @see InsertQueryInterface::addRowOfValues
  199. * @param array $values One row of values to insert into the table. Must
  200. * match the order of columns specified with the setColumns() method.
  201. * @access public
  202. */
  203. function setValues($values) {
  204. $this->addRowOfValues($values);
  205. }
  206.  
  207. /**
  208. * Sets the autoincrement column.
  209. * Sets the autoincrement column. This could be useful with Oracle, for example.
  210. * @param string $column The autoincrement column.
  211. * @param string $sequence The sequence to use for generating new ids.
  212. * @access public
  213. */
  214. function setAutoIncrementColumn($column, $sequence) {
  215. // In MySQL, this is irrelevant. Auto_Increment columns should
  216. // be defined as such in the table definition.
  217. // ** parameter validation
  218. $stringRule = StringValidatorRule::getRule();
  219. ArgumentValidator::validate($column, $stringRule, true);
  220. ArgumentValidator::validate($sequence, $stringRule, true);
  221. // ** end of parameter validation
  222. $this->_autoIncrementColumn = $column;
  223. $this->_sequence = $sequence;
  224. }
  225.  
  226.  
  227. /**
  228. * Resets the query.
  229. * @access public
  230. */
  231. function reset() {
  232. parent::reset();
  233.  
  234. // an UPDATE query
  235. $this->_type = INSERT;
  236.  
  237. // default query configuration:
  238. // no table to insert into
  239. $this->_table = "";
  240.  
  241. // no columns to insert
  242. $this->_columns = array();
  243.  
  244. // no rows of values to add
  245. $this->_values = array();
  246.  
  247. $this->_column = "";
  248. $this->_sequence = "";
  249. }
  250.  
  251. }
  252. ?>

Documentation generated on Wed, 19 Sep 2007 10:24:39 -0400 by phpDocumentor 1.3.0RC3