Exemplo n.º 1
0
 protected function UpsertRowsQuery(QueryBuilder $QueryBuilder, Table $Table, array $Rows, $ShouldReturnKeyData)
 {
     if ($ShouldReturnKeyData) {
         throw new \Storm\Drivers\Base\Relational\PlatformException('Mysql does not support returning data');
     }
     $Columns = $Table->GetColumns();
     $PrimaryKeyColumns = $Table->GetPrimaryKeyColumns();
     $ColumnNames = array_keys($Columns);
     $PrimaryKeyColumnNames = array_keys($PrimaryKeyColumns);
     $TableName = $Table->GetName();
     $DerivedTableName = $TableName . 'Values';
     $PrimaryKeyIdentifiers = [];
     foreach ($PrimaryKeyColumnNames as $ColumnName) {
         $PrimaryKeyIdentifiers[] = [$TableName, $ColumnName];
     }
     $QueryBuilder->AppendIdentifier('INSERT INTO #', [$TableName]);
     $QueryBuilder->AppendIdentifiers('(#)', $ColumnNames, ',');
     /*
      * MySQL cannot prepare a statment with an inline table with only a
      * single row. Bug reported.
      */
     if (count($Rows) === 1) {
         $this->AppendDataAsInlineRow($QueryBuilder, $Table->GetColumns(), reset($Rows));
     } else {
         $this->AppendDataAsInlineTable($QueryBuilder, $Table->GetColumns(), $DerivedTableName, $Rows);
     }
     $this->AppendOnDuplicateKeyUpdate($QueryBuilder, $TableName, $Columns, $PrimaryKeyIdentifiers);
 }
Exemplo n.º 2
0
 protected function InsertRowsIndividually(IConnection $Connection, Table $Table, array $Rows, callable $PostIndividualInsertCallback)
 {
     $TableName = $Table->GetName();
     $Columns = $Table->GetColumns();
     $ColumnNames = array_keys($Columns);
     $QueryBuilder = $Connection->QueryBuilder();
     $this->AppendInsert($QueryBuilder, $TableName, $ColumnNames);
     $QueryBuilder->Append('(');
     foreach ($QueryBuilder->Delimit($Columns, ',') as $Column) {
         $QueryBuilder->AppendColumnData($Column, null);
     }
     $QueryBuilder->Append(')');
     $PreparedInsert = $QueryBuilder->Build();
     $Bindings = $PreparedInsert->GetBindings();
     $ColumnValues = array_values($Columns);
     $ParameterTypes = $this->GetParamterTypes($ColumnValues);
     foreach ($Rows as $Row) {
         foreach ($ColumnValues as $Count => $Column) {
             $Value = $Row[$Column];
             $Bindings->Bind($Value, $Value === null ? ParameterType::Null : $ParameterTypes[$Count], $Count);
         }
         $PreparedInsert->Execute();
         $PostIndividualInsertCallback($Row);
     }
 }
Exemplo n.º 3
0
 protected function DeletePrimaryKeysQuery(QueryBuilder $QueryBuilder, Table $Table, array $PrimaryKeys)
 {
     $TableName = $Table->GetName();
     $DerivedTableName = $TableName . 'PrimaryKeys';
     $TransformedDerivedTableName = $TableName . 'PersistencePrimaryKeys';
     $PrimaryKeysColumns = $Table->GetPrimaryKeyColumns();
     $PrimaryKeyNames = array_keys($PrimaryKeysColumns);
     $QueryBuilder->AppendIdentifier('DELETE # FROM # INNER JOIN (', [$TableName]);
     $this->StandardPersister->AppendDataAsInlineTable($QueryBuilder, $PrimaryKeysColumns, $DerivedTableName, $PrimaryKeys);
     $QueryBuilder->AppendIdentifier(') #', [$TransformedDerivedTableName]);
     $QueryBuilder->Append(' ON ');
     foreach ($QueryBuilder->Delimit($PrimaryKeyNames, ' AND ') as $PrimaryKeyName) {
         $QueryBuilder->AppendIdentifier('# = ', [$TableName, $PrimaryKeyName]);
         $QueryBuilder->AppendIdentifier('#', [$TransformedDerivedTableName, $PrimaryKeyName]);
     }
 }
Exemplo n.º 4
0
 protected function UpsertRowsQuery(QueryBuilder $QueryBuilder, Table $Table, array $Rows, $ShouldReturnKeyData)
 {
     if ($ShouldReturnKeyData) {
         throw new \Storm\Drivers\Base\Relational\PlatformException('SQLite does not support returning data');
     }
     $Columns = $Table->GetColumns();
     $PrimaryKeyColumns = $Table->GetPrimaryKeyColumns();
     $ColumnNames = array_keys($Columns);
     $PrimaryKeyColumnNames = array_keys($PrimaryKeyColumns);
     $TableName = $Table->GetName();
     $DerivedTableName = $TableName . 'Values';
     $PrimaryKeyIdentifiers = [];
     foreach ($PrimaryKeyColumnNames as $ColumnName) {
         $PrimaryKeyIdentifiers[] = [$TableName, $ColumnName];
     }
     $QueryBuilder->AppendIdentifier('INSERT OR REPLACE INTO #', [$TableName]);
     $QueryBuilder->AppendIdentifiers('(#)', $ColumnNames, ',');
     //TODO: Fix unique constraint conflicts
     $this->AppendDataAsInlineTable($QueryBuilder, $Table->GetColumns(), $DerivedTableName, $Rows);
 }
Exemplo n.º 5
0
 public static function AppendAlterTable(QueryBuilder $Builder, Relational\Table $Table)
 {
     $Builder->AppendIdentifier('ALTER TABLE # ', [$Table->GetName()]);
 }
Exemplo n.º 6
0
 protected function AppendDropTableQuery(QueryBuilder $QueryBuilder, Table $Table)
 {
     $QueryBuilder->AppendIdentifier('DROP TABLE #', [$Table->GetName()]);
 }