Beispiel #1
0
        protected override BulkOptions SetDefaults(BulkOptions defaultOptions)
        {
            base.SetDefaults(defaultOptions);
            if (!(defaultOptions is SqlBulkOptions opt))
            {
                return(this);
            }

            if (!BatchSize.HasValue)
            {
                BatchSize = opt.BatchSize;
            }

            if (!BulkCopyTimeout.HasValue)
            {
                BulkCopyTimeout = opt.BulkCopyTimeout;
            }

            if (!SqlBulkCopyOptions.HasValue)
            {
                SqlBulkCopyOptions = opt.SqlBulkCopyOptions;
            }

            if (!EnableStreaming.HasValue)
            {
                EnableStreaming = opt.EnableStreaming;
            }

            if (ColumnDefinitionOptions == null)
            {
                ColumnDefinitionOptions = opt.ColumnDefinitionOptions?.Clone();
            }

            return(this);
        }
Beispiel #2
0
        public static ColumnDefinitionOptions FromOption(ColumnDefinitionOptions option, params string[] primaryKey)
        {
            var o = option.Clone();

            o.PrimaryKey = primaryKey.ToList();
            return(o);
        }
Beispiel #3
0
 protected SqlBulkOptions(bool defaultOptions)
     : base(defaultOptions)
 {
     if (defaultOptions)
     {
         this.BatchSize               = null;
         this.BulkCopyTimeout         = null;
         this.SqlBulkCopyOptions      = System.Data.SqlClient.SqlBulkCopyOptions.Default;
         this.EnableStreaming         = null;
         this.ColumnDefinitionOptions = ColumnDefinitionOptions.Default;
     }
 }
Beispiel #4
0
 public SqlBulkOptions(int?batchSize = null, int?bulkCopyTimeout = null, SqlBulkCopyOptions?sqlBulkCopyOptions               = null,
                       FieldsSelector?fieldsSelector = null, bool?caseSensitiveFieldsMatching   = null, bool?enableStreaming = null,
                       bool?createTable = null, ColumnDefinitionOptions columnDefinitionOptions = null, bool?ignoreDataReaderSchemaTable = null,
                       bool?checkTableIfNotExistsBeforeCreation = null)
     : base(fieldsSelector, caseSensitiveFieldsMatching, createTable, ignoreDataReaderSchemaTable, checkTableIfNotExistsBeforeCreation)
 {
     this.BatchSize               = batchSize;
     this.BulkCopyTimeout         = bulkCopyTimeout;
     this.SqlBulkCopyOptions      = sqlBulkCopyOptions;
     this.EnableStreaming         = enableStreaming;
     this.ColumnDefinitionOptions = columnDefinitionOptions;
 }
Beispiel #5
0
 protected internal SqlBulkOptions(BulkOptions bulkOptions)
     : base(bulkOptions)
 {
     if (bulkOptions is SqlBulkOptions opt)
     {
         this.BatchSize               = opt.BatchSize;
         this.BulkCopyTimeout         = opt.BulkCopyTimeout;
         this.SqlBulkCopyOptions      = opt.SqlBulkCopyOptions;
         this.EnableStreaming         = opt.EnableStreaming;
         this.ColumnDefinitionOptions = opt.ColumnDefinitionOptions;
     }
     else
     {
         this.BatchSize               = null;
         this.BulkCopyTimeout         = null;
         this.SqlBulkCopyOptions      = null;
         this.EnableStreaming         = null;
         this.ColumnDefinitionOptions = null;
     }
 }
        public static IEnumerable <ColumnDefinition> GetColumnDefinitions(this IDataReader reader, ColumnDefinitionOptions options = null, bool ignoreSchemaTable = false)
        {
            if (reader is DbDataReader && !ignoreSchemaTable)
            {
                var sqlReader  = (DbDataReader)reader;
                var schema     = sqlReader.GetColumnSchema();
                var optionKeys = options?.PrimaryKey?.ToArray() ?? new string[0];

                foreach (var row in schema)
                {
                    string name = row.ColumnName;
                    yield return(new ColumnDefinition()
                    {
                        Name = name,
                        TypeName = row.DataTypeName,
                        IsPrimaryKey = row.IsKey ?? optionKeys.Contains(name),
                        MaxLength = checked ((short)((row.IsLong ?? false) ? -1 : (row.ColumnSize ?? -1))),
                        Precision = checked ((byte)(row.NumericPrecision ?? 0)),
                        Scale = checked ((byte)(row.NumericScale ?? 0)),
                        IsNullable = row.AllowDBNull ?? false
                    });
                }
            }
            else
            {
                for (int i = 0; i < reader.FieldCount; i++)
                {
                    Type   type = reader.GetFieldType(i);
                    string name = reader.GetName(i);
                    yield return(ColumnDefinition.FromFieldType(type, name, options));
                }
            }
        }
Beispiel #7
0
        public static ColumnDefinition FromFieldType(Type type, string name, ColumnDefinitionOptions options = null)
        {
            var  opt          = options ?? ColumnDefinitionOptions.Default;
            bool isPrimaryKey = opt.PrimaryKey?.Contains(name) ?? false;

            if (type.Equals(typeof(bool)))
            {
                return(new ColumnDefinition(name, "bit", isPrimaryKey, 0, 0, 0, false));
            }
            else if (type.Equals(typeof(bool?)))
            {
                return(new ColumnDefinition(name, "bit", isPrimaryKey, 0, 0, 0, true));
            }
            if (type.Equals(typeof(byte)))
            {
                return(new ColumnDefinition(name, "tinyint", isPrimaryKey, 0, 0, 0, false));
            }
            else if (type.Equals(typeof(byte?)))
            {
                return(new ColumnDefinition(name, "tinyint", isPrimaryKey, 0, 0, 0, true));
            }
            if (type.Equals(typeof(short)))
            {
                return(new ColumnDefinition(name, "smallint", isPrimaryKey, 0, 0, 0, false));
            }
            else if (type.Equals(typeof(short?)))
            {
                return(new ColumnDefinition(name, "smallint", isPrimaryKey, 0, 0, 0, true));
            }
            if (type.Equals(typeof(int)))
            {
                return(new ColumnDefinition(name, "int", isPrimaryKey, 0, 0, 0, false));
            }
            else if (type.Equals(typeof(int?)))
            {
                return(new ColumnDefinition(name, "int", isPrimaryKey, 0, 0, 0, true));
            }
            if (type.Equals(typeof(long)))
            {
                return(new ColumnDefinition(name, "bigint", isPrimaryKey, 0, 0, 0, false));
            }
            else if (type.Equals(typeof(long?)))
            {
                return(new ColumnDefinition(name, "bigint", isPrimaryKey, 0, 0, 0, true));
            }
            if (type.Equals(typeof(float)))
            {
                return(new ColumnDefinition(name, "real", isPrimaryKey, 0, 0, 0, false));
            }
            else if (type.Equals(typeof(float?)))
            {
                return(new ColumnDefinition(name, "real", isPrimaryKey, 0, 0, 0, true));
            }
            if (type.Equals(typeof(double)))
            {
                return(new ColumnDefinition(name, "float", isPrimaryKey, 0, 0, 0, false));
            }
            else if (type.Equals(typeof(double?)))
            {
                return(new ColumnDefinition(name, "float", isPrimaryKey, 0, 0, 0, true));
            }
            if (type.Equals(typeof(decimal)))
            {
                return(new ColumnDefinition(name, "decimal", isPrimaryKey, 0, opt.Precision, opt.Scale, false));
            }
            else if (type.Equals(typeof(decimal?)))
            {
                return(new ColumnDefinition(name, "decimal", isPrimaryKey, 0, opt.Precision, opt.Scale, true));
            }
            if (type.Equals(typeof(Guid)))
            {
                return(new ColumnDefinition(name, "uniqueidentifier", isPrimaryKey, 0, 0, 0, false));
            }
            else if (type.Equals(typeof(Guid?)))
            {
                return(new ColumnDefinition(name, "uniqueidentifier", isPrimaryKey, 0, 0, 0, true));
            }
            if (type.Equals(typeof(DateTime)))
            {
                return(new ColumnDefinition(name, "datetime2", isPrimaryKey, 0, 0, opt.DateTimeScale, false));
            }
            else if (type.Equals(typeof(DateTime?)))
            {
                return(new ColumnDefinition(name, "datetime2", isPrimaryKey, 0, 0, opt.DateTimeScale, true));
            }
            if (type.Equals(typeof(string)))
            {
                return(new ColumnDefinition(name, "nvarchar", isPrimaryKey, opt.StringMaxLength, 0, 0, true));
            }
            if (type.Equals(typeof(byte[])))
            {
                return(new ColumnDefinition(name, "varbinary", isPrimaryKey, opt.BytesMaxLength, 0, 0, true));
            }

            if (opt.ThrowIfUnsupportedType)
            {
                throw new ArgumentOutOfRangeException(nameof(type), type, "Unsupported type");
            }

            return(null);
        }
Beispiel #8
0
 public static string CreateTableScript <T>(T proto, string tableName, bool checkIfNotExists = false,
                                            ColumnDefinitionOptions options = null, FromTypeOption fromTypeOption = FromTypeOption.Default)
 {
     return(CreateTableScript <T>(tableName, checkIfNotExists, options, fromTypeOption));
 }
Beispiel #9
0
 public static string CreateTableScript <T>(string tableName, bool checkIfNotExists = false,
                                            ColumnDefinitionOptions options         = null, FromTypeOption fromTypeOption = FromTypeOption.Default)
 {
     return(FieldSettings.FromType <T>(fromTypeOption).GetColumnDefinitions(options).CreateTableScript(tableName, checkIfNotExists));
 }
Beispiel #10
0
 public static IEnumerable <ColumnDefinition> GetColumnDefinitions <T>(this IEnumerable <FieldSettings <T> > fieldSettings, ColumnDefinitionOptions options = null)
 {
     foreach (var item in fieldSettings)
     {
         yield return(ColumnDefinition.FromFieldType(item.FieldType, item.Name, options));
     }
 }
Beispiel #11
0
 public static string GetCreateTableScript <T>(this IEnumerable <T> values, string tableName, bool checkIfNotExists = false,
                                               ColumnDefinitionOptions options = null, FromTypeOption fromTypeOption = FromTypeOption.Default)
 {
     return(GetColumnDefinitions(values, options, fromTypeOption).CreateTableScript(tableName, checkIfNotExists));
 }
Beispiel #12
0
 public static IEnumerable <ColumnDefinition> GetColumnDefinitions <T>(this IEnumerable <T> values, ColumnDefinitionOptions options = null,
                                                                       FromTypeOption fromTypeOption = FromTypeOption.Default)
 {
     return(values.GetFieldSettings(fromTypeOption).GetColumnDefinitions(options));
 }