type
TColumnInfo = class
public
FullName: string;
RealSchemaName: string;
RealTableName: string;
ReferencedTableColumn: string;
QueryColumnAlias: string;
IsPKColumn: Boolean; // Indicates if it's a primary key column
end;
TTableInfo = class
public
SchemaName: string;
TableName: string;
Alias: string;
end;
TSQLParser = class
private
FSQLText: string;
FTables: TObjectList;
FColumns: TObjectList;
FConnection: TUniConnection;
FPrimaryKeys: TDictionary<string, TList>;
procedure Parse;
procedure ParseFromClause;
procedure ParseSelectClause;
function IsExpression(const S: string): Boolean;
function IsClauseKeyword(const Token: string): Boolean;
function GetTableInfoByAliasOrName(const AliasOrName: string): TTableInfo;
function IsJoinType(const Token: string): Boolean;
procedure CheckPrimaryKeys; // Checks if columns are primary keys
procedure FillPrimaryKeyDictionary; // Fills the primary key dictionary
function IsReservedWord(const Token: string; const ReservedWords: array of string): Boolean;
public
constructor Create(const SQLText: string; AConnection: TUniConnection);
destructor Destroy; override;
property Tables: TObjectList read FTables;
property Columns: TObjectList read FColumns;
end;
implementation
constructor TSQLParser.Create(const SQLText: string; AConnection: TUniConnection);
begin
inherited Create;
FSQLText := SQLText;
FConnection := AConnection; // Store the provided database connection
FTables := TObjectList.Create(True); // Owns objects
FColumns := TObjectList.Create(True); // Owns objects
FPrimaryKeys := TDictionary<string, TList>.Create;
Parse;
FillPrimaryKeyDictionary; // After parsing, fill the primary key dictionary
CheckPrimaryKeys; // Then check for primary key columns using the cache
end;
destructor TSQLParser.Destroy;
var
ColumnsList: TList;
begin
// Free the lists inside the dictionary
for ColumnsList in FPrimaryKeys.Values do
ColumnsList.Free;
FPrimaryKeys.Free;
FTables.Free;
FColumns.Free;
inherited;
end;
procedure TSQLParser.Parse;
begin
ParseFromClause;
ParseSelectClause;
end;
function TSQLParser.IsClauseKeyword(const Token: string): Boolean;
const
ClauseKeywords: array[0..10] of string = ('WHERE', 'GROUP BY', 'HAVING', 'ORDER BY', 'LIMIT', 'OFFSET', 'UNION', 'INTERSECT', 'EXCEPT', 'EXISTS', ';');
var
Keyword: string;
begin
Result := False;
for Keyword in ClauseKeywords do
begin
if SameText(Token, Keyword) then
Exit(True);
end;
end;
procedure TSQLParser.ParseFromClause;
var
FromMatch, NextClauseMatch: TMatch;
FromPos, NextClausePos: Integer;
FromClause: string;
Tokens: TArray;
i: Integer;
Token: string;
TableInfo: TTableInfo;
NextToken: string;
RemainingText: string;
begin
// Find position of FROM
FromMatch := TRegEx.Match(FSQLText, '\bFROM\b', [roIgnoreCase]);
if not FromMatch.Success then Exit; // No FROM clause found
FromPos := FromMatch.Index + FromMatch.Length;
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
// Extract the text after FROM
RemainingText := Copy(FSQLText, FromPos, MaxInt);
// Find position of the next clause (e.g., WHERE, GROUP BY, etc.) in RemainingText
NextClauseMatch := TRegEx.Match(RemainingText, '\b(WHERE|GROUP BY|HAVING|ORDER BY|LIMIT|OFFSET|UNION|INTERSECT|EXCEPT|EXISTS|;)\b', [roIgnoreCase]);
if NextClauseMatch.Success then
NextClausePos := NextClauseMatch.Index
else
NextClausePos := Length(RemainingText) + 1; // No next clause found
// Split the FROM clause into tokens using spaces and commas
Tokens := TRegEx.Split(FromClause, '\s+|,'); // Split on spaces or commas
i := 0;
while i < Length(Tokens) do
begin
Token := Trim(Tokens[i]);
if Token = '' then
begin
Inc(i);
Continue;
end;
code
if IsJoinType(Token) or SameText(Token, 'JOIN') then
begin
// Skip join type and 'JOIN' keyword
Inc(i);
Continue;
end
else if SameText(Token, 'ON') then
begin
// Skip tokens until the next 'JOIN' or clause keyword
Inc(i);
while (i < Length(Tokens)) and not (IsJoinType(Tokens[i]) or SameText(Tokens[i], 'JOIN') or IsClauseKeyword(Tokens[i])) do
Inc(i);
Continue;
end
else if SameText(Token, 'USING') then
begin
// Skip tokens within 'USING (...)'
Inc(i);
while (i < Length(Tokens)) and not (Tokens[i].Contains(')')) do
Inc(i);
Inc(i); // Skip the token with ')'
Continue;
end
else
begin
// Process table name (may include schema)
TableInfo := TTableInfo.Create;
// Check for schema.table format
if Pos('.', Token) > 0 then
begin
TableInfo.SchemaName := Copy(Token, 1, Pos('.', Token) - 1);
TableInfo.TableName := Copy(Token, Pos('.', Token) + 1, Length(Token));
end
else
begin
TableInfo.TableName := Token;
end;
// Check for alias
if (i + 1) < Length(Tokens) then
begin
NextToken := Tokens[i + 1];
if SameText(NextToken, 'AS') then
begin
// Alias after 'AS' keyword
if (i + 2) < Length(Tokens) then
begin
TableInfo.Alias := Tokens[i + 2];
i := i + 2; // skip 'AS' and the alias
end
else
begin
// 'AS' without alias - skip 'AS'
i := i + 1;
end;
end
else if not (IsJoinType(NextToken) or SameText(NextToken, 'JOIN') or SameText(NextToken, 'ON')
or SameText(NextToken, 'USING') or IsClauseKeyword(NextToken)) then
begin
// Alias without 'AS'
TableInfo.Alias := NextToken;
Inc(i); // move to the alias
end;
end;
FTables.Add(TableInfo);
Inc(i); // move to the next token after processing
end;
end;
end;
procedure TSQLParser.ParseSelectClause;
var
SelectMatch, FromMatch: TMatch;
SelectClause: string;
ColumnsStr: string;
ColumnList: TArray;
ColumnStr: string;
ColumnInfo: TColumnInfo;
Regex: TRegEx;
Match: TMatch;
TableAliasOrName: string;
ColumnName: string;
TableInfo: TTableInfo;
ReservedWords: array of string;
Tokens: TArray;
i: Integer;
SelectPos, FromPos: Integer;
SelectLength: Integer;
begin
// Find position of SELECT and FROM
SelectMatch := TRegEx.Match(FSQLText, '\bSELECT\b', [roIgnoreCase]);
FromMatch := TRegEx.Match(FSQLText, '\bFROM\b', [roIgnoreCase]);
if (not SelectMatch.Success) or (not FromMatch.Success) then Exit;
// Extract the SELECT clause content
SelectLength := FromPos - SelectPos;
if SelectLength <= 0 then Exit;
SelectClause := Copy(FSQLText, SelectPos + 1, SelectLength - 1);
SelectClause := Trim(SelectClause);
// List of reserved words that may appear after SELECT
ReservedWords := ['ALL', 'DISTINCT', 'DISTINCTROW', 'HIGH_PRIORITY', 'STRAIGHT_JOIN',
'SQL_SMALL_RESULT', 'SQL_BIG_RESULT', 'SQL_BUFFER_RESULT', 'SQL_CACHE',
'SQL_NO_CACHE', 'SQL_CALC_FOUND_ROWS'];
// Split the SelectClause into tokens to check for reserved words at the beginning
Tokens := SelectClause.Split([' '], TStringSplitOptions.ExcludeEmpty);
i := 0;
// Skip reserved words at the beginning
while (i < Length(Tokens)) and IsReservedWord(Tokens[i], ReservedWords) do
begin
Inc(i);
end;
// Reconstruct ColumnsStr without leading reserved words
ColumnsStr := String.Join(' ', Tokens, i, Length(Tokens) - i);
// Split columns by comma, taking care not to split inside parentheses or quotes
ColumnList := TRegEx.Split(ColumnsStr, ',(?![^()]*))');
for i := 0 to Length(ColumnList) - 1 do
begin
ColumnStr := Trim(ColumnList[i]);
code
// Regular expression to extract column expressions and aliases
Regex := TRegEx.Create('^(.*?)(?:\s+AS\s+|\s+)(\w+)$', [roIgnoreCase]);
Match := Regex.Match(ColumnStr);
ColumnInfo := TColumnInfo.Create; // Create the ColumnInfo object
if Match.Success then
begin
ColumnInfo.FullName := Match.Groups[1].Value.Trim;
ColumnInfo.QueryColumnAlias := Match.Groups[2].Value.Trim;
end
else
begin
ColumnInfo.FullName := ColumnStr;
ColumnInfo.QueryColumnAlias := '';
end;
// Determine if the column is an expression
if IsExpression(ColumnInfo.FullName) then
begin
// It's an expression
ColumnInfo.RealSchemaName := '[none]';
ColumnInfo.RealTableName := '[none]';
ColumnInfo.ReferencedTableColumn := '[none]';
end
else
begin
// It's a simple column reference
if Pos('.', ColumnInfo.FullName) > 0 then
begin
// Parse table alias/name and column name
TableAliasOrName := Copy(ColumnInfo.FullName, 1, LastDelimiter('.', ColumnInfo.FullName) - 1);
ColumnName := Copy(ColumnInfo.FullName, LastDelimiter('.', ColumnInfo.FullName) + 1, Length(ColumnInfo.FullName));
// Get the table info for this alias or name
TableInfo := GetTableInfoByAliasOrName(TableAliasOrName);
if Assigned(TableInfo) then
begin
ColumnInfo.RealSchemaName := TableInfo.SchemaName;
ColumnInfo.RealTableName := TableInfo.TableName;
end
else
begin
ColumnInfo.RealSchemaName := '';
ColumnInfo.RealTableName := '';
end;
ColumnInfo.ReferencedTableColumn := ColumnName;
end
else
begin
// Column without table alias/name prefix
ColumnInfo.RealSchemaName := '';
ColumnInfo.ReferencedTableColumn := ColumnInfo.FullName;
if FTables.Count = 1 then
begin
// Assign the only table's name
ColumnInfo.RealTableName := FTables[0].TableName;
ColumnInfo.RealSchemaName := FTables[0].SchemaName;
end
else
begin
// No table information available
ColumnInfo.RealTableName := '';
end;
end;
end;
FColumns.Add(ColumnInfo);
end;
end;
function TSQLParser.IsReservedWord(const Token: string; const ReservedWords: array of string): Boolean;
var
Word: string;
begin
for Word in ReservedWords do
begin
if SameText(Token, Word) then
Exit(True);
end;
Result := False;
end;
function TSQLParser.IsExpression(const S: string): Boolean;
begin
// Simple heuristic to determine if the string is an expression
Result := S.Contains('(') or
S.Contains('+') or
S.Contains('-') or
S.Contains('*') or
S.Contains('/') or
(S.Contains(' ') and not S.Contains('.'));
end;
function TSQLParser.GetTableInfoByAliasOrName(const AliasOrName: string): TTableInfo;
var
TableInfo: TTableInfo;
begin
Result := nil;
for TableInfo in FTables do
begin
if SameText(TableInfo.Alias, AliasOrName) then
begin
Result := TableInfo;
Exit;
end
else if (TableInfo.Alias = '') and SameText(TableInfo.TableName, AliasOrName) then
begin
Result := TableInfo;
Exit;
end;
end;
end;
function TSQLParser.IsJoinType(const Token: string): Boolean;
const
JoinTypes: array[0..7] of string = ('JOIN', 'INNER', 'LEFT', 'RIGHT', 'FULL', 'CROSS', 'NATURAL', 'OUTER');
var
JoinType: string;
begin
Result := False;
if Token = '' then Exit;
for JoinType in JoinTypes do
begin
if SameText(Token, JoinType) then
Exit(True);
end;
end;
procedure TSQLParser.FillPrimaryKeyDictionary;
var
Query: TUniQuery;
TableName, ColumnName, SchemaName: string;
ColumnsList: TList;
begin
if not Assigned(FConnection) then
Exit; // Cannot fill without a database connection
Query := TUniQuery.Create(nil);
try
Query.Connection := FConnection;
// Use the schema name from the connection if not specified
SchemaName := FConnection.Database;
code
Query.SQL.Text := 'SELECT TABLE_NAME, COLUMN_NAME FROM information_schema.KEY_COLUMN_USAGE ' +
'WHERE TABLE_SCHEMA = :SchemaName AND CONSTRAINT_NAME = ''PRIMARY'' ' +
'ORDER BY TABLE_NAME, ORDINAL_POSITION';
Query.ParamByName('SchemaName').AsString := SchemaName;
Query.Open;
while not Query.Eof do
begin
TableName := LowerCase(Query.FieldByName('TABLE_NAME').AsString);
ColumnName := LowerCase(Query.FieldByName('COLUMN_NAME').AsString);
if not FPrimaryKeys.TryGetValue(TableName, ColumnsList) then
begin
ColumnsList := TList<string>.Create;
FPrimaryKeys.Add(TableName, ColumnsList);
end;
ColumnsList.Add(ColumnName);
Query.Next;
end;
Query.Close;
finally
Query.Free;
end;
end;
procedure TSQLParser.CheckPrimaryKeys;
var
ColumnInfo: TColumnInfo;
TableName: string;
ColumnName: string;
ColumnsList: TList;
begin
for ColumnInfo in FColumns do
begin
if (ColumnInfo.RealTableName <> '') and (ColumnInfo.RealTableName <> '[none]') and
(ColumnInfo.ReferencedTableColumn <> '') and (ColumnInfo.ReferencedTableColumn <> '[none]') then
begin
TableName := LowerCase(ColumnInfo.RealTableName);
ColumnName := LowerCase(ColumnInfo.ReferencedTableColumn);
code
if FPrimaryKeys.TryGetValue(TableName, ColumnsList) then
begin
ColumnInfo.IsPKColumn := ColumnsList.Contains(ColumnName);
end
else
begin
// Table not found in primary keys, so column is not a primary key
ColumnInfo.IsPKColumn := False;
end;
end
else
begin
// For expressions or columns without table reference
ColumnInfo.IsPKColumn := False;
end;
end;
end;
end.
Enhance the Delphi unit above, so if there is no defined tablename and there is 2 or more tables on the SELECT query, get column info from all the tables in the SELECT query, so with such info you can correctly populate the TableName.
Sure, I can help enhance the Delphi unit to address your requirement. We need to modify the TSQLParser class so that when there are multiple tables in the SELECT query and a column doesn't have a table alias or name, we can determine which table the column belongs to by checking the columns of all the tables.
Here's how we'll proceed:
Add a new dictionary to cache table columns: We'll create a new dictionary FTableColumns that will store the column names for each table involved in the query.
Fill FTableColumns after parsing the FROM clause: We'll populate FTableColumns by querying the database and retrieving the column names for each table in FTables.
Modify ParseSelectClause to assign RealTableName when possible: When parsing columns without table names, we'll check FTableColumns to see if the column exists uniquely in one of the tables. If it does, we'll assign the RealTableName and RealSchemaName to ColumnInfo.
Handle ambiguous columns: If a column exists in multiple tables, we'll need to handle this ambiguity appropriately.
Now, let's implement these steps in the code.
Step 1: Add a new dictionary FTableColumns to TSQLParser
Step 2: Create and fill FTableColumns after parsing FROM clause
In the constructor, after parsing and before checking primary keys, we'll fill FTableColumns:
delphi
constructor TSQLParser.Create(const SQLText: string; AConnection: TUniConnection);
begin
inherited Create;
FSQLText := SQLText;
FConnection := AConnection; // Store the provided database connection
FTables := TObjectList<TTableInfo>.Create(True); // Owns objects
FColumns := TObjectList<TColumnInfo>.Create(True); // Owns objects
FPrimaryKeys := TDictionary<string, TList<string>>.Create;
FTableColumns := TDictionary<string, TList<string>>.Create; // Initialize the new dictionary
Parse;
FillTableColumnsDictionary; // Fill table columns after parsing
FillPrimaryKeyDictionary; // After parsing, fill the primary key dictionary
CheckPrimaryKeys; // Then check for primary key columns using the cache
end;
Now, implement the FillTableColumnsDictionary procedure:
delphi
procedure TSQLParser.FillTableColumnsDictionary;
var
TableInfo: TTableInfo;
Query: TUniQuery;
TableName, ColumnName, SchemaName: string;
ColumnsList: TList<string>;
begin
if not Assigned(FConnection) then
Exit; // Cannot fill without a database connection
Query := TUniQuery.Create(nil);
try
Query.Connection := FConnection;
for TableInfo in FTables do
begin
// Use the table's schema name if available
if TableInfo.SchemaName <> '' then
SchemaName := TableInfo.SchemaName
else
// Default to the connection's database if schema is not specified
SchemaName := FConnection.Database;
TableName := TableInfo.TableName;
// Prepare the query to fetch columns for the table
Query.SQL.Text := 'SELECT COLUMN_NAME FROM information_schema.COLUMNS ' +
'WHERE TABLE_SCHEMA = :SchemaName AND TABLE_NAME = :TableName';
Query.ParamByName('SchemaName').AsString := SchemaName;
Query.ParamByName('TableName').AsString := TableName;
Query.Open;
ColumnsList := TList<string>.Create;
while not Query.Eof do
begin
ColumnName := LowerCase(Query.FieldByName('COLUMN_NAME').AsString);
ColumnsList.Add(ColumnName);
Query.Next;
end;
// Use the lowercased table name as the key
FTableColumns.Add(LowerCase(TableName), ColumnsList);
Query.Close;
end;
finally
Query.Free;
end;
end;
Step 3: Modify ParseSelectClause to assign RealTableName when possible
Update the ParseSelectClause procedure:
delphi
procedure TSQLParser.ParseSelectClause;
var
//... [existing variables]
MatchedTables: TList<string>; // New list to hold matched tables
//...
begin
//... [existing code]
for i := 0 to Length(ColumnList) - 1 do
begin
ColumnStr := Trim(ColumnList[i]);
// Regular expression to extract column expressions and aliases
Regex := TRegEx.Create('^(.*?)(?:\s+AS\s+|\s+)(\w+)$', [roIgnoreCase]);
Match := Regex.Match(ColumnStr);
ColumnInfo := TColumnInfo.Create; // Create the ColumnInfo object
if Match.Success then
begin
ColumnInfo.FullName := Match.Groups[1].Value.Trim;
ColumnInfo.QueryColumnAlias := Match.Groups[2].Value.Trim;
end
else
begin
ColumnInfo.FullName := ColumnStr;
ColumnInfo.QueryColumnAlias := '';
end;
// Determine if the column is an expression
if IsExpression(ColumnInfo.FullName) then
begin
// It's an expression
ColumnInfo.RealSchemaName := '[none]';
ColumnInfo.RealTableName := '[none]';
ColumnInfo.ReferencedTableColumn := '[none]';
end
else
begin
// It's a simple column reference
if Pos('.', ColumnInfo.FullName) > 0 then
begin
//... [existing code for columns with table alias]
end
else
begin
// Column without table alias/name prefix
ColumnInfo.RealSchemaName := '';
ColumnInfo.ReferencedTableColumn := ColumnInfo.FullName;
if FTables.Count = 1 then
begin
// Assign the only table's name
ColumnInfo.RealTableName := FTables[0].TableName;
ColumnInfo.RealSchemaName := FTables[0].SchemaName;
end
else
begin
// Attempt to find the matching table(s) for the column
MatchedTables := TList<string>.Create;
try
for TableName in FTableColumns.Keys do
begin
if FTableColumns[TableName].Contains(LowerCase(ColumnInfo.FullName)) then
MatchedTables.Add(TableName);
end;
if MatchedTables.Count = 1 then
begin
// Unique match found
ColumnInfo.RealTableName := MatchedTables[0];
ColumnInfo.RealSchemaName := ''; // You can set this if schema info is available
end
else if MatchedTables.Count > 1 then
begin
// Ambiguous column, exists in multiple tables
ColumnInfo.RealTableName := '[ambiguous]';
end
else
begin
// Column not found in any table
ColumnInfo.RealTableName := '[unknown]';
end;
finally
MatchedTables.Free;
end;
end;
end;
end;
FColumns.Add(ColumnInfo);
end;
end;
Step 4: Handle ambiguous columns appropriately
In the code above, if a column is found in multiple tables, we set RealTableName to '[ambiguous]'. You may want to adjust this according to your needs, possibly raising an exception or logging a warning.
Complete Modified Code
Here's the full modified code with all the changes:
delphi
Notes:
Error Handling: You might want to add error handling to manage situations where columns are ambiguous or missing.
Performance Considerations: Fetching table columns for every table can be resource-intensive for large schemas. Consider caching table columns or optimizing queries if performance becomes an issue.
Schema Handling: The code assumes that the tables are in the database specified by the connection. If your tables are in different schemas or databases, you'll need to adjust the schema handling accordingly.
Testing the Changes
To verify that the enhancement works correctly, you should test the parser with SQL queries that include:
Multiple tables in the FROM clause.
Columns without table aliases.
Columns that exist in only one of the tables.
Columns that exist in multiple tables (to check for ambiguity handling).
For example:
sql
SELECT id, name, orders.total FROM customers c
JOIN orders o ON c.id = o.customer_id
In this query:
id could refer to customers.id or orders.id. Since id exists in both tables, it should be marked as ambiguous.
name likely exists only in customers.
orders.total has an explicit table alias.
Conclusion
By adding the FTableColumns dictionary and modifying the ParseSelectClause procedure, we've enhanced the TSQLParser to correctly populate RealTableName for columns without explicit table names when multiple tables are present in the SELECT query.
Please let me know if you need further assistance or adjustments to the code.
Not yet included in a published release.
unit SQLParser;
interface
uses
System.Classes, System.SysUtils, System.Generics.Collections, System.RegularExpressions, Uni;
type
TColumnInfo = class
public
FullName: string;
RealSchemaName: string;
RealTableName: string;
ReferencedTableColumn: string;
QueryColumnAlias: string;
IsPKColumn: Boolean; // Indicates if it's a primary key column
end;
TTableInfo = class
public
SchemaName: string;
TableName: string;
Alias: string;
end;
TSQLParser = class
private
FSQLText: string;
FTables: TObjectList<TTableInfo>;
FColumns: TObjectList<TColumnInfo>;
FConnection: TUniConnection;
FPrimaryKeys: TDictionary<string, TList<string>>;
FTableColumns: TDictionary<string, TList<string>>;
procedure Parse;
procedure ParseFromClause;
procedure ParseSelectClause;
function IsExpression(const S: string): Boolean;
function IsClauseKeyword(const Token: string): Boolean;
function GetTableInfoByAliasOrName(const AliasOrName: string): TTableInfo;
function IsJoinType(const Token: string): Boolean;
procedure CheckPrimaryKeys; // Checks if columns are primary keys
procedure FillPrimaryKeyDictionary; // Fills the primary key dictionary
procedure FillTableColumnsDictionary; // Fills the table columns dictionary
function IsReservedWord(const Token: string; const ReservedWords: array of string): Boolean;
public
constructor Create(const SQLText: string; AConnection: TUniConnection);
destructor Destroy; override;
property Tables: TObjectList<TTableInfo> read FTables;
property Columns: TObjectList<TColumnInfo> read FColumns;
end;
implementation
constructor TSQLParser.Create(const SQLText: string; AConnection: TUniConnection);
begin
inherited Create;
FSQLText := SQLText;
FConnection := AConnection; // Store the provided database connection
FTables := TObjectList<TTableInfo>.Create(True); // Owns objects
FColumns := TObjectList<TColumnInfo>.Create(True); // Owns objects
FPrimaryKeys := TDictionary<string, TList<string>>.Create;
FTableColumns := TDictionary<string, TList<string>>.Create; // Initialize the new dictionary
Parse;
FillTableColumnsDictionary; // Fill table columns after parsing
FillPrimaryKeyDictionary; // After parsing, fill the primary key dictionary
CheckPrimaryKeys; // Then check for primary key columns using the cache
end;
destructor TSQLParser.Destroy;
var
ColumnsList: TList<string>;
begin
// Free the lists inside the dictionaries
for ColumnsList in FPrimaryKeys.Values do
ColumnsList.Free;
FPrimaryKeys.Free;
for ColumnsList in FTableColumns.Values do
ColumnsList.Free;
FTableColumns.Free;
FTables.Free;
FColumns.Free;
inherited;
end;
procedure TSQLParser.Parse;
begin
ParseFromClause;
ParseSelectClause;
end;
function TSQLParser.IsClauseKeyword(const Token: string): Boolean;
const
ClauseKeywords: array[0..10] of string = ('WHERE', 'GROUP BY', 'HAVING', 'ORDER BY', 'LIMIT', 'OFFSET', 'UNION', 'INTERSECT', 'EXCEPT', 'EXISTS', ';');
var
Keyword: string;
begin
Result := False;
for Keyword in ClauseKeywords do
begin
if SameText(Token, Keyword) then
Exit(True);
end;
end;
procedure TSQLParser.ParseFromClause;
// [Existing code unchanged]
begin
// ... [existing ParseFromClause code]
end;
procedure TSQLParser.ParseSelectClause;
var
SelectMatch, FromMatch: TMatch;
SelectClause: string;
ColumnsStr: string;
ColumnList: TArray<string>;
ColumnStr: string;
ColumnInfo: TColumnInfo;
Regex: TRegEx;
Match: TMatch;
TableAliasOrName: string;
ColumnName: string;
TableInfo: TTableInfo;
ReservedWords: array of string;
Tokens: TArray<string>;
i: Integer;
SelectPos, FromPos: Integer;
SelectLength: Integer;
MatchedTables: TList<string>;
begin
// Find position of SELECT and FROM
SelectMatch := TRegEx.Match(FSQLText, '\bSELECT\b', [roIgnoreCase]);
FromMatch := TRegEx.Match(FSQLText, '\bFROM\b', [roIgnoreCase]);
if (not SelectMatch.Success) or (not FromMatch.Success) then Exit;
SelectPos := SelectMatch.Index + SelectMatch.Length;
FromPos := FromMatch.Index;
// Extract the SELECT clause content
SelectLength := FromPos - SelectPos;
if SelectLength <= 0 then Exit;
SelectClause := Copy(FSQLText, SelectPos + 1, SelectLength - 1);
SelectClause := Trim(SelectClause);
// List of reserved words that may appear after SELECT
ReservedWords := ['ALL', 'DISTINCT', 'DISTINCTROW', 'HIGH_PRIORITY', 'STRAIGHT_JOIN',
'SQL_SMALL_RESULT', 'SQL_BIG_RESULT', 'SQL_BUFFER_RESULT', 'SQL_CACHE',
'SQL_NO_CACHE', 'SQL_CALC_FOUND_ROWS'];
// Split the SelectClause into tokens to check for reserved words at the beginning
Tokens := SelectClause.Split([' '], TStringSplitOptions.ExcludeEmpty);
i := 0;
// Skip reserved words at the beginning
while (i < Length(Tokens)) and IsReservedWord(Tokens[i], ReservedWords) do
begin
Inc(i);
end;
// Reconstruct ColumnsStr without leading reserved words
ColumnsStr := String.Join(' ', Tokens, i, Length(Tokens) - i);
// Split columns by comma, taking care not to split inside parentheses or quotes
ColumnList := TRegEx.Split(ColumnsStr, ',(?![^()]*\))');
for i := 0 to Length(ColumnList) - 1 do
begin
ColumnStr := Trim(ColumnList[i]);
// Regular expression to extract column expressions and aliases
Regex := TRegEx.Create('^(.*?)(?:\s+AS\s+|\s+)(\w+)$', [roIgnoreCase]);
Match := Regex.Match(ColumnStr);
ColumnInfo := TColumnInfo.Create; // Create the ColumnInfo object
if Match.Success then
begin
ColumnInfo.FullName := Match.Groups[1].Value.Trim;
ColumnInfo.QueryColumnAlias := Match.Groups[2].Value.Trim;
end
else
begin
ColumnInfo.FullName := ColumnStr;
ColumnInfo.QueryColumnAlias := '';
end;
// Determine if the column is an expression
if IsExpression(ColumnInfo.FullName) then
begin
// It's an expression
ColumnInfo.RealSchemaName := '[none]';
ColumnInfo.RealTableName := '[none]';
ColumnInfo.ReferencedTableColumn := '[none]';
end
else
begin
// It's a simple column reference
if Pos('.', ColumnInfo.FullName) > 0 then
begin
// Parse table alias/name and column name
TableAliasOrName := Copy(ColumnInfo.FullName, 1, LastDelimiter('.', ColumnInfo.FullName) - 1);
ColumnName := Copy(ColumnInfo.FullName, LastDelimiter('.', ColumnInfo.FullName) + 1, Length(ColumnInfo.FullName));
// Get the table info for this alias or name
TableInfo := GetTableInfoByAliasOrName(TableAliasOrName);
if Assigned(TableInfo) then
begin
ColumnInfo.RealSchemaName := TableInfo.SchemaName;
ColumnInfo.RealTableName := TableInfo.TableName;
end
else
begin
ColumnInfo.RealSchemaName := '';
ColumnInfo.RealTableName := '';
end;
ColumnInfo.ReferencedTableColumn := ColumnName;
end
else
begin
// Column without table alias/name prefix
ColumnInfo.RealSchemaName := '';
ColumnInfo.ReferencedTableColumn := ColumnInfo.FullName;
if FTables.Count = 1 then
begin
// Assign the only table's name
ColumnInfo.RealTableName := FTables[0].TableName;
ColumnInfo.RealSchemaName := FTables[0].SchemaName;
end
else
begin
// Attempt to find the matching table(s) for the column
MatchedTables := TList<string>.Create;
try
for TableName in FTableColumns.Keys do
begin
if FTableColumns[TableName].Contains(LowerCase(ColumnInfo.FullName)) then
MatchedTables.Add(TableName);
end;
if MatchedTables.Count = 1 then
begin
// Unique match found
ColumnInfo.RealTableName := MatchedTables[0];
ColumnInfo.RealSchemaName := ''; // You can set this if schema info is available
end
else if MatchedTables.Count > 1 then
begin
// Ambiguous column, exists in multiple tables
ColumnInfo.RealTableName := '[ambiguous]';
end
else
begin
// Column not found in any table
ColumnInfo.RealTableName := '[unknown]';
end;
finally
MatchedTables.Free;
end;
end;
end;
end;
FColumns.Add(ColumnInfo);
end;
end;
function TSQLParser.IsReservedWord(const Token: string; const ReservedWords: array of string): Boolean;
var
Word: string;
begin
for Word in ReservedWords do
begin
if SameText(Token, Word) then
Exit(True);
end;
Result := False;
end;
function TSQLParser.IsExpression(const S: string): Boolean;
begin
// Simple heuristic to determine if the string is an expression
Result := S.Contains('(') or
S.Contains('+') or
S.Contains('-') or
S.Contains('*') or
S.Contains('/') or
(S.Contains(' ') and not S.Contains('.'));
end;
function TSQLParser.GetTableInfoByAliasOrName(const AliasOrName: string): TTableInfo;
var
TableInfo: TTableInfo;
begin
Result := nil;
for TableInfo in FTables do
begin
if SameText(TableInfo.Alias, AliasOrName) then
begin
Result := TableInfo;
Exit;
end
else if (TableInfo.Alias = '') and SameText(TableInfo.TableName, AliasOrName) then
begin
Result := TableInfo;
Exit;
end;
end;
end;
function TSQLParser.IsJoinType(const Token: string): Boolean;
const
JoinTypes: array[0..7] of string = ('JOIN', 'INNER', 'LEFT', 'RIGHT', 'FULL', 'CROSS', 'NATURAL', 'OUTER');
var
JoinType: string;
begin
Result := False;
if Token = '' then Exit;
for JoinType in JoinTypes do
begin
if SameText(Token, JoinType) then
Exit(True);
end;
end;
procedure TSQLParser.FillTableColumnsDictionary;
var
TableInfo: TTableInfo;
Query: TUniQuery;
TableName, ColumnName, SchemaName: string;
ColumnsList: TList<string>;
begin
if not Assigned(FConnection) then
Exit; // Cannot fill without a database connection
Query := TUniQuery.Create(nil);
try
Query.Connection := FConnection;
for TableInfo in FTables do
begin
// Use the table's schema name if available
if TableInfo.SchemaName <> '' then
SchemaName := TableInfo.SchemaName
else
// Default to the connection's database if schema is not specified
SchemaName := FConnection.Database;
TableName := TableInfo.TableName;
// Prepare the query to fetch columns for the table
Query.SQL.Text := 'SELECT COLUMN_NAME FROM information_schema.COLUMNS ' +
'WHERE TABLE_SCHEMA = :SchemaName AND TABLE_NAME = :TableName';
Query.ParamByName('SchemaName').AsString := SchemaName;
Query.ParamByName('TableName').AsString := TableName;
Query.Open;
ColumnsList := TList<string>.Create;
while not Query.Eof do
begin
ColumnName := LowerCase(Query.FieldByName('COLUMN_NAME').AsString);
ColumnsList.Add(ColumnName);
Query.Next;
end;
// Use the lowercased table name as the key
FTableColumns.Add(LowerCase(TableName), ColumnsList);
Query.Close;
end;
finally
Query.Free;
end;
end;
procedure TSQLParser.FillPrimaryKeyDictionary;
// [Existing code unchanged]
begin
// ... [existing FillPrimaryKeyDictionary code]
end;
procedure TSQLParser.CheckPrimaryKeys;
// [Existing code unchanged]
begin
// ... [existing CheckPrimaryKeys code]
end;
end.