function GenerateDelphiClassFromQuery(const SQLText: string; const ClassName: string;…
- User
function GenerateDelphiClassFromQuery(const SQLText: string; const ClassName: string; AConnection: TUniConnection): string; var Parser: TSQLParser; ColumnInfo: TColumnInfo; i: Integer; FieldsList: TObjectList; AttrsLine: string; FieldName, PropertyName: string; DelphiFieldType: string; ResultList: TStringList; AttributesList: TList;
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;
begin ResultList := TStringList.Create; try ResultList.Add('type'); ResultList.Add(Format(' T%s = class', [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]; // Map TFieldType to Delphi types case ColumnInfo.DataType of ftString, ftWideString: DelphiFieldType := 'string'; ftInteger, ftSmallint, ftShortInt, ftWord, ftByte: DelphiFieldType := 'Integer'; ftLargeint: DelphiFieldType := 'Int64'; ftFloat, ftCurrency, ftBCD, ftFMTBcd: DelphiFieldType := 'Double'; ftDate, ftTime, ftDateTime, ftTimeStamp: DelphiFieldType := 'TDateTime'; ftBoolean: DelphiFieldType := 'Boolean'; else DelphiFieldType := 'Variant'; // Default to Variant for unsupported types 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); // Initialize the list of attributes AttributesList := TList<string>.Create; try // Build custom attributes if not ColumnInfo.RealTableName.IsEmpty and not ((ColumnInfo.RealTableName = '[none]') or (ColumnInfo.RealTableName = '[ambiguous]') or (ColumnInfo.RealTableName = '[unknown]')) then begin // Insert Table attribute AttributesList.Add(Format('Table(''%s'')', [ColumnInfo.RealTableName])); end; if ColumnInfo.IsPKColumn then begin AttributesList.Add('PrimaryKey'); end; if not ColumnInfo.ReferencedTableColumn.IsEmpty and (ColumnInfo.ReferencedTableColumn <> '[none]') then begin AttributesList.Add(Format('RefColumn(''%s'')', [ColumnInfo.ReferencedTableColumn])); 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 F%s: %s;', [AttrsLine, FieldName, DelphiFieldType])) else ResultList.Add(Format(' F%s: %s;', [FieldName, DelphiFieldType])); finally AttributesList.Free; end; end; // Generate public property declarations ResultList.Add(' public'); for i := 0 to FieldsList.Count - 1 do begin ColumnInfo := FieldsList[i]; // Use QueryColumnAlias as the field name or the column name if ColumnInfo.QueryColumnAlias <> '' then FieldName := ColumnInfo.QueryColumnAlias else FieldName := ColumnInfo.ReferencedTableColumn; // Sanitize FieldName FieldName := CreateValidIdentifier(FieldName); // Create property name (could be same as FieldName) PropertyName := FieldName; ResultList.Add(Format(' property %s: %s read F%s write F%s;', [PropertyName, DelphiFieldType, FieldName, FieldName])); end; ResultList.Add(' end;'); ResultList.Add(''); finally Parser.Free; end; Result := ResultList.Text; - Assistant