Esempio n. 1
0
 internal SqlExpression Value(Type clrType, ProviderType sqlType, object value, bool isClientSpecified, Expression sourceExpression)
 {
     if (typeof(Type).IsAssignableFrom(clrType) && value != null)
     {
         var metaType = model.GetMetaType((Type)value);
         return(StaticType(metaType, sourceExpression));
     }
     return(new SqlValue(clrType, sqlType, value, isClientSpecified, sourceExpression));
 }
Esempio n. 2
0
        /// <summary>
        /// Analyzes the member access expression provided as parameter and
        /// returns an appropiated member access.
        /// </summary>
        /// <param name="member">
        /// The member access to analyze.
        /// </param>
        /// <returns>
        /// A System.Linq.Expressions.Expression.
        /// </returns>
        protected override Expression VisitMember(MemberExpression member)
        {
            if (member == null)
            {
                throw new ArgumentNullException("member");
            }

            Type memberType = member.Type;

            if (member.Expression != null)
            {
                Type objectType = member.Expression.Type;

                MetaModel model = this.Repository.GetModel();

                MetaType memberMetaType = model.GetMetaType(memberType);
                MetaType objectMetaType = model.GetMetaType(objectType);

                if (objectMetaType.Table != null)
                {
                    if (memberMetaType.Table != null)
                    {
                        // old implementation
                        MethodInfo selector = this.IndexesCache.GetType().GetMethod("GetSingleByPropertyValue", BindingFlags.Instance | BindingFlags.NonPublic)
                                              .MakeGenericMethod(memberType);

                        return(this.CreateExplicitJoinMethodCall(member, objectMetaType, selector));
                    }
                    else
                    {
                        // replace with join on associated table [grouped by fk], to grouping key
                        var listInterface = memberType.GetInterfaces().Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IList <>)).SingleOrDefault();

                        if (listInterface != null)
                        {
                            var listElementType = listInterface.GetGenericArguments()[0];

                            var listElementMetaType = model.GetMetaType(listElementType);

                            if (listElementMetaType.Table != null)
                            {
                                MethodInfo selector = this.IndexesCache.GetType().GetMethod("GetAllByPropertyValue", BindingFlags.Instance | BindingFlags.NonPublic)
                                                      .MakeGenericMethod(listElementType);

                                return(this.CreateExplicitJoinMethodCall(member, objectMetaType, selector));
                            }
                        }
                    }
                }
            }

            return(base.VisitMember(member));
        }
Esempio n. 3
0
        /// <summary>
        /// Gets the metatype for entity type
        /// </summary>
        /// <param name="sourceType">
        /// Entity type
        /// </param>
        /// <returns>
        /// Metatype for entity type.
        /// </returns>
        private MetaType GetMetaType(Type sourceType)
        {
            MetaModel model = this.GetModel();
            MetaType  type  = model.GetMetaType(sourceType);

            return(type);
        }
        public static string GetPrimaryKeyName <T>() where T : class
        {
            try
            {
                if (checkExistingContext())
                {
                    // get the table by the type passed in
                    var table = context.GetTable <T>();

                    // get the metamodel mappings (database to
                    // domain objects)
                    MetaModel modelMapping = table.Context.Mapping;

                    // get the data members for this type
                    ReadOnlyCollection <MetaDataMember> dataMembers
                        = modelMapping.GetMetaType(typeof(T))
                          .DataMembers;

                    // find the primary key field and return its
                    // name
                    return((dataMembers.Single <MetaDataMember>(m
                                                                => m.IsPrimaryKey)).Name);
                }
                else
                {
                    throw new Exception();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Método Genérico Obter por PK
        /// </summary>
        /// <typeparam name="T">Tipagem da Classe</typeparam>
        /// <param name="ID">ID do Objeto usado como filtro</param>
        /// <param name="dataContext">Conntexto</param>
        /// <returns>Objeto com ID</returns>
        public T ObterPorID(int ID, DataContext dataContext)
        {
            try
            {
                using (dataContext)
                {
                    var tabela = dataContext.GetTable <T>();

                    MetaModel modelMap = tabela.Context.Mapping;
                    ReadOnlyCollection <MetaDataMember> dataMembers = modelMap.GetMetaType(typeof(T)).DataMembers;

                    string pk = (dataMembers.Single <MetaDataMember>(m => m.IsPrimaryKey)).Name;

                    return(tabela.SingleOrDefault <T>(delegate(T t)
                    {
                        String membroID = t.GetType().GetProperty(pk).GetValue(t, null).ToString();

                        return membroID.ToString() == ID.ToString();
                    }));
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
Esempio n. 6
0
        public T SelPorLlave <T>(String id) where T : class
        {
            try
            {
                // get the table by the type passed in
                var table = this._DB.GetTable <T>();

                // get the metamodel mappings (database to
                // domain objects)
                MetaModel modelMap = table.Context.Mapping;

                // get the data members for this type
                ReadOnlyCollection <MetaDataMember> dataMembers
                    = modelMap.GetMetaType(typeof(T)).DataMembers;

                // find the primary key field name
                // by checking for IsPrimaryKey
                string pk =
                    (dataMembers.Single <MetaDataMember>(m =>
                                                         m.IsPrimaryKey)).Name;

                // return a single object where the id argument
                // matches the primary key field value
                return(table.SingleOrDefault <T>(delegate(T t)
                {
                    String memberId = t.GetType().GetProperty(pk).GetValue(t, null).ToString();
                    return memberId.ToString() == id.ToString();
                }));
            }
            catch (Exception)
            {
                MessageBox.Show("Ha ocurrido un error.\nEl registro no ha sido encontrado", "SysLab", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return(null);
            }
        }
Esempio n. 7
0
            internal override SqlExpression VisitTreat(SqlUnary t)
            {
                t.Operand = this.VisitExpression(t.Operand);
                Type treatType    = t.ClrType;
                Type originalType = model.GetMetaType(t.Operand.ClrType).InheritanceRoot.Type;

                // .NET nullability rules are that typeof(int)==typeof(int?). Let's be consistent with that:
                treatType    = TypeSystem.GetNonNullableType(treatType);
                originalType = TypeSystem.GetNonNullableType(originalType);

                if (treatType == originalType)
                {
                    return(t.Operand);
                }
                if (treatType.IsAssignableFrom(originalType))
                {
                    t.Operand.SetClrType(treatType);
                    return(t.Operand);
                }
                if (!treatType.IsAssignableFrom(originalType) && !originalType.IsAssignableFrom(treatType))
                {
                    if (!treatType.IsInterface && !originalType.IsInterface)
                    {                     // You can't tell when there's an interface involved.
                        // We statically know the TREAT will result in NULL.
                        return(sql.TypedLiteralNull(treatType, t.SourceExpression));
                    }
                }

                //return base.VisitTreat(t);
                return(t);
            }
Esempio n. 8
0
        public override T Reed(int id)
        {
            using (var connection = GetConnection(DBName, login, password))
            {
                // get the table by the type passed in
                var table = connection.GetTable <T>();

                // get the metamodel mappings (database to domain objects)
                MetaModel modelMap = table.Context.Mapping;

                // get the data members for this type
                ReadOnlyCollection <MetaDataMember> dataMembers
                    = modelMap.GetMetaType(typeof(T)).DataMembers;

                // find the primary key field name
                // by checking for IsPrimaryKey
                string pk = (dataMembers.Single <MetaDataMember>(m => m.IsPrimaryKey)).Name;

                // return a single object where the id argument
                // matches the primary key field value
                return(table.SingleOrDefault <T>(delegate(T t)
                {
                    //some reflection
                    Type type = typeof(T);
                    var Entrails = type.GetFields();
                    var ooo = Entrails[0].GetValue(t);

                    return (int)ooo == id;
                }));
            }
        }
Esempio n. 9
0
        // Methods
        internal static MetaType GetSourceMetaType(SqlNode node, MetaModel model)
        {
            Visitor visitor = new Visitor();

            visitor.Visit(node);
            Type nonNullableType = TypeSystem.GetNonNullableType(visitor.sourceType);

            return(model.GetMetaType(nonNullableType));
        }
Esempio n. 10
0
        public override MetaType GetMetaType(Type type)
        {
            MetaTable metaTable;

            if (_customtables.TryGetValue(type, out metaTable))
            {
                return(metaTable.RowType);
            }
            return(_orgmodel.GetMetaType(type));
        }
Esempio n. 11
0
        /// <summary>
        /// Get a MetaType that represents the dynamic type of the given node.
        /// </summary>
        internal static MetaType GetSourceMetaType(SqlNode node, MetaModel model)
        {
            Visitor v = new Visitor();

            v.Visit(node);
            Type type = v.sourceType;

            type = TypeSystem.GetNonNullableType(type); // Emulate CLR's behavior: strip nullability from type.
            return(model.GetMetaType(type));
        }
 public override MetaType GetMetaType(Type type)
 {
     if (_customtypes.ContainsKey(type))
     {
         return(_customtypes[type]);
     }
     else
     {
         return(_orgmodel.GetMetaType(type));
     }
 }
Esempio n. 13
0
 private void ValidateTypesToLoadWith(MetaModel metaModel)
 {
     foreach (var typeToLoadWith in typesToLoadWith)
     {
         var metaType = metaModel.GetMetaType(typeToLoadWith);
         if (metaType.HasInheritance && metaType.InheritanceRoot != metaType)
         {
             throw new InvalidOperationException($"Type {metaType.Type} is not the root type of the inheritance mapping hierarchy," +
                                                 $" so it can't be used for automatic loading.");
         }
     }
 }
Esempio n. 14
0
        public void GetEntity_AdditionalMappedMembersInSubTypesDontCauseException()
        {
            _readerMock
            .Expect(mock => mock.GetValue(2))
            .Return(1);
            _readerMock
            .Expect(mock => mock.GetValue(3))
            .Return("Supplier"); //return value of discriminator column
            _readerMock
            .Expect(mock => mock.GetValue(1))
            .Return("www.homepage.com");

            _reverseMappingResolverMock
            .Expect(mock => mock.GetMetaDataMembers(typeof(ContactWithInheritanceHierarchy)))
            .Return(
                _metaModel.GetMetaType(typeof(ContactWithInheritanceHierarchy.SupplierContact)).DataMembers.ToArray()
                );

            var rowWrapper = new RowWrapper(_readerMock, _reverseMappingResolverMock);

            var columnIDs = new[]
            {
                new ColumnID("HomePage", 1),
                new ColumnID("ContactID", 2),
                new ColumnID("ContactType", 3)
            };

            var contact = rowWrapper.GetEntity <ContactWithInheritanceHierarchy> (columnIDs);

            var expectedContact = new ContactWithInheritanceHierarchy.SupplierContact();

            expectedContact.ContactID   = 1;
            expectedContact.ContactType = "Supplier";
            expectedContact.HomePage    = "www.homepage.com";

            _readerMock.VerifyAllExpectations();
            _reverseMappingResolverMock.VerifyAllExpectations();

            Assert.That(contact.Equals(expectedContact), Is.True);
        }
Esempio n. 15
0
        public T GetById(int id)
        {
            MetaModel modelMap = itemsTable.Context.Mapping;

            // get the data members for this type
            ReadOnlyCollection <MetaDataMember> dataMembers = modelMap.GetMetaType(typeof(T)).DataMembers;

            // find the primary key field name
            // by checking for IsPrimaryKey
            string pk = (dataMembers.Single <MetaDataMember>(m => m.IsPrimaryKey)).Name;

            // return a single object where the id argument
            // matches the primary key field value
            return(itemsTable.SingleOrDefault(i => i.GetType().GetProperty(pk).GetValue(i, null).ToString() == id.ToString()));
        }
        public static T SelectByPK <T>(List <String> ids) where T : class
        {
            try
            {
                if (checkExistingContext())
                {
                    // get the table by the type passed in
                    var table = context.GetTable <T>();

                    // get the metamodel mappings (database to
                    // domain objects)
                    MetaModel modelMap = table.Context.Mapping;

                    // get the data members for this type
                    ReadOnlyCollection <MetaDataMember> dataMembers = modelMap.GetMetaType(typeof(T)).DataMembers;

                    // find the primary key field name
                    // by checking for IsPrimaryKey
                    List <string> pks = new List <string>();
                    foreach (string partKey in ids)
                    {
                        List <MetaDataMember> metaDataPKs = dataMembers.Where <MetaDataMember>(m => m.IsPrimaryKey).Distinct().ToList();;

                        if (metaDataPKs != null)
                        {
                            foreach (MetaDataMember mdm in metaDataPKs)
                            {
                                if (!pks.Contains(mdm.Name))
                                {
                                    pks.Add(mdm.Name);
                                }
                            }
                        }
                    }


                    // return a single object where the id argument
                    // matches the primary key field value

                    return(table.SingleOrDefault <T>(delegate(T t)
                    {
                        List <string> memberIds = new List <string>();
                        foreach (string partKey in pks)
                        {
                            String memberId = t.GetType().GetProperty(partKey).GetValue(t, null).ToString();
                            memberIds.Add(memberId);
                        }
                        int index = 0;
                        bool isKey = true;
                        foreach (string memberId in memberIds)
                        {
                            if (!(memberId.ToString() == ids[index++].ToString()))
                            {
                                isKey = false;
                                break;
                            }
                        }
                        return isKey;
                    }));
                }
                else
                {
                    throw new Exception();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
 public override MetaType GetMetaType(Type type)
 {
     return(source.GetMetaType(type));
 }
Esempio n. 18
0
 public override MetaType GetMetaType(Type type)
 {
     return(model.GetMetaType(type));
 }