4 * Dear God Why Do I Have To Write This (Dumb SQL Builder)
7 * $insert = CRM_Utils_SQL_Insert::into('mytable')
8 * ->row(array('col1' => '1', 'col2' => '2' ))
9 * ->row(array('col1' => '2b', 'col2' => '1b'));
10 * echo $insert->toSQL();
12 * Note: In MySQL, numeric values may be escaped. Except for NULL values,
13 * it's reasonable for us to simply escape all values by default -- without
14 * any knowledge of the underlying schema.
18 * - No knowledge of the underlying SQL API (except for escaping -- CRM_Core_DAO::escapeString)
19 * - No knowledge of the underlying data model
21 * - SQL clauses correspond to PHP functions ($select->where("foo_id=123"))
23 class CRM_Utils_SQL_Insert
{
25 private $verb = 'INSERT INTO';
38 * Array<string> list of column names
43 * Create a new INSERT query.
45 * @param string $table
46 * Table-name and optional alias.
47 * @return CRM_Utils_SQL_Insert
49 public static function into($table) {
50 return new self($table);
54 * Insert a record based on a DAO.
56 * @param \CRM_Core_DAO $dao
57 * @return \CRM_Utils_SQL_Insert
58 * @throws \CRM_Core_Exception
60 public static function dao(CRM_Core_DAO
$dao) {
61 $table = CRM_Core_DAO
::getLocaleTableName($dao->getTableName());
63 foreach ((array) $dao as $key => $value) {
64 if ($value === 'null') {
65 $value = NULL; // Blerg!!!
67 // Skip '_foobar' and '{\u00}*_options' and 'N'.
68 if (preg_match('/[a-zA-Z]/', $key{0}) && $key !== 'N') {
72 return self
::into($table)->row($row);
76 * Create a new SELECT query.
78 * @param string $table
79 * Table-name and optional alias.
81 public function __construct($table) {
82 $this->table
= $table;
83 $this->rows
= array();
89 * @param array $columns
91 * @return CRM_Utils_SQL_Insert
92 * @throws \CRM_Core_Exception
94 public function columns($columns) {
95 if ($this->columns
!== NULL) {
96 throw new CRM_Core_Exception("Column order already specified.");
98 $this->columns
= $columns;
107 * @return CRM_Utils_SQL_Insert
109 public function rows($rows) {
110 foreach ($rows as $row) {
121 * @return CRM_Utils_SQL_Insert
122 * @throws CRM_Core_Exception
124 public function row($row) {
125 $columns = array_keys($row);
127 if ($this->columns
=== NULL) {
129 $this->columns
= $columns;
131 elseif (array_diff($this->columns
, $columns) !== array()) {
132 throw new CRM_Core_Exception("Inconsistent column names");
135 $escapedRow = array();
136 foreach ($this->columns
as $column) {
137 $escapedRow[$column] = $this->escapeString($row[$column]);
139 $this->rows
[] = $escapedRow;
145 * Use REPLACE INTO instead of INSERT INTO.
147 * @param bool $asReplace
149 * @return CRM_Utils_SQL_Insert
151 public function usingReplace($asReplace = TRUE) {
152 $this->verb
= $asReplace ?
'REPLACE INTO' : 'INSERT INTO';
159 * @param string|NULL $value
162 * SQL expression, e.g. "it\'s great" (with-quotes) or NULL (without-quotes)
164 protected function escapeString($value) {
165 return $value === NULL ?
'NULL' : '"' . CRM_Core_DAO
::escapeString($value) . '"';
174 public function toSQL() {
175 $columns = "`" . implode('`,`', $this->columns
) . "`";
176 $sql = "{$this->verb} {$this->table} ({$columns}) VALUES";
179 foreach ($this->rows
as $row) {
180 $sql .= "{$nextDelim}\n(" . implode(',', $row) . ")";