Title: LittleTable, a component that create ADO tables via SQL
Question: ADO tables
Answer:
/////////////////////////////
// //
// LittleTable //
// //
// SQL Table Ceating //
// for ADODB //
// //
// //
// Unit written by //
// //
// Simone Di Cicco //
// simone.dicicco@tin.it //
// simone.dicicco@email.it //
// //
/////////////////////////////
unit LittleTable;
interface
uses Windows, Messages, SysUtils, Classes, ADODB, db;
const
FAuthor = 'Simone Di Cicco';
FVersion = '1.0';
type
TTableVars = (Long, Number, Varchar, Memo,
Long_NotNull, Number_NotNull, Varchar_NotNull);
TLittleTables = class(TComponent)
protected
FADOQuery : TADOQuery;
FADOConnection: TADOConnection;
FDataSource : TDataSource;
FTableFiels : TStringList;
FSQLCode : TStringList;
FTableName : string;
private
published
property ADOQuery : TADOQuery read FADOQuery write FADOQuery;
property ADOConnection: TADOConnection read FADOConnection write FADOConnection;
property DataSource : TDataSource read FDataSource write FDataSource;
property TableName : string read FTableName write FTableName;
public
constructor Create(AOwner: TComponent); override;
//destructor Destroy; override;
procedure ClearSQL;
procedure AddField(Name: string; FieldType: TTableVars; TypeLength: Integer);
procedure CreateTable(SaveSQLToFile: Boolean; FileName: string);
end;
procedure Register;
implementation
{ TLittleTables }
procedure Register;
begin
RegisterComponents('Simone Di Cicco', [TLittleTables]);
end;
procedure TLittleTables.AddField(Name: string; FieldType: TTableVars; TypeLength: Integer);
var
FieldT: string;
begin
case FieldType of
Memo : FieldT := 'Memo';
Long : FieldT := 'Long';
Number : FieldT := 'Number';
Varchar : FieldT := 'Varchar('+IntToStr(TypeLength)+')';
Long_NotNull : FieldT := 'Long Not Null';
Number_NotNull : FieldT := 'Number Not Null';
Varchar_NotNull: FieldT := 'Varchar('+IntToStr(TypeLength)+') Not Null';
end;
FTableFiels.Add(Name+' '+FieldT);
end;
procedure TLittleTables.ClearSQL;
begin
FTableFiels.Clear;
FSQLCode.Clear;
end;
constructor TLittleTables.Create(AOwner: TComponent);
begin
inherited;
FTableFiels := TStringList.Create;
FTableFiels.Clear;
FSQLCode := TStringList.Create;
FSQLCode.Clear;
end;
procedure TLittleTables.CreateTable(SaveSQLToFile: Boolean; FileName: string);
var
FieldCount, Counter: integer;
LastStr, NewLastStr: string;
begin
FADOQuery.SQL.Clear;
FieldCount := 0;
Counter := 0;
FieldCount := FTableFiels.Count;
FSQLCode.Add('CREATE TABLE '+FTableName+' (');
repeat
FSQLCode.Add(FTableFiels.Strings[Counter]+',');
inc(Counter, 1);
until Counter = FieldCount;
FSQLCode.Add(')');
LastStr := FSQLCode.Strings[FieldCount];
NewLastStr := Copy(LastStr, 0, Length(LastStr) - 1);
FSQLCode.Strings[FieldCount] := NewLastStr;
FADOQuery.DataSource := FDataSource;
FADOQuery.Connection := FADOConnection;
FADOQuery.SQL.AddStrings(FSQLCode);
//FADOQuery.Open;
FADOQuery.ExecSQL;
if SaveSQLToFile = True then
begin
FSQLCode.SaveToFile(FileName);
end;
end;
end.