type
TExportFormat = (
efMDB,
efExcel3, efExcel4, efExcel5, efExcel8,
efLotusWK1, efLotusWK3, efLotusWK4, // Lotus WK4 is not working
efDBase3, efDBase4, efDBase5,
efParadox3, efParadox4, efParadox5,
efText,
efHTML,
efXML
);
const
SQL_Mask = 'SELECT %s INTO [%s] IN "%s" %s FROM %s';
// | | | | |
// fields | | | |
// dest table | | |
// dest database | |
// database format |
// source dataset
DBCodes : array[TExportFormat] of string = (
'',
'"Excel 3.0;"', '"Excel 4.0;"', '"Excel 5.0;"', '"Excel 8.0;"',
'"Lotus WK1;"', '"Lotus WK3;"', '"Lotus WK4;"',
'"dBASE III;"', '"dBASE IV;"', '"dBASE 5.0;"',
'"Paradox 3.x;"', '"Paradox 4.x;"', '"Paradox 5.x;"',
'"Text;"',
'"HTML Export;"',
''
);
function ADOExportQuery(AConnection:TADOConnection;
ASourceSQL,ADestFile:string;
AFormat:TExportFormat; ADestTableName:string) : integer;
const
WhiteSpace = [' ', #13, #10, #9];
Ls = 6; // = Length('SELECT');
Lf = 4; // = Length('FROM');
var
SQL : string;
p, p1, p2, p3 : integer;
done : boolean;
sql1, sql2, _sql : string;
begin
result := -1;
if (ASourceSQL='') or (ADestFile='') or (AConnection=nil) then
raise Exception.Create('Incomplete parameters.');
if AFormat=efXML then begin
result := ADOExportToXML(AConnection, ASourceSQL, ADestFile);
end else begin
while (ASourceSQL<>'') and (ASourceSQL[1] in WhiteSpace) do
System.Delete(ASourceSQL, 1, 1);
_sql := Uppercase(ASourceSQL) + ' ';
p1 := Ls+1;
while (_sql<>'') and (_sql[p1] in WhiteSpace) do inc(p1);
p2 := 0;
done := false;
while not done do begin
p := pos('FROM', _sql);
if p<>0 then begin
done := (_sql[p-1] in WhiteSpace) and (_sql[p+Lf+1] in WhiteSpace);
if not done then begin
p2 := p2 + p;
_sql := copy(_sql, p+Lf, Length(_sql));
end;
end else
done := true;
end;
p3 := p2 + Lf;
while (p2>0) and (ASourceSQL[p2] in WhiteSpace)
do dec(p2);
while (p3<Length(ASourceSQL)) and (ASourceSQL[p3] in WhiteSpace)
do inc(p3);
sql1 := copy(ASourceSQL, p1, p2-p1-1); // between "SELECT" and "FROM"
sql2 := copy(ASourceSQL, p3, Length(ASourceSQL)); // part after "FROM"
result := ADOExportTable(AConnection, sql2, sql1, ADestFile, AFormat,
ADestTableName);
end;
end;
function ADOExportTable(AConnection:TADOConnection;
ASourceTable,AFieldList,ADestFile:string;
AFormat:TExportFormat; ADestTableName:string) : integer;
var
SQL : string;
wasConn : boolean;
fdir : string;
fnam : string;
L : TStringList;
begin
result := -1;
if (ASourceTable='') or (ADestFile='') or (AConnection=nil) then
raise Exception.Create('Incomplete parameters.');
if AFieldList='' then AFieldList := '*';
if AFormat=efXML then begin
SQL := Format('SELECT %s FROM %s', [AFieldList, ASourceTable]);
result := ADOExportToXML(AConnection, SQL, ADestFile);
end else begin
fdir := ExtractFileDir(ADestFile);
fnam := ExtractfileName(ADestFile);
case AFormat of
efDBase3,
efDBase4,
efDBase5,
efParadox3,
efParadox4,
efParadox5,
efLotusWK1,
efText,
efHTML :
begin
if FileExists(ADestFile) then DeleteFile(ADestFile);
SQL := Format(SQL_Mask,
[AFieldList, fnam, fdir, DBCodes[AFormat], ASourceTable]);
end;
efLotusWK3,
//efLotusWK4, --> not working !
efExcel3,
efExcel4 :
begin
if FileExists(ADestFile) then DeleteFile(ADestFile);
SQL := Format(SQL_Mask,
[AFieldList, fnam, ADestFile, DBCodes[AFormat], ASourceTable]);
end;
efExcel5,
efExcel8 :
begin
if FileExists(ADestFile) then DeleteFile(ADestFile);
SQL := Format(SQL_Mask,
[AFieldList, ADestTableName, ADestFile, DBCodes[AFormat],
ASourceTable]);
end;
efMDB :
SQL := Format(SQL_Mask,
[AFieldList, ADestTableName, ADestFile, DBCodes[AFormat],
ASourceTable]);
else
raise Exception.Create('ExportTable: Fileformat not supported.');
end;
with AConnection do begin
wasConn := Connected;
Connected := true;
Execute(SQL, result);
if not wasConn then Connected := false;
end;
end;
end;