private CodeTypeDeclaration CreateClass(ClassDefinition table) { CodeTypeDeclaration type = new CodeTypeDeclaration(); type.Name = table.ToString(); type.IsClass = true; type.IsPartial = true; type.BaseTypes.Add(new CodeTypeReference(typeof(CsDO.Lib.DataObject))); type.CustomAttributes.Add(new CodeAttributeDeclaration("Serializable", new CodeAttributeArgument[] { })); if (!String.IsNullOrEmpty(table.Alias) && !table.Table.Equals(table.Alias)) { type.CustomAttributes.Add(new CodeAttributeDeclaration("Table", new CodeAttributeArgument[] { new CodeAttributeArgument(new CodePrimitiveExpression(table.Table)) })); } #region class comments type.Comments.AddRange(InsertDocumentation(Documetation.Remarks, new string[] { "Persistence class that maps table '" + table + "'", "Warning: Each property maps a column, use the attribute", "Column to mark properties that should not be persisted." })); type.Comments.AddRange(InsertDocumentation(Documetation.SeeAlso, new string[] { "Column" })); #endregion return type; }
public static void ReadSchema(IDataBase driver, ClassDefinition table) { IDbConnection connection = driver.getConnection(); try { DataTable schema = new DataTable(); if (connection.GetType() == typeof(NpgsqlConnection)) { NpgsqlCommand command = driver.getCommand("SELECT * FROM \"" + table.Table + "\"") as NpgsqlCommand; NpgsqlDataAdapter adapter = driver.getDataAdapter(command) as NpgsqlDataAdapter; adapter.FillSchema(schema, SchemaType.Source); } else { DbCommand command = driver.getCommand("SELECT * FROM \"" + table.Table + "\""); DbDataAdapter adapter = driver.getDataAdapter(command) as DbDataAdapter; adapter.FillSchema(schema, SchemaType.Source); } foreach (DataColumn c in schema.Columns) { table.Columns.Add(new FieldDefinition(c.ColumnName, c.DataType)); } foreach (DataColumn pk in schema.PrimaryKey) { foreach (FieldDefinition field in table.Columns) if (field.ColumnName.Equals(pk.ColumnName)) field.PrimaryKey = true; } if (connection.GetType() == typeof(NpgsqlConnection)) { NpgsqlCommand command = driver.getCommand(@"SELECT tc.constraint_schema, tc.constraint_name, kcu.table_schema, kcu.table_name, kcu.column_name, ctu.table_schema as dest_table_schema, ctu.table_name as dest_table_name, ccu.column_name as dest_column_name FROM information_schema.table_constraints tc INNER JOIN information_schema.constraint_table_usage ctu ON ctu.constraint_name = tc.constraint_name INNER JOIN information_schema.constraint_column_usage ccu ON ccu.constraint_name = tc.constraint_name INNER JOIN information_schema.key_column_usage kcu ON kcu.constraint_name = tc.constraint_name WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name = '" + table + "';") as NpgsqlCommand; DbDataReader dr = command.ExecuteReader(); while (dr.Read()) { foreach (FieldDefinition field in table.Columns) if (field.ColumnName.Equals(dr["column_name"].ToString())) { field.ForeignKey = true; field.ForeignKeyType = dr["dest_table_name"].ToString(); } } } } finally { connection.Close(); connection.Dispose(); } }
public string Run(ClassDefinition table, string path) { using (CodeDomProvider provider = new CSharpCodeProvider()) { CodeCompileUnit unit = new CodeCompileUnit(); CodeNamespace codeNamespace = new CodeNamespace(namespaceName); codeNamespace.Imports.Add(new CodeNamespaceImport("System")); codeNamespace.Imports.Add(new CodeNamespaceImport("CsDO.Lib")); #region code comments codeNamespace.Comments.AddRange(InsertComments(new string[] { "This file was generated automatically by CsDO.CodeGenerator.", "", "DO NOT EDIT THIS FILE !", "", "If you need to extend this class, create another file and use this code:", @" public partial class " + table + @" { } " })); #endregion unit.Namespaces.Add(codeNamespace); CodeTypeDeclaration type = CreateClass(table); codeNamespace.Types.Add(type); SchemaReader.ReadSchema(driver, table); WriteFields(table.Columns, type); WriteProperties(table.Columns, type); //WriteConstructor(list, type); CodeGeneratorOptions options = new CodeGeneratorOptions(); options.BlankLinesBetweenMembers = true; options.BracingStyle = "C"; options.IndentString = "\t"; TextWriter writer = new StringWriter(); string filename = Path.GetFullPath(path) + Path.DirectorySeparatorChar + namespaceName + "." + table + ".cs"; if (File.Exists(filename)) File.Delete(filename); FileStream fs = new FileStream(filename, FileMode.CreateNew, FileAccess.ReadWrite); try { provider.GenerateCodeFromCompileUnit(unit, writer, options); string code = writer.ToString(); byte[] bytes = new byte[code.Length]; for (int i = 0; i < code.Length; i++) bytes[i] = (byte) code[i]; fs.Write(bytes, 0, bytes.Length); return code; } finally { fs.Close(); writer.Close(); } } }