/** * {@inheritdoc} */ public function buildInsertQuery(IInsertBuilder $query) { $ignoreSql = $query->getIsIgnore() ? ' IGNORE' : ''; $whatSql = $this->quoteIdentifier($query->getTableName()); $setSql = "\nSET " . $this->buildSetPart($query->getValues()); $onDuplicateKeyValues = $query->getOnDuplicateKeyValues(); $onDuplicateKeySql = ''; if (!empty($onDuplicateKeyValues) && !$query->getIsIgnore()) { $onDuplicateKeySql = "\nON DUPLICATE KEY UPDATE " . $this->buildSetPart($onDuplicateKeyValues); } return 'INSERT' . $ignoreSql . ' INTO ' . $whatSql . $setSql . $onDuplicateKeySql; }
/** * Эмулирует INSERT ON DUPLICATE KEY UPDATE * @internal * @param IInsertBuilder $query insert-запрос * @return string */ protected function buildInsertUpdateQueries(IInsertBuilder $query) { $values = $query->getValues(); $whatSql = $this->quoteIdentifier($query->getTableName()); $valuesSql = $this->buildValuesPart($values); $result = 'INSERT OR IGNORE INTO ' . $whatSql . $valuesSql . ";\n"; $whereConditions = []; $columns = $query->getOnDuplicateKeyColumns(); foreach ($values as $columnName => $placeholder) { if (in_array($columnName, $columns)) { $columnName = $this->quoteIdentifier($columnName); $placeholder = $this->protectExpressionValue($placeholder); $whereConditions[] = $columnName . ' = ' . $placeholder; } } $whereSql = "\nWHERE " . (count($whereConditions) ? implode(' AND ', $whereConditions) : '1'); $setSql = $this->buildSetPart($query->getOnDuplicateKeyValues()); $result .= 'UPDATE ' . $whatSql . $setSql . $whereSql . ';'; return $result; }