mirror of
				https://github.com/nsnail/FreeSql.git
				synced 2025-11-03 00:55:26 +08:00 
			
		
		
		
	- add Oracle ToSqlBatchIdentityColumn
This commit is contained in:
		@@ -23,6 +23,78 @@ namespace FreeSql.Oracle.Curd
 | 
			
		||||
        public override long ExecuteIdentity() => base.SplitExecuteIdentity(_batchValuesLimit > 0 ? _batchValuesLimit : 500, _batchParameterLimit > 0 ? _batchParameterLimit : 999);
 | 
			
		||||
        public override List<T1> ExecuteInserted() => base.SplitExecuteInserted(_batchValuesLimit > 0 ? _batchValuesLimit : 500, _batchParameterLimit > 0 ? _batchParameterLimit : 999);
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// 批量插入时,如果有序列 + DbInsertValue 设置,则用这个
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        /// <returns></returns>
 | 
			
		||||
        public string ToSqlBatchIdentityColumn()
 | 
			
		||||
        {
 | 
			
		||||
            if (_source == null || _source.Any() == false) return null;
 | 
			
		||||
            var cols = new List<ColumnInfo>();
 | 
			
		||||
            foreach (var col in _table.Columns.Values)
 | 
			
		||||
            {
 | 
			
		||||
                if (col.Attribute.IsIdentity && _insertIdentity == false && string.IsNullOrEmpty(col.DbInsertValue)) continue;
 | 
			
		||||
                if (col.Attribute.IsIdentity == false && _ignore.ContainsKey(col.Attribute.Name)) continue;
 | 
			
		||||
 | 
			
		||||
                cols.Add(col);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            _identCol = null;
 | 
			
		||||
            var sb = new StringBuilder();
 | 
			
		||||
            var tmpsb = new StringBuilder();
 | 
			
		||||
            sb.Append("INSERT INTO ").Append(_commonUtils.QuoteSqlName(TableRuleInvoke())).Append("(");
 | 
			
		||||
            var colidx = 0;
 | 
			
		||||
            foreach (var col in cols)
 | 
			
		||||
            {
 | 
			
		||||
                if (col.Attribute.IsIdentity) _identCol = col;
 | 
			
		||||
                if (colidx > 0)
 | 
			
		||||
                {
 | 
			
		||||
                    sb.Append(", ");
 | 
			
		||||
                    tmpsb.Append(", ");
 | 
			
		||||
                }
 | 
			
		||||
                var colname = _commonUtils.QuoteSqlName(col.Attribute.Name);
 | 
			
		||||
                sb.Append(colname);
 | 
			
		||||
                tmpsb.Append(col.Attribute.IsIdentity && !string.IsNullOrEmpty(col.DbInsertValue) ? col.DbInsertValue : colname);
 | 
			
		||||
                ++colidx;
 | 
			
		||||
            }
 | 
			
		||||
            sb.Append(") ").Append("\r\nSELECT ").Append(tmpsb.ToString()).Append(" FROM \r\n(\r\n");
 | 
			
		||||
            tmpsb.Clear();
 | 
			
		||||
 | 
			
		||||
            _params = _noneParameter ? new DbParameter[0] : new DbParameter[colidx * _source.Count];
 | 
			
		||||
            var specialParams = new List<DbParameter>();
 | 
			
		||||
            var didx = 0;
 | 
			
		||||
            foreach (var d in _source)
 | 
			
		||||
            {
 | 
			
		||||
                if (_source.Count > 1) sb.Append("\r\n  UNION ALL\r\n ");
 | 
			
		||||
                sb.Append("  SELECT ");
 | 
			
		||||
                var colidx2 = 0;
 | 
			
		||||
                foreach (var col in cols)
 | 
			
		||||
                {
 | 
			
		||||
                    if (colidx2 > 0) sb.Append(", ");
 | 
			
		||||
                    if (string.IsNullOrEmpty(col.DbInsertValue) == false)
 | 
			
		||||
                        sb.Append(col.DbInsertValue);
 | 
			
		||||
                    else
 | 
			
		||||
                    {
 | 
			
		||||
                        object val = col.GetDbValue(d);
 | 
			
		||||
                        if (val == null && col.Attribute.IsNullable == false) val = col.CsType == typeof(string) ? "" : Utils.GetDataReaderValue(col.CsType.NullableTypeOrThis(), null);//#384
 | 
			
		||||
                        if (_noneParameter)
 | 
			
		||||
                            sb.Append(_commonUtils.GetNoneParamaterSqlValue(specialParams, _noneParameterFlag, col.Attribute.MapType, val));
 | 
			
		||||
                        else
 | 
			
		||||
                        {
 | 
			
		||||
                            sb.Append(_commonUtils.QuoteWriteParamter(col.Attribute.MapType, _commonUtils.QuoteParamterName($"{col.CsName}_{didx}")));
 | 
			
		||||
                            _params[didx * colidx + colidx2] = _commonUtils.AppendParamter(null, $"{col.CsName}_{didx}", col, col.Attribute.MapType, val);
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                    if (didx == 0) sb.Append(" as ").Append(col.Attribute.Name);
 | 
			
		||||
                    ++colidx2;
 | 
			
		||||
                }
 | 
			
		||||
                sb.Append(" FROM dual ");
 | 
			
		||||
                ++didx;
 | 
			
		||||
            }
 | 
			
		||||
            sb.Append(")");
 | 
			
		||||
            if (_noneParameter && specialParams.Any()) _params = specialParams.ToArray();
 | 
			
		||||
            return sb.ToString();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public override string ToSql()
 | 
			
		||||
        {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user