Skip to content

Commit

Permalink
#25 Leave JOINs supported by all databases, add rawJoin for the rest
Browse files Browse the repository at this point in the history
  • Loading branch information
irar2 committed Dec 21, 2016
1 parent 8a31d36 commit 18ee9f4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 71 deletions.
24 changes: 4 additions & 20 deletions Sources/Join.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,12 @@ public enum Join: Buildable {
case join(Table)
/// The SQL LEFT OUTER JOIN statement.
case left(Table)
/// The SQL RIGHT OUTER JOIN statement.
case right(Table)
/// The SQL FULL OUTER JOIN statement.
case full(Table)
/// The SQL CROSS JOIN statement.
case cross(Table)
/// The SQL NATURAL INNER JOIN statement.
case natural(Table)
/// The SQL NATURAL LEFT OUTER JOIN statement.
case naturalLeft(Table)
/// The SQL NATURAL RIGHT OUTER JOIN statement.
case naturalRight(Table)
/// The SQL NATURAL FULL OUTER JOIN statement.
case naturalFull(Table)
/// A String with a join statement.
case raw(String, Table)

/// Build the query component using `QueryBuilder`.
///
Expand All @@ -48,20 +40,12 @@ public enum Join: Buildable {
return try " JOIN " + table.build(queryBuilder: queryBuilder)
case .left(let table):
return try " LEFT JOIN " + table.build(queryBuilder: queryBuilder)
case .right(let table):
return try " RIGHT JOIN " + table.build(queryBuilder: queryBuilder)
case .full(let table):
return try " FULL JOIN " + table.build(queryBuilder: queryBuilder)
case .cross(let table):
return try " CROSS JOIN " + table.build(queryBuilder: queryBuilder)
case .natural(let table):
return try " NATURAL JOIN " + table.build(queryBuilder: queryBuilder)
case .naturalLeft(let table):
return try " NATURAL LEFT JOIN " + table.build(queryBuilder: queryBuilder)
case .naturalRight(let table):
return try " NATURAL RIGHT JOIN " + table.build(queryBuilder: queryBuilder)
case .naturalFull(let table):
return try " NATURAL FULL JOIN " + table.build(queryBuilder: queryBuilder)
case .raw(let raw, let table):
return try " " + raw + " " + table.build(queryBuilder: queryBuilder)
}
}
}
Expand Down
49 changes: 5 additions & 44 deletions Sources/Select.swift
Original file line number Diff line number Diff line change
Expand Up @@ -406,26 +406,6 @@ public struct Select: Query {
return new
}

/// Create an SQL SELECT RIGHT JOIN statement.
///
/// - Parameter table: The right table used in performing the join. The left table is the table field of this `Select` instance.
/// - Returns: A new instance of Select corresponding to the SELECT RIGHT JOIN.
public func rightJoin(_ table: Table) -> Select {
var new = self
new.joins.append((.right(table), nil, nil))
return new
}

/// Create an SQL SELECT FULL JOIN statement.
///
/// - Parameter table: The right table used in performing the join. The left table is the table field of this `Select` instance.
/// - Returns: A new instance of Select corresponding to the SELECT FULL JOIN.
public func fullJoin(_ table: Table) -> Select {
var new = self
new.joins.append((.full(table), nil, nil))
return new
}

/// Create an SQL SELECT CROSS JOIN statement.
///
/// - Parameter table: The right table used in performing the join. The left table is the table field of this `Select` instance.
Expand All @@ -446,33 +426,14 @@ public struct Select: Query {
return new
}

/// Create an SQL SELECT NATURAL LEFT JOIN statement.
///
/// - Parameter table: The right table used in performing the join. The left table is the table field of this `Select` instance.
/// - Returns: A new instance of Select corresponding to the SELECT NATURAL LEFT JOIN.
public func naturalLeftJoin(_ table: Table) -> Select {
var new = self
new.joins.append((.naturalLeft(table), nil, nil))
return new
}

/// Create an SQL SELECT NATURAL RIGHT JOIN statement.
///
/// - Parameter table: The right table used in performing the join. The left table is the table field of this `Select` instance.
/// - Returns: A new instance of Select corresponding to the SELECT NATURAL RIGHT JOIN.
public func naturalRightJoin(_ table: Table) -> Select {
var new = self
new.joins.append((.naturalRight(table), nil, nil))
return new
}

/// Create an SQL SELECT NATURAL FULL JOIN statement.
/// Create a join statement with the type of join specified in the String.
///
/// - Parameter raw: A String containg a join to apply.
/// - Parameter table: The right table used in performing the join. The left table is the table field of this `Select` instance.
/// - Returns: A new instance of Select corresponding to the SELECT NATURAL FULL JOIN.
public func naturalFullJoin(_ table: Table) -> Select {
/// - Returns: A new instance of Select corresponding to the join.
public func rawJoin(_ raw: String, _ table: Table) -> Select {
var new = self
new.joins.append((.naturalFull(table), nil, nil))
new.joins.append((.raw(raw, table), nil, nil))
return new
}
}
14 changes: 7 additions & 7 deletions Tests/SwiftKueryTests/TestJoin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@ class TestJoin: XCTestCase {
let connection = createConnection()

var s = Select(from: myTable1)
.rightJoin(myTable2)
.join(myTable2)
.on(myTable1.b == myTable2.b)
var kuery = connection.descriptionOf(query: s)
var query = "SELECT * FROM table1Join RIGHT JOIN table2Join ON table1Join.b = table2Join.b"
var query = "SELECT * FROM table1Join JOIN table2Join ON table1Join.b = table2Join.b"
XCTAssertEqual(kuery, query, "\nError in query construction: \n\(kuery) \ninstead of \n\(query)")

s = Select(from: myTable1)
.naturalJoin(myTable2)
.on(myTable1.b == myTable2.b)
.naturalLeftJoin(myTable3)
.rawJoin("NATURAL LEFT JOIN", myTable3)
.on(myTable1.b == myTable3.b)
kuery = connection.descriptionOf(query: s)
query = "SELECT * FROM table1Join NATURAL JOIN table2Join ON table1Join.b = table2Join.b NATURAL LEFT JOIN table3Join ON table1Join.b = table3Join.b"
Expand All @@ -89,12 +89,12 @@ class TestJoin: XCTestCase {
XCTAssertEqual(kuery, query, "\nError in query construction: \n\(kuery) \ninstead of \n\(query)")

s = Select(from: t1)
.naturalRightJoin(t2)
.join(t2)
.on(t1.b == t2.b)
.naturalFullJoin(t3)
.rawJoin("NATURAL FULL JOIN", t3)
.on(t1.b == t3.b)
kuery = connection.descriptionOf(query: s)
query = "SELECT * FROM table1Join AS t1 NATURAL RIGHT JOIN table2Join AS t2 ON t1.b = t2.b NATURAL FULL JOIN table3Join AS t3 ON t1.b = t3.b"
query = "SELECT * FROM table1Join AS t1 JOIN table2Join AS t2 ON t1.b = t2.b NATURAL FULL JOIN table3Join AS t3 ON t1.b = t3.b"
XCTAssertEqual(kuery, query, "\nError in query construction: \n\(kuery) \ninstead of \n\(query)")

s = Select(from: myTable1)
Expand All @@ -105,7 +105,7 @@ class TestJoin: XCTestCase {
XCTAssertEqual(kuery, query, "\nError in query construction: \n\(kuery) \ninstead of \n\(query)")

s = Select(from: t1)
.fullJoin(t2)
.rawJoin("FULL JOIN", t2)
.using(t1.b)
kuery = connection.descriptionOf(query: s)
query = "SELECT * FROM table1Join AS t1 FULL JOIN table2Join AS t2 USING (b)"
Expand Down

0 comments on commit 18ee9f4

Please sign in to comment.