示例#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);
        }
示例#2
0
        public void PrimaryColumnSchemaDefinitionTest()
        {
            TableColumn col = new TableColumn();
            col.Name = "id";
            col.IsPrimaryKey = true;
            col.Type = "INT";

            string columnDef = col.getColumnDefinition();
            string expectedDef = "id INT PRIMARY KEY ASC";

            Assert.AreEqual (expectedDef, columnDef);
        }
示例#3
0
        public void NormalColumnSchemaDefinitionTest()
        {
            TableColumn col = new TableColumn();
            col.Name = "name";
            col.IsPrimaryKey = false;
            col.Type = "STRING";

            string columnDef = col.getColumnDefinition();
            string expectedDef = "name STRING";

            Assert.AreEqual (expectedDef, columnDef);
        }
示例#4
0
 // can throw an exception if constraints are not respected
 public virtual void AddColumn(TableColumn column)
 {
     // check if added column is really a part of the same physical table as other columns
     if (canColumnBeAdded(column)) {
         if (column.IsPrimaryKey) {
             addPrimaryKey (column);
         } else if (column.IsForeignKey) {
             addForeignKey (column);
         }
         this.columns.Add (column);
     } else {
         throw new ArgumentException("Logical table cannnot contain columns from more than one physical table.");
     }
 }
        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;
        }
示例#6
0
        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;
        }
        public void TestCase()
        {
            TableColumn idCol = new TableColumn();
            idCol.Name = "id";
            idCol.Type = "INT";
            idCol.IsPrimaryKey = true;

            TableColumn nameCol = new TableColumn();
            nameCol.Name = "name";
            nameCol.Type = "STRING";

            TableDefinition table = new TableDefinition();
            table.Name = "TestTable";

            table.addColumn(idCol);
            table.addColumn(nameCol);

            string retrievedSchema = table.getTableSchema();
            string expectedSchema = "CREATE TABLE TestTable (id INT PRIMARY KEY ASC, name STRING);";

            Assert.AreEqual(expectedSchema, retrievedSchema);
        }
示例#8
0
        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;
        }
示例#9
0
 public virtual void addColumn(TableColumn column)
 {
     this.columns.Add (column);
     column.Table = this;
 }
示例#10
0
 public TableColumn GetForeignKey(TableColumn referencedPrimaryKey)
 {
     return this.foreignKeys.Find ((x) => x.ReferencedColumn.Equals(referencedPrimaryKey));
 }
示例#11
0
 private bool canColumnBeAdded(TableColumn column)
 {
     return column.Table.Equals (this.PartOfTable);
 }
示例#12
0
 private void addPrimaryKey(TableColumn column)
 {
     // check if primary key is already assigned
     if (this.primaryKey == null) {
         this.primaryKey = column;
     } else {
         throw new ArgumentException("Logical table cannot contain more than one primary key column.");
     }
 }
示例#13
0
 private void addForeignKey(TableColumn column)
 {
     // check if it references the same primary key as any other foreign key already included
     if (this.foreignKeys.Find ((x) => x.ReferencedColumn.Equals(column.ReferencedColumn)) != null) {
         throw new ArgumentException("Another foreign key in this table references the same primary key.");
     } else {
         this.foreignKeys.Add (column);
     }
 }
示例#14
0
 private DataColumn GetDataColumn(DataTable dataTable, TableColumn col)
 {
     return dataTable.Columns.Find ((x) => x.Name.Equals (col.Name));
 }