Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
barnuri committed Nov 27, 2024
1 parent 6e526c8 commit 03e5cd7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions CSharp.OpenSource.LinqToKql/ORMGen/ORMGenaratedModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ public class ORMGenaratedModel
public string TypeName { get; internal set; }
public string KQL { get; internal set; }
public string TableOrFunctionDeclaration { get; internal set; }
public ORMGeneratorDatabaseConfig DatabaseConfig { get; set; }
}
24 changes: 20 additions & 4 deletions CSharp.OpenSource.LinqToKql/ORMGen/ORMGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,20 @@ protected virtual async Task GenerateDbContextAsync(List<ORMGenaratedModel> mode
{
if (index != 0) { lines.Add(""); }
lines.Add($"{TAB}public virtual IQueryable<{model.TypeName}> {model.TableOrFunctionDeclaration}");
lines.Add($"{TAB}{TAB}=> CreateQuery<{model.TypeName}>($\"{model.KQL}\");");
lines.Add($"{TAB}{TAB}=> CreateQuery<{model.TypeName}>($\"{model.KQL}\", \"{model.DatabaseConfig.DatabaseName}\");");
}
lines.Add($"}}");
await File.WriteAllTextAsync(Config.DbContextFilePath, WrapContentWithNamespaceAndUsing(lines, usings, Config.DbContextNamespace));
}

protected virtual string WrapContentWithNamespaceAndUsing(List<string> lines, List<string> usings, string @namespace)
protected virtual string WrapContentWithNamespaceAndUsing(List<string> lines, List<string> usings, string @namespace, string? referenceHint = null)
{
var res = new List<string>();
res.Add($"// <auto-generated> This file has been auto generated by {typeof(ORMGenerator).FullName}. </auto-generated>");
if (referenceHint != null)
{
res.Add($"// <auto-generated> {referenceHint} </auto-generated>");
}
res.Add("#pragma warning disable IDE1006 // Naming Styles");
if (Config.EnableNullable) { res.Add("#nullable enable"); }
usings = usings.Where(x => !string.IsNullOrEmpty(x))
Expand Down Expand Up @@ -132,7 +136,12 @@ protected virtual async Task<ORMGenaratedModel> GenerateFunctionModelAsync(ShowF
lines.Add($"{TAB}public virtual {DataTypeTranslate(column.DataType)} {column.ColumnName} {{ get; set; }}");
}
lines.Add($"}}");
var fileContent = WrapContentWithNamespaceAndUsing(lines, usings, Config.ModelsNamespace);
var fileContent = WrapContentWithNamespaceAndUsing(
lines,
usings,
Config.ModelsNamespace,
referenceHint: $"database('{dbConfig.DatabaseName}').{function.Name}{function.Parameters}"
);
var modelFolder = dbConfig.ModelSubFolderName != null
? Path.Combine(Config.ModelsFolderPath, dbConfig.ModelSubFolderName)
: Config.ModelsFolderPath;
Expand All @@ -144,6 +153,7 @@ protected virtual async Task<ORMGenaratedModel> GenerateFunctionModelAsync(ShowF
TypeName = typeName,
TableOrFunctionDeclaration = $"{typeName}({csharpParams})",
KQL = $"{function.Name}({kqlParams})",
DatabaseConfig = dbConfig,
};
}

Expand All @@ -169,7 +179,12 @@ protected virtual async Task<ORMGenaratedModel> GenerateTableModelAsync(ORMGener
lines.Add($"{TAB}public virtual {DataTypeTranslate(column.ColumnType)} {column.ColumnName} {{ get; set; }}");
}
lines.Add($"}}");
var fileContent = WrapContentWithNamespaceAndUsing(lines, usings, Config.ModelsNamespace);
var fileContent = WrapContentWithNamespaceAndUsing(
lines,
usings,
Config.ModelsNamespace,
referenceHint: $"database('{dbConfig.DatabaseName}').{table.Name}"
);
var modelFolder = dbConfig.ModelSubFolderName != null
? Path.Combine(Config.ModelsFolderPath, dbConfig.ModelSubFolderName)
: Config.ModelsFolderPath;
Expand All @@ -181,6 +196,7 @@ protected virtual async Task<ORMGenaratedModel> GenerateTableModelAsync(ORMGener
TypeName = typeName,
TableOrFunctionDeclaration = typeName,
KQL = table.Name,
DatabaseConfig = dbConfig,
};
}

Expand Down

0 comments on commit 03e5cd7

Please sign in to comment.