Skip to content

Commit

Permalink
Merge pull request #215 from vapor/null-nil
Browse files Browse the repository at this point in the history
nil/null
  • Loading branch information
tanner0101 authored Mar 27, 2017
2 parents b6de6d8 + 581d242 commit 841dadf
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
10 changes: 5 additions & 5 deletions Sources/Fluent/Query/Filter/Query+Filter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ extension QueryRepresentable {
_ entity: T.Type,
_ field: String,
_ comparison: Filter.Comparison,
_ value: NodeRepresentable
_ value: NodeRepresentable?
) throws -> Query<Self.E> {
let query = try makeQuery()
let value = try value.makeNode(in: query.context)
let value = try value?.makeNode(in: query.context) ?? .null
let filter = Filter(entity, .compare(field, comparison, value))
return try query.filter(filter)
}
Expand All @@ -28,7 +28,7 @@ extension QueryRepresentable {
public func filter<T: Entity>(
_ entity: T.Type,
_ field: String,
_ value: NodeRepresentable
_ value: NodeRepresentable?
) throws -> Query<Self.E> {
return try makeQuery()
.filter(entity, field, .equals, value)
Expand All @@ -39,7 +39,7 @@ extension QueryRepresentable {
public func filter(
_ field: String,
_ comparison: Filter.Comparison,
_ value: NodeRepresentable
_ value: NodeRepresentable?
) throws -> Query<Self.E> {
return try makeQuery()
.filter(E.self, field, comparison, value)
Expand All @@ -49,7 +49,7 @@ extension QueryRepresentable {
@discardableResult
public func filter(
_ field: String,
_ value: NodeRepresentable
_ value: NodeRepresentable?
) throws -> Query<Self.E> {
return try makeQuery()
.filter(field, .equals, value)
Expand Down
6 changes: 4 additions & 2 deletions Tests/FluentTests/PivotTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ class PivotTests: XCTestCase {

try atom.compounds.add(compound)

guard let (sql, _) = lqd.lastQuery else {
guard let (sql, values) = lqd.lastQuery else {
XCTFail("No query recorded")
return
}

XCTAssertEqual(
sql,
"INSERT INTO `atom_compound` (`\(Atom.foreignIdKey)`, `\(Compound.foreignIdKey)`) VALUES (?, ?)"
"INSERT INTO `atom_compound` (`\(Pivot<Atom, Compound>.idKey)`, `\(Atom.foreignIdKey)`, `\(Compound.foreignIdKey)`) VALUES (?, ?, ?)"
)

XCTAssertEqual(values.count, 3)
}

static let allTests = [
Expand Down
41 changes: 41 additions & 0 deletions Tests/FluentTests/SQLSerializerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,45 @@ class SQLSerializerTests: XCTestCase {
XCTAssertEqual(statement, "DELETE `compounds` FROM `compounds` JOIN `atoms` ON `compounds`.`id` = `atoms`.`compound_id` WHERE `atoms`.`name` = ?")
XCTAssertEqual(values.count, 1)
}

func testNull() throws {
let query = Query<Compound>(db)
let string: String? = nil
try query.filter("foo", string)
let (statement, values) = serialize(query)
XCTAssertEqual(statement, "SELECT `compounds`.* FROM `compounds` WHERE `compounds`.`foo` IS NULL")
XCTAssertEqual(values.count, 0)
}

func testNotNull() throws {
let query = Query<Compound>(db)
let string: String? = nil
try query.filter("foo", .notEquals, string)
let (statement, values) = serialize(query)
XCTAssertEqual(statement, "SELECT `compounds`.* FROM `compounds` WHERE `compounds`.`foo` IS NOT NULL")
XCTAssertEqual(values.count, 0)
}

func testNodeNull() throws {
let query = Query<Compound>(db)
try query.filter("foo", Node.null)
let (statement, values) = serialize(query)
XCTAssertEqual(statement, "SELECT `compounds`.* FROM `compounds` WHERE `compounds`.`foo` IS NULL")
XCTAssertEqual(values.count, 0)
}

func testPlainNil() throws {
let query = Query<Compound>(db)
try query.filter("foo", nil)
let (statement, values) = serialize(query)
XCTAssertEqual(statement, "SELECT `compounds`.* FROM `compounds` WHERE `compounds`.`foo` IS NULL")
XCTAssertEqual(values.count, 0)
}
}







0 comments on commit 841dadf

Please sign in to comment.