Exemplo n.º 1
0
        public override int InsertBatch <T>(
            DbManager db,
            string insertText,
            IEnumerable <T> collection,
            MemberMapper[]                 members,
            int maxBatchSize,
            DbManager.ParameterProvider <T> getParameters)
        {
            if (db.Transaction != null)
            {
                return(base.InsertBatch(db, insertText, collection, members, maxBatchSize, getParameters));
            }

            var idx = insertText.IndexOf('\n');
            var tbl = insertText.Substring(0, idx).Substring("INSERT INTO ".Length).TrimEnd('\r');
            var rd  = new BulkCopyReader(members, collection);
            var bc  = new SqlBulkCopy((SqlConnection)db.Connection)
            {
                BatchSize            = maxBatchSize,
                DestinationTableName = tbl,
            };

            foreach (var memberMapper in members)
            {
                bc.ColumnMappings.Add(new SqlBulkCopyColumnMapping(memberMapper.Ordinal, memberMapper.Name));
            }

            bc.WriteToServer(rd);

            return(rd.Count);
        }
Exemplo n.º 2
0
        public override int InsertBatch <T>(
            DbManager db,
            string insertText,
            IEnumerable <T> collection,
            MemberMapper[]                 members,
            int maxBatchSize,
            DbManager.ParameterProvider <T> getParameters)
        {
            var idx = insertText.IndexOf('\n');
            var tbl = insertText.Substring(0, idx).Substring("INSERT INTO ".Length).TrimEnd('\r');
            var rd  = new BulkCopyReader(members, collection);
            var bc  = new SqlBulkCopy((SqlConnection)db.Connection, SqlDataProvider.SqlBulkCopyOptions, (SqlTransaction)db.Transaction)
            {
                BatchSize            = maxBatchSize,
                DestinationTableName = tbl,
            };

            for (var index = 0; index < members.Length; index++)
            {
                bc.ColumnMappings.Add(new SqlBulkCopyColumnMapping(index, members[index].Name));
            }

            bc.WriteToServer(rd);

            return(rd.Count);
        }
Exemplo n.º 3
0
 protected int ExecuteForEach(
     DbManager db,
     IEnumerable <T> collection,
     MemberMapper[] members,
     int maxBatchSize,
     DbManager.ParameterProvider <T> getParameters)
 {
     return(db.ExecuteForEach(collection, members, maxBatchSize, getParameters));
 }
Exemplo n.º 4
0
 public virtual int InsertBatchWithIdentity <T>(
     DbManager db,
     string insertText,
     IEnumerable <T> collection,
     MemberMapper[] members,
     int maxBatchSize,
     DbManager.ParameterProvider <T> getParameters)
 {
     throw new NotImplementedException("Insert batch with identity is not implemented!");
 }
Exemplo n.º 5
0
 public virtual int InsertBatch <T>(
     DbManager db,
     string insertText,
     IEnumerable <T> collection,
     MemberMapper[] members,
     int maxBatchSize,
     DbManager.ParameterProvider <T> getParameters)
 {
     db.SetCommand(insertText);
     return(db.ExecuteForEach(collection, members, maxBatchSize, getParameters));
 }
Exemplo n.º 6
0
 public override int InsertBatchWithIdentity <T>(
     DbManager db,
     string insertText,
     IEnumerable <T> collection,
     Mapping.MemberMapper[]          members,
     int maxBatchSize,
     DbManager.ParameterProvider <T> getParameters)
 {
     if (db.UseQueryText && Name == ProviderFullName.Oracle)
     {
         List <string> sqlList = _dataProviderInterpreter.GetInsertBatchSqlList(insertText, collection, members, maxBatchSize, true);
         return(ExecuteSqlList(db, sqlList));
     }
     return(base.InsertBatchWithIdentity(db, insertText, collection, members, maxBatchSize, getParameters));
 }
Exemplo n.º 7
0
 public override int InsertBatch <T>(
     DbManager db,
     string insertText,
     IEnumerable <T> collection,
     Mapping.MemberMapper[]          members,
     int maxBatchSize,
     DbManager.ParameterProvider <T> getParameters)
 {
     if (Name == ProviderFullName.Oracle)
     {
         if (db.UseQueryText)
         {
             List <string> sqlList = _dataProviderInterpreter.GetInsertBatchSqlList(insertText, collection, members, maxBatchSize, false);
             return(ExecuteSqlList(db, sqlList));
         }
         throw new NotSupportedException("Set UseQueryText = true on the current generic data provider!");
     }
     return(base.InsertBatch(db, insertText, collection, members, maxBatchSize, getParameters));
 }
Exemplo n.º 8
0
        public override int InsertBatch <T>(
            DbManager db,
            string insertText,
            IEnumerable <T> collection,
            MemberMapper[]                 members,
            int maxBatchSize,
            DbManager.ParameterProvider <T> getParameters)
        {
            //return base.InsertBatch(db, insertText, collection, members, maxBatchSize, getParameters);

            var idx = insertText.IndexOf('\n');
            var tbl = insertText.Substring(0, idx).Substring("INSERT INTO ".Length).TrimEnd('\r');
            var rd  = new BulkCopyReader(members, collection);
            var bc  = new SqlBulkCopy((SqlConnection)db.Connection)
            {
                BatchSize            = maxBatchSize,
                DestinationTableName = tbl,
            };

            bc.WriteToServer(rd);

            return(rd.Count);
        }
        public override int InsertBatch <T>(
            DbManager db,
            string insertText,
            IEnumerable <T> collection,
            MemberMapper[] members,
            int maxBatchSize,
            DbManager.ParameterProvider <T> getParameters)
        {
            var sb  = new StringBuilder();
            var sp  = new OracleSqlProvider();
            var n   = 0;
            var cnt = 0;
            var str = "\t" + insertText
                      .Substring(0, insertText.IndexOf(") VALUES ("))
                      .Substring(7)
                      .Replace("\r", "")
                      .Replace("\n", "")
                      .Replace("\t", " ")
                      .Replace("( ", "(")
                      //.Replace("  ", " ")
                      + ") VALUES (";

            foreach (var item in collection)
            {
                if (sb.Length == 0)
                {
                    sb.AppendLine("INSERT ALL");
                }

                sb.Append(str);

                foreach (var member in members)
                {
                    var value = member.GetValue(item);

                    if (value is Nullable <DateTime> )
                    {
                        value = ((DateTime?)value).Value;
                    }

                    if (value is DateTime)
                    {
                        var dt = (DateTime)value;
                        sb.Append(string.Format("to_timestamp('{0:dd.MM.yyyy HH:mm:ss.ffffff}', 'DD.MM.YYYY HH24:MI:SS.FF6')", dt));
                    }
                    else
                    {
                        sp.BuildValue(sb, value);
                    }

                    sb.Append(", ");
                }

                sb.Length -= 2;
                sb.AppendLine(")");

                n++;

                if (n >= maxBatchSize)
                {
                    sb.AppendLine("SELECT * FROM dual");

                    var sql = sb.ToString();

                    if (DbManager.TraceSwitch.TraceInfo)
                    {
                        DbManager.WriteTraceLine("\n" + sql.Replace("\r", ""), DbManager.TraceSwitch.DisplayName);
                    }

                    cnt += db.SetCommand(sql).ExecuteNonQuery();

                    n         = 0;
                    sb.Length = 0;
                }
            }

            if (n > 0)
            {
                sb.AppendLine("SELECT * FROM dual");

                var sql = sb.ToString();

                if (DbManager.TraceSwitch.TraceInfo)
                {
                    DbManager.WriteTraceLine("\n" + sql.Replace("\r", ""), DbManager.TraceSwitch.DisplayName);
                }

                cnt += db.SetCommand(sql).ExecuteNonQuery();
            }

            return(cnt);
        }