Exemplo n.º 1
0
        protected override string GetDataSourceName(DataConnection dbConnection)
        {
            var connection = _provider.TryGetProviderConnection(dbConnection, dbConnection.Connection);

            if (connection == null)
            {
                return(string.Empty);
            }

            return(_provider.Adapter.GetHostName?.Invoke(connection) ?? connection.DataSource);
        }
Exemplo n.º 2
0
        protected override string GetDataSourceName(DataConnection dataConnection)
        {
            var connection = _provider.TryGetProviderConnection(dataConnection.Connection, dataConnection.MappingSchema);

            if (connection == null)
            {
                return(string.Empty);
            }

            return(_provider.Adapter.GetHostName(connection));
        }
Exemplo n.º 3
0
        protected override BulkCopyRowsCopied ProviderSpecificCopy <T>(
            ITable <T> table,
            BulkCopyOptions options,
            IEnumerable <T> source)
        {
            if (table.DataContext is DataConnection dataConnection && dataConnection.Transaction == null && _provider.Adapter.BulkCopy != null)
            {
                var connection = _provider.TryGetProviderConnection(dataConnection.Connection, dataConnection.MappingSchema);

                if (connection != null)
                {
                    var ed        = dataConnection.MappingSchema.GetEntityDescriptor(typeof(T));
                    var columns   = ed.Columns.Where(c => !c.SkipOnInsert || options.KeepIdentity == true && c.IsIdentity).ToList();
                    var sb        = _provider.CreateSqlBuilder(dataConnection.MappingSchema);
                    var rd        = new BulkCopyReader <T>(dataConnection, columns, source);
                    var sqlopt    = OracleProviderAdapter.OracleBulkCopyOptions.Default;
                    var rc        = new BulkCopyRowsCopied();
                    var tableName = GetTableName(sb, options, table);

                    if (options.UseInternalTransaction == true)
                    {
                        sqlopt |= OracleProviderAdapter.OracleBulkCopyOptions.UseInternalTransaction;
                    }

                    using (var bc = _provider.Adapter.BulkCopy.Create(connection, sqlopt))
                    {
                        var notifyAfter = options.NotifyAfter == 0 && options.MaxBatchSize.HasValue
                                                        ? options.MaxBatchSize.Value
                                                        : options.NotifyAfter;

                        if (notifyAfter != 0 && options.RowsCopiedCallback != null)
                        {
                            bc.NotifyAfter = options.NotifyAfter;

                            bc.OracleRowsCopied += (sender, args) =>
                            {
                                rc.RowsCopied = args.RowsCopied;
                                options.RowsCopiedCallback(rc);
                                if (rc.Abort)
                                {
                                    args.Abort = true;
                                }
                            };
                        }

                        if (options.MaxBatchSize.HasValue)
                        {
                            bc.BatchSize = options.MaxBatchSize.Value;
                        }

                        if (options.BulkCopyTimeout.HasValue)
                        {
                            bc.BulkCopyTimeout = options.BulkCopyTimeout.Value;
                        }
                        else if (Configuration.Data.BulkCopyUseConnectionCommandTimeout)
                        {
                            bc.BulkCopyTimeout = connection.ConnectionTimeout;
                        }

                        bc.DestinationTableName = tableName;

                        for (var i = 0; i < columns.Count; i++)
                        {
                            bc.ColumnMappings.Add(_provider.Adapter.BulkCopy.CreateColumnMapping(i, columns[i].ColumnName));
                        }

                        TraceAction(
                            dataConnection,
                            () => "INSERT BULK " + tableName + "(" + string.Join(", ", columns.Select(x => x.ColumnName)) + Environment.NewLine,
                            () => { bc.WriteToServer(rd); return(rd.Count); });
                    }

                    if (rc.RowsCopied != rd.Count)
                    {
                        rc.RowsCopied = rd.Count;

                        if (options.NotifyAfter != 0 && options.RowsCopiedCallback != null)
                        {
                            options.RowsCopiedCallback(rc);
                        }
                    }

                    return(rc);
                }
            }


            return(MultipleRowsCopy(table, options, source));
        }
Exemplo n.º 4
0
        protected override BulkCopyRowsCopied ProviderSpecificCopy <T>(
            ITable <T> table,
            BulkCopyOptions options,
            IEnumerable <T> source)
        {
            // database name is not a part of table FQN in oracle
            var serverName = options.ServerName ?? table.ServerName;

            if (table.DataContext is DataConnection dataConnection && _provider.Adapter.BulkCopy != null &&
                serverName == null)
            {
                var connection = _provider.TryGetProviderConnection(dataConnection.Connection, dataConnection.MappingSchema);

                if (connection != null)
                {
                    var ed      = dataConnection.MappingSchema.GetEntityDescriptor(typeof(T));
                    var columns = ed.Columns.Where(c => !c.SkipOnInsert || options.KeepIdentity == true && c.IsIdentity).ToList();
                    var sb      = _provider.CreateSqlBuilder(dataConnection.MappingSchema);

                    // ODP.NET doesn't bulk copy doesn't work if columns that require escaping:
                    // - if escaping applied, pre-flight validation fails as it performs uppercase comparison and quotes make it fail with
                    //   InvalidOperationException: Column mapping is invalid
                    // - if escaping not applied - if fails as expected on server, because it treats passed name as uppercased name
                    //   and gives "ORA-00904: "STRINGVALUE": invalid identifier" error
                    // That's quite common error in bulk copy implementation error by providers...
                    var supported = true;
                    foreach (var column in columns)
                    {
                        if (column.ColumnName != sb.ConvertInline(column.ColumnName, ConvertType.NameToQueryField))
                        {
                            // fallback to sql-based copy
                            // TODO: we should add support for by-ordinal column mapping to workaround it
                            supported = false;
                            break;
                        }
                    }

                    if (supported)
                    {
                        var rd     = new BulkCopyReader <T>(dataConnection, columns, source);
                        var sqlopt = OracleProviderAdapter.OracleBulkCopyOptions.Default;
                        var rc     = new BulkCopyRowsCopied();

                        var tableName  = sb.ConvertInline(options.TableName ?? table.TableName, ConvertType.NameToQueryTable);
                        var schemaName = options.SchemaName ?? table.SchemaName;
                        if (schemaName != null)
                        {
                            schemaName = sb.ConvertInline(schemaName, ConvertType.NameToSchema);
                        }

                        if (options.UseInternalTransaction == true)
                        {
                            sqlopt |= OracleProviderAdapter.OracleBulkCopyOptions.UseInternalTransaction;
                        }

                        using (var bc = _provider.Adapter.BulkCopy.Create(connection, sqlopt))
                        {
                            var notifyAfter = options.NotifyAfter == 0 && options.MaxBatchSize.HasValue
                                                        ? options.MaxBatchSize.Value
                                                        : options.NotifyAfter;

                            if (notifyAfter != 0 && options.RowsCopiedCallback != null)
                            {
                                bc.NotifyAfter = options.NotifyAfter;

                                bc.OracleRowsCopied += (sender, args) =>
                                {
                                    rc.RowsCopied = args.RowsCopied;
                                    options.RowsCopiedCallback(rc);
                                    if (rc.Abort)
                                    {
                                        args.Abort = true;
                                    }
                                };
                            }

                            if (options.MaxBatchSize.HasValue)
                            {
                                bc.BatchSize = options.MaxBatchSize.Value;
                            }

                            if (options.BulkCopyTimeout.HasValue)
                            {
                                bc.BulkCopyTimeout = options.BulkCopyTimeout.Value;
                            }
                            else if (Configuration.Data.BulkCopyUseConnectionCommandTimeout)
                            {
                                bc.BulkCopyTimeout = connection.ConnectionTimeout;
                            }

                            bc.DestinationTableName  = tableName;
                            bc.DestinationSchemaName = schemaName;

                            for (var i = 0; i < columns.Count; i++)
                            {
                                bc.ColumnMappings.Add(_provider.Adapter.BulkCopy.CreateColumnMapping(i, columns[i].ColumnName));
                            }

                            TraceAction(
                                dataConnection,
                                () => "INSERT BULK " + (schemaName == null ? tableName : schemaName + "." + tableName) + "(" + string.Join(", ", columns.Select(x => x.ColumnName)) + ")" + Environment.NewLine,
                                () => { bc.WriteToServer(rd); return(rd.Count); });
                        }

                        if (rc.RowsCopied != rd.Count)
                        {
                            rc.RowsCopied = rd.Count;

                            if (options.NotifyAfter != 0 && options.RowsCopiedCallback != null)
                            {
                                options.RowsCopiedCallback(rc);
                            }
                        }

                        return(rc);
                    }
                }
            }


            return(MultipleRowsCopy(table, options, source));
        }