Пример #1
0
 public override IRelationConfig AutoExpression()
 {
     if (this.LeftTable.Flags.HasFlag(TableFlags.AutoColumns) && this.LeftTable.PrimaryKey != null)
     {
         this.Expression.Left = this.Expression.CreateColumn(this.LeftTable.PrimaryKey);
     }
     if (this.RightTable.Flags.HasFlag(TableFlags.AutoColumns) && this.LeftTable.PrimaryKey != null)
     {
         var column = default(IColumnConfig);
         if (this.RightTable.TryCreateColumn(
                 ColumnConfig.By(
                     Conventions.RelationColumn(this.LeftTable),
                     Factories.Type.Create(
                         TypeConfig.By(
                             this.LeftTable.PrimaryKey.ColumnType.Type,
                             this.LeftTable.PrimaryKey.ColumnType.Size,
                             this.LeftTable.PrimaryKey.ColumnType.Precision,
                             this.LeftTable.PrimaryKey.ColumnType.Scale,
                             this.Flags.HasFlag(RelationFlags.AllowNull)
                             )
                         )
                     ),
                 out column
                 ))
         {
             this.Expression.Right = this.Expression.CreateColumn(column);
             column.IsForeignKey   = true;
         }
     }
     return(this);
 }
Пример #2
0
        public IIndexConfig Create(ITableConfig table, string identifier, IEnumerable <string> columnNames, IndexFlags?flags)
        {
            var columns = columnNames.Select(
                columnName => table.GetColumn(ColumnConfig.By(columnName, ColumnFlags.None))
                ).Where(column => column != null);

            return(this.Create(table, identifier, columns, flags));
        }
Пример #3
0
        public override IRelationConfig AutoExpression()
        {
            var left  = this.Expression.SetLeft(this.CreateConstraint());
            var right = this.Expression.SetRight(this.CreateConstraint());

            if (this.LeftTable.Flags.HasFlag(TableFlags.AutoColumns) && this.LeftTable.PrimaryKey != null)
            {
                var column = default(IColumnConfig);
                left.Left = this.Expression.CreateColumn(this.LeftTable.PrimaryKey);
                if (this.MappingTable.TryCreateColumn(
                        ColumnConfig.By(
                            Conventions.RelationColumn(this.LeftTable),
                            Factories.Type.Create(
                                TypeConfig.By(
                                    this.LeftTable.PrimaryKey.ColumnType.Type,
                                    this.LeftTable.PrimaryKey.ColumnType.Size,
                                    this.LeftTable.PrimaryKey.ColumnType.Precision,
                                    this.LeftTable.PrimaryKey.ColumnType.Scale,
                                    this.Flags.HasFlag(RelationFlags.AllowNull)
                                    )
                                )
                            ),
                        out column
                        ))
                {
                    left.Right          = this.Expression.CreateColumn(column);
                    column.IsForeignKey = true;
                }
            }
            this.Expression.Operator = this.Expression.CreateOperator(QueryOperator.AndAlso);
            if (this.RightTable.Flags.HasFlag(TableFlags.AutoColumns) && this.RightTable.PrimaryKey != null)
            {
                var column = default(IColumnConfig);
                right.Left = this.Expression.CreateColumn(this.RightTable.PrimaryKey);
                if (this.MappingTable.TryCreateColumn(
                        ColumnConfig.By(
                            Conventions.RelationColumn(this.RightTable),
                            Factories.Type.Create(
                                TypeConfig.By(
                                    this.RightTable.PrimaryKey.ColumnType.Type,
                                    this.RightTable.PrimaryKey.ColumnType.Size,
                                    this.RightTable.PrimaryKey.ColumnType.Precision,
                                    this.RightTable.PrimaryKey.ColumnType.Scale,
                                    this.Flags.HasFlag(RelationFlags.AllowNull)
                                    )
                                )
                            ),
                        out column
                        ))
                {
                    right.Right         = this.Expression.CreateColumn(column);
                    column.IsForeignKey = true;
                }
            }
            return(this);
        }
Пример #4
0
        public static IColumnConfig Column(this ITableConfig table, PropertyInfo property, ColumnFlags?flags = null)
        {
            var selector = ColumnConfig.By(property, flags);
            var column   = table.GetColumn(selector);

            if (column == null)
            {
                column = table.CreateColumn(selector);
            }
            return(column);
        }
Пример #5
0
        public static IColumnConfig Column(this ITableConfig table, string columnName, ColumnFlags?flags = null)
        {
            var selector = ColumnConfig.By(columnName, flags);
            var column   = table.GetColumn(selector);

            if (column == null)
            {
                column = table.CreateColumn(selector);
            }
            return(column);
        }
Пример #6
0
        public void CommonTableExpression_1()
        {
            switch (this.ProviderType)
            {
            case ProviderType.SqlCe:
                Assert.Ignore("The provider does not support common table expressions.");
                return;
            }

            var set  = this.Database.Set <Test002>(this.Transaction);
            var data = new List <Test002>();

            set.Clear();
            set.AddRange(new[]
            {
                new Test002()
                {
                    Name = "1"
                },
                new Test002()
                {
                    Name = "2"
                },
                new Test002()
                {
                    Name = "3"
                }
            });
            var query    = this.Database.QueryFactory.Build();
            var subQuery = this.Database.QueryFactory.Build();

            subQuery.Output.AddOperator(QueryOperator.Star);
            subQuery.Source.AddTable(set.Table);
            subQuery.Filter.AddColumn(
                set.Table.GetColumn(ColumnConfig.By("Name"))
                ).Right = query.Filter.CreateConstant("2");
            query.With.AddCommonTableExpression(new[] { subQuery, subQuery }).Alias = "Extent1";
            query.Output.AddOperator(QueryOperator.Star);
            query.Source.AddTable(
                this.Database.Config.Transient.CreateTable(
                    TableConfig.By("Extent1", TableFlags.None)
                    )
                );
            set.Fetch = query;
            foreach (var item in set)
            {
                Assert.AreEqual("2", item.Name);
            }
        }
Пример #7
0
        protected virtual bool TryGetColumn(MemberInfo member, Expression expression, out IColumnConfig result)
        {
            var property = member as PropertyInfo;

            if (property == null)
            {
                result = default(IColumnConfig);
                return(false);
            }
            var table = this.Database.Config.GetTable(TableConfig.By(expression.Type));

            if (table == null)
            {
                result = default(IColumnConfig);
                return(false);
            }
            result = table.GetColumn(ColumnConfig.By(property));
            return(result != null);
        }
Пример #8
0
        public IEnumerable <IColumnConfig> GetColumns(IDatabase database, ITableConfig table, ITransactionSource transaction = null)
        {
            var properties = new EntityPropertyEnumerator(table.TableType);
            var columns    = new List <IColumnConfig>();

            foreach (var property in properties)
            {
                if (!ColumnValidator.Validate(database, property))
                {
                    continue;
                }
                var column = Factories.Column.Create(table, ColumnConfig.By(property));
                if (!ColumnValidator.Validate(database, table, column, transaction))
                {
                    continue;
                }
                columns.Add(column);
            }
            return(columns);
        }
Пример #9
0
 public IRelationConfig Create<T, TRelation>(ITableConfig<T> table, string identifier, PropertyInfo property, RelationFlags? flags)
 {
     var elementType = default(Type);
     var attribute = property.GetCustomAttribute<RelationAttribute>(true) ?? new RelationAttribute()
     {
         Identifier = identifier
     };
     if (string.IsNullOrEmpty(attribute.Identifier))
     {
         attribute.Identifier = string.Format("{0}_{1}", table.TableName, property.Name);
     }
     if (!attribute.IsFlagsSpecified)
     {
         if (flags.HasValue)
         {
             attribute.Flags = flags.Value;
         }
         else
         {
             attribute.Flags = Defaults.Relation.Flags;
         }
     }
     var relation = default(IRelationConfig);
     if (property.PropertyType.IsCollection(out elementType))
     {
         switch (attribute.Flags.EnsureMultiplicity(RelationFlags.OneToMany).GetMultiplicity())
         {
             case RelationFlags.OneToMany:
                 relation = (IRelationConfig)this.Members.Invoke(this, "CreateOneToMany", new[] { typeof(T), elementType }, table, attribute, property);
                 break;
             case RelationFlags.ManyToMany:
                 relation = (IRelationConfig)this.Members.Invoke(this, "CreateManyToMany", new[] { typeof(T), elementType }, table, attribute, property);
                 break;
             default:
                 throw new NotImplementedException();
         }
     }
     else
     {
         relation = this.CreateOneToOne<T, TRelation>(table, attribute, property);
     }
     if (!string.IsNullOrEmpty(attribute.LeftColumn))
     {
         relation.Expression.Left = relation.Expression.CreateColumn(
             relation.LeftTable.CreateColumn(
                 ColumnConfig.By(
                     attribute.LeftColumn,
                     Factories.Type.Create(TypeConfig.By(property))
                 )
             ).With(column => column.IsForeignKey = true)
         );
     }
     if (!string.IsNullOrEmpty(attribute.RightColumn))
     {
         relation.Expression.Right = relation.Expression.CreateColumn(
             relation.RightTable.CreateColumn(
                 ColumnConfig.By(
                     attribute.RightColumn,
                     Factories.Type.Create(TypeConfig.By(property))
                 )
             ).With(column => column.IsForeignKey = true)
         );
     }
     return relation;
 }
Пример #10
0
        public void OneTimeSetUp()
        {
            var provider = this.CreateProvider();

            try
            {
                provider.DeleteDatabase(this.InitialCatalog);
            }
            catch
            {
                //Nothing to do.
            }
            provider.CreateDatabase(this.InitialCatalog);
            this.Database = this.CreateDatabase();
            var tables = new[]
            {
                this.Database.Config.Transient.CreateTable(
                    TableConfig.By(typeof(Test001), TableFlags.AutoColumns | TableFlags.AutoIndexes)
                    ).With(table =>
                {
                    table.CreateColumn(ColumnConfig.By("Field4", ColumnFlags.None)).With(column =>
                    {
                        column.ColumnType = Factories.Type.Create(TypeConfig.By(DbType.Int32, isNullable: true));
                    });
                    table.CreateColumn(ColumnConfig.By("Field5", ColumnFlags.None)).With(column =>
                    {
                        column.ColumnType = Factories.Type.Create(TypeConfig.By(DbType.Double, isNullable: true));
                    });
                }),
                this.Database.Config.Transient.CreateTable(
                    TableConfig.By(typeof(Test003), TableFlags.AutoColumns | TableFlags.AutoIndexes)
                    ).With(table =>
                {
                    table.CreateColumn(ColumnConfig.By("Test002_Id", ColumnFlags.None)).With(column =>
                    {
                        column.ColumnType = Factories.Type.Create(TypeConfig.By(DbType.Int32, isNullable: true));
                    });
                }),
                this.Database.Config.Transient.CreateTable(
                    TableConfig.By(typeof(Test004), TableFlags.AutoColumns | TableFlags.AutoIndexes)
                    ).With(table =>
                {
                    table.CreateColumn(ColumnConfig.By("Test002_Id", ColumnFlags.None)).With(column =>
                    {
                        column.ColumnType = Factories.Type.Create(TypeConfig.By(DbType.Int32, isNullable: true));
                    });
                }),
                this.Database.Config.Transient.CreateTable(
                    TableConfig.By(typeof(Test002), TableFlags.AutoColumns | TableFlags.AutoIndexes | TableFlags.AutoRelations)
                    ).With(table =>
                {
                    table.CreateColumn(ColumnConfig.By("Version", ColumnFlags.None)).With(column =>
                    {
                        column.ColumnType = Factories.Type.Create(TypeConfig.By(DbType.Binary, size: 8, isNullable: true));
                    });
                }),
                this.Database.Config.Transient.CreateTable(
                    TableConfig.By(
                        this.Database.Config.Transient.CreateTable(
                            TableConfig.By(typeof(Test002), TableFlags.AutoColumns)
                            ),
                        this.Database.Config.Transient.CreateTable(
                            TableConfig.By(typeof(Test004), TableFlags.AutoColumns)
                            ),
                        TableFlags.AutoColumns | TableFlags.AutoRelations
                        )
                    ),
                this.Database.Config.Transient.CreateTable(
                    TableConfig.By(typeof(Test005), TableFlags.AutoColumns | TableFlags.AutoIndexes)
                    ),
                this.Database.Config.Transient.CreateTable(
                    TableConfig.By(typeof(Test006), TableFlags.AutoColumns | TableFlags.AutoIndexes)
                    ),
                this.Database.Config.Transient.CreateTable(
                    TableConfig.By(typeof(Test008), TableFlags.AutoColumns | TableFlags.AutoIndexes)
                    ).With(table =>
                {
                    table.CreateColumn(ColumnConfig.By("Test007_Id", ColumnFlags.None)).With(column =>
                    {
                        column.ColumnType = Factories.Type.Create(TypeConfig.By(DbType.Guid, isNullable: true));
                    });
                }),
                this.Database.Config.Transient.CreateTable(
                    TableConfig.By(typeof(Test009), TableFlags.AutoColumns | TableFlags.AutoIndexes)
                    ).With(table =>
                {
                    table.CreateColumn(ColumnConfig.By("Test007_Id", ColumnFlags.None)).With(column =>
                    {
                        column.ColumnType = Factories.Type.Create(TypeConfig.By(DbType.Guid, isNullable: true));
                    });
                }),
                this.Database.Config.Transient.CreateTable(
                    TableConfig.By(typeof(Test007), TableFlags.AutoColumns | TableFlags.AutoIndexes | TableFlags.AutoRelations)
                    ),
                this.Database.Config.Transient.CreateTable(
                    TableConfig.By(
                        this.Database.Config.Transient.CreateTable(
                            TableConfig.By(typeof(Test007), TableFlags.AutoColumns)
                            ),
                        this.Database.Config.Transient.CreateTable(
                            TableConfig.By(typeof(Test009), TableFlags.AutoColumns)
                            ),
                        TableFlags.AutoColumns | TableFlags.AutoRelations
                        )
                    )
            };

            foreach (var table in tables)
            {
                var query = this.Database.SchemaFactory.Add(table, Defaults.Schema.Flags).Build();
                this.Database.Execute(query);
                this.Database.Schema.Reset();
            }
        }