unit uTable;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, Grids, DB, DBGrids,
  ABSMain,
  ABSConst, DBCtrls, ExtCtrls,                                                                                  
  ABSConverts,
  ABSTypes, Mask;

type
  TfrmTable = class(TForm)
    Panel1: TPanel;
    eTableName: TEdit;
    Label1: TLabel;
    dbgFields: TDBGrid;
    Label2: TLabel;
    Splitter1: TSplitter;
    Panel2: TPanel;
    Panel3: TPanel;
    Label3: TLabel;
    dbgIndexes: TDBGrid;
    Panel4: TPanel;
    BitBtn2: TBitBtn;
    BitBtn1: TBitBtn;
    Panel5: TPanel;
    dbgIndecFields: TDBGrid;
    Splitter2: TSplitter;
    Label4: TLabel;
    DBText1: TDBText;
    btnFieldUp: TSpeedButton;
    btnFieldDown: TSpeedButton;
    btnIndexFieldUp: TSpeedButton;
    btnIndexFieldDown: TSpeedButton;
    btnAddField: TSpeedButton;
    SpeedButton1: TSpeedButton;
    btnAddIndexField: TSpeedButton;
    btnDeleteIndexField: TSpeedButton;
    btnAddIndex: TSpeedButton;
    btnDeleteIndex: TSpeedButton;
    procedure FormShow(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure dbgIndexesKeyPress(Sender: TObject; var Key: Char);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnFieldUpClick(Sender: TObject);
    procedure btnFieldDownClick(Sender: TObject);
    procedure btnAddFieldClick(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure btnAddIndexFieldClick(Sender: TObject);
    procedure btnDeleteIndexFieldClick(Sender: TObject);
    procedure btnAddIndexClick(Sender: TObject);
    procedure btnDeleteIndexClick(Sender: TObject);
    procedure btnIndexFieldUpClick(Sender: TObject);
    procedure btnIndexFieldDownClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure AllPost;
  end;

var
  frmTable: TfrmTable;

implementation

uses main;

{$R *.dfm}

procedure TfrmTable.FormShow(Sender: TObject);
var
  i: Integer;
  sl: TStringList;
begin
  // Fill Types
  With Self.dbgFields.Columns[3].PickList do
   begin
    Clear;
    sl := TStringList.Create;
    try
      for i := 1 to Integer(high(TABSAdvancedFieldType)) do
        sl.Add(AftToStr(TABSAdvancedFieldType(i)));
      //for i := Low(SQLFieldTypes) to High(SQLFieldTypes) do
      // if (sl.IndexOf(SQLFieldTypes[i].SqlName) = -1) then
      //   sl.Add(SQLFieldTypes[i].SqlName);
      //sl.Sort;
      AddStrings(sl);
    finally
     sl.Free;
    end;
   end;
end;


procedure TfrmTable.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  if (ModalResult = mrOk) and (Trim(eTableName.Text)='') then
   begin
    CanClose := False;
    MessageDlg('Table Name cannot be blank', mtError,[mbOk], 0);
   end;
end;

procedure TfrmTable.dbgIndexesKeyPress(Sender: TObject; var Key: Char);
begin
  if (Key=#13) then AllPost;
end;

procedure TfrmTable.AllPost;
begin
  if (dbgFields.DataSource.DataSet.State in [dsEdit,dsInsert]) then
    dbgFields.DataSource.DataSet.Post;
  if (dbgIndexes.DataSource.DataSet.State in [dsEdit,dsInsert]) then
    dbgIndexes.DataSource.DataSet.Post;
  if (dbgIndecFields.DataSource.DataSet.State in [dsEdit,dsInsert]) then
    dbgIndecFields.DataSource.DataSet.Post;
end;

procedure TfrmTable.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  AllPost;
end;

procedure TfrmTable.btnFieldUpClick(Sender: TObject);
begin
  MoveRow(dbgFields.DataSource.DataSet, True);
end;

procedure TfrmTable.btnFieldDownClick(Sender: TObject);
begin
  MoveRow(dbgFields.DataSource.DataSet, False);
end;

procedure TfrmTable.btnAddFieldClick(Sender: TObject);
begin
  dbgFields.DataSource.DataSet.Last;
  dbgFields.DataSource.DataSet.Append;
end;

procedure TfrmTable.SpeedButton1Click(Sender: TObject);
begin
  with dbgFields.DataSource.DataSet do
   if ((State = dsInsert) or (RecordCount > 0)) then
    Delete;
end;

procedure TfrmTable.btnAddIndexFieldClick(Sender: TObject);
begin
  dbgIndecFields.DataSource.DataSet.Last;
  dbgIndecFields.DataSource.DataSet.Append;
end;

procedure TfrmTable.btnDeleteIndexFieldClick(Sender: TObject);
begin
  with dbgIndecFields.DataSource.DataSet do
   if ((State = dsInsert) or (RecordCount > 0)) then
    Delete;
end;

procedure TfrmTable.btnAddIndexClick(Sender: TObject);
begin
  dbgIndexes.DataSource.DataSet.Last;
  dbgIndexes.DataSource.DataSet.Append;
end;

procedure TfrmTable.btnDeleteIndexClick(Sender: TObject);
begin
  with dbgIndexes.DataSource.DataSet do
   if ((State = dsInsert) or (RecordCount > 0)) then
    Delete;
end;

procedure TfrmTable.btnIndexFieldUpClick(Sender: TObject);
begin
  MoveRow(dbgIndecFields.DataSource.DataSet, True);
end;

procedure TfrmTable.btnIndexFieldDownClick(Sender: TObject);
begin
  MoveRow(dbgIndecFields.DataSource.DataSet, False);
end;


end.
