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

introduces joins into select from

parent b0a8bfe6
No related branches found
No related tags found
No related merge requests found
package unibz.cs.semint.kprime.domain.dql
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty
import unibz.cs.semint.kprime.domain.ddl.Column
class From {
@JacksonXmlProperty(isAttribute = true)
var tableName=String()
@JacksonXmlProperty(isAttribute = true)
var alias=String()
@JacksonXmlProperty(isAttribute = true)
var joinOn=String()
var joins : ArrayList<Join> ? = null
}
\ No newline at end of file
package unibz.cs.semint.kprime.domain.dql
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty
class Join {
@JacksonXmlProperty(isAttribute = true)
var joinOn=String()
@JacksonXmlProperty(isAttribute = true)
var joinTable=String()
@JacksonXmlProperty(isAttribute = true)
var joinType=String()
}
\ No newline at end of file
......@@ -34,17 +34,9 @@ class SQLizeSelectUseCase {
.map { a -> "\"${a.name}\"" }.toList().joinToString(",") + System.lineSeparator()
sql += "FROM "
for (from in select.from) {
if (from.joinOn.isEmpty()) {
sql += " ${from.tableName}"
if (!from.alias.isEmpty()) sql += " AS ${from.alias}"
sql += System.lineSeparator()
}
else {
if (from.alias.isEmpty())
sql += " JOIN ${from.tableName} ON ${from.joinOn}"
else
sql += "JOIN ${from.tableName} AS ${from.alias} ON ${from.joinOn}"
}
}
if (!select.where.condition.isEmpty()) {
sql += "WHERE ${select.where.condition}" //+ System.lineSeparator()
......
......@@ -51,7 +51,7 @@ class QueryTest {
<attributes name="Surname"/>
</attributes>
<from>
<from tableName="Table1" alias="" joinOn=""/>
<from tableName="Table1" alias=""/>
</from>
<where condition="Name='Gigi'"/>
</select>
......@@ -86,7 +86,7 @@ class QueryTest {
// given
val select = Select()
val att = Attribute()
att.name="ww"
att.name="ww"
select.attributes.add(att)
val from = From()
from.tableName="tab"
......@@ -102,6 +102,27 @@ class QueryTest {
""".trimIndent(),selectSql)
}
@Test
fun test_simple_join_to_sql(){
// given
val select = Select()
val att = Attribute()
att.name="ww"
select.attributes.add(att)
val from = From()
from.tableName="Orders"
select.from.add(from)
select.where.condition="a = b"
// when
val selectSql = SQLizeSelectUseCase().sqlize(select)
// then
assertEquals1("""
SELECT "ww"
FROM Orders
WHERE a = b
""".trimIndent(),selectSql)
}
@Test
fun test_from_union_query_to_xml() {
// given
......@@ -117,7 +138,7 @@ class QueryTest {
<attributes name="Surname"/>
</attributes>
<from>
<from tableName="Table1" alias="" joinOn=""/>
<from tableName="Table1" alias=""/>
</from>
<where condition="Name='Gigi'"/>
</select>
......@@ -129,7 +150,7 @@ class QueryTest {
<attributes name="Surname"/>
</attributes>
<from>
<from tableName="Table2" alias="" joinOn=""/>
<from tableName="Table2" alias=""/>
</from>
<where condition="Name='Gigi'"/>
</selects>
......
......@@ -65,7 +65,7 @@ class XMLDeserializerDatabaseTest {
<attributes name="name"/>
</attributes>
<from>
<from tableName="people" alias="" joinOn=""/>
<from tableName="people" alias=""/>
</from>
<where condition=""/>
</select>
......@@ -76,7 +76,7 @@ class XMLDeserializerDatabaseTest {
<attributes name="name"/>
</attributes>
<from>
<from tableName="people" alias="" joinOn=""/>
<from tableName="people" alias=""/>
</from>
<where condition=""/>
</select>
......
......@@ -78,7 +78,7 @@ class XMLSerializerChangeSetTest {
<attributes name="Surname"/>
</attributes>
<from>
<from tableName="film" alias="" joinOn=""/>
<from tableName="film" alias=""/>
</from>
<where condition="Name='Gigi'"/>
</select>
......
......@@ -80,7 +80,7 @@ class XMLSerializerDatabaseTest {
<attributes name="name"/>
</attributes>
<from>
<from tableName="people" alias="" joinOn=""/>
<from tableName="people" alias=""/>
</from>
<where condition=""/>
</select>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment