123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using Oracle.ManagedDataAccess.Client;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace SqlSugar
- {
- public class OracleBlukCopy
- {
- internal List<IGrouping<int, DbColumnInfo>> DbColumnInfoList { get; set; }
- internal SqlSugarProvider Context { get; set; }
- internal ISqlBuilder Builder { get; set; }
- internal InsertBuilder InsertBuilder { get; set; }
- internal object[] Inserts { get; set; }
- public int ExecuteBulkCopy()
- {
- if (DbColumnInfoList == null || DbColumnInfoList.Count == 0) return 0;
- if (Inserts.First().GetType() == typeof(DataTable))
- {
- return WriteToServer();
- }
- DataTable dt = GetCopyData();
- OracleBulkCopy bulkCopy = GetBulkCopyInstance();
- bulkCopy.DestinationTableName = InsertBuilder.GetTableNameString;
- try
- {
- bulkCopy.WriteToServer(dt);
- }
- catch (Exception ex)
- {
- CloseDb();
- throw ex;
- }
- CloseDb();
- return DbColumnInfoList.Count;
- }
- public async Task<int> ExecuteBulkCopyAsync()
- {
- if (DbColumnInfoList == null || DbColumnInfoList.Count == 0) return 0;
- if (Inserts.First().GetType() == typeof(DataTable))
- {
- return WriteToServer();
- }
- DataTable dt = GetCopyData();
- OracleBulkCopy bulkCopy = GetBulkCopyInstance();
- bulkCopy.DestinationTableName = InsertBuilder.GetTableNameString;
- try
- {
- await Task.Run(() => bulkCopy.WriteToServer(dt));
- }
- catch (Exception ex)
- {
- CloseDb();
- throw ex;
- }
- CloseDb();
- return DbColumnInfoList.Count;
- }
- private int WriteToServer()
- {
- var dt = this.Inserts.First() as DataTable;
- if (dt == null)
- return 0;
- Check.Exception(dt.TableName == "Table", "dt.TableName can't be null ");
- dt = GetCopyWriteDataTable(dt);
- OracleBulkCopy copy = GetBulkCopyInstance();
- copy.DestinationTableName = this.Builder.GetTranslationColumnName(dt.TableName);
- copy.WriteToServer(dt);
- CloseDb();
- return dt.Rows.Count;
- }
- private DataTable GetCopyWriteDataTable(DataTable dt)
- {
- var result = this.Context.Ado.GetDataTable("select * from " + this.Builder.GetTranslationColumnName(dt.TableName) + " where 1 > 2 ");
- foreach (DataRow item in dt.Rows)
- {
- DataRow dr = result.NewRow();
- foreach (DataColumn column in result.Columns)
- {
- if (dt.Columns.Cast<DataColumn>().Select(it => it.ColumnName.ToLower()).Contains(column.ColumnName.ToLower()))
- {
- dr[column.ColumnName] = item[column.ColumnName];
- if (dr[column.ColumnName] == null)
- {
- dr[column.ColumnName] = DBNull.Value;
- }
- }
- }
- result.Rows.Add(dr);
- }
- result.TableName = dt.TableName;
- return result;
- }
- private OracleBulkCopy GetBulkCopyInstance()
- {
- if (this.Context.Ado.Connection.State == ConnectionState.Closed)
- {
- this.Context.Ado.Connection.Open();
- }
- OracleBulkCopy copy;
- if (this.Context.Ado.Transaction == null)
- {
- copy = new OracleBulkCopy((OracleConnection)this.Context.Ado.Connection, Oracle.ManagedDataAccess.Client.OracleBulkCopyOptions.Default);
- }
- else
- {
- copy = new OracleBulkCopy((OracleConnection)this.Context.Ado.Connection, OracleBulkCopyOptions.UseInternalTransaction);
- }
- return copy;
- }
- private DataTable GetCopyData()
- {
- var dt = this.Context.Ado.GetDataTable("select * from " + InsertBuilder.GetTableNameString + " where 1 > 2 ");
- foreach (var rowInfos in DbColumnInfoList)
- {
- var dr = dt.NewRow();
- foreach (DataColumn item in dt.Columns)
- {
- var rows = rowInfos.ToList();
- var value = rows.FirstOrDefault(it =>
- it.DbColumnName.Equals(item.ColumnName, StringComparison.CurrentCultureIgnoreCase) ||
- it.PropertyName.Equals(item.ColumnName, StringComparison.CurrentCultureIgnoreCase)
- );
- if (value != null)
- {
- if (value.Value != null && UtilMethods.GetUnderType(value.Value.GetType()) == UtilConstants.DateType)
- {
- if (value.Value != null && value.Value.ToString() == DateTime.MinValue.ToString())
- {
- value.Value = Convert.ToDateTime("1900/01/01");
- }
- }
- if (value.Value == null)
- {
- value.Value = DBNull.Value;
- }
- dr[item.ColumnName] = value.Value;
- }
- }
- dt.Rows.Add(dr);
- }
- if (this.InsertBuilder.OracleSeqInfoList != null && this.InsertBuilder.OracleSeqInfoList.Any())
- {
- var ids = this.InsertBuilder.OracleSeqInfoList.Select(it => it.Value).ToList();
- var columnInfo = this.InsertBuilder.EntityInfo.Columns.Where(it => !string.IsNullOrEmpty(it.OracleSequenceName)).First();
- var identityName = columnInfo.DbColumnName;
- ids.Add(this.Context.Ado.GetInt(" select " + columnInfo.OracleSequenceName + ".nextval from dual"));
- int i = 0;
- foreach (DataRow item in dt.Rows)
- {
- item[identityName] = ids[i];
- ++i;
- }
- }
- return dt;
- }
- private void CloseDb()
- {
- if (this.Context.CurrentConnectionConfig.IsAutoCloseConnection && this.Context.Ado.Transaction == null)
- {
- this.Context.Ado.Connection.Close();
- }
- }
- }
- }
|