/lib/vendor/symfony/lib/plugins/sfPropelPlugin/lib/vendor/propel-generator/classes/propel/engine/builder/sql/DataSQLBuilder.php

https://github.com/frhumanes/PLM · PHP · 264 lines · 107 code · 33 blank · 124 comment · 4 complexity · 8da1ff760be1e11bcad56904f0768520 MD5 · raw file

  1. <?php
  2. /*
  3. * $Id: DataSQLBuilder.php 1262 2009-10-26 20:54:39Z francois $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://propel.phpdb.org>.
  20. */
  21. require_once 'propel/engine/builder/DataModelBuilder.php';
  22. require_once 'propel/engine/database/model/PropelTypes.php';
  23. /**
  24. * Baseclass for SQL data dump SQL building classes.
  25. *
  26. * @author Hans Lellelid <hans@xmpl.org>
  27. * @package propel.engine.builder.sql
  28. */
  29. abstract class DataSQLBuilder extends DataModelBuilder {
  30. /**
  31. * Perform any reset between runs of this builder.
  32. *
  33. * This can be used, for example, to clear any stored start/end SQL.
  34. */
  35. public static function reset()
  36. {
  37. // does nothing by default
  38. }
  39. /**
  40. * Gets any SQL to place at the start of all the row inserts.
  41. *
  42. * @return string
  43. */
  44. public static function getDatabaseStartSql()
  45. {
  46. return '';
  47. }
  48. /**
  49. * Gets any SQL to place at the end of all the row inserts.
  50. *
  51. * @return string
  52. */
  53. public static function getDatabaseEndSql()
  54. {
  55. return '';
  56. }
  57. /**
  58. * Gets any SQL to place before row inserts for a new table.
  59. *
  60. * @return string
  61. */
  62. public function getTableStartSql()
  63. {
  64. return '';
  65. }
  66. /**
  67. * Gets any SQL to place at the end of row inserts for a table.
  68. *
  69. * @return string
  70. */
  71. public function getTableEndSql()
  72. {
  73. return '';
  74. }
  75. /**
  76. * The main method in this class, returns the SQL for INSERTing data into a row.
  77. * @param DataRow $row The row to process.
  78. * @return string
  79. */
  80. public function buildRowSql(DataRow $row)
  81. {
  82. $sql = "";
  83. $platform = $this->getPlatform();
  84. $table = $this->getTable();
  85. $sql .= "INSERT INTO ".$this->quoteIdentifier($this->prefixTablename($this->getTable()->getName()))." (";
  86. // add column names to SQL
  87. $colNames = array();
  88. foreach ($row->getColumnValues() as $colValue) {
  89. $colNames[] = $this->quoteIdentifier($colValue->getColumn()->getName());
  90. }
  91. $sql .= implode(',', $colNames);
  92. $sql .= ") VALUES (";
  93. $colVals = array();
  94. foreach ($row->getColumnValues() as $colValue) {
  95. $colVals[] = $this->getColumnValueSql($colValue);
  96. }
  97. $sql .= implode(',', $colVals);
  98. $sql .= ");
  99. ";
  100. return $sql;
  101. }
  102. /**
  103. * Gets the propertly escaped (and quoted) value for a column.
  104. * @param ColumnValue $colValue
  105. * @return mixed The proper value to be added to the string.
  106. */
  107. protected function getColumnValueSql(ColumnValue $colValue)
  108. {
  109. $column = $colValue->getColumn();
  110. $method = 'get' . $column->getPhpNative() . 'Sql';
  111. return $this->$method($colValue->getValue());
  112. }
  113. /**
  114. * Gets a representation of a binary value suitable for use in a SQL statement.
  115. * Default behavior is true = 1, false = 0.
  116. * @param boolean $value
  117. * @return int
  118. */
  119. protected function getBooleanSql($value)
  120. {
  121. return (int) $value;
  122. }
  123. /**
  124. * Gets a representation of a BLOB/LONGVARBINARY value suitable for use in a SQL statement.
  125. * @param mixed $blob Blob object or string data.
  126. * @return string
  127. */
  128. protected function getBlobSql($blob)
  129. {
  130. // they took magic __toString() out of PHP5.0.0; this sucks
  131. if (is_object($blob)) {
  132. return $this->getPlatform()->quote($blob->__toString());
  133. } else {
  134. return $this->getPlatform()->quote($blob);
  135. }
  136. }
  137. /**
  138. * Gets a representation of a CLOB/LONGVARCHAR value suitable for use in a SQL statement.
  139. * @param mixed $clob Clob object or string data.
  140. * @return string
  141. */
  142. protected function getClobSql($clob)
  143. {
  144. // they took magic __toString() out of PHP5.0.0; this sucks
  145. if (is_object($clob)) {
  146. return $this->getPlatform()->quote($clob->__toString());
  147. } else {
  148. return $this->getPlatform()->quote($clob);
  149. }
  150. }
  151. /**
  152. * Gets a representation of a date value suitable for use in a SQL statement.
  153. * @param string $value
  154. * @return string
  155. */
  156. protected function getDateSql($value)
  157. {
  158. return "'" . date('Y-m-d', strtotime($value)) . "'";
  159. }
  160. /**
  161. * Gets a representation of a decimal value suitable for use in a SQL statement.
  162. * @param double $value
  163. * @return float
  164. */
  165. protected function getDecimalSql($value)
  166. {
  167. return (float) $value;
  168. }
  169. /**
  170. * Gets a representation of a double value suitable for use in a SQL statement.
  171. * @param double $value
  172. * @return double
  173. */
  174. protected function getDoubleSql($value)
  175. {
  176. return (double) $value;
  177. }
  178. /**
  179. * Gets a representation of a float value suitable for use in a SQL statement.
  180. * @param float $value
  181. * @return float
  182. */
  183. protected function getFloatSql($value)
  184. {
  185. return (float) $value;
  186. }
  187. /**
  188. * Gets a representation of an integer value suitable for use in a SQL statement.
  189. * @param int $value
  190. * @return int
  191. */
  192. protected function getIntSql($value)
  193. {
  194. return (int) $value;
  195. }
  196. /**
  197. * Gets a representation of a NULL value suitable for use in a SQL statement.
  198. * @return null
  199. */
  200. protected function getNullSql()
  201. {
  202. return 'NULL';
  203. }
  204. /**
  205. * Gets a representation of a string value suitable for use in a SQL statement.
  206. * @param string $value
  207. * @return string
  208. */
  209. protected function getStringSql($value)
  210. {
  211. return $this->getPlatform()->quote($value);
  212. }
  213. /**
  214. * Gets a representation of a time value suitable for use in a SQL statement.
  215. * @param string $value
  216. * @return string
  217. */
  218. protected function getTimeSql($paramIndex, $value)
  219. {
  220. return "'" . date('H:i:s', strtotime($value)) . "'";
  221. }
  222. /**
  223. * Gets a representation of a timestamp value suitable for use in a SQL statement.
  224. * @param string $value
  225. * @return string
  226. */
  227. function getTimestampSql($value)
  228. {
  229. return "'" . date('Y-m-d H:i:s', strtotime($value)) . "'";
  230. }
  231. }