To enhance the ORM_GenerateDelphiClassFromQuery unit to fully utilize the capabilities of the custom attributes defined in ORM_CustomAttributes, we'll expand the GenerateDelphiClassFromQuery function. This enhancement involves:
-
Incorporating Additional Attributes:
GridColumnDisplay: Specifies grid display properties such as column index, caption, width, and alignment.
EnumValues: Defines allowed enumeration values for a field.
Translation: Maps stored values to display values.
ReadOnlyCol: Marks a field as read-only in grids and UI.
FmtProp: Sets formatting properties based on the field type.
-
Inferring Attribute Parameters:
- Grid Properties: Assign default values based on the column order or allow customization via column comments or metadata.
- Enum and Translation: Detect if a column should have enum values or translations based on metadata or naming conventions.
-
Extending Metadata Handling:
- Ensure that
TSQLColumnInfo includes necessary metadata such as descriptions, constraints, or custom annotations that can be used to infer the additional attributes.
Below is the enhanced ORM_GenerateDelphiClassFromQuery unit with the improved GenerateDelphiClassFromQuery function:
unit ORM_GenerateDelphiClassFromQuery;
interface
uses
Uni, DB, System.Generics.Collections, SysUtils, System.Character,
System.Classes, SQLParserEnhanced;
function GenerateDelphiClassFromQuery(const SQLText: string; const ClassName: string; AConnection: TUniConnection): string;
implementation
function GenerateDelphiClassFromQuery(const SQLText: string; const ClassName: string; AConnection: TUniConnection): string;
var
Parser: TSQLParser;
ColumnInfo: TSQLColumnInfo;
i, PropIndex: Integer;
FieldsList: TObjectList<TSQLColumnInfo>;
AttrsLine: string;
FieldName, PropertyName: string;
NullableDelphiType, WriteMethod: string;
ResultList: TStringList;
AttributesList: TList<string>;
GridColumnIndex: Integer;
GridCaption: string;
GridWidth: Integer;
GridAlignment: TAlignment;
HasGridAlignment: Boolean;
EnumValuesStr: string;
TranslationStr: string;
ReadOnly: Boolean;
// Helper functions
function CreateValidIdentifier(const Name: string): string;
var
i: Integer;
c: Char;
begin
Result := '';
for i := 1 to Length(Name) do
begin
c := Name[i];
if c.IsLetterOrDigit or (c = '_') then
Result := Result + c
else
Result := Result + '_'; // Replace invalid chars with '_'
end;
// Ensure the identifier does not start with a digit or invalid character
if (Result = '') or not (Result[1].IsLetter or (Result[1] = '_')) then
Result := '_' + Result;
end;
function GetDelphiTypeMapping(FieldType: TFieldType; out NullableDelphiType, WriteMethod: string): Boolean;
begin
Result := True;
case FieldType of
TFieldType.ftString, TFieldType.ftWideString, TFieldType.ftMemo, TFieldType.ftWideMemo:
begin
NullableDelphiType := 'TNullable<String>';
WriteMethod := 'Set_Info_NullableString';
end;
TFieldType.ftInteger, TFieldType.ftByte, TFieldType.ftSmallint, TFieldType.ftWord, TFieldType.ftLongWord, TFieldType.ftShortint:
begin
NullableDelphiType := 'TNullable<Integer>';
WriteMethod := 'Set_Info_NullableInteger';
end;
TFieldType.ftLargeint:
begin
NullableDelphiType := 'TNullable<Int64>';
WriteMethod := 'Set_Info_NullableInt64';
end;
TFieldType.ftSingle, TFieldType.ftFloat, TFieldType.ftExtended:
begin
NullableDelphiType := 'TNullable<Extended>';
WriteMethod := 'Set_Info_NullableExtended';
end;
TFieldType.ftDate, TFieldType.ftTime, TFieldType.ftDateTime, TFieldType.ftTimeStamp:
begin
NullableDelphiType := 'TNullable<TDateTime>';
WriteMethod := 'Set_Info_NullableDateTime';
end;
TFieldType.ftCurrency:
begin
NullableDelphiType := 'TNullable<Currency>';
WriteMethod := 'Set_Info_NullableCurrency';
end;
TFieldType.ftFMTBcd, TFieldType.ftBCD:
begin
NullableDelphiType := 'TNullable<Currency>';
WriteMethod := 'Set_Info_NullableCurrency';
end;
TFieldType.ftBoolean:
begin
NullableDelphiType := 'TNullable<Boolean>';
WriteMethod := 'Set_Info_NullableBoolean';
end;
else
begin
NullableDelphiType := 'TNullable<Variant>';
WriteMethod := 'Set_Info_NullableVariant';
Result := False;
end;
end;
end;
begin
ResultList := TStringList.Create;
try
ResultList.Add('type');
ResultList.Add(Format(' T%s = class(TQryRecInfo)', [ClassName]));
ResultList.Add(' private');
Parser := TSQLParser.Create(SQLText, AConnection);
try
FieldsList := Parser.Columns; // Get the columns info
// Iterate over the columns to generate field declarations
for i := 0 to FieldsList.Count - 1 do
begin
ColumnInfo := FieldsList[i];
if not GetDelphiTypeMapping(ColumnInfo.DataType, NullableDelphiType, WriteMethod) then
begin
// Handle unsupported type if necessary
end;
// Use QueryColumnAlias as the field name or the column name
if ColumnInfo.QueryColumnAlias <> '' then
FieldName := ColumnInfo.QueryColumnAlias
else
FieldName := ColumnInfo.ReferencedTableColumn;
// Sanitize the FieldName (remove special characters, etc.)
FieldName := CreateValidIdentifier(FieldName);
FieldName := '_' + FieldName; // Prefix with '_'
// Initialize the list of attributes
AttributesList := TList<string>.Create;
try
// [Table('table_name')]
if not ColumnInfo.RealTableName.IsEmpty and
not ((ColumnInfo.RealTableName = '[none]') or
(ColumnInfo.RealTableName = '[ambiguous]') or
(ColumnInfo.RealTableName = '[unknown]')) then
begin
AttributesList.Add(Format('Table(''%s'')', [ColumnInfo.RealTableName]));
end;
// [PrimaryKey]
if ColumnInfo.IsPKColumn then
begin
AttributesList.Add('PrimaryKey');
end;
// [RefColumn('column_name')]
if not ColumnInfo.ReferencedTableColumn.IsEmpty and
(ColumnInfo.ReferencedTableColumn <> '[none]') then
begin
AttributesList.Add(Format('RefColumn(''%s'')', [ColumnInfo.ReferencedTableColumn]));
end;
// [ReadOnlyCol] - assuming primary keys are read-only
ReadOnly := ColumnInfo.IsPKColumn;
if ReadOnly then
begin
AttributesList.Add('ReadOnlyCol');
end;
// [FmtProp(tiType)] - based on the field type
var FormatType: TORMFormatType;
case ColumnInfo.DataType of
TFieldType.ftCurrency, TFieldType.ftFMTBcd, TFieldType.ftBCD:
FormatType := tiMoney;
TFieldType.ftDate, TFieldType.ftTime, TFieldType.ftDateTime, TFieldType.ftTimeStamp:
FormatType := tiDateTime;
TFieldType.ftBoolean:
FormatType := tiBoolean;
TFieldType.ftInteger, TFieldType.ftByte, TFieldType.ftSmallint, TFieldType.ftWord,
TFieldType.ftLargeint, TFieldType.ftLongWord, TFieldType.ftShortint:
FormatType := tiInteger;
TFieldType.ftFloat, TFieldType.ftSingle, TFieldType.ftExtended:
FormatType := tiFloat;
else
FormatType := tiString;
end;
AttributesList.Add(Format('FmtProp(ti%s)', [GetEnumName(TypeInfo(TORMFormatType), Ord(FormatType))]));
// [GridColumnDisplay(index, 'Caption', width, alignment)]
// For simplicity, using the order as index and column name as caption
GridColumnIndex := i; // Zero-based index
GridCaption := ColumnInfo.ColumnName; // Could be improved by using comments or metadata
GridWidth := 100; // Default width, can be customized
GridAlignment := taLeftJustify; // Default alignment
HasGridAlignment := False; // Set to True if alignment is customized
// Example: Detect alignment based on data type
case ColumnInfo.DataType of
TFieldType.ftInteger, TFieldType.ftLargeint, TFieldType.ftFloat,
TFieldType.ftCurrency, TFieldType.ftBoolean:
begin
GridAlignment := taRightJustify;
HasGridAlignment := True;
end;
TFieldType.ftDate, TFieldType.ftTime, TFieldType.ftDateTime, TFieldType.ftTimeStamp:
begin
GridAlignment := taCenter;
HasGridAlignment := True;
end;
else
begin
GridAlignment := taLeftJustify;
HasGridAlignment := False;
end;
end;
if HasGridAlignment then
AttributesList.Add(Format('GridColumnDisplay(%d, ''%s'', %d, ta%s)', [
GridColumnIndex,
GridCaption,
GridWidth,
GetEnumName(TypeInfo(TAlignment), Ord(GridAlignment)).Substring(2) // Remove 'ta' prefix
]))
else
AttributesList.Add(Format('GridColumnDisplay(%d, ''%s'', %d)', [
GridColumnIndex,
GridCaption,
GridWidth
]));
// [EnumValues('a,b,c')]
if ColumnInfo.IsEnum then
begin
// Assuming ColumnInfo.EnumValues is a comma-separated string
EnumValuesStr := ColumnInfo.EnumValues;
if EnumValuesStr <> '' then
begin
AttributesList.Add(Format('EnumValues(''%s'')', [EnumValuesStr]));
end;
end;
// [Translation('a=Active,b=Inactive')]
if ColumnInfo.HasTranslations then
begin
// Assuming ColumnInfo.Translations is a comma-separated "key=value" string
TranslationStr := ColumnInfo.Translations;
if TranslationStr <> '' then
begin
AttributesList.Add(Format('Translation(''%s'')', [TranslationStr]));
end;
end;
// Combine attributes
if AttributesList.Count > 0 then
AttrsLine := Format(' [%s]', [String.Join(', ', AttributesList.ToArray)])
else
AttrsLine := '';
// Add field declaration
if AttrsLine <> '' then
ResultList.Add(Format('%s %s: %s;', [AttrsLine, FieldName, NullableDelphiType]))
else
ResultList.Add(Format(' %s: %s;', [FieldName, NullableDelphiType]));
finally
AttributesList.Free;
end;
end;
// Generate published property declarations
ResultList.Add(' published');
PropIndex := 0;
for i := 0 to FieldsList.Count - 1 do
begin
ColumnInfo := FieldsList[i];
if not GetDelphiTypeMapping(ColumnInfo.DataType, NullableDelphiType, WriteMethod) then
begin
// Handle unsupported type if necessary
end;
// Use the sanitized FieldName (without '_')
if ColumnInfo.QueryColumnAlias <> '' then
FieldName := ColumnInfo.QueryColumnAlias
else
FieldName := ColumnInfo.ReferencedTableColumn;
FieldName := CreateValidIdentifier(FieldName);
// PropertyName is same as FieldName
PropertyName := FieldName;
// FieldName with '_'
FieldName := '_' + FieldName;
// Add property declaration
ResultList.Add(Format(' property %s: %s index %d read %s write %s;', [
PropertyName,
NullableDelphiType,
PropIndex,
FieldName,
WriteMethod
]));
Inc(PropIndex);
end;
ResultList.Add(' end;');
ResultList.Add('');
finally
Parser.Free;
end;
Result := ResultList.Text;
finally
ResultList.Free;
end;
end;
end.
Enhancements Detailed Explanation
-
GridColumnDisplay Attribute:
- Index Assignment: Uses the column order (
i) as the default grid column index.
- Caption Assignment: Utilizes the column name as the default caption. This can be enhanced by extracting comments or descriptions from the database schema if available.
- Width and Alignment: Sets a default width of
100. Alignment is inferred based on the data type:
- Numeric and Boolean Types: Right-justified.
- Date and Time Types: Centered.
- String Types: Left-justified.
-
FmtProp Attribute:
- Determines the format type (
tiMoney, tiDateTime, tiBoolean, etc.) based on the field's data type and assigns the corresponding FmtProp attribute.
-
EnumValues and Translation Attributes:
- EnumValues: If a column is marked as an enum (
ColumnInfo.IsEnum), it adds the EnumValues attribute with the allowed values.
- Translation: If a column has translations (
ColumnInfo.HasTranslations), it adds the Translation attribute with the key-value pairs.
-
ReadOnlyCol Attribute:
- Automatically marks primary key columns as read-only by adding the
ReadOnlyCol attribute.
Assumptions and Requirements
-
Extended TSQLColumnInfo: The TSQLColumnInfo class must have additional properties to support the enhanced attributes:
IsEnum: Boolean: Indicates if the column represents an enumeration.
EnumValues: string: A comma-separated list of allowed enumeration values.
HasTranslations: Boolean: Indicates if the column has translation mappings.
Translations: string: A comma-separated list of key=value pairs for translations.
-
Error Handling: The enhanced generator assumes that the additional metadata (like enum values and translations) is correctly populated. Implement appropriate error handling as needed based on your project's requirements.
-
Customization: For more dynamic and flexible attribute assignments, consider:
- Parsing Column Comments: Extracting metadata from column descriptions or comments in the database.
- Configuration Files: Allowing external configuration files (e.g., JSON or XML) to define attribute properties that are not directly inferable from the database schema.
Example Enhancement Applied
Given the enhanced generator, here's how the previously provided TEmployeeProject class can be automatically generated with the additional attributes:
type
TEmployeeProject = class(TQryRecInfo)
private
[Table('employees'), PrimaryKey, RefColumn('emp_id'), ReadOnlyCol, FmtProp(tiInteger), GridColumnDisplay(0, 'EmployeeID', 100, taLeftJustify)]
_EmployeeID: TNullable<Integer>;
[Table('employees'), PrimaryKey, RefColumn('dept_id'), ReadOnlyCol, FmtProp(tiInteger), GridColumnDisplay(1, 'DepartmentID', 100, taLeftJustify)]
_DepartmentID: TNullable<Integer>;
[Table('employees'), RefColumn('emp_name'), FmtProp(tiString), GridColumnDisplay(2, 'EmployeeName', 100, taLeftJustify)]
_EmployeeName: TNullable<String>;
[Table('projects'), PrimaryKey, RefColumn('proj_id'), ReadOnlyCol, FmtProp(tiInteger), GridColumnDisplay(3, 'ProjectID', 100, taLeftJustify)]
_ProjectID: TNullable<Integer>;
[Table('projects'), RefColumn('proj_name'), FmtProp(tiString), GridColumnDisplay(4, 'ProjectName', 100, taLeftJustify)]
_ProjectName: TNullable<String>;
[Table('projects'), RefColumn('proj_budget'), FmtProp(tiMoney), GridColumnDisplay(5, 'ProjectBudget', 120, taRightJustify)]
_ProjectBudget: TNullable<Currency>;
[FmtProp(tiMoney), GridColumnDisplay(6, 'BudgetLimit', 100, taRightJustify)]
_BudgetLimit: Currency;
[FmtProp(tiInteger), GridColumnDisplay(7, 'Age', 80, taRightJustify)]
_Age: Integer;
[FmtProp(tiBoolean), GridColumnDisplay(8, 'Older', 80, taCenter)]
_Older: Boolean;
[Table('projects'), RefColumn('proj_date'), FmtProp(tiDateTime), GridColumnDisplay(9, 'ProjectDate', 120, taCenter)]
_ProjectDate: TNullable<TDateTime>;
[Table('projects'), RefColumn('proj_starttime'), FmtProp(tiDateTime), GridColumnDisplay(10, 'ProjectStartMoment', 150, taCenter)]
_ProjectStartMoment: TNullable<TDateTime>;
[EnumValues('active,inactive,suspended'), Translation('active=Active,inactive=Inactive,suspended=Suspended'), FmtProp(tiString), GridColumnDisplay(11, 'ProjectStatus', 120, taLeftJustify)]
_ProjectStatus: TNullable<String>;
[EnumValues('X,N,Q'), Translation('X=Cancelled,N=Normal,Q=Blocked'), FmtProp(tiString), GridColumnDisplay(12, 'EmployeeStatus', 120, taLeftJustify)]
_EmployeeStatus: TNullable<String>;
published
property EmployeeID: TNullable<Integer> index 0 read _EmployeeID write SetInfoNullableInteger;
property DepartmentID: TNullable<Integer> index 1 read _DepartmentID write SetInfoNullableInteger;
property EmployeeName: TNullable<String> index 2 read _EmployeeName write SetInfoNullableString;
property ProjectID: TNullable<Integer> index 3 read _ProjectID write SetInfoNullableInteger;
property ProjectName: TNullable<String> index 4 read _ProjectName write SetInfoNullableString;
property ProjectBudget: TNullable<Currency> index 5 read _ProjectBudget write SetInfoNullableCurrency;
property BudgetLimit: Currency index 6 read _BudgetLimit write SetInfoCurrency;
property Age: Integer index 7 read _Age write SetInfoInteger;
property Older: Boolean index 8 read _Older write SetInfoBoolean;
property ProjectDate: TNullable<TDateTime> index 9 read _ProjectDate write SetInfoNullableDateTime;
property ProjectStartMoment: TNullable<TDateTime> index 10 read _ProjectStartMoment write SetInfoNullableDateTime;
property ProjectStatus: TNullable<String> index 11 read _ProjectStatus write SetInfoNullableString;
property EmployeeStatus: TNullable<String> index 12 read _EmployeeStatus write SetInfoNullableString;
end;
Conclusion
By enhancing the GenerateDelphiClassFromQuery function, we've enabled it to leverage the full suite of custom attributes, thereby generating more robust and feature-rich Delphi classes. This allows for better integration with UI components (like grids), data validation through enums and translations, and improved code maintainability.
Ensure that your TSQLParser and TSQLColumnInfo classes are updated to provide the necessary metadata required for these enhancements. Depending on your database schema and conventions, you might need to implement additional logic to accurately infer and assign attribute parameters.