Skip to content

Commit

Permalink
Merge pull request #12 from harlanhaskins/master
Browse files Browse the repository at this point in the history
Add fix for SQLite strings being coerced to numbers internally
  • Loading branch information
tanner0101 authored Oct 5, 2016
2 parents e7763a2 + 1da764e commit fec46a2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Sources/FluentSQLite/SQLiteDriver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class SQLiteDriver: Fluent.Driver {
*/
@discardableResult
public func query<T: Entity>(_ query: Query<T>) throws -> Node {
let serializer = GeneralSQLSerializer(sql: query.sql)
let serializer = SQLiteSerializer(sql: query.sql)
let (statement, values) = serializer.serialize()
let results = try database.execute(statement) { statement in
try self.bind(statement: statement, to: values)
Expand All @@ -42,7 +42,7 @@ public class SQLiteDriver: Fluent.Driver {
}

public func schema(_ schema: Schema) throws {
let serializer = GeneralSQLSerializer(sql: schema.sql)
let serializer = SQLiteSerializer(sql: schema.sql)
let (statement, values) = serializer.serialize()
try _ = raw(statement, values)
}
Expand Down
21 changes: 21 additions & 0 deletions Sources/FluentSQLite/SQLiteSerializer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Fluent
import SQLite

/**
SQLite-specific overrides for the GeneralSQLSerializer
*/
public class SQLiteSerializer: GeneralSQLSerializer {
/**
Serializes a SQLite data type.
*/
public override func sql(_ type: Schema.Field.DataType) -> String {
// SQLite has a design where any data type that does not contain `TEXT`,
// `CLOB`, or `CHAR` will be treated with `NUMERIC` affinity.
// All SQLite `STRING` fields should instead be declared with `TEXT`.
// More information: https://www.sqlite.org/datatype3.html
if case .string(_) = type {
return "TEXT"
}
return super.sql(type)
}
}
39 changes: 32 additions & 7 deletions Tests/FluentSQLiteTests/SQLiteDriverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,23 @@ class SQLite3Tests: XCTestCase {
override func setUp() {
driver = SQLiteDriver.makeTestConnection()
database = Database(driver)
}


func testSaveAndFind() {
_ = try! driver.raw("DROP TABLE IF EXISTS `posts`")
do {
_ = try driver.raw("CREATE TABLE IF NOT EXISTS `posts` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `title` CHAR(255), `text` CHAR(255))")
try Post.revert(database)
try Post.prepare(database)
Post.database = database
} catch {
XCTFail("Could not create table \(error)")
}
}

func testSaveAndFind() {
// try! database.create("posts") { creator in
// creator.id()
// creator.string("title")
// creator.string("text")
// }

var post = Post(id: nil, title: "Vapor & Tests", text: "Lorem ipsum etc...")
Post.database = database

do {
try post.save()
Expand All @@ -58,6 +57,32 @@ class SQLite3Tests: XCTestCase {
XCTFail("Could not find post: \(error)")
}


}

/**
This test ensures that a string containing a large number will
remain encoded as a string and not get coerced to a number internally.
*/
func testLargeNumericInput() {
let longNumericName = String(repeating: "1", count: 1000)
do {
var post = Post(id: nil,
title: longNumericName,
text: "Testing long number...")
try post.save()
} catch {
XCTFail("Could not create post: \(error)")
}

do {
let post = try Post.find(1)
XCTAssertNotNil(post)
XCTAssertEqual(post?.title, longNumericName)
} catch {
XCTFail("Could not find post: \(error)")
}

}

}

0 comments on commit fec46a2

Please sign in to comment.