function GetTableNameFromSQLEx(
const SQL:
string; IdOption: IDENTIFIEROption):
string;
var
Start: PChar;
Token:
string;
SQLToken, CurSection: TSQLToken;
begin
Result := '
';
Start := PChar(
SQL);
CurSection := stUnknown;
repeat
SQLToken := NextSQLTokenEx(Start, Token, CurSection, IdOption);
if SQLToken
in SQLSections
then CurSection := SQLToken;
until SQLToken
in [stEnd, stFrom];
if SQLToken = stFrom
then
begin
repeat
SQLToken := NextSQLTokenEx(Start, Token, CurSection, IdOption);
if SQLToken
in SQLSections
then
CurSection := SQLToken
else
// stValue is returned if TableNames contain quote chars.
if (SQLToken = stTableName)
or (SQLToken = stValue)
then
begin
if AnsiRightStr(Token, 1) = '
;'
then
Token := AnsiLeftStr(Token, Token.Length-1);
if AnsiRightStr(Token, 1) = '
)'
then
Token := AnsiLeftStr(Token, Token.Length-1);
Result := Trim(Token);
while (Start[0] = '
.')
and not (SQLToken
in [stEnd])
do
begin
SQLToken := NextSqlTokenEx(Start, Token, CurSection, IdOption);
Result := Result + '
.' + Token;
end;
Exit;
end;
until (CurSection <> stFrom)
or (SQLToken
in [stEnd, stTableName]);
end;
end;