/// <summary> /// Inspects the properties of an an object looking for those that make up the unique identifier /// and from this returns a list of variables usable in a prepared sql statement /// with names and values for these /// </summary> /// <param name="Entity"></param> /// <returns></returns> public static List <ISQLDMLStatementVariable> GetFilterNameValuePairs(object Entity) { List <ISQLDMLStatementVariable> vars = new List <ISQLDMLStatementVariable> { new SQLDMLStatementVariable { Name = "@Tenant", Value = "TR1" } }; EntityDescriber describer = new EntityDescriber(Entity); foreach (var property in describer.AllPropertyValues()) { var value = property.Value; if (value != null) { vars.Add(new SQLDMLStatementVariable { Name = "@" + property.Name, Value = value }); } } return(vars); }
/// <summary> /// Inspects the properties of an an object looking for those that make up the unique identifier /// and from this and returns a list of variables usable in a prepared sql statement /// with names and values for these /// </summary> /// <param name="Entity"></param> /// <returns></returns> public static string BuildTableCreateForEntitySql(object Entity) { EntityDescriber describer = new EntityDescriber(Entity); string sql = $"create table {Entity.GetType().Name} ("; sql += "Tenant varchar(100), "; var type = Entity.GetType(); string fieldListSql = ""; int i = 0; var properties = describer.AllPropertyValues(); foreach (var property in properties) { fieldListSql += (i > 0 ? ", " : "") + property.Name + " " + MapPropertyTypeToDBType(property.Type.Name); i++; } List <EntityProperty> primaryKeys = null; primaryKeys = describer.PrimaryKeys(); int primKeyCount = 0; string primaryKey = "PRIMARY KEY("; primaryKey += "Tenant, "; foreach (var pk in primaryKeys) { primaryKey += (primKeyCount > 0 ? ", " : "") + pk.Name; primKeyCount++; } List <EntityProperty> uniqueKeys = null; uniqueKeys = describer.UniqueKeys(); int uniqueKeyCount = 0; string uniqueKey = $"CONSTRAINT UQ_{Entity.GetType().Name} UNIQUE("; uniqueKey += "Tenant, "; foreach (var uk in uniqueKeys) { uniqueKey += (uniqueKeyCount > 0 ? ", " : "") + uk.Name; uniqueKeyCount++; } sql += fieldListSql; if (primKeyCount > 0) { sql += ", " + primaryKey + ")"; } if (uniqueKeyCount > 0) { sql += ", " + uniqueKey + ")"; } sql += ")"; return(sql); }
private static List <string> GetObjectPropertyNames(object Entity) { EntityDescriber describer = new EntityDescriber(Entity); return(describer.AllPropertyValues().Select(x => x.Name).ToList <string>()); }