Ejemplo n.º 1
0
        /// <summary>
        /// Betch insert
        /// </summary>
        /// <typeparam name="T">Entity</typeparam>
        /// <param name="list">Entity list</param>
        /// <param name="isIdAutoGrow">Is id auto grow?</param>
        /// <param name="trans">Transaction</param>
        public virtual void Save <T>(List <T> list, bool isIdAutoGrow = true, DbTransaction trans = null) where T : Entity
        {
            var sql      = new StringBuilder();
            var typeName = typeof(T).FullName;
            var pk       = IntrospectionManager.GetPrimaryMemberName(typeName);
            var setValue = IntrospectionManager.GetSetDelegate(typeName);

            var isNeedCommit   = false;
            var isSupportBatch = ProviderManager.GetProvider(ProviderName).IsSupportBatch;

            if (isSupportBatch && ProviderName != DataBaseNames.Oracle)
            {
                sql.Append(ProviderManager.GetProvider(ProviderName).BatchBegin);
            }
            else if (trans == null)
            {
                var conn = NativeSql.GetConn();
                conn.Open();
                trans        = conn.BeginTransaction();
                isNeedCommit = true;
            }

            foreach (var item in list)
            {
                item.Key = Key;
                var sqlT = item.GetSaveSql(isIdAutoGrow);
                if (isSupportBatch && ProviderName != DataBaseNames.Oracle)
                {
                    sql.Append(sqlT);
                    if (isIdAutoGrow)
                    {
                        sql.Append(item.GetLastInsertIdSql());
                    }
                }
                else
                {
                    if (ProviderName == DataBaseNames.Oracle)
                    {
                        sqlT = sqlT.Remove(sqlT.LastIndexOf(";"), 1);
                    }
                    NativeSql.ExecuteNonQuery(trans, sqlT);
                    if (isIdAutoGrow)
                    {
                        object idValue = NativeSql.ExecuteScalar(trans, item.GetLastInsertIdSql());
                        if (idValue != null)
                        {
                            var pkType = IntrospectionManager.GetPrimaryKeyType(typeName);
                            setValue(item, pk, idValue.ChangeType(pkType));
                        }
                    }
                }
            }

            if (isSupportBatch && ProviderName != DataBaseNames.Oracle)
            {
                sql.Append(ProviderManager.GetProvider(ProviderName).BatchEnd);

                DataSet ds;
                if (trans == null)
                {
                    ds = NativeSql.ExecuteDataSet(sql.ToString());
                }
                else
                {
                    ds = NativeSql.ExecuteDataSet(trans, sql.ToString());
                }

                if (isIdAutoGrow)
                {
                    var pkType = IntrospectionManager.GetPrimaryKeyType(typeName);
                    for (int i = 0; i < list.Count; i++)
                    {
                        object idValue = ds.Tables[i].Rows[0][0];
                        setValue(list[i], pk, idValue.ChangeType(pkType));
                    }
                }
            }

            if (!isSupportBatch && isNeedCommit)
            {
                var conn = trans.Connection;
                trans.Commit();
                trans.Dispose();
                conn.Close();
                conn.Dispose();
            }
        }