public static DatabaseSchemaInfo GetDatabaseSchemaInfo(SqlConnection connection) { DatabaseSchemaInfo schemaInfo = new DatabaseSchemaInfo(); schemaInfo.Tables = GetTables(connection); schemaInfo.StoredProcedures = GetStoredProcedures(connection); return(schemaInfo); }
static void Main(string[] args) { if (args == null || args.Length < 4) { Console.WriteLine("Usage: connectionstring outputfile namespace classname"); return; } String connectionString = args[0]; String outputFileName = args[1]; String ns = args[2]; String dataAccessClassName = args[3]; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); DatabaseSchemaInfo schema = DatabaseSchemaExtractor.GetDatabaseSchemaInfo(connection); schema.Namespace = ns; schema.ClassName = dataAccessClassName; SimpleDataAccessGenerator.GenerateDatabaseAccessCode(schema, outputFileName); } }
public static void GenerateDatabaseAccessCode(DatabaseSchemaInfo schemaInfo, String outputFilename) { CodeStringBuilder code = new CodeStringBuilder(); // usings code.AppendLine("using System;"); code.AppendLine("using System.Collections.Generic;"); code.AppendLine(); // namespace code.CodeBlockBegin("namespace {0}", schemaInfo.Namespace); // model code.AppendLine("#region Models"); code.AppendLine(); foreach (var t in schemaInfo.Tables) { CSharpTextGenerator.GenerateModel(code, t); code.AppendLine(); } code.AppendLine("#endregion"); code.AppendLine(); // class - DataAccess wrapper code.CodeBlockBegin("public partial class {0} : IDisposable", schemaInfo.ClassName); // variable code.AppendLine(); code.AppendLine("private System.Data.SqlClient.SqlConnection connection;"); code.AppendLine("private System.Data.SqlClient.SqlTransaction transaction;"); code.AppendLine("private int transactionCounter;"); code.AppendLine("private String connectionString;"); code.AppendLine("private bool externalResource;"); code.AppendLine(); // constructor 1 code.AppendFormat("public {0}(String connectionString)", schemaInfo.ClassName); code.CodeBlockBegin(); code.AppendLine("this.externalResource = false;"); code.AppendLine("this.connectionString = connectionString;"); code.CodeBlockEnd(); code.AppendLine(); // constructor 2 code.AppendFormat("public {0}(System.Data.SqlClient.SqlConnection connection, System.Data.SqlClient.SqlTransaction transaction)", schemaInfo.ClassName); code.CodeBlockBegin(); code.AppendLine("this.externalResource = true;"); code.AppendLine("this.connection = connection;"); code.AppendLine("this.transaction = transaction;"); code.CodeBlockEnd(); code.AppendLine(); foreach (var t in schemaInfo.Tables) { String entityTypeName = t.ClassName; String mappingMethodName = "Read" + entityTypeName; code.AppendLineFormat("#region Upsert, Insert, Update, Delete, Select, Mapping - {0}", t.ToString()); code.AppendLine(); CSharpTextGenerator.GenerateMapping(code, mappingMethodName, entityTypeName, t.Columns); code.AppendLine(); SimpleDataAccessGenerator.GenerateUpsertMethod(code, t); code.AppendLine(); SimpleDataAccessGenerator.GenerateInsertMethod(code, t); code.AppendLine(); SimpleDataAccessGenerator.GenerateUpdateMethod(code, t); code.AppendLine(); SimpleDataAccessGenerator.GenerateSelectMethod(code, t, null, false, mappingMethodName); code.AppendLine(); SimpleDataAccessGenerator.GenerateSelectCountMethod(code, t); code.AppendLine(); SimpleDataAccessGenerator.GenerateSelectPagedMethod(code, t, mappingMethodName); code.AppendLine(); Dictionary <String, Filter> filterColumnLists = new Dictionary <string, Filter>(); foreach (var i in t.Indexes) { String key = String.Join(":", i.Columns.OrderBy(ci => ci.Name).Select(ci => ci.Name)); if (!filterColumnLists.ContainsKey(key)) { filterColumnLists.Add(key, new Filter { IsUnique = i.IsUnique, Columns = i.Columns }); } } foreach (var fk in t.ForeignKeys) { String key = String.Join(":", fk.Columns.OrderBy(ci => ci.Name).Select(ci => ci.Name)); if (!filterColumnLists.ContainsKey(key)) { filterColumnLists.Add(key, new Filter { IsUnique = false, Columns = fk.Columns }); } } foreach (KeyValuePair <String, Filter> keyValue in filterColumnLists) { SimpleDataAccessGenerator.GenerateSelectMethod(code, t, keyValue.Value.Columns, keyValue.Value.IsUnique, mappingMethodName); code.AppendLine(); SimpleDataAccessGenerator.GenerateDeleteMethod(code, t, keyValue.Value.Columns); code.AppendLine(); } code.AppendLine("#endregion"); code.AppendLine(); } foreach (StoredProcedureInfo storedProcedureInfo in schemaInfo.StoredProcedures) { code.AppendLine("#region Stored Procedures"); code.AppendLine(); CSharpTextGenerator.GenerateStoredProcedureMethod(code, storedProcedureInfo); code.AppendLine("#endregion"); code.AppendLine(); } code.Append(@" private void PopConnection(System.Data.SqlClient.SqlCommand command) { if (this.connection != null) { command.Connection = this.connection; command.Transaction = this.transaction; } else { command.Connection = new System.Data.SqlClient.SqlConnection(this.connectionString); command.Connection.Open(); } } private void PushConnection(System.Data.SqlClient.SqlCommand command) { System.Data.SqlClient.SqlConnection connection = command.Connection; System.Data.SqlClient.SqlTransaction transaction = command.Transaction; command.Connection = null; command.Transaction = null; if (connection != null && this.connection != connection) { connection.Close(); } } public void BeginTransaction(System.Data.IsolationLevel isolationLevel = System.Data.IsolationLevel.Unspecified) { if (this.connection == null) { this.connection = new System.Data.SqlClient.SqlConnection(this.connectionString); this.connection.Open(); } if (this.transaction == null) { this.transaction = this.connection.BeginTransaction(isolationLevel); } else { if (isolationLevel != System.Data.IsolationLevel.Unspecified && this.transaction.IsolationLevel != isolationLevel) { throw new InvalidOperationException(""Transaction isolation level mismatch.""); } } ++this.transactionCounter; } public void CommitTransaction() { if (this.transaction == null || this.transactionCounter <= 0) { throw new InvalidOperationException(""currentTransaction""); } --this.transactionCounter; if (this.transactionCounter == 0) { this.transaction.Commit(); this.transaction = null; } } public void RollbackTransaction() { if (this.transaction == null || this.transactionCounter <= 0) { throw new InvalidOperationException(""currentTransaction""); } this.transactionCounter = 0; this.transaction.Rollback(); this.transaction = null; } public void Dispose() { if (this.externalResource) { return; } try { if (this.transaction != null) { this.transaction.Rollback(); this.transaction = null; this.transactionCounter = 0; } } finally { if (this.connection != null) { this.connection.Close(); this.connection = null; } } } "); code.CodeBlockEnd(); code.CodeBlockEnd(); File.WriteAllText(outputFilename, code.ToString()); }