Skip to content
This repository
tree: 301a36b002
Fetching contributors…

Cannot retrieve contributors at this time

executable file 140 lines (121 sloc) 3.91 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
unit NewGen;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, IBConnection, sqldb, FileUtil, LResources, Forms, Controls,
  Graphics, Dialogs, StdCtrls, Buttons;

type

  { TfmNewGen }

  TfmNewGen = class(TForm)
    bbCreateGen: TBitBtn;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    bbCreateTrigger: TBitBtn;
    cbTables: TComboBox;
    cbFields: TComboBox;
    cxTrigger: TCheckBox;
    edGenName: TEdit;
    gbTrigger: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    meSQL: TMemo;
    SQLQuery1: TSQLQuery;
    procedure bbCreateGenClick(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure bbCreateTriggerClick(Sender: TObject);
    procedure cbTablesChange(Sender: TObject);
    procedure cxTriggerChange(Sender: TObject);
  private
    { private declarations }
    ibConnection: TIBConnection;
    SQLTrans: TSQLTransaction;
  public
    DatabaseIndex: Integer;
    procedure Init(dbIndex: Integer);
    { public declarations }
  end;

var
  fmNewGen: TfmNewGen;

implementation

{ TfmNewGen }

uses main;

procedure TfmNewGen.bbCreateGenClick(Sender: TObject);
begin
  if Trim(edGenName.Text) <> '' then
  begin
    SQLQuery1.SQL.Text:= 'create generator ' + edGenName.Text;
    SQLQuery1.ExecSQL;
    SQLTrans.Commit;
    fmMain.AddToSQLHistory(fmMain.RegisteredDatabases[DatabaseIndex].RegRec.Title, 'DDL', SQLQuery1.SQL.Text);
    MessageDlg('Generator ' + edGenName.Text + ' has been created successfully', mtInformation, [mbOK], 0);
    gbTrigger.Enabled:= True;
    cxTrigger.Enabled:= True;
  end
  else
    MessageDlg('You should write Generator name', mtError, [mbOK], 0);
end;

procedure TfmNewGen.BitBtn2Click(Sender: TObject);
begin
  if (cbTables.ItemIndex = -1) or (cbFields.ItemIndex = -1) then
    MessageDlg('You should select a table and a field', mtError, [mbOk], 0)
  else
  if Trim(edGenName.Text) = '' then
    MessageDlg('You should enter generator name', mtError, [mbOK], 0)
  else
  begin
    meSQL.Clear;
    meSQL.Lines.Add('CREATE TRIGGER ' + Trim(edGenName.Text) + ' FOR ' + cbTables.Text);
    meSQL.Lines.Add('ACTIVE BEFORE INSERT POSITION 0 ');
    meSQL.Lines.Add('AS BEGIN ');
    meSQL.Lines.Add('IF (NEW.' + cbFields.Text + ' IS NULL OR NEW.' + cbFields.Text + ' = 0) THEN ');
    meSQL.Lines.Add(' NEW.' + cbFields.Text + ' = GEN_ID(' + edGenName.Text + ', 1);');
    meSQL.Lines.Add('END');
    bbCreateTrigger.Enabled:= True;
  end;
end;

procedure TfmNewGen.bbCreateTriggerClick(Sender: TObject);
begin
  SQLQuery1.SQL.Text:= meSQL.Lines.Text;
  SQLQuery1.ExecSQL;
  SQLTrans.Commit;
  fmMain.AddToSQLHistory(fmMain.RegisteredDatabases[DatabaseIndex].RegRec.Title, 'DDL', SQLQuery1.SQL.Text);
  MessageDlg('Auto Increment Trigger has been created successfully for the table ' + cbTables.Text,
    mtInformation, [mbOK], 0);
  ModalResult:= mrOK;
end;

procedure TfmNewGen.cbTablesChange(Sender: TObject);
var
  FType: string;
begin
  if cbTables.ItemIndex <> -1 then
  begin
    fmMain.GetFields(DatabaseIndex, cbTables.Text, nil);
    cbFields.Clear;
    while not fmMain.SQLQuery1.EOF do
    begin
      FType:= Trim(fmMain.SQLQuery1.FieldByName('Field_Type_Str').AsString);
      if (FType = 'INTEGER') or (FType = 'INT64') or (FType = 'SMALLINT') then
        cbFields.Items.Add(Trim(fmMain.SQLQuery1.FieldByName('Field_Name').AsString));
      fmMain.SQLQuery1.Next;
    end;
    fmMain.SQLQuery1.Close;

  end;
end;

procedure TfmNewGen.cxTriggerChange(Sender: TObject);
begin
  gbTrigger.Enabled:= cxTrigger.Checked;
end;

procedure TfmNewGen.Init(dbIndex: Integer);
begin
  DatabaseIndex:= dbIndex;
  ibConnection:= fmMain.RegisteredDatabases[dbIndex].IBConnection;
  SQLTrans:= fmMain.RegisteredDatabases[dbIndex].SQLTrans;;
  SQLQuery1.DataBase:= ibConnection;
  cxTrigger.Checked:= False;
  bbCreateTrigger.Enabled:= False;
  meSQL.Clear;
end;

initialization
  {$I newgen.lrs}

end.
Something went wrong with that request. Please try again.