Example #1
0
 public SqlOrderByClause Field(SqlField field, bool isDescending)
 {
     return(Expr(field, isDescending));
 }
Example #2
0
 public SqlOrderByClause Field(SqlField field)
 {
     return(Expr(field, false));
 }
Example #3
0
        public SqlTable([JetBrains.Annotations.NotNull] MappingSchema mappingSchema, Type objectType, string physicalName = null) : this()
        {
            if (mappingSchema == null)
            {
                throw new ArgumentNullException(nameof(mappingSchema));
            }

            var ed = mappingSchema.GetEntityDescriptor(objectType);

            Database     = ed.DatabaseName;
            Schema       = ed.SchemaName;
            Name         = ed.TableName;
            ObjectType   = objectType;
            PhysicalName = physicalName ?? Name;

            foreach (var column in ed.Columns)
            {
                var field = new SqlField
                {
                    SystemType       = column.MemberType,
                    Name             = column.MemberName,
                    PhysicalName     = column.ColumnName,
                    CanBeNull        = column.CanBeNull,
                    IsPrimaryKey     = column.IsPrimaryKey,
                    PrimaryKeyOrder  = column.PrimaryKeyOrder,
                    IsIdentity       = column.IsIdentity,
                    IsInsertable     = !column.SkipOnInsert,
                    IsUpdatable      = !column.SkipOnUpdate,
                    DataType         = column.DataType,
                    DbType           = column.DbType,
                    Length           = column.Length,
                    Precision        = column.Precision,
                    Scale            = column.Scale,
                    CreateFormat     = column.CreateFormat,
                    ColumnDescriptor = column,
                };

                Add(field);

                if (field.DataType == DataType.Undefined)
                {
                    var dataType = mappingSchema.GetDataType(field.SystemType);

                    if (dataType.DataType == DataType.Undefined)
                    {
                        var canBeNull = field.CanBeNull;

                        dataType = mappingSchema.GetUnderlyingDataType(field.SystemType, ref canBeNull);

                        field.CanBeNull = canBeNull;
                    }

                    field.DataType = dataType.DataType;

                    if (field.Length == null)
                    {
                        field.Length = dataType.Length;
                    }

                    if (field.Precision == null)
                    {
                        field.Precision = dataType.Precision;
                    }

                    if (field.Scale == null)
                    {
                        field.Scale = dataType.Scale;
                    }
                }
            }

            var identityField = GetIdentityField();

            if (identityField != null)
            {
                var cd = ed[identityField.Name];
                SequenceAttributes = cd.SequenceName == null ? null : new[] { cd.SequenceName };
            }
        }
Example #4
0
        public SqlTable(MappingSchema mappingSchema, Type objectType, string?physicalName = null)
            : this()
        {
            if (mappingSchema == null)
            {
                throw new ArgumentNullException(nameof(mappingSchema));
            }

            var ed = mappingSchema.GetEntityDescriptor(objectType);

            Server       = ed.ServerName;
            Database     = ed.DatabaseName;
            Schema       = ed.SchemaName;
            Name         = ed.TableName;
            ObjectType   = objectType;
            PhysicalName = physicalName ?? Name;
            TableOptions = ed.TableOptions;

            foreach (var column in ed.Columns)
            {
                var field = new SqlField(column);

                Add(field);

                if (field.Type !.Value.DataType == DataType.Undefined)
                {
                    var dataType = mappingSchema.GetDataType(field.Type !.Value.SystemType);

                    if (dataType.Type.DataType == DataType.Undefined)
                    {
                        dataType = mappingSchema.GetUnderlyingDataType(field.Type !.Value.SystemType, out var canBeNull);

                        if (canBeNull)
                        {
                            field.CanBeNull = true;
                        }
                    }

                    field.Type = field.Type !.Value.WithDataType(dataType.Type.DataType);

                    // try to get type from converter
                    if (field.Type !.Value.DataType == DataType.Undefined)
                    {
                        try
                        {
                            var converter = mappingSchema.GetConverter(
                                field.Type !.Value,
                                new DbDataType(typeof(DataParameter)), true);

                            var parameter = converter?.ConvertValueToParameter?.Invoke(DefaultValue.GetValue(field.Type !.Value.SystemType, mappingSchema));
                            if (parameter != null)
                            {
                                field.Type = field.Type !.Value.WithDataType(parameter.DataType);
                            }
                        }
                        catch
                        {
                            // converter cannot handle default value?
                        }
                    }

                    if (field.Type !.Value.Length == null)
                    {
                        field.Type = field.Type !.Value.WithLength(dataType.Type.Length);
                    }
                    if (field.Type !.Value.Precision == null)
                    {
                        field.Type = field.Type !.Value.WithPrecision(dataType.Type.Precision);
                    }
                    if (field.Type !.Value.Scale == null)
                    {
                        field.Type = field.Type !.Value.WithScale(dataType.Type.Scale);
                    }
                }
            }

            var identityField = GetIdentityField();

            if (identityField != null)
            {
                var cd = ed[identityField.Name] !;
                SequenceAttributes = cd.SequenceName == null ? null : new[] { cd.SequenceName };
            }
        }
Example #5
0
 private void AddField(SqlField field)
 {
     field.Table = this;
     SourceFields.Add(field);
 }
Example #6
0
 public SqlSelectClause Field(SqlField field, string alias)
 {
     AddOrFindColumn(new SqlColumn(SelectQuery, field, alias));
     return(this);
 }
Example #7
0
 public SqlGroupByClause Field(SqlField field)
 {
     return(Expr(field));
 }
Example #8
0
 public SqlOrderByClause FieldDesc(SqlField field)
 {
     return(Expr(field, true));
 }
Example #9
0
        public SqlTable([JetBrains.Annotations.NotNull] MappingSchema mappingSchema, Type objectType, string physicalName = null) : this()
        {
            if (mappingSchema == null)
            {
                throw new ArgumentNullException(nameof(mappingSchema));
            }

            var ed = mappingSchema.GetEntityDescriptor(objectType);

            Database     = ed.DatabaseName;
            Schema       = ed.SchemaName;
            Name         = ed.TableName;
            ObjectType   = objectType;
            PhysicalName = physicalName ?? Name;

            foreach (var column in ed.Columns)
            {
                var field = new SqlField(column);

                Add(field);

                if (field.DataType == DataType.Undefined)
                {
                    var dataType = mappingSchema.GetDataType(field.SystemType);

                    if (dataType.DataType == DataType.Undefined)
                    {
                        var canBeNull = field.CanBeNull;

                        dataType = mappingSchema.GetUnderlyingDataType(field.SystemType, ref canBeNull);

                        field.CanBeNull = canBeNull;
                    }

                    field.DataType = dataType.DataType;

                    // try to get type from converter
                    if (field.DataType == DataType.Undefined)
                    {
                        try
                        {
                            var converter = mappingSchema.GetConverter(
                                new DbDataType(field.SystemType, field.DataType, field.DbType, field.Length),
                                new DbDataType(typeof(DataParameter)), true);

                            var parameter = converter?.ConvertValueToParameter?.Invoke(DefaultValue.GetValue(field.SystemType, mappingSchema));
                            if (parameter != null)
                            {
                                field.DataType = parameter.DataType;
                            }
                        }
                        catch
                        {
                            // converter cannot handle default value?
                        }
                    }

                    if (field.Length == null)
                    {
                        field.Length = dataType.Length;
                    }

                    if (field.Precision == null)
                    {
                        field.Precision = dataType.Precision;
                    }

                    if (field.Scale == null)
                    {
                        field.Scale = dataType.Scale;
                    }
                }
            }

            var identityField = GetIdentityField();

            if (identityField != null)
            {
                var cd = ed[identityField.Name];
                SequenceAttributes = cd.SequenceName == null ? null : new[] { cd.SequenceName };
            }
        }
Example #10
0
        public SqlTable([JetBrains.Annotations.NotNull] MappingSchema mappingSchema, Type objectType) : this()
        {
            if (mappingSchema == null)
            {
                throw new ArgumentNullException("mappingSchema");
            }

            var ed = mappingSchema.GetEntityDescriptor(objectType);

            Database     = ed.DatabaseName;
            Owner        = ed.SchemaName;
            Name         = ed.TableName;
            ObjectType   = objectType;
            PhysicalName = Name;

            foreach (var column in ed.Columns)
            {
                var field = new SqlField
                {
                    SystemType       = column.MemberType,
                    Name             = column.MemberName,
                    PhysicalName     = column.ColumnName,
                    Nullable         = column.CanBeNull,
                    IsPrimaryKey     = column.IsPrimaryKey,
                    PrimaryKeyOrder  = column.PrimaryKeyOrder,
                    IsIdentity       = column.IsIdentity,
                    IsInsertable     = !column.SkipOnInsert,
                    IsUpdatable      = !column.SkipOnUpdate,
                    DataType         = column.DataType,
                    DbType           = column.DbType,
                    Length           = column.Length,
                    Precision        = column.Precision,
                    Scale            = column.Scale,
                    ColumnDescriptor = column,
                };

                Add(field);

                if (field.DataType == DataType.Undefined)
                {
                    var dataType = mappingSchema.GetDataType(field.SystemType);

                    if (dataType.DataType == DataType.Undefined)
                    {
                        var canBeNull = field.Nullable;

                        dataType = mappingSchema.GetUnderlyingDataType(field.SystemType, ref canBeNull);

                        field.Nullable = canBeNull;
                    }

                    field.DataType = dataType.DataType;

                    if (field.Length == null)
                    {
                        field.Length = dataType.Length;
                    }
                }
            }

            var identityField = GetIdentityField();

            if (identityField != null)
            {
                var cd = ed[identityField.Name];

                SequenceAttributes = mappingSchema.GetAttributes <SequenceNameAttribute>(
                    cd.MemberAccessor.MemberInfo, a => a.Configuration);
            }
        }
Example #11
0
 internal SqlDataType(SqlField field)
     : this(field.Type)
 {
 }