public static function buildQueryUpdate( QuoteInterface $quoting, $table, array $primaryKeys, array $data, array $dataInitial, array $returning = null )
    {

        if( !count($data) === 0 ) {
            throw new NothingToDoException("Nothing to generate.");
        }

        $columnNames = array_keys( $data[0] );
        $columnNamesInitial = array_keys( $dataInitial[0] );

        // select statement
        $dataAsSql = "VALUES " . (
            $columnNamesInitial
                ? self::dataTuplesInitial( $data, $dataInitial )
                : self::dataTuples( $data )
        );

        $columns = array();
        $where = array();

        // core data
        foreach( array_keys( $data[0] ) as $key => $column ) {

            $sqlFragment = sprintf(
                '%s = "uc"."%s"',
                $quoting->quoteIdent( $column ),
                $columnNames[$key]
            );

            $columns[] = $sqlFragment;

            if( array_key_exists( $column, $primaryKeys ) ) {
                $where[$column] = '"t".'.$sqlFragment;
            }

        }

        // dataInitial
        // all dataInitial columns are primary keys, see self::extractColumnInfoFromDataTypes()
        foreach( array_keys( $dataInitial[0] ) as $key => $column ) {

            // overwrite existing where definition
            $where[$column] = sprintf(
                '"t".%s = "uc"."__initial_%s"',
                $quoting->quoteIdent( $column ),
                $columnNames[$key]
            );

            $columnNames[] = "__initial_{$column}";

        }

        if( !count($columns) ) {
            throw new NothingToDoException("Nothing to update for `{$table}`. Every column passed in the data array is a primary key column.");
        }

        return sprintf( <<<SQL
UPDATE
    %s AS "t"
SET
    %s
FROM
    (
       %s
    ) AS uc ( "%s" )
WHERE
    %s
%s;
SQL
,
            $quoting->quoteIdent( $table ),
            implode( ', ', $columns ),
            $dataAsSql,
            implode( '", "', $columnNames ),
            implode( ' AND ', $where ),
            '' #static::getReturningClause( $returning, 't' )
        );

    }
public static function buildQueryUpdate( QuoteInterface $quoting, $table, array $primaryKeys, array $data, array $dataInitial, array $returning = null )
    {

        if( !count($data) === 0 ) {
            throw new NothingToDoException("Nothing to generate.");
        }

        $columnNames = array_keys( $data[0] );
        $columnNamesInitial = array_keys( $dataInitial[0] );

        // select statement
        $dataAsSql = "VALUES " . (
            $columnNamesInitial
                ? self::dataTuplesInitial( $data, $dataInitial )
                : self::dataTuples( $data )
        );

        $columns = array();
        $where = array();

        // core data
        foreach( array_keys( $data[0] ) as $key => $column ) {

            $sqlFragment = sprintf(
                '%s = "uc"."%s"',
                $quoting->quoteIdent( $column ),
                $columnNames[$key]
            );

            $columns[] = $sqlFragment;

            if( array_key_exists( $column, $primaryKeys ) ) {
                $where[$column] = '"t".'.$sqlFragment;
            }

        }

        // dataInitial
        // all dataInitial columns are primary keys, see self::extractColumnInfoFromDataTypes()
        foreach( array_keys( $dataInitial[0] ) as $key => $column ) {

            // overwrite existing where definition
            $where[$column] = sprintf(
                '"t".%s = "uc"."__initial_%s"',
                $quoting->quoteIdent( $column ),
                $columnNames[$key]
            );

            $columnNames[] = "__initial_{$column}";

        }

        if( !count($columns) ) {
            throw new NothingToDoException("Nothing to update for `{$table}`. Every column passed in the data array is a primary key column.");
        }

        return sprintf( <<<SQL
UPDATE
    %s AS "t"
SET
    %s
FROM
    (
       %s
    ) AS uc ( "%s" )
WHERE
    %s
%s;
SQL
,
            $quoting->quoteIdent( $table ),
            implode( ', ', $columns ),
            $dataAsSql,
            implode( '", "', $columnNames ),
            implode( ' AND ', $where ),
            '' #static::getReturningClause( $returning, 't' )
        );

    }
public static function buildQueryUpdate ( QuoteInterface $ quoting , $ table , array $ primaryKeys , array $ data , array $ dataInitial , array $ returning = null ) { if ( ! count ( $ data ) === 0 ) { throw new NothingToDoException ( "Nothing to generate." ) ; } $ columnNames = array_keys ( $ data [ 0 ] ) ; $ columnNamesInitial = array_keys ( $ dataInitial [ 0 ] ) ; // select statement $ dataAsSql = "VALUES " . ( $ columnNamesInitial ? self :: dataTuplesInitial ( $ data , $ dataInitial ) : self :: dataTuples ( $ data ) ) ; $ columns = array ( ) ; $ where = array ( ) ; // core data foreach ( array_keys ( $ data [ 0 ] ) as $ key => $ column ) { $ sqlFragment = sprintf ( '%s = "uc"."%s"' , $ quoting -> quoteIdent ( $ column ) , $ columnNames [ $ key ] ) ; $ columns [ ] = $ sqlFragment ; if ( array_key_exists ( $ column , $ primaryKeys ) ) { $ where [ $ column ] = '"t".' . $ sqlFragment ; } } // dataInitial // all dataInitial columns are primary keys, see self::extractColumnInfoFromDataTypes() foreach ( array_keys ( $ dataInitial [ 0 ] ) as $ key => $ column ) { // overwrite existing where definition $ where [ $ column ] = sprintf ( '"t".%s = "uc"."__initial_%s"' , $ quoting -> quoteIdent ( $ column ) , $ columnNames [ $ key ] ) ; $ columnNames [ ] = "__initial_{$column}" ; } if ( ! count ( $ columns ) ) { throw new NothingToDoException ( "Nothing to update for `{$table}`. Every column passed in the data array is a primary key column." ) ; } return sprintf (  <<<SQL
UPDATE
    %s AS "t"
SET
    %s
FROM
    (
       %s
    ) AS uc ( "%s" )
WHERE
    %s
%s;
SQL , $ quoting -> quoteIdent ( $ table ) , implode ( ', ' , $ columns ) , $ dataAsSql , implode ( '", "' , $ columnNames ) , implode ( ' AND ' , $ where ) , '' #static::getReturningClause( $returning, 't' ) ) ; }
Update query for a data array
@param string $table
@param array $primaryKeys
@param array $data
@return string SQL insert statement
Update query for a data array