private static void writeColumnProperty(TextWriter writer, Column column) { CodeGenerationStatics.AddSummaryDocComment(writer, "This object will " + (column.AllowsNull && !column.NullValueExpression.Any() ? "sometimes" : "never") + " be null."); writer.WriteLine( "public " + column.DataTypeName + " " + Utility.GetCSharpIdentifier(column.PascalCasedNameExceptForOracle) + " { get { return __basicRow." + Utility.GetCSharpIdentifier(column.PascalCasedName) + "; } }"); }
internal static void Generate(DBConnection cn, TextWriter writer, string baseNamespace, IDatabase database, IEnumerable <string> tableNames) { writer.WriteLine("namespace " + baseNamespace + ".TableConstants {"); foreach (var table in tableNames) { CodeGenerationStatics.AddSummaryDocComment(writer, "This object represents the constants of the " + table + " table."); writer.WriteLine("public class " + Utility.GetCSharpIdentifier(table.TableNameToPascal(cn) + "Table") + " {"); CodeGenerationStatics.AddSummaryDocComment(writer, "The name of this table."); writer.WriteLine("public const string Name = \"" + table + "\";"); foreach (var column in new TableColumns(cn, table, false).AllColumnsExceptRowVersion) { CodeGenerationStatics.AddSummaryDocComment(writer, "Contains schema information about this column."); writer.WriteLine("public class " + Utility.GetCSharpIdentifier(column.PascalCasedNameExceptForOracle + "Column") + " {"); CodeGenerationStatics.AddSummaryDocComment(writer, "The name of this column."); writer.WriteLine("public const string Name = \"" + column.Name + "\";"); CodeGenerationStatics.AddSummaryDocComment( writer, "The size of this column. For varchars, this is the length of the biggest string that can be stored in this column."); writer.WriteLine("public const int Size = " + column.Size + ";"); writer.WriteLine("}"); } writer.WriteLine("}"); } writer.WriteLine("}"); }
internal static void WriteRowClasses( TextWriter writer, IEnumerable <Column> columns, Action <TextWriter> transactionPropertyWriter, Action <TextWriter> toModificationMethodWriter) { // BasicRow writer.WriteLine("internal class BasicRow {"); foreach (var column in columns.Where(i => !i.IsRowVersion)) { writer.WriteLine("private readonly " + column.DataTypeName + " " + getMemberVariableName(column) + ";"); } writer.WriteLine("internal BasicRow( DbDataReader reader ) {"); foreach (var column in columns.Where(i => !i.IsRowVersion)) { writer.WriteLine($"{getMemberVariableName( column )} = {column.GetDataReaderValueExpression( "reader" )};"); } writer.WriteLine("}"); foreach (var column in columns.Where(i => !i.IsRowVersion)) { writer.WriteLine( "internal " + column.DataTypeName + " " + Utility.GetCSharpIdentifier(column.PascalCasedName) + " { get { return " + getMemberVariableName(column) + "; } }"); } writer.WriteLine("}"); // Row CodeGenerationStatics.AddSummaryDocComment(writer, "Holds data for a row of this result."); writer.WriteLine("public partial class Row: IEquatable<Row> {"); writer.WriteLine("private readonly BasicRow __basicRow;"); writer.WriteLine("internal Row( BasicRow basicRow ) {"); writer.WriteLine("__basicRow = basicRow;"); writer.WriteLine("}"); foreach (var column in columns.Where(i => !i.IsRowVersion)) { writeColumnProperty(writer, column); } // NOTE: Being smarter about the hash code could make searches of the collection faster. writer.WriteLine("public override int GetHashCode() { "); // NOTE: Catch an exception generated by not having any uniquely identifying columns and rethrow it as a ApplicationException. writer.WriteLine("return " + Utility.GetCSharpIdentifier(columns.First(c => c.UseToUniquelyIdentifyRow).PascalCasedNameExceptForOracle) + ".GetHashCode();"); writer.WriteLine("}"); // Object override of GetHashCode writer.WriteLine( @"public static bool operator == ( Row row1, Row row2 ) => Equals( row1, row2 ); public static bool operator !=( Row row1, Row row2 ) => !Equals( row1, row2 );" ); writer.WriteLine("public override bool Equals( object obj ) {"); writer.WriteLine("return Equals( obj as Row );"); writer.WriteLine("}"); // Object override of Equals writer.WriteLine("public bool Equals( Row other ) {"); writer.WriteLine("if( other == null ) return false;"); var condition = ""; foreach (var column in columns.Where(c => c.UseToUniquelyIdentifyRow)) { condition = StringTools.ConcatenateWithDelimiter( " && ", condition, Utility.GetCSharpIdentifier(column.PascalCasedNameExceptForOracle) + " == other." + Utility.GetCSharpIdentifier(column.PascalCasedNameExceptForOracle)); } writer.WriteLine("return " + condition + ";"); writer.WriteLine("}"); // Equals method transactionPropertyWriter(writer); toModificationMethodWriter(writer); writer.WriteLine("}"); // class }