private static void WriteExists(this SourceBuilder sb, Entity entity) { var entityName = entity.DisplayName.Replace(".", "").Replace("::", "_"); var fullPrimaryKey = entity.GetFullPrimaryKey(); var parameters = fullPrimaryKey.Select(fr => { var field = fr.ResolvedField; return(field.Type.ToCSharp(), field.Name.ToCamelCase()); }); sb.PublicStaticMethod("global::SqlKata.Query", $"Exists_{entityName}", parameters, sb => { var viewName = entity.GetViewName(); sb.AL($@"return new global::SqlKata.Query(""{viewName}"")"); sb.I(sb => { foreach (var field in fullPrimaryKey) { var fieldName = field.FieldName; var parameterName = fieldName.ToCamelCase(); sb.AL($@".Where(""{fieldName}"", {parameterName})"); } sb.AL(@".SelectRaw(""1"")"); sb.AL(@".Limit(1);"); }); }); }
private static void WriteExistsUniqueKey(this SourceBuilder sb, Entity entity) { var entityName = entity.DisplayName.Replace(".", "").Replace("::", "_"); foreach (var key in entity.UniqueKeys) { var fields = key.FieldReferences.Select(fr => fr.ResolvedField); var methodNamePostfix = string.Join("_", fields.Select(f => f.Name)); var parameters = fields.Select(f => { return(f.Type.ToCSharp(), f.Name.ToCamelCase()); }); sb.PublicStaticMethod("global::SqlKata.Query", $"Exists_{entityName}_UK_{methodNamePostfix}", parameters, sb => { var viewName = entity.GetViewName(); sb.AL($@"return new global::SqlKata.Query(""{viewName}"")"); sb.I(sb => { foreach (var field in fields) { var fieldName = field.Name; var parameterName = fieldName.ToCamelCase(); sb.AL($@".Where(""{fieldName}"", {parameterName})"); } sb.AL(@".SelectRaw(""1"")"); sb.AL(@".Limit(1);"); }); }); } }
public void TestComplete() { // Arrange var sb = new SourceBuilder(); // Act sb.AL("namespace foo"); sb.B(sb => { sb.AL("public class Bar"); sb.B(sb => { sb.A("System.Console.WriteLine(", indent: true); sb.A("\"Hello World!\""); sb.A(");"); sb.NL(); sb.I(sb => { sb.AL("// Indented comment"); }); }); }); var value = sb.ToString(); // Assert Assert.Equal(@"namespace foo { public class Bar { System.Console.WriteLine(""Hello World!""); // Indented comment } } ", value); }
private static void WriteGet(this SourceBuilder sb, Entity entity) { var entityName = entity.DisplayName.Replace(".", "").Replace("::", "_"); var fullPrimaryKey = entity.GetFullPrimaryKey().ToList(); var parameters = fullPrimaryKey.Select(fr => { var field = fr.ResolvedField; return(field.Type.ToCSharp(), field.Name.ToCamelCase()); }).ToList(); var allFields = fullPrimaryKey.Select(fr => fr.ResolvedField).Concat(entity.Fields).Distinct().ToList(); var stackedFullPrimaryKey = new Stack <FieldReference>(fullPrimaryKey); var stackedParameters = new Stack <(string Type, string Name)>(parameters); var i = stackedParameters.Count; while (i >= 0) { sb.PublicStaticMethod("global::SqlKata.Query", $"Get_{entityName}", stackedParameters.Reverse(), sb => { var viewName = entity.GetViewName(); sb.AL($@"return new global::SqlKata.Query(""{viewName}"")"); sb.I(sb => { foreach (var field in stackedFullPrimaryKey.Reverse()) { var fieldName = field.FieldName; var parameterName = fieldName.ToCamelCase(); sb.AL($@".Where(""{fieldName}"", {parameterName})"); } allFields.ForEach((field, last) => { var fieldName = field.Name; var semicolon = last ? ";" : ""; sb.AL($@".Select(""{fieldName}""){semicolon}"); }); }); }); if (stackedParameters.Count > 0) { stackedFullPrimaryKey.Pop(); stackedParameters.Pop(); } i--; } }
private static void WriteInsert(this SourceBuilder sb, Entity entity) { bool IsOptional(Field field) { return(!field.IsPartOfParentKey(entity) && (field.HasDefault() || field.Nullable)); } var entityName = entity.DisplayName.Replace(".", "").Replace("::", "_"); var fullPrimaryKey = entity.GetFullPrimaryKey(); var allFields = fullPrimaryKey.Select(fr => fr.ResolvedField).Where(f => f.IsInsertablePK(entity)).Concat(entity.Fields.Where(f => f.IsInsertable(entity))).Distinct(); var parameters = allFields.Select(f => (IsOptional(f), f.Type.ToCSharp(), f.Name.ToCamelCase())); sb.PublicStaticMethod("global::SqlKata.Query", $"Insert_{entityName}", parameters, sb => { sb.AL("var _columns = new global::System.Collections.Generic.List<string>();"); sb.AL("var _values = new global::System.Collections.Generic.List<object?>();"); foreach (var field in allFields) { var fieldName = field.Name; var parameterName = fieldName.ToCamelCase(); var optional = IsOptional(field); if (optional) { sb.AL($"if ({parameterName} != null)"); sb.B(sb => { sb.AL($@"_columns.Add(""{fieldName}"");"); sb.AL($@"_values.Add({parameterName});"); }); } else { sb.AL($@"_columns.Add(""{fieldName}"");"); sb.AL($@"_values.Add({parameterName});"); } } var tableName = entity.GetTableName(); sb.AL($@"return new global::SqlKata.Query(""{tableName}"")"); sb.I(sb => { sb.AL(@".AsInsert(_columns, _values);"); }); }); }
private static void WriteGetUniqueKey(this SourceBuilder sb, Entity entity) { var entityName = entity.DisplayName.Replace(".", "").Replace("::", "_"); var fullPrimaryKey = entity.GetFullPrimaryKey().ToList(); var allFields = fullPrimaryKey.Select(fr => fr.ResolvedField).Concat(entity.Fields).Distinct().ToList(); foreach (var key in entity.UniqueKeys) { var fields = key.FieldReferences.Select(fr => fr.ResolvedField); var methodNamePostfix = string.Join("_", fields.Select(f => f.Name)); var parameters = fields.Select(f => { return(f.Type.ToCSharp(), f.Name.ToCamelCase()); }); sb.PublicStaticMethod("global::SqlKata.Query", $"Get_{entityName}_UK_{methodNamePostfix}", parameters, sb => { var viewName = entity.GetViewName(); sb.AL($@"return new global::SqlKata.Query(""{viewName}"")"); sb.I(sb => { foreach (var field in fields) { var fieldName = field.Name; var parameterName = fieldName.ToCamelCase(); sb.AL($@".Where(""{fieldName}"", {parameterName})"); } allFields.ForEach((field, last) => { var fieldName = field.Name; var semicolon = last ? ";" : ""; sb.AL($@".Select(""{fieldName}""){semicolon}"); }); }); }); } }
private static void WriteCount(this SourceBuilder sb, Entity entity) { var entityName = entity.DisplayName.Replace(".", "").Replace("::", "_"); var fullPrimaryKey = entity.GetFullPrimaryKey().ToList(); var fields = fullPrimaryKey.Take(fullPrimaryKey.Count - 1).Select(fr => { var field = fr.ResolvedField; return(field.Type.ToCSharp(), field.Name, field.Name.ToCamelCase()); }).ToList(); var stackedGroupable = new Stack <(string Type, string Name, string NameCamel)>(fields); var i = stackedGroupable.Count; while (i >= 0) { var stackedPivotable = new Stack <(string Type, string Name, string NameCamel)>(fields.Take(i)); var j = stackedPivotable.Count; while (j >= 0) { if (j == 0) { var parameters = stackedGroupable.Select(p => (p.Type, p.NameCamel)).Reverse(); var arguments = string.Join(", ", stackedGroupable.Reverse().Select(p => p.NameCamel)); sb.PublicStaticMethod("global::SqlKata.Query", $"Count_{entityName}", parameters, sb => { var viewName = entity.GetViewName(); sb.AL($@"return new global::SqlKata.Query(""{viewName}"")"); sb.I(sb => { foreach (var field in stackedGroupable.Reverse()) { var fieldName = field.Name; var parameterName = field.NameCamel; sb.AL($@".Where(""{fieldName}"", {parameterName})"); } foreach (var field in stackedPivotable.Reverse()) { var fieldName = field.Name; sb.AL($@".GroupBy(""{fieldName}"")"); } foreach (var field in stackedPivotable.Reverse()) { var fieldName = field.Name; sb.AL($@".Select(""{fieldName}"")"); } sb.AL(".AsCount();"); }); }); } else if (i > 0) { var parameters = stackedPivotable.Skip(1).Select(p => (p.Type, p.NameCamel)).Reverse(); var groupByName = string.Join("_", stackedGroupable.Reverse().Select(p => p.Name)); sb.PublicStaticMethod("global::SqlKata.Query", $"Count_{entityName}_GroupBy_{groupByName}", parameters, sb => { var viewName = entity.GetViewName(); sb.AL($@"return new global::SqlKata.Query(""{viewName}"")"); sb.I(sb => { foreach (var field in stackedPivotable.Skip(1).Reverse()) { var fieldName = field.Name; var parameterName = field.NameCamel; sb.AL($@".Where(""{fieldName}"", {parameterName})"); } foreach (var field in stackedGroupable.Reverse()) { var fieldName = field.Name; sb.AL($@".GroupBy(""{fieldName}"")"); } foreach (var field in stackedGroupable.Reverse()) { var fieldName = field.Name; sb.AL($@".Select(""{fieldName}"")"); } sb.AL(@".SelectRaw(""COUNT(*) as Count"");"); }); }); } if (stackedPivotable.Count > 0) { stackedPivotable.Pop(); } j--; } if (stackedGroupable.Count > 0) { stackedGroupable.Pop(); } i--; } }