<%@ CodeTemplate Language="C#" TargetLanguage="Text" Description="Template description here." %> <%@ Property Name="ConnectionString" Type="System.String" Category="Context" Description="Database connection string."%> <%@ Assembly Name="SchemaExplorer" %> <%@ Assembly Name="SchemaExplorer.SqlSchemaProvider" %> <%@ Assembly Name="CodeSmith.BaseTemplates" %> <%@ Assembly Name="System.Data" %> <%@ Import Namespace="SchemaExplorer" %> <%@ Import Namespace="System.Data" %> // This file was generated from the <%=SourceDatabase.Name%> database at <%=DateTime.Now.ToString()%>. using System; using System.Data; using System.Data.SqlClient; using System.Collections; namespace Corillian.Corporate.Internal.DatabaseFacade { #region class <%=SourceDatabase.Name%>DbCommands public class <%=SourceDatabase.Name%>DbCommands : IDisposable { SqlConnection _connection = null; public <%=SourceDatabase.Name%>DbCommands(string connectionString) { this._connection = new SqlConnection(connectionString); this._connection.Open(); } public void Dispose() { this._connection.Dispose(); } <% foreach (CommandSchema cs in SourceDatabase.Commands) { // Ignore *CCSP* sprocs. if (cs.FullName.IndexOf("CCSP") < 0) { string returnType = GetReturnType(cs); %> public <%=returnType%> <%=GetMethodName(cs)%>(<%=GetMethodParameterList(cs)%>) { SqlCommand command = new SqlCommand("<%=cs.FullName%>", this._connection); command.CommandType = CommandType.StoredProcedure; <% foreach (ParameterSchema ps in cs.InputParameters) { if (IsValidParameter(ps)) { //Log("DataType is {0}", ps.DataType); if (ps.AllowDBNull) { switch (ps.DataType) { case DbType.String: case DbType.StringFixedLength: case DbType.AnsiString: case DbType.AnsiStringFixedLength: %> command.Parameters.Add(new SqlParameter("<%=ps.Name%>", (<%=GetCSharpParameterName(ps)%> == null) ? System.DBNull.Value : (object) <%=GetCSharpParameterName(ps)%>)); <% break; case DbType.Date: case DbType.DateTime: %> command.Parameters.Add(new SqlParameter("<%=ps.Name%>", (<%=GetCSharpParameterName(ps)%> == DateTime.MinValue) ? System.DBNull.Value : (object) <%=GetCSharpParameterName(ps)%>)); <% break; default: %> command.Parameters.Add(new SqlParameter("<%=ps.Name%>", <%=GetCSharpParameterName(ps)%>)); <% break; } } else { %> command.Parameters.Add(new SqlParameter("<%=ps.Name%>", <%=GetCSharpParameterName(ps)%>)); <% } } } if (cs.AllOutputParameters.Count > 0) { // For now, only pick up the first output param ParameterSchema ps = cs.AllOutputParameters[0]; %> SqlParameter outputParameter = null; outputParameter = new SqlParameter("<%=ps.Name%>", new <%=GetCSharpVariableType(ps.DataType)%>()); outputParameter.Direction = ParameterDirection.Output; command.Parameters.Add(outputParameter); <% } if (returnType.Equals("void")) { %> command.ExecuteNonQuery(); <% } else if (returnType.Equals("IDataReader")) { %> return command.ExecuteReader(); <% } else { %> command.ExecuteNonQuery(); return (<%=returnType%>) outputParameter.Value; <% } %> } <% } } %> } #endregion #region Domain objects <% foreach (CommandSchema cmd in GetDistinctDomainObjects(SourceDatabase.Commands)) { //Log("[Schema Objects] {0}", GetDomainObjectName(cmd)); if (cmd.CommandResults != null && cmd.CommandResults.Count > 0) { CommandResultSchema ts = cmd.CommandResults[0]; string tableName = ExtractTableNameFromProcName(cmd); %> #region class <%=tableName%> public class <%=tableName%> { #region Fields <% foreach (CommandResultColumnSchema cs in ts.Columns) { %> private <%=GetCSharpVariableType(cs.DataType)%> <%=GetFieldName(cs)%>; <% if (GetCSharpTypeFromDbType(cs.DataType).IsValueType) { %> private bool <%=GetFieldName(cs)%>Specified = false; <% } } %> #endregion #region Properties <% foreach (CommandResultColumnSchema cs in ts.Columns) { if (GetCSharpTypeFromDbType(cs.DataType).IsValueType) { %> public <%=GetCSharpVariableType(cs.DataType)%> <%=GetPropertyName(cs)%> { get { return this.<%=GetFieldName(cs)%>; } set { this.<%=GetFieldName(cs)%> = value; this.<%=GetFieldName(cs)%>Specified = true; } } public bool <%=GetPropertyName(cs)%>Specified { get { return this.<%=GetFieldName(cs)%>Specified; } } <% } else { %> public <%=GetCSharpVariableType(cs.DataType)%> <%=GetPropertyName(cs)%> { get { return this.<%=GetFieldName(cs)%>; } set { this.<%=GetFieldName(cs)%> = value; } } <% } } %> #endregion #region Constructors public <%=tableName%>() { } #endregion #region Converters public static <%=tableName%> ConvertFromDataReader(IDataReader reader) { <%=tableName%> item = new <%=tableName%>(); reader.Read(); item.ConvertFromRecord(reader as IDataRecord); reader.Close(); return item; } public static IList ConvertMultipleRecordsFromDataReader(IDataReader reader) { ArrayList result = new ArrayList(); while (reader.Read()) { <%=tableName%> item = new <%=tableName%>(); item.ConvertFromRecord(reader as IDataRecord); result.Add(item); } reader.Close(); return result; } protected virtual void ConvertFromRecord(IDataRecord record) { <% foreach (CommandResultColumnSchema cs in ts.Columns) { %> try { object val = record["<%=cs.Name%>"]; if (val != null && val != System.DBNull.Value) { this.<%=GetPropertyName(cs)%> = (<%=GetCSharpVariableType(cs.DataType)%>) val; } } catch (System.IndexOutOfRangeException) { } <% } %> } #endregion } #endregion <% } } %> #endregion }