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

            o.PrimaryKey = primaryKey.ToList();
            return(o);
        }
Beispiel #2
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 #3
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 #4
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 #5
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));
     }
 }
 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));
 }
 public static IEnumerable <ColumnDefinition> GetColumnDefinitions <T>(this IEnumerable <T> values, ColumnDefinitionOptions options = null,
                                                                       FromTypeOption fromTypeOption = FromTypeOption.Default)
 {
     return(values.GetFieldSettings(fromTypeOption).GetColumnDefinitions(options));
 }
        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));
                }
            }
        }