Skip to content
Snippets Groups Projects
Commit 8653ec85 authored by npedot's avatar npedot
Browse files

adds complete joins to select from

parent c08a6611
No related branches found
No related tags found
No related merge requests found
......@@ -10,4 +10,9 @@ class From {
var alias=String()
var joins : ArrayList<Join> ? = null
fun addJoin(join: Join ) {
if (joins==null) joins = ArrayList<Join>()
joins!!.add(join)
}
}
\ No newline at end of file
......@@ -4,10 +4,14 @@ import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty
class Join {
@JacksonXmlProperty(isAttribute = true)
var joinOn=String()
var joinOnLeft=String()
@JacksonXmlProperty(isAttribute = true)
var joinTable=String()
var joinLeftTable=String()
@JacksonXmlProperty(isAttribute = true)
var joinType=String()
@JacksonXmlProperty(isAttribute = true)
var joinOnRight=String()
@JacksonXmlProperty(isAttribute = true)
var joinRightTable=String()
}
\ No newline at end of file
package unibz.cs.semint.kprime.usecase.common
import unibz.cs.semint.kprime.domain.ddl.Database
import unibz.cs.semint.kprime.domain.dql.*
class SQLizeSelectUseCase {
......@@ -37,6 +36,12 @@ class SQLizeSelectUseCase {
sql += " ${from.tableName}"
if (!from.alias.isEmpty()) sql += " AS ${from.alias}"
sql += System.lineSeparator()
if (from.joins!=null){
for (join in from.joins as ArrayList<Join>) {
sql += "${join.joinType} JOIN" + System.lineSeparator()
sql += "ON ${join.joinLeftTable}.${join.joinOnLeft} = ${join.joinRightTable}.${join.joinOnRight}" + System.lineSeparator()
}
}
}
if (!select.where.condition.isEmpty()) {
sql += "WHERE ${select.where.condition}" //+ System.lineSeparator()
......
......@@ -111,6 +111,13 @@ class QueryTest {
select.attributes.add(att)
val from = From()
from.tableName="Orders"
val join = Join()
join.joinLeftTable = "Orders"
join.joinOnLeft = "customerId"
join.joinRightTable = "Customer"
join.joinOnRight = "customerId"
join.joinType = "INNER"
from.addJoin(join)
select.from.add(from)
select.where.condition="a = b"
// when
......@@ -119,6 +126,47 @@ class QueryTest {
assertEquals1("""
SELECT "ww"
FROM Orders
INNER JOIN
ON Orders.customerId = Customer.customerId
WHERE a = b
""".trimIndent(),selectSql)
}
@Test
fun test_multiple_join_to_sql(){
// given
val select = Select()
val att = Attribute()
att.name="ww"
select.attributes.add(att)
val from = From()
from.tableName="Orders"
val join = Join()
join.joinLeftTable = "Orders"
join.joinOnLeft = "customerId"
join.joinRightTable = "Customer"
join.joinOnRight = "customerId"
join.joinType = "INNER"
from.addJoin(join)
val join2 = Join()
join2.joinLeftTable = "Customer"
join2.joinOnLeft = "orderId"
join2.joinRightTable = "Sales"
join2.joinOnRight = "orderId"
join2.joinType = "LEFT"
from.addJoin(join2)
select.from.add(from)
select.where.condition="a = b"
// when
val selectSql = SQLizeSelectUseCase().sqlize(select)
// then
assertEquals1("""
SELECT "ww"
FROM Orders
INNER JOIN
ON Orders.customerId = Customer.customerId
LEFT JOIN
ON Customer.orderId = Sales.orderId
WHERE a = b
""".trimIndent(),selectSql)
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment