Example #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();
            }
        }
Example #2
0
        /// <summary>
        /// Insert entity to database
        /// </summary>
        /// <param name="isIdAutoGrow">Id is or isn't auto grow </param>
        /// <param name="transaction">Transaction</param>
        public virtual void Save(bool isIdAutoGrow = true, DbTransaction transaction = null)
        {
            _entityStatus = "saved";
            var isNeedCommit   = false;
            var isSupportBatch = ProviderManager.GetProvider(ProviderName).IsSupportBatch;
            var pk             = IntrospectionManager.GetPrimaryMemberName(TableFullName);
            var sql            = "";

            if (isSupportBatch && ProviderName != DataBaseNames.Oracle)
            {
                sql += ProviderManager.GetProvider(ProviderName).BatchBegin;
            }
            else if (transaction == null)
            {
                DbConnection conn = _sqlHelper.GetConn();
                conn.Open();
                transaction  = conn.BeginTransaction();
                isNeedCommit = true;
            }

            if (isSupportBatch && ProviderName != DataBaseNames.Oracle)
            {
                sql += GetSaveSql(isIdAutoGrow);
                if (isIdAutoGrow)
                {
                    sql += GetLastInsertIdSql();
                }
            }
            else
            {
                var sqlT = GetSaveSql(isIdAutoGrow);
                if (ProviderName == DataBaseNames.Oracle)
                {
                    sqlT = sqlT.Remove(sqlT.LastIndexOf(";"), 1);
                }
                _sqlHelper.ExecuteNonQuery(transaction, sqlT);
                if (isIdAutoGrow)
                {
                    var idValue = _sqlHelper.ExecuteScalar(transaction, GetLastInsertIdSql());

                    if (idValue != null)
                    {
                        var pkType = IntrospectionManager.GetPrimaryKeyType(TableFullName);
                        SetValue(pk, idValue.ChangeType(pkType));
                    }
                }
            }

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

            if (isSupportBatch && ProviderName != DataBaseNames.Oracle)
            {
                object idValue;
                if (transaction == null)
                {
                    idValue = _sqlHelper.ExecuteScalar(sql);
                }
                else
                {
                    idValue = _sqlHelper.ExecuteScalar(transaction, sql);
                }

                if (isIdAutoGrow && idValue != null)
                {
                    var pkType = IntrospectionManager.GetPrimaryKeyType(TableFullName);
                    SetValue(pk, idValue.ChangeType(pkType));
                }
            }

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