Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Improve creating multiple table joins #6

Closed
hisystems opened this Issue Mar 4, 2012 · 1 comment

Comments

Projects
None yet
1 participant
Owner

hisystems commented Mar 4, 2012

Currently, the multiple table joins are supported, however it is not very intuitive when creating two or more table joins. Creating one table join is somewhat intuitive:

        Dim table1 As New SQLSelectTable("Table1")
        Dim table2 As New SQLSelectTable("Table2")

        Dim selectStatement As New SQLSelect()
        With selectStatement.Tables.Joins.Add(table1, SQLSelectTableJoin.Type.Inner, table2)
            .Where.Add("Table1Key", SQL.ComparisonOperator.EqualTo, "Table2Key")
        End With

However, creating multiple joins is not, because the second join is really based on the results of the first join, and not really from table2 to table3 as the code reads. The join is from table1+table2 to table3:

        Dim table1 As New SQLSelectTable("Table1")
        Dim table2 As New SQLSelectTable("Table2")
        Dim table3 As New SQLSelectTable("Table3")

        Dim selectStatement As New SQLSelect()
        selectStatement.Tables.Add(table1)

        With selectStatement.Tables.Joins.Add(table1, SQLSelectTableJoin.Type.Inner, table2)
            .Where.Add("Table1Key", SQL.ComparisonOperator.EqualTo, "Table2Key")
        End With

        With selectStatement.Tables.Joins.Add(table2, SQLSelectTableJoin.Type.Inner, table3)
            .Where.Add("Table2Key", SQL.ComparisonOperator.EqualTo, "Table3Key")
        End With
SELECT * FROM 
(([Table1] INNER JOIN [Table2] ON [Table1].[Table1Key] = [Table2].[Table2Key]) 
INNER JOIN [Table3] ON [Table2].[Table2Key] = [Table3].[Table3Key])

@ghost ghost assigned hisystems Mar 4, 2012

Owner

hisystems commented Mar 4, 2012

The best solution would be to still utilise the SQLSelectTableJoins collection, but instead from the SQLSelectTableJoin add a function called JoinTo. This will maintain the existing API while providing a more intuitive approach. For example:

Dim table1 As New SQLSelectTable("Table1")
Dim table2 As New SQLSelectTable("Table2")
Dim table3 As New SQLSelectTable("Table3")

Dim selectStatement As New SQLSelect()
selectStatement.Tables.Add(table1)

Dim table1Table2Join = selectStatement.Tables.Joins.Add(table1, SQLSelectTableJoin.Type.Inner, table2)
table1Table2Join.Where.Add("Table1Key", SQL.ComparisonOperator.EqualTo, "Table2Key")

Dim table2Table3Join = table1Table2Join.JoinTo(SQLSelectTableJoin.Type.Inner, table2)
table2Table3Join.Where.Add("Table2Key", SQL.ComparisonOperator.EqualTo, "Table3Key")

The only issue will be that the left field name for the second join will be unknown, in which case the table joins should be changed to use an SQLFieldExpression (which also indicates the associated table).

@hisystems hisystems closed this in 6f65497 Mar 4, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment