Parsing SQL to get fields & tables
Posted: 27.02.2008, 23:09
Hi
I'm wanting to use a dynamic SQL dataset (SQLite ":memory:" dataset) to link to some non-SQL tables which I'll populate into the dataset at runtime.
The theory works fine but I need a way of determining the tables/fields before the real SQL is run (as it'll need to populate at this point - there's too much data to do all the tables at once).
The following code gets the initial tables and fields from a basic SQL select statement :
var idx:integer;
ZSQLiteStatementAnalyser:TZSQLiteStatementAnalyser;
ZSQLiteTokenizer:TZSQLiteTokenizer;
ZSelectSchema:IZSelectSchema;
begin
ZSQLiteTokenizer := TZSQLiteTokenizer.Create;
ZSQLiteStatementAnalyser := TZSQLiteStatementAnalyser.Create;
ZSelectSchema := ZSQLiteStatementAnalyser.DefineSelectSchemaFromQuery(ZSQLiteTokenizer,'select Firstname, Lastname from Table1, Table2 where Table1.id = Table2.id and Age > 30');
for idx := 0 to ZSelectSchema.TableCount-1 do begin
ShowMessage(ZSelectSchema.Tables[idx].Table);
end;
for idx := 0 to ZSelectSchema.FieldCount-1 do begin
ShowMessage(ZSelectSchema.Fields[idx].Field);
end;
but it doesn't get the "joined" fields and "where clause" fields -
is there way of doing this?
Chrs
I'm wanting to use a dynamic SQL dataset (SQLite ":memory:" dataset) to link to some non-SQL tables which I'll populate into the dataset at runtime.
The theory works fine but I need a way of determining the tables/fields before the real SQL is run (as it'll need to populate at this point - there's too much data to do all the tables at once).
The following code gets the initial tables and fields from a basic SQL select statement :
var idx:integer;
ZSQLiteStatementAnalyser:TZSQLiteStatementAnalyser;
ZSQLiteTokenizer:TZSQLiteTokenizer;
ZSelectSchema:IZSelectSchema;
begin
ZSQLiteTokenizer := TZSQLiteTokenizer.Create;
ZSQLiteStatementAnalyser := TZSQLiteStatementAnalyser.Create;
ZSelectSchema := ZSQLiteStatementAnalyser.DefineSelectSchemaFromQuery(ZSQLiteTokenizer,'select Firstname, Lastname from Table1, Table2 where Table1.id = Table2.id and Age > 30');
for idx := 0 to ZSelectSchema.TableCount-1 do begin
ShowMessage(ZSelectSchema.Tables[idx].Table);
end;
for idx := 0 to ZSelectSchema.FieldCount-1 do begin
ShowMessage(ZSelectSchema.Fields[idx].Field);
end;
but it doesn't get the "joined" fields and "where clause" fields -
is there way of doing this?
Chrs