{ $Id: PDOSQLUpdate.pas 32 2007-01-22 01:35:20Z jmarino $

############################################################################################

	Pascal Data Objects Library
	Copyright © 2006 John Marino, http://www.synsport.com
	Project site: http://pdo.sourceforge.net

############################################################################################

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 2.1 of the License, or (at your option) any later version.

	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

############################################################################################}

unit PDOSQLUpdate;

{$I directives.inc}

interface

uses PDOClasses, sysUtils; //, PDOSysUtils;


{$IFDEF ENABLE_MYSQL}
function assemble_update_mysql (
    const tablename: Ansistring;
    const set_expressions: Ansistring;
    const condition: Ansistring = '';
    const order: Ansistring = '';
    const limit: longword = 0): ansistring;

function assemble_multiupdate_mysql (
    const table_joins: Ansistring;
    const set_expressions: Ansistring;
    const condition: ansistring = ''): ansistring;
{$ENDIF}

{$IFDEF ENABLE_POSTGRESQL}
function assemble_update_pgsql (
    const tablename: Ansistring;
    const set_expressions: Ansistring;
    const condition: Ansistring = '';
    const order: Ansistring = '';
    const limit: longword = 0): ansistring;
{$ENDIF}

{$IFDEF ENABLE_FIREBIRD}
function assemble_update_firebird_15 (
    const tablename: Ansistring;
    const set_expressions: Ansistring;
    const condition: Ansistring = '';
    const order: Ansistring = '';
    const limit: longword = 0): ansistring;

function assemble_update_firebird_20 (
    const tablename: Ansistring;
    const set_expressions: Ansistring;
    const condition: Ansistring = '';
    const order: Ansistring = '';
    const limit: longword = 0): ansistring;
{$ENDIF}

implementation

{$IFDEF ENABLE_MYSQL}
function assemble_update_mysql (
    const tablename: Ansistring;
    const set_expressions: Ansistring;
    const condition: Ansistring = '';
    const order: Ansistring = '';
    const limit: longword = 0): ansistring;
var
    str_condition: ansistring;
    str_order: ansistring;
    str_limit: ansistring;

begin
    str_order     := '';
    str_limit     := '';
    str_condition := '';

    if limit <> 0      then str_limit     := format ('LIMIT %d', [limit]);
    if order <> ''     then str_order     := 'ORDER BY ' + order;
    if condition <> '' then str_condition := 'WHERE ' + condition;

    Result := format ('UPDATE %S SET %s %s %s %s', [
        tablename,
        set_expressions,
        str_condition,
        str_order,
        str_limit
    ]);
end;

function assemble_multiupdate_mysql (
    const table_joins: Ansistring;
    const set_expressions: Ansistring;
    const condition: ansistring = ''): ansistring;
var
    str_condition: ansistring;

begin
    if condition <> '' then str_condition := 'WHERE ' + condition;

    Result := format ('UPDATE %s SET %s %s', [
        table_joins,
        set_expressions,
        str_condition
    ]);
end;



{$ENDIF}

{$IFDEF ENABLE_POSTGRESQL}
function assemble_update_pgsql (
    const tablename: Ansistring;
    const set_expressions: Ansistring;
    const condition: Ansistring = '';
    const order: Ansistring = '';
    const limit: longword = 0): ansistring;
var
    str_condition: ansistring;
    str_order: ansistring;

begin
    str_condition := '';
    if condition <> '' then str_condition := 'WHERE ' + condition;

    if limit = 0 then
        Result := format ('UPDATE ONLY %s SET %s %s',
                    [tablename,
                    set_expressions,
                    condition])
    else
        begin
            str_order := '';
            if order <> '' then str_order := 'ORDER BY ' + order;

            Result := format ('UPDATE ONLY %s SET %s WHERE ctid = (SELECT ctid FROM %s %s %s LIMIT %d)', [
                        tablename,
                        set_expressions,
                        tablename,
                        str_condition,
                        str_order,
                        limit
            ]);
        end;
end;
{$ENDIF}

{$IFDEF ENABLE_FIREBIRD}
function assemble_update_firebird_20 (
    const tablename: Ansistring;
    const set_expressions: Ansistring;
    const condition: Ansistring = '';
    const order: Ansistring = '';
    const limit: longword = 0): ansistring;
var
    str_condition: ansistring;
    str_order: ansistring;
    str_limit: ansistring;
begin
    str_order     := '';
    str_limit     := '';
    str_condition := '';

    if limit <> 0 then
        begin
            str_limit := format ('ROWS %d', [limit]);
            if order <> '' then str_order := 'ORDER BY ' + order;
        end;
    if condition <> '' then str_condition := 'WHERE ' + condition;

    Result := format ('UPDATE %s SET %s %s %s %s', [
        tablename,
        set_expressions,
        str_condition,
        str_order,
        str_limit
    ]);
end;



function assemble_update_firebird_15 (
    const tablename: Ansistring;
    const set_expressions: Ansistring;
    const condition: Ansistring = '';
    const order: Ansistring = '';
    const limit: longword = 0): ansistring;
var
    str_condition: ansistring;
    str_order: ansistring;
begin
    str_condition := '';
    if condition <> '' then str_condition := 'WHERE ' + condition;

    if limit = 0 then
        Result := format ('UPDATE %s SET %s %s', [
            tablename,
            set_expressions,
            condition])
    else
        begin
            str_order := '';
            if order <> '' then str_order := 'ORDER BY ' + order;

            Result := format ('UPDATE ONLY %s SET %s WHERE RDB$DB_KEY = (SELECT FIRST %d RDB$DB_KEY FROM %s %s %s)', [
                        tablename,
                        set_expressions,
                        limit,
                        tablename,
                        str_condition,
                        str_order
            ]);
        end;
end;
{$ENDIF}


end.
