public Field2DbRelations GetAttributes() { Field2DbRelations attrs = Field2DbRelations.None; if (IsPK) { if (!IsAutoIncrement) { attrs = Field2DbRelations.PK; } else { attrs = Field2DbRelations.PrimaryKey; } } else { if (!IsNullable && !string.IsNullOrEmpty(DefaultValue)) { attrs = Field2DbRelations.InsertDefault | Field2DbRelations.SyncInsert; } else if (!string.IsNullOrEmpty(DefaultValue)) { attrs = Field2DbRelations.SyncInsert; } } return(attrs); }
public EntityPropertyDefinition(string propertyName, string propertyAlias, Field2DbRelations attributes, string description, AccessLevel fieldAccessLevel, AccessLevel propertyAccessLevel, TypeDefinition type, SourceFragmentDefinition sf, EntityDefinition entity) : base(propertyName, propertyAlias, type, attributes, description, fieldAccessLevel, propertyAccessLevel, entity) { if (!type.IsEntityType) { throw new ArgumentException(string.Format("EntityProperty type must be a entity type. Passed {0}", type.Identifier)); } _sf = sf; }
protected PropertyDefinition(string propertyName, string propertyAlias, TypeDefinition type, Field2DbRelations attributes, string description, AccessLevel fieldAccessLevel, AccessLevel propertyAccessLevel, EntityDefinition entity) { _name = propertyName; _type = type; _propertyAlias = string.IsNullOrEmpty(propertyAlias) ? propertyName : propertyAlias; _attributes = attributes; _description = description; _fieldAccessLevel = fieldAccessLevel; _propertyAccessLevel = propertyAccessLevel; Entity = entity; GenerateAttribute = true; Interfaces = new List <PropertyInterface>(); }
protected PropertyDefinition AppendColumn(EntityDefinition e, SourceFieldDefinition c, out bool created, bool transforRawNamesToReadableForm, bool capitalizeNames, bool caseSensitive) { created = false; //var x = e.OwnProperties.OfType<ScalarPropertyDefinition>().Where(pd => // pd.SourceFieldExpression.Trim(']', '[') == c.SourceFieldExpression.Trim('[',']') //); var nsfn = c.SourceFieldExpression.Trim('[', ']'); if (!caseSensitive) { nsfn = nsfn.ToLower(); } var x = e.OwnProperties.Where(pd => { ScalarPropertyDefinition sp = pd as ScalarPropertyDefinition; if (sp != null) { var spsf = sp.SourceFieldExpression.Trim(']', '['); if (!caseSensitive) { spsf = spsf.ToLower(); } return(spsf == nsfn); } else { EntityPropertyDefinition ep = pd as EntityPropertyDefinition; if (ep != null) { return(ep.SourceFields.Any(item => { var spsf = item.SourceFieldExpression.Trim(']', '['); if (!caseSensitive) { spsf = spsf.ToLower(); } return spsf == nsfn; })); } } return(false); } ); PropertyDefinition pe = null; ScalarPropertyDefinition pes = x.OfType <ScalarPropertyDefinition>().SingleOrDefault(); if (pes != null) { pe = pes; } else { pe = x.OfType <EntityPropertyDefinition>().SingleOrDefault(); } GetSourceFragment(c.SourceFragment); if (pe == null) { Field2DbRelations attrs = c.GetAttributes(); string name = GetName(c.SourceFieldExpression); if (!SourceView.GetSourceFields(c.SourceFragment).Any(item => GetName(item.SourceFieldExpression).Equals(Trim(name, transforRawNamesToReadableForm), StringComparison.InvariantCultureIgnoreCase))) { name = Trim(name, transforRawNamesToReadableForm); } if (capitalizeNames) { name = Capitalize(name); } if ((attrs & Field2DbRelations.PK) == Field2DbRelations.PK && c.IsNullable) { throw new WXMLException(string.Format("Column {0}.{1} cannot be nullable since it's a primary key", c.SourceFragment, c.SourceFieldExpression)); } var t = GetClrType(c.SourceType, c.IsNullable, ref attrs); pe = new ScalarPropertyDefinition(e, name, name, attrs, "Auto generated from column " + c.SourceFieldExpression, t, c, AccessLevel.Private, AccessLevel.Public ); e.AddProperty(pe); created = true; } else { Field2DbRelations attrs = c.GetAttributes(); if (attrs == Field2DbRelations.None) { attrs = pe.Attributes; } ScalarPropertyDefinition sp = pe as ScalarPropertyDefinition; if (sp != null) { if (!pe.PropertyType.IsUserType && (attrs & Field2DbRelations.PK) != Field2DbRelations.PK && sp.SourceType != c.SourceType) { pe.PropertyType = GetClrType(c.SourceType, c.IsNullable, ref attrs); } sp.SourceField = c; } if ((attrs & Field2DbRelations.PK) == Field2DbRelations.PK && e.BaseEntity != null && e.BaseEntity.GetPkProperties().Any()) { attrs = attrs & ~Field2DbRelations.PK; } if (attrs == Field2DbRelations.None) { attrs = pe.Attributes; } pe.Attributes = attrs; } return(pe); }
protected EntityPropertyDefinition AppendFK(EntityDefinition e, SourceFragmentDefinition sf, SourceConstraint fk, relation1to1 rb, out bool created, bool transforRawNamesToReadableForm, bool capitalizeNames, bool caseSensitive) { created = false; var rels = SourceView.GetFKRelations(fk); SourceFragmentDefinition m = rels.First().PKField.SourceFragment; EntityDefinition re = e; if (sf != m) { re = GetEntity(m, rb, transforRawNamesToReadableForm, capitalizeNames, caseSensitive); } string rid = "t" + re.Name; TypeDefinition td = Model.GetType(rid, false); if (td == null) { td = new TypeDefinition(rid, re); Model.AddType(td); } string propAlias = td.Entity.Name; if (rels.Count() == 1) { propAlias = Trim(GetName(rels.First().FKField.SourceFieldExpression), true); } if (capitalizeNames) { propAlias = Capitalize(propAlias); } string propName = propAlias; EntityPropertyDefinition ep = null; //try //{ ep = (EntityPropertyDefinition)e.OwnProperties .SingleOrDefault(item => item.Identifier == propAlias); if (ep == null) { ep = e.OwnProperties.OfType <EntityPropertyDefinition>().SingleOrDefault(item => fk.SourceFields.All(sf2 => item.SourceFields.Any(sff => sf2.SourceFieldExpression.Trim('[', ']') == sff.SourceFieldExpression.Trim('[', ']')) )); } if (ep == null) { var one2one = fk.SourceFields.All(sss => sss.IsPK); if (one2one && e.BaseEntity == re) { foreach (var sfd_ in fk.SourceFields) { bool x; var xx = AppendColumn(e, sfd_, out x, transforRawNamesToReadableForm, capitalizeNames, caseSensitive); RaiseOnPropertyCreated(xx, x); } return(null); } int cnt = e.OwnProperties.Count(p => p.Name == propName); if (cnt > 0) { propName = propName + cnt; propAlias = propAlias + cnt; } SourceFragmentDefinition sfd = GetSourceFragment(sf); ep = new EntityPropertyDefinition(propName, propAlias, Field2DbRelations.None, "Auto generated from constraint " + fk.ConstraintName, AccessLevel.Private, AccessLevel.Public, td, sfd, e); e.AddProperty(ep); created = true; foreach (SourceReferences rel in rels) { SourceFieldDefinition fld = SourceView.GetSourceFields(sf).Single(item => item.SourceFieldExpression == rel.FKField.SourceFieldExpression); ScalarPropertyDefinition pk = re.GetPkProperties().SingleOrDefault(item => item.SourceFieldExpression == rel.PKField.SourceFieldExpression); if (pk == null) { if (rel.PKConstraint.ConstraintType != SourceConstraint.UniqueConstraintTypeName) { throw new WXMLException(string.Format("Cannot find pk for constraint {0}", rel.PKConstraint.ConstraintName)); } pk = re.GetProperties().OfType <ScalarPropertyDefinition>().Single(item => item.SourceFieldExpression == rel.PKField.SourceFieldExpression); } ep.AddSourceField(pk.PropertyAlias, fld.SourceFieldExpression, null, fld.SourceType, fld.SourceTypeSize, fld.IsNullable, fld.DefaultValue ); } } else { if (ep.Description == "Auto generated from constraint " + fk.ConstraintName) { ep.PropertyType = td; } } foreach (SourceFieldDefinition pkField in SourceView.GetFKRelations(fk) .Select(item => item.FKField) .Where(item => item.IsPK)) { string pkPropAlias = GetName(pkField.SourceFieldExpression); if (!SourceView.GetSourceFields(pkField.SourceFragment).Any(item => GetName(item.SourceFieldExpression).Equals(Trim(pkPropAlias, transforRawNamesToReadableForm), StringComparison.InvariantCultureIgnoreCase))) { pkPropAlias = Trim(pkPropAlias, transforRawNamesToReadableForm); } //string pkPropAlias = Trim(GetName(pkField.SourceFieldExpression), transforRawNamesToReadableForm); if (capitalizeNames) { pkPropAlias = Capitalize(pkPropAlias); } string pkPropName = pkPropAlias; PropertyDefinition pe = e.OwnProperties .SingleOrDefault(pd => pd.Identifier == pkPropAlias); Field2DbRelations attrs = pkField.GetAttributes(); TypeDefinition pkType = GetClrType(pkField.SourceType, pkField.IsNullable, ref attrs); bool pkCreated = pe == null; if (pkCreated) { int cnt = e.OwnProperties.Count(p => p.Name == pkPropName); if (cnt > 0) { pkPropName = pkPropName + cnt; //pkPropAlias = pkPropAlias + cnt; } pe = new ScalarPropertyDefinition(e, pkPropName, pkPropAlias, attrs, "Auto generated from column " + pkField.SourceFieldExpression, pkType, pkField, AccessLevel.Private, AccessLevel.Public); e.AddProperty(pe); } else { l1: if (pe is ScalarPropertyDefinition) { pe.Attributes |= attrs; pe.PropertyType = pkType; ((ScalarPropertyDefinition)pe).SourceField = pkField; } else { int cnt = e.OwnProperties.Count(p => p.Identifier == pkPropAlias); if (cnt > 0) { if (e.OwnProperties.Any(item => item.Identifier == GetName(pkField.SourceFieldExpression))) { pkPropAlias = pkPropAlias + cnt; } else { pkPropAlias = GetName(pkField.SourceFieldExpression); } } pkPropName = pkPropAlias; pe = e.OwnProperties.SingleOrDefault(item => item.PropertyAlias == pkPropAlias); if (pe != null) { if (pe is EntityPropertyDefinition) { throw new WXMLParserException(string.Format("Property {0} expected to be of type ScalarPropertyDefinition", pe.Identifier)); } goto l1; } pe = new ScalarPropertyDefinition(e, pkPropName, pkPropAlias, attrs, "Auto generated from column " + pkField.SourceFieldExpression, pkType, pkField, AccessLevel.Private, AccessLevel.Public); e.AddProperty(pe); } } RaiseOnPropertyCreated(pe, pkCreated); } //} //catch //{ // int i = 10; //} return(ep); }
private TypeDefinition GetClrType(string dbType, bool nullable, ref Field2DbRelations attr) { TypeDefinition t = null; string id = null; string type = null; switch (dbType) { case "rowversion": case "timestamp": id = "tBytes"; type = "System.Byte[]"; attr = attr | Field2DbRelations.RowVersion; break; case "varchar": case "nvarchar": case "char": case "nchar": case "text": case "ntext": case "enum": case "mediumtext": id = "tString"; type = "System.String"; break; case "int": case "mediumint": id = "tInt32"; type = "System.Int32"; break; case "smallint": id = "tInt16"; type = "System.Int16"; break; case "bigint": id = "tInt64"; type = "System.Int64"; break; case "tinyint": id = "tByte"; type = "System.Byte"; break; case "datetime": case "smalldatetime": case "date": id = "tDateTime"; type = "System.DateTime"; break; case "time": id = "tTime"; type = "System.Timespan"; break; case "money": case "numeric": case "decimal": case "smallmoney": id = "tDecimal"; type = "System.Decimal"; break; case "float": case "double": id = "tDouble"; type = "System.Double"; break; case "real": id = "tSingle"; type = "System.Single"; break; case "varbinary": case "binary": id = "tBytes"; type = "System.Byte[]"; break; case "bit": id = "tBoolean"; type = "System.Boolean"; break; case "xml": id = "tXML"; type = "System.Xml.XmlDocument"; break; case "uniqueidentifier": id = "tGUID"; type = "System.Guid"; break; case "image": id = "tBytes"; type = "System.Byte[]"; break; default: throw new ArgumentException("Unknown database type " + dbType); } if (nullable) { id += "nullable"; } t = Model.GetType(id, false); if (t == null) { Type tp = GetTypeByName(type); if (nullable && tp.IsValueType) { type = String.Format("System.Nullable`1[{0}]", type); } t = new TypeDefinition(id, type); Model.AddType(t); } return(t); }
//public ScalarPropertyDefinition(string propertyName) // : this(null, propertyName, propertyName, Field2DbRelations.None, null, null, null, AccessLevel.Private, AccessLevel.Public) //{ //} //public ScalarPropertyDefinition(string propertyName, string propertyAlias, Field2DbRelations attributes, string description, // TypeDefinition type, SourceFieldDefinition sf, // AccessLevel fieldAccessLevel, AccessLevel propertyAccessLevel) // : this(null, propertyName, propertyAlias, attributes, description, type, sf, fieldAccessLevel, propertyAccessLevel) //{ //} public ScalarPropertyDefinition(EntityDefinition entity, string propertyName, string propertyAlias, Field2DbRelations attributes, string description, TypeDefinition type, SourceFieldDefinition sf, AccessLevel fieldAccessLevel, AccessLevel propertyAccessLevel) : base(propertyName, propertyAlias, type, attributes, description, fieldAccessLevel, propertyAccessLevel, entity) { _sf = sf; }
public bool HasAttribute(Field2DbRelations attribute) { return((_attributes & attribute) == attribute); }
public static string GetType(SourceFieldDefinition field, TypeDefinition propType, Field2DbRelations attrs, bool unicodeStrings) { string result = field.SourceType; if (string.IsNullOrEmpty(result)) { switch (propType.ClrType.FullName) { case "System.Boolean": result = "bit"; break; case "System.Byte": result = "tinyint"; break; case "System.Int16": case "System.SByte": result = "smallint"; break; case "System.Int32": case "System.UInt16": result = "int"; break; case "System.Int64": case "System.UInt32": result = "bigint"; break; case "System.UInt64": result = "decimal"; break; case "System.Decimal": result = "money"; break; case "System.Single": result = "real"; break; case "System.Double": result = "float"; break; case "System.String": result = string.Format(unicodeStrings ? "nvarchar({0})" : "varchar({0})", field.SourceTypeSize.HasValue ? field.SourceTypeSize.Value : 50); break; case "System.Char": result = unicodeStrings ? "nchar(1)" : "char(1)"; break; case "System.Xml.XmlDocument": case "System.Xml.XmlDocumentFragment": case "System.Xml.Linq.XDocument": case "System.Xml.Linq.XElement": result = "xml"; break; case "System.DateTime": result = "datetime"; break; case "System.GUID": result = "uniqueidentifier"; break; case "System.Char[]": result = string.Format(unicodeStrings ? "nvarchar({0})" : "varchar({0})", field.SourceTypeSize.HasValue ? field.SourceTypeSize.Value : 50); break; case "System.Byte[]": if ((attrs & Field2DbRelations.RV) == Field2DbRelations.RV) { result = "rowversion"; } else { result = string.Format("varbinary({0})", field.SourceTypeSize.HasValue ? field.SourceTypeSize.Value : 50); } break; default: throw new NotSupportedException(propType.ClrType.FullName); } } else if (field.SourceTypeSize.HasValue && !result.Contains(field.SourceTypeSize.Value.ToString())) { result += string.Format("({0})", field.SourceTypeSize.Value); } return(result); }