Ver Mensaje Individual
  #9  
Antiguo 31-08-2010
cloayza cloayza is offline
Miembro
 
Registrado: may 2003
Ubicación: San Pedro de la Paz, Chile
Posts: 947
Reputación: 25
cloayza Tiene un aura espectacularcloayza Tiene un aura espectacular
Referente a la pregunta, escribi un pequeno codigo que hace lo que requiere...

Pero como dice Movorack, si es un codigo mas complejo no servira o hay que implementar las restricciones y demas...

Espero te ayude...

Aqui va...
Trabaja en base a 2 vectores de tipo Variant, uno para los campos y otro para los valores, ademas se le debe indicar el nombre de la tabla...

Las funciones habilitadas son:
Select, Insert, Delete, Update;

Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TSQLType=(sqSelect, sqInsert, sqDelete, sqUpdate);

  TSQLArray=array of Variant;

  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    Label1: TLabel;
    Label3: TLabel;
    Edit1: TEdit;
    chkInsert: TRadioButton;
    chkSelect: TRadioButton;
    chkUpdate: TRadioButton;
    chkDelete: TRadioButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    AFields, AValues:TSQLArray;
    function CreateSQL(fTable:string; fFields:TSQLArray; fValues:TSQLArray;fSqlType:TSQLType):string;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function TForm1.CreateSQL(fTable:string; fFields:TSQLArray; fValues:TSQLArray;fSqlType:TSQLType):string;
var
   i:Integer;
   fSql,fVal:string;

   function GetValue():String;
   begin
        case VarType(fValues[i]) of
             varString,
             varDate     : Result:=QuotedStr(fValues[i]);
             varByte,
             varWord,
             varShortInt,
             varSmallint,
             varInteger,
             varLongWord,
             varInt64    : Result:=IntToStr(fValues[i]);
             varSingle,
             varDouble   : Result:=FloatTostr(fValues[i]);
             varBoolean  : if fValues[i] then
                              Result:=QuotedStr('True')
                           else
                              Result:=QuotedStr('False');
        end;
end;
begin
     case (fSqlType) Of
         sqSelect:begin
                       fSql:='';
                       for i := Low(fFields)to High(fFields) do
                       begin
                            fSql:=fSql+fFields[i];
                            if i< High(fFields) then
                               fSql:=fSql+', '
                       end;
                       Result:=Format('SELECT %s FROM %s',[fSql, fTable]);
                  end;
         sqInsert:begin
                       fSql:=''; fVal:='';
                       for i := Low(fFields)to High(fFields) do
                       begin
                            fSql:=fSql+fFields[i];

                            fVal:=fVal+GetValue();

                            if i< High(fFields) then
                            begin
                                 fSql:=fSql+', ';
                                 fVal:=fVal+',';
                            end;
                       end;
                       Result:=Format('INSERT INTO %s (%s) VALUES (%s)',[fTable,fSQL, fVal]);
                  end;
         sqDelete,
         sqUpdate:begin
                       fSql:=''; fVal:='';
                       for i := Low(fFields)to High(fFields) do
                       begin
                            fVal:=GetValue();

                            {Concatena Campo y Valor a asignar...}
                            fSql:=fSql+Format('%s=%s',[fFields[i],fVal]);

                            if i< High(fFields) then
                            begin
                                 if fSqlType=sqUpdate then
                                    fSql:=fSql+', '
                                 else
                                    fSql:=fSql+' AND '
                            end;
                       end;

                       if fSqlType=sqUpdate then
                          Result:=Format('UPDATE %s SET %s;',[fTable,fSQL, fVal])
                       else
                          Result:=Format('DELETE FROM %s WHERE (%s)',[fTable,fSQL, fVal])
                  end;
     end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
     {Dimensiona los vectores}
     SetLength(AFields,6);
     SetLength(AValues,6);

     {Asigna los nombre de los campos...}
     AFields[0]:='Id';
     AFields[1]:='Nombre';
     AFields[2]:='Fecha';
     AFields[3]:='Logico';
     AFields[4]:='Num_Entero';
     AFields[5]:='Num_Real'; 

    {Asigna los valores de cada campo...}
     AValues[0]:=10;
     AValues[1]:='Club Delphi';
     AValues[2]:=Date;
     AValues[3]:=True;
     AValues[4]:=199999;
     AValues[5]:=1000.00;

    {Se llama al procedimiento CreateSQL...}
    {Edit1.Text ->Para ingresar el nombre de la tabla...}
     
     if chkSelect.checked then
        memo1.lines.Text:=CreateSQL(Edit1.Text, AFields, AValues, sqSelect)
     else if chkInsert.Checked then
        memo1.lines.Text:=CreateSQL(Edit1.Text, AFields, AValues, sqInsert)
     else if chkDelete.Checked then
        memo1.lines.Text:=CreateSQL(Edit1.Text, AFields, AValues, sqDelete)
     else if chkUpdate.Checked then
        memo1.lines.Text:=CreateSQL(Edit1.Text, AFields, AValues, sqUpdate)
end;
end.
Responder Con Cita