Ejemplo n.º 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ReferenceInfo"/> class.
 /// </summary>
 /// <param name="store">The store.</param>
 /// <param name="name">The name.</param>
 /// <param name="referenceType">Type of the reference.</param>
 /// <param name="propertyInfo">The property info.</param>
 public ReferenceInfo(MetaDataStore store, string name, Type referenceType, PropertyInfo propertyInfo)
     : base(store,
         name,
         store.GetTableInfoFor(referenceType).PrimaryKey.DotNetType,
         store.GetTableInfoFor(referenceType).PrimaryKey.DbType,
         propertyInfo)
 {
     this.referenceType = referenceType;
 }
Ejemplo n.º 2
0
 public void UpdateEntity(Type type, object entity, SqlCommand command)
 {
     using (var reader = command.ExecuteReader())
     {
         reader.Read();
         var tableInfo = metaDataStore.GetTableInfoFor(type);
         Hydrate(tableInfo, entity, GetValuesFromCurrentRow(reader));
     }
 }
Ejemplo n.º 3
0
        public void Insert <TEntity>(IEnumerable <TEntity> entities, int batchSize, int commandTimeOut)
        {
            var tableInfo       = MetaDataStore.GetTableInfoFor <TEntity>();
            var insertStatement = tableInfo.GetInsertStatementWithoutReturningTheIdentityValue();

            var sqlCommandSet = new SqlCommandSetWrapper {
                Connection = Connection, Transaction = Transaction
            };

            foreach (var entity in entities)
            {
                var currentCommand = CreateCommand();
                currentCommand.CommandText = insertStatement;

                foreach (var parameterInfo in tableInfo.GetParametersForInsert(entity))
                {
                    currentCommand.CreateAndAddInputParameter(parameterInfo.DbType, "@" + parameterInfo.Name, parameterInfo.Value);
                }

                sqlCommandSet.Append(currentCommand);

                if (sqlCommandSet.CommandCount == batchSize)
                {
                    ExecuteCurrentBatch(sqlCommandSet);
                    sqlCommandSet = new SqlCommandSetWrapper {
                        Connection = Connection, Transaction = Transaction
                    };
                }
            }

            if (sqlCommandSet.CommandCount > 0)
            {
                ExecuteCurrentBatch(sqlCommandSet);
            }
        }
Ejemplo n.º 4
0
        public TResult GetSingleResult <TResult>()
        {
            var tableInfo = metaDataStore.GetTableInfoFor <TResult>();

            if (tableInfo == null)
            {
                var scalar = (TResult)command.ExecuteScalar();
                command.Dispose();
                return(scalar);
            }

            var result = hydrater.HydrateEntity <TResult>(command);

            command.Dispose();
            return(result);
        }
Ejemplo n.º 5
0
 public IEnumerable <TEntity> FindAll <TEntity>()
 {
     using (var command = CreateCommand())
     {
         command.CommandText = MetaDataStore.GetTableInfoFor <TEntity>().GetSelectStatementForAllFields().ToString();
         return(Hydrater.HydrateEntities <TEntity>(command));
     }
 }
Ejemplo n.º 6
0
 public void Delete <TEntity>(TEntity entity)
 {
     using (var command = CreateCommand())
     {
         var tableInfo = MetaDataStore.GetTableInfoFor <TEntity>();
         command.CommandText = tableInfo.GetDeleteStatement();
         object id = tableInfo.PrimaryKey.PropertyInfo.GetValue(entity, null);
         command.CreateAndAddInputParameter(tableInfo.PrimaryKey.DbType, tableInfo.GetPrimaryKeyParameterName(), id);
         command.ExecuteNonQuery();
         SessionLevelCache.Remove(entity);
     }
 }
Ejemplo n.º 7
0
        public void InitializeProxy(object proxy, Type targetType)
        {
            using (var command = CreateCommand())
            {
                var tableInfo = MetaDataStore.GetTableInfoFor(targetType);
                var query     = tableInfo.GetSelectStatementForAllFields();
                tableInfo.AddWhereByIdClause(query);

                object id = tableInfo.PrimaryKey.PropertyInfo.GetValue(proxy, null);
                command.CommandText = query.ToString();
                command.CreateAndAddInputParameter(tableInfo.PrimaryKey.DbType, tableInfo.GetPrimaryKeyParameterName(), id);

                Hydrater.UpdateEntity(targetType, proxy, command);
            }
        }
Ejemplo n.º 8
0
        public TEntity Update <TEntity>(TEntity entity)
        {
            using (var command = CreateCommand())
            {
                var tableInfo = MetaDataStore.GetTableInfoFor <TEntity>();

                command.CommandText = tableInfo.GetUpdateStatement();

                foreach (var parameterInfo in tableInfo.GetParametersForUpdate(entity))
                {
                    command.CreateAndAddInputParameter(parameterInfo.DbType, parameterInfo.Name, parameterInfo.Value);
                }

                command.ExecuteNonQuery();
                return(entity);
            }
        }
Ejemplo n.º 9
0
        public TEntity Insert <TEntity>(TEntity entity)
        {
            using (var command = CreateCommand())
            {
                var tableInfo = MetaDataStore.GetTableInfoFor <TEntity>();

                command.CommandText = tableInfo.GetInsertStatement();

                foreach (var parameterInfo in tableInfo.GetParametersForInsert(entity))
                {
                    command.CreateAndAddInputParameter(parameterInfo.DbType, parameterInfo.Name, parameterInfo.Value);
                }

                object id = Convert.ChangeType(command.ExecuteScalar(), tableInfo.PrimaryKey.DotNetType);
                tableInfo.PrimaryKey.PropertyInfo.SetValue(entity, id, null);
                SessionLevelCache.Store(typeof(TEntity), id, entity);
                return(entity);
            }
        }
Ejemplo n.º 10
0
        public TEntity Get <TEntity>(object id)
        {
            var cachedEntity = SessionLevelCache.TryToFind(typeof(TEntity), id);

            if (cachedEntity != null)
            {
                return((TEntity)cachedEntity);
            }

            using (var command = CreateCommand())
            {
                var tableInfo = MetaDataStore.GetTableInfoFor <TEntity>();

                var query = tableInfo.GetSelectStatementForAllFields();
                tableInfo.AddWhereByIdClause(query);

                command.CommandText = query.ToString();
                command.CreateAndAddInputParameter(tableInfo.PrimaryKey.DbType, tableInfo.GetPrimaryKeyParameterName(), id);
                return(Hydrater.HydrateEntity <TEntity>(command));
            }
        }
Ejemplo n.º 11
0
        public static object Extend(this object obj1, params object[] obj2)
        {
            IDictionary <string, object> obj_dict1 = new Dictionary <string, object>();
            var tableInfo1 = MetaDataStore.GetTableInfoFor(obj1.GetType());

            if (typeof(System.Dynamic.IDynamicMetaObjectProvider).IsAssignableFrom(obj1.GetType()))
            {
                // dynamic type
                if (obj1.GetType() == typeof(System.Dynamic.ExpandoObject))
                {
                    obj_dict1 = (IDictionary <string, object>)obj1;
                }
            }
            else if (typeof(IDictionary).IsAssignableFrom(obj1.GetType()))
            {
                // dictionary
                var p = obj1 as IDictionary;
                foreach (var item in p.Keys)
                {
                    obj_dict1.Add(item.ToString(), p[item]);
                }
            }
            else
            {
                foreach (PropertyInfo pi in obj1.GetType().GetProperties())
                {
                    // Check whether the property has any information
                    var key = pi.Name;
                    if (tableInfo1 != null)
                    {
                        var ci = tableInfo1.GetColumn(pi);
                        if (ci != null)
                        {
                            key = ci.Name;
                        }
                    }
                    obj_dict1.Add(key, pi.GetValue(obj1, null) ?? DBNull.Value);
                }
            }

            foreach (var obj in obj2)
            {
                IDictionary <string, object> obj_dict2 = new Dictionary <string, object>();
                var tableInfo2 = MetaDataStore.GetTableInfoFor(obj.GetType());
                if (typeof(System.Dynamic.IDynamicMetaObjectProvider).IsAssignableFrom(obj.GetType()))
                {
                    // dynamic type
                    if (obj.GetType() == typeof(System.Dynamic.ExpandoObject))
                    {
                        obj_dict2 = (IDictionary <string, object>)obj;
                    }
                }
                else if (typeof(IDictionary).IsAssignableFrom(obj.GetType()))
                {
                    // dictionary
                    var p = obj as IDictionary;
                    foreach (var item in p.Keys)
                    {
                        obj_dict2.Add(item.ToString(), p[item]);
                    }
                }
                else
                {
                    foreach (PropertyInfo pi in obj.GetType().GetProperties())
                    {
                        // Check whether the property has any information
                        var key = pi.Name;
                        if (tableInfo2 != null)
                        {
                            var ci = tableInfo2.GetColumn(pi);
                            if (ci != null)
                            {
                                key = ci.Name;
                            }
                        }
                        obj_dict2.Add(key, pi.GetValue(obj, null) ?? DBNull.Value);
                    }
                }
                // merge
                foreach (var key in obj_dict2.Keys)
                {
                    obj_dict1.Add(key, obj_dict2[key]);
                }
            }
            return(obj_dict1);
        }
Ejemplo n.º 12
0
 public IQuery CreateQuery <TEntity>(string whereClause)
 {
     return(CreateQuery(metaDataStore.GetTableInfoFor <TEntity>().GetSelectStatementForAllFields() + " " + whereClause));
 }