Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 2 of 2 total
Thread Generate in memory table from TEDBTable component on a form
Mon, Dec 24 2007 6:24 AMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

Like the subject says

procedure SelfMakeMemoryTable(WhichTable: TEDBTable; SetExclusive, AndOpen: boolean);
var
Cntr: integer;
Maker: TEDBQuery;
iFlds: string;
tblName: string;
idxFlds: TStringList;
idxDesc: TStringList;
const
dq = '"';
function GetFieldType(Fld: TField): string;
begin
 case Fld.DataType of
  ftInteger: Result := 'INTEGER';
  ftString: Result := 'VARCHAR(' + IntToStr(Fld.Size) + ')';
  ftFixedChar: Result := 'CHAR(' + IntToStr(Fld.Size) + ')';
  ftBoolean: Result := 'BOOLEAN';
  ftDate: Result := 'DATE';
  ftFloat, ftCurrency: Result := 'FLOAT';
  ftTime: Result := 'TIME';
  ftMemo: Result := 'CLOB COLLATE "ANSI"';
  ftBlob: Result := 'BLOB COLLATE "ANSI"';
  ftGUID: Result := 'GUID COLLATE "ANSI"';
  ftDateTime: Result := 'TIMESTAMP';
  ftSmallInt: Result := 'SMALLINT';
  ftAutoInc: Result := 'INTEGER GENERATED ALWAYS AS IDENTITY (START WITH 0, INCREMENT BY 1) NOT NULL';
  ftLargeInt: Result := 'BIGINT';
//   ftCurrency: Result := 'DECIMAL(0, 2)';
  ftBCD: Result := 'DECIMAL(0, 4)';
  ftBytes: Result := 'BYTE(' + IntToStr(Fld.Size) + ')';
 else Result := 'VARCHAR(' + IntToStr(Fld.Size) + ')';
 end;
end;
procedure GetIdxInfo(FldList, DescList: string; DescWanted: boolean);
var
 lCntr: integer;
begin
 idxFlds.Clear;
 idxDesc.Clear;
 for lCntr := 1 to CountOccurancesOf(FldList, ';') + 1 do idxFlds.Add(SubFld(FldList, ';', lCntr));
 if DescWanted and (DescList <> '') then begin
  for lCntr := 1 to CountOccurancesOf(DescList, ';') + 1 do idxDesc.Add(SubFld(DescList, ';', lCntr));
 end;
end;
function FormIndex(idx: TIndexDef; IsPrimary: boolean): string;
var
 idxCntr: integer;
begin
 Result := '';
 GetIdxInfo(idx.Fields, idx.DescFields, (ixDescending in idx.Options));
 if idxFlds.Count > 0 then begin
  if not IsPrimary then Result := 'CREATE ';
  if IsPrimary then Result := Result + 'CONSTRAINT "PK" PRIMARY KEY('
  else Result := Result + 'INDEX "' + idx.Name + '" ON "' + tblName + '" (';
  iFlds := dq + idxFlds[0] + dq;
  if (not IsPrimary) and (ixCaseInsensitive in idx.Options) then iFlds := iFlds + ' COLLATE "ANSI_CI" ';
  if (not IsPrimary) and (ixDescending in idx.Options)
   and (idxDesc.IndexOf(idxFlds[0]) > -1) then iFlds := iFlds + ' DESC';
  iFlds := iFlds + ',';
  for idxCntr := 1 to idxFlds.Count - 1 do begin
   iFlds := iFlds + dq + idxFlds[idxCntr] + dq;
   if (not IsPrimary) and (ixDescending in idx.Options)
    and (idxDesc.IndexOf(idxFlds[idxCntr]) > -1) then iFlds := iFlds + ' DESC';
   iFlds := iFlds + ',';
  end;
  Delete(iFlds, Length(iFlds), 1);
  Result := Result + iFlds + ') ';
  if not IsPrimary then Result := Result + ';';
 end;
end;
begin
idxFlds := TStringList.Create;
idxDesc := TStringList.Create;
tblName := WhichTable.TableName;
WhichTable.Close;
WhichTable.Exclusive := True;
Maker := TEDBQuery.Create(nil);
Maker.SessionName := WhichTable.SessionName;
Maker.DatabaseName := WhichTable.DatabaseName;
if DoesTableExist(WhichTable) then begin
 Maker.SQL.Add('DROP TABLE "' + tblName + '";');
 try
  Maker.ExecSQL;
 except
 end;
 Maker.Close;
 Maker.SQL.Clear;
end;
Maker.SQL.Add('CREATE TABLE "' + tblName + '"');
Maker.SQL.Add('(');
for Cntr := 0 to WhichTable.FieldList.Count - 1 do begin
 if WhichTable.FieldList[Cntr].FieldKind = fkData
  then Maker.SQL.Add('"' + WhichTable.FieldList[Cntr].FieldName + '" ' + GetFieldType(WhichTable.FieldList[Cntr]) + ',');
end;
for Cntr := 0 to WhichTable.IndexDefs.Count - 1 do begin
 if ixPrimary in WhichTable.IndexDefs[Cntr].Options then begin
  Maker.SQL.Add(FormIndex(WhichTable.IndexDefs[Cntr], True));
  Break;
 end;
end;
if Maker.SQL[Maker.SQL.Count - 1][Length(Maker.SQL[Maker.SQL.Count - 1])] = ',' then
 Maker.SQL[Maker.SQL.Count - 1] := Copy(Maker.SQL[Maker.SQL.Count - 1], 1, Length(Maker.SQL[Maker.SQL.Count - 1]) - 1);
Maker.SQL.Add(');');
Maker.ExecSQL;
Maker.Close;
Maker.SQL.Clear;
for Cntr := 0 to WhichTable.IndexDefs.Count - 1 do begin
 if not (ixPrimary in WhichTable.IndexDefs[Cntr].Options) then begin
  Maker.Close;
  Maker.SQL.Text := FormIndex(WhichTable.IndexDefs[Cntr], False);
  maker.SQL.SaveToFile('c:\zap\xx.sql');
  Maker.ExecSQL;
 end;
end;
Maker.Free;
idxFlds.Free;
idxDesc.Free;
WhichTable.Exclusive := SetExclusive;
WhichTable.Active := AndOpen;
end;

Roy Lambert
Wed, Jan 9 2008 4:27 AMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

Its been pointed out I missed a couple of routines. Here they are

Roy Lambert

function SubFld(const InputString: string; const Separator: Char; const FieldNumber: integer): string;
var
Testing: integer;
FieldStart: integer;
CurrentField: integer;
begin
Testing := 1;
FieldStart := Testing;
CurrentField := 1;
while (Testing <= length(InputString)) do begin
 if InputString[Testing] = Separator then begin
  inc(CurrentField);
  if CurrentField > FieldNumber then break else FieldStart := Testing + 1;
 end;
 inc(Testing);
end;
if CurrentField >= FieldNumber then Result := copy(InputString, FieldStart, Testing - FieldStart) else Result := '';
end;

function CountOccurancesOf(InStr, CompStr: string): integer;
var
strCntr: integer;
begin
Result := 0;
for strCntr := 1 to Length(InStr) do if InStr[strCntr] = CompStr then inc(Result);
end;
Image