public override string CreateTableQuery(Type entity) { TableAttribute tableInfo = EntityCache.Get(entity); StringBuilder createSQL = new StringBuilder($"CREATE TABLE {tableInfo.FullName} ("); string primaryKeyCols = string.Empty; for (int i = 0; i < tableInfo.Columns.Count; i++) { ColumnAttribute col = tableInfo.Columns.ElementAt(i).Value; if (col.IsPrimaryKey) { primaryKeyCols += col.Name + ","; //13-May-15 Ritesh - fixed bug when primary key type is varchar size was ignored createSQL.Append($"{col.Name} {GetDBTypeWithSize(col.ColumnDbType, col.Size)} NOT NULL "); if (col.PrimaryKeyInfo.IsIdentity) { createSQL.Append(" IDENTITY "); } createSQL.Append(","); } else if (col.IgnoreInfo != null && col.IgnoreInfo.Insert && col.IgnoreInfo.Update) { continue; } else { createSQL.Append($"{col.Name} {GetDBTypeWithSize(col.ColumnDbType, col.Size, col.NumericScale)}"); if (IsNullableType(col.Property.PropertyType)) { createSQL.Append(" NULL "); } if (col.Name == Config.CREATEDON_COLUMN.Name || col.Name == Config.UPDATEDON_COLUMN.Name) { createSQL.Append(" DEFAULT " + CURRENTDATETIMESQL); } createSQL.Append(","); } } if (!string.IsNullOrEmpty(primaryKeyCols)) { primaryKeyCols = primaryKeyCols.RemoveLastComma(); createSQL.Append($"PRIMARY KEY CLUSTERED({primaryKeyCols})"); } createSQL.RemoveLastComma(); //Remove last comma if exists createSQL.Append(");"); if (tableInfo.IsKeyIdentity()) { createSQL.Append($"SET IDENTITY_INSERT {tableInfo.FullName} OFF"); } return(createSQL.ToString()); }
internal virtual void CreateAddCommand(IDbCommand cmd, object entity, IAuditTrail audit = null, string columnNames = null, bool doNotAppendCommonFields = false, bool overrideCreatedUpdatedOn = false) { TableAttribute tableInfo = EntityCache.Get(entity.GetType()); if (tableInfo.NeedsHistory && tableInfo.IsCreatedByEmpty(entity)) { throw new MissingFieldException("CreatedBy is required when Audit Trail is enabled"); } List <string> columns = new List <string>(); if (!string.IsNullOrEmpty(columnNames)) { columns.AddRange(columnNames.Split(',')); } else { columns.AddRange(tableInfo.DefaultInsertColumns); //Get columns from Entity attributes loaded in TableInfo } bool isPrimaryKeyEmpty = false; foreach (ColumnAttribute pkCol in tableInfo.Columns.Where(p => p.Value.IsPrimaryKey).Select(p => p.Value)) { if (pkCol.PrimaryKeyInfo.IsIdentity && tableInfo.IsKeyIdEmpty(entity, pkCol)) { isPrimaryKeyEmpty = true; //if identity remove keyfield if added in field list columns.Remove(pkCol.Name); } else if (pkCol.Property.PropertyType == typeof(Guid) && tableInfo.IsKeyIdEmpty(entity, pkCol)) { isPrimaryKeyEmpty = true; //if not identity and key not generated, generate before save tableInfo.SetKeyId(entity, pkCol, Guid.NewGuid()); } } #region append common columns if (!doNotAppendCommonFields) { if (!tableInfo.NoIsActive) { if (!columns.Contains(Config.ISACTIVE_COLUMN.Name)) { columns.Add(Config.ISACTIVE_COLUMN.Name); } bool isActive = tableInfo.GetIsActive(entity) ?? true; //when IsActive is not set then true for insert cmd.AddInParameter("@" + Config.ISACTIVE_COLUMN.Name, Config.ISACTIVE_COLUMN.ColumnDbType, isActive); if (tableInfo.NeedsHistory) { audit.AppendDetail(Config.ISACTIVE_COLUMN.Name, isActive, DbType.Boolean, null); } tableInfo.SetIsActive(entity, isActive); //Set IsActive value } if (!tableInfo.NoVersionNo) { int versionNo = tableInfo.GetVersionNo(entity) ?? 1; //set defualt versionno 1 for Insert if (versionNo == 0) { versionNo = 1; //set defualt versionno 1 for Insert even if its zero or null } if (!columns.Contains(Config.VERSIONNO_COLUMN.Name)) { columns.Add(Config.VERSIONNO_COLUMN.Name); } cmd.AddInParameter("@" + Config.VERSIONNO_COLUMN.Name, Config.VERSIONNO_COLUMN.ColumnDbType, versionNo); tableInfo.SetVersionNo(entity, versionNo); //Set VersionNo value } if (!tableInfo.NoCreatedBy) { if (!columns.Contains(Config.CREATEDBY_COLUMN.Name)) { columns.Add(Config.CREATEDBY_COLUMN.Name); } cmd.AddInParameter("@" + Config.CREATEDBY_COLUMN.Name, Config.CREATEDBY_COLUMN.ColumnDbType, tableInfo.GetCreatedBy(entity)); } if (!tableInfo.NoCreatedOn & !columns.Contains(Config.CREATEDON_COLUMN.Name)) { columns.Add(Config.CREATEDON_COLUMN.Name); } if (!tableInfo.NoUpdatedBy) { if (!columns.Contains(Config.UPDATEDBY_COLUMN.Name)) { columns.Add(Config.UPDATEDBY_COLUMN.Name); } cmd.AddInParameter("@" + Config.UPDATEDBY_COLUMN.Name, Config.UPDATEDBY_COLUMN.ColumnDbType, tableInfo.GetCreatedBy(entity)); } if (!tableInfo.NoUpdatedOn & !columns.Contains(Config.UPDATEDON_COLUMN.Name)) { columns.Add(Config.UPDATEDON_COLUMN.Name); } } #endregion //append @ before each fields to add as parameter List <string> parameters = columns.Select(c => "@" + c).ToList(); int pIndex = parameters.FindIndex(c => c == "@" + Config.CREATEDON_COLUMN.Name); if (pIndex >= 0) { var createdOn = Helper.GetDateTimeOrDatabaseDateTimeSQL(tableInfo.GetCreatedOn(entity), this, overrideCreatedUpdatedOn); if (createdOn is string) { parameters[pIndex] = (string)createdOn; } else { cmd.AddInParameter(parameters[pIndex], Config.CREATEDON_COLUMN.ColumnDbType, createdOn); } //parameters[pIndex] = (string)Helper.GetDateTimeOrDatabaseDateTimeSQL(tableInfo.GetCreatedOn(entity), this, overrideCreatedUpdatedOn); } pIndex = parameters.FindIndex(c => c == "@" + Config.UPDATEDON_COLUMN.Name); if (pIndex >= 0) { var updatedOn = Helper.GetDateTimeOrDatabaseDateTimeSQL(tableInfo.GetUpdatedOn(entity), this, overrideCreatedUpdatedOn); if (updatedOn is string) { parameters[pIndex] = (string)updatedOn; } else { cmd.AddInParameter(parameters[pIndex], Config.CREATEDON_COLUMN.ColumnDbType, updatedOn); } //parameters[pIndex] = (string)Helper.GetDateTimeOrDatabaseDateTimeSQL(tableInfo.GetUpdatedOn(entity), this, overrideCreatedUpdatedOn); } StringBuilder cmdText = new StringBuilder(); cmdText.Append($"INSERT INTO {tableInfo.FullName} ({string.Join(",", columns)}) VALUES({string.Join(",", parameters)});"); if (tableInfo.IsKeyIdentity() && isPrimaryKeyEmpty) { //add query to get inserted id cmdText.Append(LASTINSERTEDROWIDSQL); } //remove common columns and parameters already added above columns.RemoveAll(c => c == Config.CREATEDON_COLUMN.Name || c == Config.CREATEDBY_COLUMN.Name || c == Config.UPDATEDON_COLUMN.Name || c == Config.UPDATEDBY_COLUMN.Name || c == Config.VERSIONNO_COLUMN.Name || c == Config.ISACTIVE_COLUMN.Name); cmd.CommandType = CommandType.Text; cmd.CommandText = cmdText.ToString(); for (int i = 0; i < columns.Count(); i++) { tableInfo.Columns.TryGetValue(columns[i], out ColumnAttribute columnInfo); //find column attribute DbType dbType = DbType.Object; object columnValue = null; if (columnInfo != null && columnInfo.GetMethod != null) { dbType = columnInfo.ColumnDbType; columnValue = columnInfo.GetAction(entity); if (tableInfo.NeedsHistory) { audit.AppendDetail(columns[i], columnValue, dbType, null); } } cmd.AddInParameter("@" + columns[i], dbType, columnValue); } }