public TableMapping(Type type, IEnumerable <PropertyInfo> properties, CreateFlags createFlags = CreateFlags.None, IColumnInformationProvider infoProvider = null) { if (infoProvider == null) { infoProvider = new DefaultColumnInformationProvider(); } MappedType = type; var tableAttr = type.GetTypeInfo().GetCustomAttributes <TableAttribute>().FirstOrDefault(); TableName = tableAttr != null ? tableAttr.Name : MappedType.Name; var props = properties; var cols = new List <Column>(); foreach (var p in props) { var ignore = infoProvider.IsIgnored(p); if (p.CanWrite && !ignore) { cols.Add(new Column(p, createFlags)); } } Columns = cols.ToArray(); foreach (var c in Columns) { if (c.IsAutoInc && c.IsPK) { _autoPk = c; } if (c.IsPK) { PKs.Add(c); } } HasAutoIncPK = _autoPk != null; if (PK != null) { GetByPrimaryKeySql = string.Format("select * from \"{0}\" where \"{1}\" = ?", TableName, PK.Name); PkWhereSql = PKs.Aggregate(new StringBuilder(), (sb, pk) => sb.AppendFormat(" \"{0}\" = ? and", pk.Name), sb => sb.Remove(sb.Length - 3, 3).ToString()); GetByPrimaryKeysSql = String.Format("select * from \"{0}\" where {1}", TableName, PkWhereSql); } else { // People should not be calling Get/Find without a PK GetByPrimaryKeysSql = GetByPrimaryKeySql = string.Format("select * from \"{0}\" limit 1", TableName); } }
public Column(PropertyInfo prop, CreateFlags createFlags = CreateFlags.None, IColumnInformationProvider infoProvider = null) { if (infoProvider == null) { infoProvider = new DefaultColumnInformationProvider(); } _prop = prop; Name = infoProvider.GetColumnName(prop); //If this type is Nullable<T> then Nullable.GetUnderlyingType returns the T, otherwise it returns null, so get the actual type instead ColumnType = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType; Collation = Orm.Collation(prop); IsPK = Orm.IsPK(prop) || (((createFlags & CreateFlags.ImplicitPK) == CreateFlags.ImplicitPK) && string.Compare(prop.Name, Orm.ImplicitPkName, StringComparison.OrdinalIgnoreCase) == 0); var isAuto = Orm.IsAutoInc(prop) || (IsPK && ((createFlags & CreateFlags.AutoIncPK) == CreateFlags.AutoIncPK)); IsAutoGuid = isAuto && ColumnType == typeof(Guid); IsAutoInc = isAuto && !IsAutoGuid; DefaultValue = Orm.GetDefaultValue(prop); Indices = Orm.GetIndices(prop); if (!Indices.Any() && !IsPK && ((createFlags & CreateFlags.ImplicitIndex) == CreateFlags.ImplicitIndex) && Name.EndsWith(Orm.ImplicitIndexSuffix, StringComparison.OrdinalIgnoreCase)) { Indices = new[] { new IndexedAttribute() }; } IsNullable = !(IsPK || Orm.IsMarkedNotNull(prop)); MaxStringLength = Orm.MaxStringLength(prop); }