Exemple #1
0
        public static string getCmdInsert(ISqlAdapter adapter, ICollection <KeyValuePair <string, Object> > entityToInsert, string tableName, string keyProperties = null)
        {
            var sbColumnList    = new StringBuilder(null);
            var sbParameterList = new StringBuilder(null);
            var index           = 0;

            foreach (var item in entityToInsert)
            {
                var property = (KeyValuePair <string, object>)item;
                //
                if (!string.IsNullOrEmpty(keyProperties) && property.Key.ToUpper() == keyProperties.ToUpper())
                {
                    continue;
                }
                else
                {
                    index++;
                }
                // Column
                adapter.AppendColumnName(sbColumnList, property.Key);
                if (index < entityToInsert.Count)
                {
                    sbColumnList.Append(",");
                }
                // Parameter
                adapter.AppendParametr(sbParameterList, property.Key);
                if (index < entityToInsert.Count)
                {
                    sbParameterList.Append(",");
                }
            }
            var cmd = $"insert into {tableName} ({sbColumnList.ToString().Trim(',')}) values ({sbParameterList.ToString().Trim(',')})";

            return(cmd);
        }
        /// <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);
            var isList = false;

            sqlAdapter = sqlAdapter ?? GetFormatter(connection);
            if (type.IsArray)
            {
                isList = true;
                type   = type.GetElementType();
            }
            else if (type.IsGenericType())
            {
                var  typeInfo = type.GetTypeInfo();
                bool implementsGenericIEnumerableOrIsGenericIEnumerable =
                    typeInfo.ImplementedInterfaces.Any(ti => ti.IsGenericType() && ti.GetGenericTypeDefinition() == typeof(IEnumerable <>)) ||
                    typeInfo.GetGenericTypeDefinition() == typeof(IEnumerable <>);

                if (implementsGenericIEnumerableOrIsGenericIEnumerable)
                {
                    isList = true;
                    type   = type.GetGenericArguments()[0];
                }
            }

            var name               = GetTableName(type);
            var sbColumnList       = new StringBuilder(null);
            var allProperties      = TypePropertiesCache(type);
            var keyProperties      = KeyPropertiesCache(type).ToList();
            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, GetColumnName(property), "");
                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.AppendParametr(sbParameterList, property.Name);
                if (i < allPropertiesExceptKeyAndComputed.Count - 1)
                {
                    sbParameterList.Append(", ");
                }
            }

            return(sqlAdapter.InsertAsync(connection, transaction, commandTimeout, name, sbColumnList.ToString(),
                                          sbParameterList.ToString(), keyProperties, entityToInsert, isList));
        }