Skip to content

Commit

Permalink
Use driver encoder instead of hardcoded String data type
Browse files Browse the repository at this point in the history
  • Loading branch information
m-barthelemy committed Apr 5, 2020
1 parent 92e7552 commit 9f50300
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
9 changes: 5 additions & 4 deletions Sources/MySQL/MySQL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import Foundation
import Fluent
import SQLKit
import FluentMySQLDriver
import MySQLKit
import QueuesFluentDriver

/*
struct dbDriver {

/*struct dbDriver {
private func dbDriver(_ database: Database) -> MySQLDatabase {
return database as! MySQLDatabase
}
Expand All @@ -16,12 +17,12 @@ struct dbDriver {

func rawQuery(db: Database, query: SQLExpression) -> EventLoopFuture<UUID?> {
let sql = (db as! SQLDatabase).serialize(query)
let binds = sql.binds.map { encodeValue($0 as! String) }
let encoder = MySQLDataEncoder()
let binds = sql.binds.map { try! encoder.encode($0) }
return dbDriver(db).query(sql.sql, binds).map { row in
let id = row.first?.column(JobModel.init().$id.key.description)?.uuid
return id
}
}
}

*/
7 changes: 3 additions & 4 deletions Sources/Postgres/Postgres.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ struct dbDriver {
return database as! PostgresDatabase
}

private func encodeValue(_ value: String) -> PostgresData {
return PostgresData(string: value)
}

func rawQuery(db: Database, query: SQLExpression) -> EventLoopFuture<UUID?> {
let sql = (db as! SQLDatabase).serialize(query)
let binds = sql.binds.map { encodeValue($0 as! String) }
let encoder = PostgresDataEncoder()
let binds = sql.binds.map { try! encoder.encode($0) }

return dbDriver(db).query(sql.sql, binds).map { row in
let id = row.first?.column(JobModel.init().$id.key.description)?.uuid
return id
Expand Down
7 changes: 2 additions & 5 deletions Sources/Sqlite/Sqlite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ import QueuesFluentDriver
return database as! SQLiteDatabase
}

private func encodeValue(_ value: String) -> SQLiteData {
return SQLiteData.text(value)
}

func rawQuery(db: Database, query: SQLExpression) -> EventLoopFuture<UUID?> {
let sql = (db as! SQLDatabase).serialize(query)
let binds = sql.binds.map { encodeValue($0 as! String) }
let encoder = SQLiteDataEncoder()
let binds = sql.binds.map { try! encoder.encode($0) }
return dbDriver(db).query(sql.sql, binds).map { row in
let id = try? row.first?.decode(column: JobModel.init().$id.key.description, as: JobModel.IDValue.self)
return id
Expand Down

0 comments on commit 9f50300

Please sign in to comment.