Esempio n. 1
0
        public static bool Update <T>(this IDbConnection connection, T entityToUpdate, IDbTransaction transaction = null, int?commandTimeout = default(int?)) where T : class
        {
            IProxy proxy = entityToUpdate as IProxy;

            if (proxy != null && !proxy.IsDirty)
            {
                return(false);
            }
            Type type = typeof(T);

            if (type.IsArray)
            {
                type = type.GetElementType();
            }
            else if (type.IsGenericType())
            {
                type = type.GetGenericArguments()[0];
            }
            List <PropertyInfo> list  = KeyPropertiesCache(type).ToList();
            List <PropertyInfo> list2 = ExplicitKeyPropertiesCache(type);

            if (!list.Any() && !list2.Any())
            {
                throw new ArgumentException("Entity must have at least one [Key] or [ExplicitKey] property");
            }
            string        tableName     = GetTableName(type);
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.AppendFormat("update {0} set ", tableName);
            List <PropertyInfo> first = TypePropertiesCache(type);

            list.AddRange(list2);
            List <PropertyInfo> second    = ComputedPropertiesCache(type);
            List <PropertyInfo> list3     = first.Except(list.Union(second)).ToList();
            ISqlAdapter         formatter = GetFormatter(connection);

            for (int i = 0; i < list3.Count; i++)
            {
                PropertyInfo propertyInfo = list3.ElementAt(i);
                formatter.AppendColumnNameEqualsValue(stringBuilder, propertyInfo.Name);
                if (i < list3.Count - 1)
                {
                    stringBuilder.AppendFormat(", ");
                }
            }
            stringBuilder.Append(" where ");
            for (int j = 0; j < list.Count; j++)
            {
                PropertyInfo propertyInfo2 = list.ElementAt(j);
                formatter.AppendColumnNameEqualsValue(stringBuilder, propertyInfo2.Name);
                if (j < list.Count - 1)
                {
                    stringBuilder.AppendFormat(" and ");
                }
            }
            return(SqlMapper.Execute(connection, stringBuilder.ToString(), (object)entityToUpdate, transaction, commandTimeout, (CommandType?)null) > 0);
        }
Esempio n. 2
0
        public static bool Delete <T>(this IDbConnection connection, T entityToDelete, IDbTransaction transaction = null, int?commandTimeout = default(int?)) where T : class
        {
            if (entityToDelete == null)
            {
                throw new ArgumentException("Cannot Delete null Object", "entityToDelete");
            }
            Type type = typeof(T);

            if (type.IsArray)
            {
                type = type.GetElementType();
            }
            else if (type.IsGenericType())
            {
                type = type.GetGenericArguments()[0];
            }
            List <PropertyInfo> list  = KeyPropertiesCache(type).ToList();
            List <PropertyInfo> list2 = ExplicitKeyPropertiesCache(type);

            if (!list.Any() && !list2.Any())
            {
                throw new ArgumentException("Entity must have at least one [Key] or [ExplicitKey] property");
            }
            string tableName = GetTableName(type);

            list.AddRange(list2);
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.AppendFormat("delete from {0} where ", tableName);
            ISqlAdapter formatter = GetFormatter(connection);

            for (int i = 0; i < list.Count; i++)
            {
                PropertyInfo propertyInfo = list.ElementAt(i);
                formatter.AppendColumnNameEqualsValue(stringBuilder, propertyInfo.Name);
                if (i < list.Count - 1)
                {
                    stringBuilder.AppendFormat(" and ");
                }
            }
            return(SqlMapper.Execute(connection, stringBuilder.ToString(), (object)entityToDelete, transaction, commandTimeout, (CommandType?)null) > 0);
        }
Esempio n. 3
0
        /// <summary>
        /// Inserts an entity into table "Ts" asynchronously using .NET 4.5 Task and returns identity id.
        /// </summary>
        /// <typeparam name="T">The type being inserted.</typeparam>
        /// <param name="connection">Open SqlConnection</param>
        /// <param name="entityToInsert">Entity to insert</param>
        /// <param name="transaction">The transaction to run under, null (the default) if none</param>
        /// <param name="commandTimeout">Number of seconds before command execution timeout</param>
        /// <param name="sqlAdapter">The specific ISqlAdapter to use, auto-detected based on connection if null</param>
        /// <returns>Identity of inserted entity</returns>
        public static Task <int> InsertAsync <T>(this IDbConnection connection, T entityToInsert, IDbTransaction transaction = null,
                                                 int?commandTimeout = null, ISqlAdapter sqlAdapter = null) where T : class
        {
            var type = typeof(T);

            sqlAdapter = sqlAdapter ?? GetFormatter(connection);

            var isList = false;

            if (type.IsArray)
            {
                isList = true;
                type   = type.GetElementType();
            }
            else if (type.GetTypeInfo().IsInterface&& type.GetTypeInfo().ImplementedInterfaces.Any(ti => ti.GetTypeInfo().IsGenericType&& ti.GetGenericTypeDefinition() == typeof(IEnumerable <>)))
            {
                isList = true;
                type   = type.GetGenericArguments() [0];
            }

            var name                              = GetTableName(type);
            var sbColumnList                      = new StringBuilder(null);
            var allProperties                     = TypePropertiesCache(type);
            var explicitKeyProperties             = ExplicitKeyPropertiesCache(type);
            var keyProperties                     = KeyPropertiesCache(type);
            var computedProperties                = ComputedPropertiesCache(type);
            var allPropertiesExceptKeyAndComputed = allProperties.Except(keyProperties.Union(computedProperties)).ToList();

            for (var i = 0; i < allPropertiesExceptKeyAndComputed.Count; i++)
            {
                var property = allPropertiesExceptKeyAndComputed[i];
                sqlAdapter.AppendColumnName(sbColumnList, property.Name);
                if (i < allPropertiesExceptKeyAndComputed.Count - 1)
                {
                    sbColumnList.Append(", ");
                }
            }

            var sbParameterList = new StringBuilder(null);

            for (var i = 0; i < allPropertiesExceptKeyAndComputed.Count; i++)
            {
                var property = allPropertiesExceptKeyAndComputed[i];
                sqlAdapter.AppendColumnNameEqualsValue(sbParameterList, property.Name);
                if (i < allPropertiesExceptKeyAndComputed.Count - 1)
                {
                    sbParameterList.Append(", ");
                }
            }

            if (!isList) //single entity
            {
                var keyProp = (keyProperties.Count == 0) ? explicitKeyProperties : keyProperties;
                return(sqlAdapter.InsertAsync(connection, transaction, commandTimeout, name, sbColumnList.ToString(),
                                              sbParameterList.ToString(), keyProp, entityToInsert));
            }

            //insert list of entities
            var cmd = $"INSERT INTO {name} ({sbColumnList}) values ({sbParameterList})";

            return(connection.ExecuteAsync(cmd, entityToInsert, transaction, commandTimeout));
        }