public static ReadOnlyCollection <Line> Create(string schema, string table, ReadOnlyCollection <Column> columns) { var textBuilder = new TextBuilder(); textBuilder.Add($"public static ReadOnlyCollection<Line> CreateInsertSqlStatement(IEnumerable<{table}> records)"); using (textBuilder.AddCSharpBlock()) { //textBuilder.Add($"var sqlTable = new SqlTable(\"{_owner}\",\"{_name}\", new[]"); //using (textBuilder.AddCSharpBlock()) //{ // sequence = new Sequence(); // foreach (var column in columns) // { // var last = sequence.Next() == columns.Count - 1; // var name = column.ColumnName; // var separator = !last ? "," : null; // textBuilder.Add($"\"{name}\"{separator}"); // } //} //textBuilder.AddToLastLine(".ToReadOnlyCollection());"); //textBuilder.Add(Line.Empty); textBuilder.Add("var columns = new[]"); using (textBuilder.AddCSharpBlock()) { foreach (var indexedColumn in columns.SelectIndexed()) { if (indexedColumn.Index > 0) { textBuilder.AddToLastLine(","); } var column = indexedColumn.Value; textBuilder.Add($"\"{column.ColumnName}\""); } } textBuilder.AddToLastLine(";"); textBuilder.Add($"var rows = records.Select(record => new[]"); using (textBuilder.AddCSharpBlock()) { foreach (var indexedColumn in columns.SelectIndexed()) { if (indexedColumn.Index > 0) { textBuilder.AddToLastLine(","); } var column = indexedColumn.Value; var methodName = MethodName.GetToSqlConstantMethodName(column.SqlDataTypeName, column.IsNullable); textBuilder.Add($"record.{column.ColumnName}.{methodName}()"); } } textBuilder.AddToLastLine(").ToReadOnlyCollection();"); textBuilder.Add($"var insertSqlStatement = InsertSqlStatementFactory.Create(\"{schema}.{table}\", columns, rows);"); textBuilder.Add("return insertSqlStatement;"); } return(textBuilder.ToLines()); }
public static ReadOnlyCollection <Line> Create(string schema, string table, Column identifierColumn, Column versionColumn, IReadOnlyCollection <Column> columns) { var arguments = new List <string>(); arguments.Add($"{table} record"); if (versionColumn != null) { var csharpTypeName = SqlDataTypeArray.SqlDataTypes.First(i => i.SqlDataTypeName == versionColumn.SqlDataTypeName).CSharpTypeName; arguments.Add($"{csharpTypeName} expected{versionColumn.ColumnName}"); } var textBuilder = new TextBuilder(); textBuilder.Add($"public static ReadOnlyCollection<Line> CreateUpdateSqlStatement({arguments.Join(", ")})"); using (textBuilder.AddCSharpBlock()) { textBuilder.Add("var setColumns = new []"); using (textBuilder.AddCSharpBlock()) foreach (var item in columns.SelectIndexed()) { if (item.Index > 0) { textBuilder.AddToLastLine(","); } var column = item.Value; var method = MethodName.GetToSqlConstantMethodName(column.SqlDataTypeName, column.IsNullable); textBuilder.Add($"new ColumnNameValue(\"{column.ColumnName}\", record.{column.ColumnName}.{method}())"); } textBuilder.AddToLastLine(";"); textBuilder.Add("var whereColumns = new[]"); using (textBuilder.AddCSharpBlock()) { var method = MethodName.GetToSqlConstantMethodName(identifierColumn.SqlDataTypeName, identifierColumn.IsNullable); textBuilder.Add($"new ColumnNameValue(\"{identifierColumn.ColumnName}\", record.{identifierColumn.ColumnName}.{method}())"); if (versionColumn != null) { textBuilder.AddToLastLine(","); method = MethodName.GetToSqlConstantMethodName(versionColumn.SqlDataTypeName, versionColumn.IsNullable); textBuilder.Add($"new ColumnNameValue(\"{versionColumn.ColumnName}\", expected{versionColumn.ColumnName}.{method}())"); } } textBuilder.AddToLastLine(";"); textBuilder.Add($"var updateSqlStatement = UpdateSqlStatementFactory.Create(\"{schema}.{table}\", setColumns, whereColumns);"); textBuilder.Add($"var validation = ValidationFactory.Create(\"update {schema}.{table} failed\");"); textBuilder.Add("var textBuilder = new TextBuilder();"); textBuilder.Add("textBuilder.Add(updateSqlStatement);"); textBuilder.Add("textBuilder.Add(validation);"); textBuilder.Add("return textBuilder.ToLines();"); } return(textBuilder.ToLines()); }
public static ReadOnlyCollection <Line> Create(string table, IReadOnlyCollection <string> columns, IReadOnlyCollection <IReadOnlyCollection <string> > rows) { Assert.IsTrue(!table.IsNullOrEmpty()); Assert.IsTrue(!table.IsNullOrEmpty()); ArgumentNullException.ThrowIfNull(columns); ArgumentNullException.ThrowIfNull(rows); Assert.IsTrue(columns.Count > 0); Assert.IsTrue(rows.All(row => row.Count == columns.Count)); var textBuilder = new TextBuilder(); textBuilder.Add($"insert into {table}({columns.Join(",")})"); textBuilder.Add("values"); using (textBuilder.Indent(1)) { foreach (var indexedRow in rows.SelectIndexed()) { if (indexedRow.Index > 0) { textBuilder.AddToLastLine(","); } var values = indexedRow.Value.Join(","); textBuilder.Add($"({values})"); } } return(textBuilder.ToLines()); }
public static ReadOnlyCollection <Line> Create(string schema, string table, Column identifierColumn, Column versionColumn) { var arguments = new List <string>(); var csharpTypeName = SqlDataTypeArray.SqlDataTypes.First(i => i.SqlDataTypeName == identifierColumn.SqlDataTypeName).CSharpTypeName; arguments.Add($"{csharpTypeName} {identifierColumn.ColumnName.ToCamelCase()}"); if (versionColumn != null) { csharpTypeName = SqlDataTypeArray.SqlDataTypes.First(i => i.SqlDataTypeName == versionColumn.SqlDataTypeName).CSharpTypeName; arguments.Add($"{csharpTypeName} {versionColumn.ColumnName.ToCamelCase()}"); } var textBuilder = new TextBuilder(); textBuilder.Add($"public static ReadOnlyCollection<Line> CreateDeleteSqlStatement({arguments.Join(", ")})"); using (textBuilder.AddCSharpBlock()) { textBuilder.Add("var whereColumns = new[]"); using (textBuilder.AddCSharpBlock()) { var method = MethodName.GetToSqlConstantMethodName(identifierColumn.SqlDataTypeName, identifierColumn.IsNullable); textBuilder.Add($"new ColumnNameValue(\"{identifierColumn.ColumnName}\", {identifierColumn.ColumnName.ToCamelCase()}.{method}())"); if (versionColumn != null) { textBuilder.AddToLastLine(","); method = MethodName.GetToSqlConstantMethodName(versionColumn.SqlDataTypeName, versionColumn.IsNullable); textBuilder.Add($"new ColumnNameValue(\"{versionColumn.ColumnName}\", {versionColumn.ColumnName.ToCamelCase()}.{method}())"); } } textBuilder.AddToLastLine(";"); textBuilder.Add($"var deleteSqlStatement = DeleteSqlStatementFactory.Create(\"{schema}.{table}\", whereColumns);"); textBuilder.Add($"var validation = ValidationFactory.Create(\"delete {schema}.{table} failed\");"); textBuilder.Add("var textBuilder = new TextBuilder();"); textBuilder.Add("textBuilder.Add(deleteSqlStatement);"); textBuilder.Add("textBuilder.Add(validation);"); textBuilder.Add("return textBuilder.ToLines();"); } return(textBuilder.ToLines()); }