Example #1
0
        public void TestCase()
        {
            LogicalQueryBuilder query = new LogicalQueryBuilder();

            TableDefinition entityTable = new TableDefinition() {Name = "Entity"};
            TableColumn pk1 = new TableColumn() {IsPrimaryKey = true, Name = "id1"};
            TableColumn name1 = new TableColumn() {Name = "name1"};
            TableColumn name2 = new TableColumn() {Name = "name2"};
            entityTable.addColumn(pk1);
            entityTable.addColumn(name1);
            entityTable.addColumn(name2);

            TableDefinition secondEntity = new TableDefinition() {Name = "SecondEntity"};
            TableColumn pk2 = new TableColumn() {IsPrimaryKey = true, Name = "id"};
            TableColumn fk1 = new TableColumn() {IsForeignKey = true, Name = "fk1", ReferencedColumn = pk1};
            secondEntity.addColumn(pk2);
            secondEntity.addColumn(fk1);

            LogicalTable log1 = new LogicalTable() {Name = "Entity_Prop", PartOfTable = entityTable, TableType = LogicalTableType.ENTITY};
            log1.AddColumn(pk1);
            log1.AddColumn(name1);
            log1.AddColumn(name2);

            LogicalTable log2 = new LogicalTable() {Name = "Entity_SecondEntity", PartOfTable = secondEntity, TableType = LogicalTableType.JOIN};
            log2.AddColumn(pk2);
            log2.AddColumn(fk1);

            LogicalTable log3 = new LogicalTable() {Name = "SecondEntity_Prop", PartOfTable = secondEntity, TableType = LogicalTableType.ENTITY};
            log3.AddColumn(pk2);

            TableDefinition thirdEntity = new TableDefinition() {Name = "ThirdEntity"};
            TableColumn pk3 = new TableColumn() {IsPrimaryKey = true, Name = "pk3"};
            thirdEntity.addColumn(pk3);

            TableDefinition joinTable = new TableDefinition() {Name = "SecondEntity_ThirdEntity"};
            TableColumn join1 = new TableColumn() {IsForeignKey = true, Name = "fk2", ReferencedColumn = pk2};
            TableColumn join2 = new TableColumn() {IsForeignKey = true, Name = "fk3", ReferencedColumn = pk3};
            joinTable.addColumn(join1);
            joinTable.addColumn(join2);

            LogicalTable log4 = new LogicalTable() {Name = "SecondEntity_ThirdEntity", PartOfTable = joinTable, TableType = LogicalTableType.JOIN};
            log4.AddColumn(join1);
            log4.AddColumn(join2);

            LogicalTable log5 = new LogicalTable() {Name = "ThirdEntity", PartOfTable = thirdEntity, TableType = LogicalTableType.ENTITY};
            log5.AddColumn(pk3);

            query.JoinTable (log1);
            query.JoinTable (log2);
            query.JoinTable (log3);
            query.JoinTable (log4);
            query.JoinTable (log5);

            string actual = query.ToNativeQuery().ToQueryString ();
            Console.WriteLine(actual);
            string expected = "Dunno";

            Assert.AreEqual (expected, actual);
        }
 // to be used for consecutive joins without forks
 public virtual LogicalQueryTable JoinTable(LogicalTable table)
 {
     if (this.lastTableAdded == null) {
         setRootTable (table);
     } else {
         attachTable (this.lastTableAdded, table);
     }
     return this.lastTableAdded;
 }
 // should throw an exception if source is not included in the tree
 public virtual LogicalQueryTable JoinTable(LogicalQueryTable source, LogicalTable joinWith)
 {
     // first check if source is the last added node, if not search the whole tree
     if (!this.logicalTables.Contains(source)) {
         throw new ArgumentException("Source table is not present in the query.");
     }
     attachTable(source, joinWith);
     return this.lastTableAdded;
 }
Example #4
0
 private DataTable ToDataTable(LogicalTable logicalTable)
 {
     DataTable dataTable = new DataTable();
     dataTable.Name = logicalTable.Name;
     foreach (TableColumn col in logicalTable.GetColumns()) {
         dataTable.AddColumn(new DataColumn() {Name = col.Name});
     }
     // TODO: cannot assume that for every table there should be unique row constraint
     dataTable.AddConstraint(new UniqueRowConstraint());
     return dataTable;
 }
        public virtual ClassMapping<object> createMapping(Type type)
        {
            ClassMapping<object> mapping = new ClassMapping<object>();
            // type
            mapping.ClassType = type;
            // table
            LogicalTable logicalTable = new LogicalTable();
            mapping.CorrespondingTable = logicalTable;

            TableDefinition table = new TableDefinition();

            Table tableAttr = AttributeUtils.getSingleAttribute<Table>(type);
            if (tableAttr != null) {
                table.Name = tableAttr.Name;
            } else {
                table.Name = generateTableName(type.Name);
            }

            logicalTable.Name = table.Name;

            // all fields including id
            foreach (FieldInfo field in getClassFields(type)) {
                if (isFieldPersistent(field)) {
                    FieldMapping<object, object> fieldMapping = new FieldMapping<object, object>() {ClassField = field};

                    TableColumn column = new TableColumn();
                    logicalTable.AddColumn(column);
                    Column colAttr = AttributeUtils.getSingleAttribute<Column>(field);
                    if (colAttr != null) {
                        column.Name = colAttr.Name;
                    } else {
                        column.Name = field.Name;
                    }
                    fieldMapping.Column = column;
                    table.addColumn(column);
                    mapping.AddPropertyMapping (fieldMapping);
                    Id idAttr = AttributeUtils.getSingleAttribute<Id>(field);
                    if (idAttr != null) {
                        mapping.IdMapping = fieldMapping;
                        column.IsPrimaryKey = true;
                        fieldMapping.IsId = true;
                    }
                }
            }
            return mapping;
        }
        private LogicalQueryTable CreatePartOfEntityTable()
        {
            LogicalQueryTable queryTable = new LogicalQueryTable() {Alias = "Entity_0"};

            LogicalTable logicalTable = new LogicalTable() {Name = "EntityPart", TableType = LogicalTableType.ENTITY};
            queryTable.Table = logicalTable;

            TableDefinition physicalTable = new TableDefinition() {Name = "Entity"};

            logicalTable.PartOfTable = physicalTable;

            TableColumn nameColumn = new TableColumn() {Name="name", Table = physicalTable};

            physicalTable.addColumn(nameColumn);
            logicalTable.AddColumn(nameColumn);

            return queryTable;
        }
        private LogicalQueryTable CreateSingleEntityTable()
        {
            LogicalQueryTable queryTable = new LogicalQueryTable() {Alias = "Entity_0"};

            LogicalTable logicalTable = new LogicalTable() {Name = "Entity", TableType = LogicalTableType.ENTITY};
            queryTable.Table = logicalTable;

            TableDefinition physicalTable = new TableDefinition() {Name = "Entity"};

            logicalTable.PartOfTable = physicalTable;

            TableColumn idColumn = new TableColumn() {IsPrimaryKey = true, Name="id", Table = physicalTable};
            TableColumn nameColumn = new TableColumn() {Name="name", Table = physicalTable};

            physicalTable.addColumn(idColumn);
            physicalTable.addColumn(nameColumn);

            logicalTable.AddColumn(idColumn);
            logicalTable.AddColumn(nameColumn);

            return queryTable;
        }
Example #8
0
 public bool IsPartOfTheSameTable(LogicalTable table)
 {
     return this.PartOfTable.Equals (table.PartOfTable);
 }
        private void attachTable(LogicalQueryTable source, LogicalTable joinWith)
        {
            // check if join can be performed, if not then exception is thrown
            validateAttachArguments(source, joinWith);

            // join tables
            LogicalQueryTable target = new LogicalQueryTable() {Table = joinWith};

            // if the join table is in fact a part of another physical table already included in source logical table
            // then dont join them
            // always join tables if the target table is of entity type
            if (!isJoinRequired (source, target)) {
                target.Alias = source.Alias;
            } else {
                string alias = createTableAlias(joinWith);
                target.Alias = alias;

                PhysicalQueryTable physicalQueryTable = new PhysicalQueryTable() {Table = joinWith.PartOfTable, Alias = alias};
                this.physicalTables.Add (physicalQueryTable);

                this.fromSource = createNewJoin (this.fromSource, source, target);
            }

            this.logicalTables.Add (target);
            this.lastTableAdded = target;
        }
 private void validateAttachArguments(LogicalQueryTable source, LogicalTable joinWith)
 {
     if (source.Table.Equals(joinWith)) {
         throw new ArgumentException("Cannot join a logical table with itself");
     } else if (source.Table.TableType.Equals (joinWith.TableType)) {
         throw new ArgumentException("Cannot join two entity logical or two join logical tables");
     }
 }
 private void setRootTable(LogicalTable rootTable)
 {
     string alias = createTableAlias(rootTable);
     LogicalQueryTable queryTable = new LogicalQueryTable() {Table = rootTable, Alias = alias};
     PhysicalQueryTable physicalQueryTable = new PhysicalQueryTable() {Table = rootTable.PartOfTable, Alias = alias};
     this.logicalTables.Add (queryTable);
     this.physicalTables.Add (physicalQueryTable);
     this.fromSource = physicalQueryTable.ToTableReference();
     this.lastTableAdded = queryTable;
 }
 private string createTableAlias(LogicalTable logicalTable)
 {
     return createTableAlias(logicalTable.PartOfTable.Name, howManyTimesUsed(logicalTable.PartOfTable));
 }