Exemplo n.º 1
0
        /// <summary>
        /// Deletes an entity using the entity metadata.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        public virtual void Delete <T>(T entity) where T : IEntity
        {
            if (entity == null)
            {
                throw new Exception("Error in Delete() method. Entity cannot be null!");
            }

            var info = MetaDataStore.GetTableInfoFor <T>();
            var keys = info.Columns.Where(c => c.Key).ToList();

            if (!keys.Any())
            {
                throw new Exception("Cannot delete Entity - no keys defined.");
            }

            // Get the parameters
            var ht = GetParametersAsHashtableFor <T>(entity, keys);

            // Execute
            var sql = string.Format(
                "DELETE FROM {0} WHERE {1}",
                info.TableName[CrudOperation.DELETE],
                string.Join(" AND ", keys.Select(k => string.Format("{0} = @{0}", k.Name)))
                );

            Execute(sql, ht);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Updates an entity using the entity metadata.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        public virtual void Update <T>(T entity) where T : IEntity
        {
            if (entity == null)
            {
                throw new Exception("Error in Update() method. Entity cannot be null!");
            }

            var info    = MetaDataStore.GetTableInfoFor <T>();
            var columns = info.Columns.ToList();
            var keys    = columns.Where(c => c.Key).ToList();

            if (!keys.Any())
            {
                throw new Exception("Cannot update Entity - no keys defined.");
            }

            var ht = GetParametersAsHashtableFor <T>(entity, columns);

            // Execute
            var sql = string.Format(
                "UPDATE {0} SET {1} WHERE {2}",
                info.TableName[CrudOperation.UPDATE],
                string.Join(",", columns.Where(c => c.DatabaseGenerated == false).Select(c => string.Format("{0} = @{0}", c.Name))),
                string.Join(" AND ", keys.Select(k => string.Format("{0} = @{0}", k.Name)))
                );

            Execute(sql, ht);
        }
Exemplo n.º 3
0
        public virtual T Find <T>(params object[] id) where T : IEntity
        {
            Hashtable ht = new Hashtable();

            var info       = MetaDataStore.GetTableInfoFor <T>();
            var columns    = info.Columns.ToList();
            var parameters = id.ToList();
            var keys       = columns.Where(c => c.Key).OrderBy(c => c.Order).ToList();

            if (parameters.Count() != keys.Count())
            {
                throw new Exception("Cannot find entity. Incorrect number of key values passed in to method.");
            }

            // Get the parameters
            for (int i = 0; i < keys.Count(); i++)
            {
                ht.Add(keys[i].Name, parameters[i]);
            }

            // Execute
            var sql = string.Format(
                "SELECT * FROM {0} WHERE {1}",
                info.TableName[CrudOperation.READ],
                string.Join(" AND ", keys.Select(k => string.Format("{0} = @{0}", k.Name)))
                );

            return(QueryEntity <T>(sql, ht).FirstOrDefault());
        }
Exemplo n.º 4
0
        private void Hydrate <T>(T entity, Hashtable values) where T : IEntity
        {
            var tableInfo = MetaDataStore.GetTableInfoFor <T>();

            var properties = typeof(T).GetProperties();

            foreach (var propertyInfo in properties)
            {
                if (propertyInfo.CanWrite)
                {
                    var columnInfo = tableInfo.GetColumn(propertyInfo);
                    if (columnInfo != null)
                    {
                        object value = values[columnInfo.Name];
                        if (value is DBNull)
                        {
                            value = null;
                        }

                        // If enum column, convert database value to enum
                        if (propertyInfo.PropertyType.IsEnum)
                        {
                            value = Enum.Parse(propertyInfo.PropertyType, value.ToString());
                        }

                        propertyInfo.SetValue(entity, value, null);
                    }
                }
            }
        }
Exemplo n.º 5
0
        public virtual void Insert <T>(T entity, string destinationTable = null) where T : IEntity
        {
            var info    = MetaDataStore.GetTableInfoFor <T>();
            var columns = info.Columns.ToList().Where(c => c.DatabaseGenerated == false);
            var ht      = GetParametersAsHashtableFor <T>(entity, columns);

            // Execute
            var sql = string.Format(
                "INSERT INTO {0} ({1}) SELECT {2}",
                destinationTable ?? info.TableName[CrudOperation.CREATE],
                string.Join(",", columns.Select(c => c.Name)),
                string.Join(",", columns.Select(c => string.Format("@{0}", c.Name)))
                );

            Execute(sql, ht);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Selects all the entities in a set.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public virtual IEnumerable <T> Select <T>(T where = null, bool buffered = true) where T : class, IEntity
        {
            var info = MetaDataStore.GetTableInfoFor <T>();
            Dictionary <string, object> whereItems = new Dictionary <string, object>();

            if (where != null)
            {
                var columns = MetaDataStore.GetTableInfoFor <T>().Columns;
                foreach (var column in columns)
                {
                    var value = column.PropertyInfo.GetValue(where);
                    var dflt  = column.PropertyInfo.PropertyType.Default();

                    if (value != null && !value.Equals(dflt))
                    {
                        whereItems.Add(column.Name, column.PropertyInfo.GetValue(where));
                    }
                }
            }

            if (whereItems.Keys.Count == 0)
            {
                var sql = string.Format(
                    "SELECT * FROM {0}",
                    info.TableName[CrudOperation.READ]
                    );
                return(QueryEntity <T>(sql, null, CommandType.Text, buffered));
            }
            else
            {
                var sql = string.Format(
                    "SELECT * FROM {0} WHERE {1}",
                    info.TableName[CrudOperation.READ],
                    string.Join(" AND ", whereItems.Keys.Select(v => string.Format("{0} = @{1}", v, v)))
                    );
                return(QueryEntity <T>(sql, whereItems, CommandType.Text, buffered));
            }
        }