private function AppendOnDuplicateKeyUpdate(QueryBuilder $QueryBuilder, $TableName, array $Columns, array $PrimaryKeyIdentifiers) { $QueryBuilder->Append(' ON DUPLICATE KEY UPDATE '); $FirstPrimaryKey = true; foreach ($QueryBuilder->Delimit($Columns, ',') as $ColumName => $Column) { $ColumnIdentifier = [$TableName, $ColumName]; if ($FirstPrimaryKey && $Column->IsPrimaryKey()) { /* * Ugly fix/hack to prevent mysql from updating primary key when encountering * a duplicate value on a seperate unique constraint. Sadly Mysql does not support * the more robust 'MERGE' operation. Furthermore there is no clean way to throw a * conditional runtime error in Mysql reliably. * * Example: Persisting a account entity with a unique username, if the user changes * their username and and a duplicate username exists, mysql could attempt to update * the other duplicate row with the new values/primary key. This should not be an * issue as then it will fail with a duplicate primary key but could lead to some * wacky edge cases that I want no part in. */ $QueryBuilder->AppendIdentifier('# = IF(', $ColumnIdentifier); foreach ($QueryBuilder->Delimit($PrimaryKeyIdentifiers, ' AND ') as $PrimaryKeyIdentifier) { $QueryBuilder->AppendIdentifier('# = VALUES(#)', $PrimaryKeyIdentifier); } $QueryBuilder->Append(','); $QueryBuilder->AppendIdentifier('VALUES(#)', $ColumnIdentifier); $QueryBuilder->Append(','); $QueryBuilder->Append('(SELECT 1 UNION ALL SELECT 1))'); $FirstPrimaryKey = false; } else { $QueryBuilder->AppendIdentifier('# = VALUES(#)', $ColumnIdentifier); } } }
protected function AppendRange(QueryBuilder $QueryBuilder, $Offset, $Limit) { $QueryBuilder->Append(' '); if ($Limit === null) { $QueryBuilder->Append('LIMIT 18446744073709551615'); } else { $QueryBuilder->AppendValue('LIMIT #', $Limit, Queries\ParameterType::Integer); } $QueryBuilder->Append(' '); $QueryBuilder->AppendValue('OFFSET #', $Offset, Queries\ParameterType::Integer); }
protected function AppendCriterion(QueryBuilder $QueryBuilder, Relational\Criterion $Criterion) { if ($Criterion->IsConstrained()) { $QueryBuilder->Append('WHERE '); } $QueryBuilder->AppendCriterion($Criterion); }
protected function AppendIf(QueryBuilder $QueryBuilder, E\IfExpression $Expression) { $QueryBuilder->Append('CASE WHEN '); $this->Append($QueryBuilder, $Expression->GetConditionExpression()); $QueryBuilder->Append(' THEN '); $this->Append($QueryBuilder, $Expression->GetIfTrueExpression()); $QueryBuilder->Append(' ELSE '); $this->Append($QueryBuilder, $Expression->GetIfFalseExpression()); $QueryBuilder->Append(' END '); }
protected function AppendLiteral(QueryBuilder $QueryBuilder, E\LiteralExpression $Expression) { $QueryBuilder->Append($Expression->GetString()); }
public function AppendNotNullable(QueryBuilder $QueryBuilder, Traits\NotNullable $Trait) { $QueryBuilder->Append('NOT NULL'); }
public function Index(QueryBuilder $QueryBuilder, Table $Table, Mysql\Tables\Index $Trait) { $QueryBuilder->AppendIdentifier($this->MapIndexType($Trait->GetType()) . ' INDEX # ', [$Trait->GetName()]); $QueryBuilder->Append('('); $First = true; foreach ($Trait->GetColumns() as $Column) { if ($First) { $First = false; } else { $QueryBuilder->Append(','); } $ColumnName = $Column->GetName(); $QueryBuilder->AppendIdentifier('#', [$ColumnName]); $QueryBuilder->Append(' ' . $this->MapIndexDirection($Trait->GetColumnDirection($QueryBuilder))); } $QueryBuilder->Append(')'); $StorageType = $Trait->GetStorageType(); if ($StorageType !== null) { $QueryBuilder->Append('USING' . $this->MapIndexStorageType($Trait->GetStorageType())); } }
public function AppendDataAsInlineRow(QueryBuilder $QueryBuilder, array $Columns, Relational\ColumnData $ColumnData) { $QueryBuilder->Append(' SELECT '); foreach ($QueryBuilder->Delimit($Columns, ', ') as $Column) { $QueryBuilder->AppendExpression(Expression::PersistData($Column, Expression::Constant($ColumnData[$Column]))); } }
protected function AppendIf(QueryBuilder $QueryBuilder, E\IfExpression $Expression) { $QueryBuilder->Append('IF'); $QueryBuilder->Append('('); $this->Append($QueryBuilder, $Expression->GetConditionExpression()); $QueryBuilder->Append(','); $this->Append($QueryBuilder, $Expression->GetIfTrueExpression()); $QueryBuilder->Append(','); $this->Append($QueryBuilder, $Expression->GetIfFalseExpression()); $QueryBuilder->Append(')'); }
private function AppendColumnPosition(QueryBuilder $QueryBuilder, Column $PreviousColumn = null) { return $PreviousColumn === null ? $QueryBuilder->Append('FIRST') : $QueryBuilder->AppendIdentifier('AFTER #', [$PreviousColumn->GetName()]); }