/// <summary> /// Creates text for the whole class /// </summary> /// <param name="dt"></param> /// <param name="nameSpace"></param> /// <returns></returns> public static string CreateClass(DataTable dt, string nameSpace) { StringBuilder textForClassCreation = new StringBuilder(); if (dt != null) { textForClassCreation.Append("using ADOCRUD.Attributes;" + Environment.NewLine); textForClassCreation.Append("using System;" + Environment.NewLine); textForClassCreation.Append("using System.Collections.Generic;" + Environment.NewLine); textForClassCreation.Append("using System.Linq;" + Environment.NewLine); textForClassCreation.Append("using System.Text;" + Environment.NewLine); textForClassCreation.Append("using System.Threading.Tasks;" + Environment.NewLine + Environment.NewLine); textForClassCreation.Append("namespace " + nameSpace + Environment.NewLine); textForClassCreation.Append("{" + Environment.NewLine); textForClassCreation.Append("[Table(\"".PadLeft(12, ' ') + dt.TableName + "\", \"" + dt.Prefix + "\")]" + Environment.NewLine); textForClassCreation.Append(("public class " + dt.TableName).PadLeft(+dt.TableName.Length + 17, ' ') + Environment.NewLine); textForClassCreation.Append("{".PadLeft(5, ' ') + Environment.NewLine); foreach (DataColumn column in dt.Columns) { if (dt.PrimaryKey.Contains(column)) { textForClassCreation.Append("[PrimaryKey]".PadLeft(20, ' ') + Environment.NewLine); } textForClassCreation.Append("[Member]".PadLeft(16, ' ') + Environment.NewLine); Type t = column.DataType; SqlDbType dbType = new SqlDbType(); DataTypeMapper.DataTypes().TryGetValue(column.DataType, out dbType); string propertyLine = CreateProperty(dbType, column.AllowDBNull, column.ColumnName); textForClassCreation.Append(propertyLine.PadLeft(propertyLine.Length + 8, ' ') + Environment.NewLine + Environment.NewLine); } textForClassCreation.Append("}".PadLeft(5, ' ') + Environment.NewLine); textForClassCreation.Append("}"); } return(textForClassCreation.ToString()); }
/// <summary> /// Generates sql command for execution for insert, update, and remove /// </summary> /// <typeparam name="T"></typeparam> /// <param name="item">Model Property</param> /// <param name="query">Sql Statement</param> /// <param name="modelProperties">Properties of the model</param> /// <param name="primaryKeyProperties">Properties of the model that are associated with the primary key</param> /// <param name="isUpdate">Is the sql an update query</param> /// <returns></returns> internal SqlCommand GenerateSqlCommand <T>(T item, string query, PropertyInfo[] modelProperties, PropertyInfo[] primaryKeyProperties, ADOCRUDEnums.Action action, string suffix) { // Initializes sql command SqlCommand cmd = new SqlCommand(query.ToString(), sqlConnection as SqlConnection, sqlTransaction as SqlTransaction); if (modelProperties != null && modelProperties.Count() > 0) { // Adds model parameters to sql command for (int i = 0; i < modelProperties.Count(); i++) { // Checks to see if the data type is in the list of handled data types if (DataTypeMapper.DataTypes().ContainsKey(modelProperties[i].PropertyType)) { SqlDbType dbType; // Grabs value from property and checks if its null // If its not null, the pass value into sqlcommand parameter // Otherwise pass in DBNull.Value as the sql parameter DataTypeMapper.DataTypes().TryGetValue(modelProperties[i].PropertyType, out dbType); object dbValue = modelProperties[i].GetValue(item, null); if (dbValue != null) { cmd.Parameters.Add(modelProperties[i].Name + suffix, dbType).Value = modelProperties[i].GetValue(item, null); } else { cmd.Parameters.Add(modelProperties[i].Name + suffix, dbType).Value = DBNull.Value; } } else { throw new Exception("Property of type " + modelProperties[i].PropertyType.Name + " does not have a corresponding sql data type"); } } } if (action == ADOCRUDEnums.Action.Insert) { // Grabs identity generated on insert cmd.Parameters.Add("primaryKey", SqlDbType.Int).Direction = ParameterDirection.Output; } else if (action == ADOCRUDEnums.Action.Update || action == ADOCRUDEnums.Action.Remove) { // Add primary key parameters to sql command for (int i = 0; i < primaryKeyProperties.Count(); i++) { // Don't add parameter if it already exist in the sql command if (!cmd.Parameters.Contains(primaryKeyProperties[i].Name + suffix)) { if (primaryKeyProperties[i].PropertyType == typeof(int)) { cmd.Parameters.Add(primaryKeyProperties[i].Name + suffix, SqlDbType.Int).Value = primaryKeyProperties[i].GetValue(item, null); } else if (primaryKeyProperties[0].PropertyType == typeof(Guid)) { cmd.Parameters.Add(primaryKeyProperties[i].Name + suffix, SqlDbType.UniqueIdentifier).Value = primaryKeyProperties[i].GetValue(item, null); } else { throw new Exception("Primary key must be an integer or GUID"); } } } } return(cmd); }