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 Namespace(
     this SourceBuilder sb,
     string ns,
     Action <SourceBuilder> builder)
 {
     sb.AL($"namespace {ns}");
     sb.B(builder);
 }
 public static void PublicStaticClass(
     this SourceBuilder sb,
     string className,
     Action <SourceBuilder> builder)
 {
     sb.AL($"public static class {className}");
     sb.B(builder);
 }
Beispiel #4
0
        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);");
                });
            });
        }