How to Create Join Script for a Query .......
Hi
i'm developing a VISUALL query builder as the same as EnterPrise manager for making Sql Server Query.I'm simulating it exactly.so my application has followings Panes:
Diagram Pane
Grid Pane
Sql Pane
Result Pane
User can drop any tables in the Diagram pane and then select fields and make relations(joins) as he/she wants for example suppose user drops Customers and Orders Tables from NorthWind Database in the diagram pane and make a join from Customers.CustomerID to Orders.CustomerID After that Sql Server in the Sql PAne
will write the followings script:
SELECT *
FROM Customers INNER JOIN
Orders ON Customers.CustomerID = Orders.CustomerID
This is a simple join and my app is able to generate script for this query. Also in the background as you might have guessed i use a object structure for saving informaton about all the joins that user has made for example for the above query i create a Join Object and fill it with this info:
Left Table Name : Customers
Left Field Name : CustomerID
Right Table Name : Orders
Right Field Name : CustomerID
Join Type : Inner join
Join Operator : =
For each join i will do this.So I have all information about Joins that user has made Visually.
But until now this was a very simple Query with one join .Imagine we have the following query.It is a right query and it has not grammatical mistake and can be Executed properly regardless of its logics.
SELECT *
FROM Orders INNER JOIN
Customers ON Orders.CustomerID = Customers.CustomerID INNER JOIN
Products ON Orders.OrderID = Products.ProductID AND
Orders.EmployeeID = Products.SupplierID AND
Customers.CompanyName = Products.ProductName
Now in my app i have this information:
Join 1:
Left Table Name : Customers
Left Field Name : CustomerID
Right Table Name : Orders
Right Field Name : CustomerID
Join Type : Inner join
Join Operator : =
Join 2:
Left Table Name : Products
Left Field Name : ProductName
Right Table Name : Customers
Right Field Name : CompanyName
Join Type : Inner join
Join Operator : =
Join 3:
Left Table Name : Products
Left Field Name : ProductID
Right Table Name : Orders
Right Field Name : OrderID
Join Type : Inner join
Join Operator : =
Join 4:
Left Table Name : Products
Left Field Name : SuplierID
Right Table Name : Orders
Right Field Name : EmployeeID
Join Type : Inner join
Join Operator : =
Now i'm looking for a instruction ,a way or an algorithm to attach above information to achieve following Join Script as mentioned sql Query.
FROM Orders INNER JOIN
Customers ON Orders.CustomerID = Customers.CustomerID INNER JOIN
Products ON Orders.OrderID = Products.ProductID AND
Orders.EmployeeID = Products.SupplierID AND
Customers.CompanyName = Products.ProductName
or in the other words a functon with this specification:
function GetJoinScriptFor(List of Join objects as a Array Parameter) : String containing Join Script.
I think at this point you should figure out my problem, am i right?
I tried to follow the lead of EnterPrise manager But sometimes
i can't guess its method.i hope you help me.
Thanks for your patience to follow my problem.
Thanks in advance.
With Best Regards.
|