From 9f50300cf37a9789aca95c1718a817372db7e1d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthieu=20Barth=C3=A9lemy?= Date: Sun, 5 Apr 2020 08:22:25 +0800 Subject: [PATCH] Use driver encoder instead of hardcoded String data type --- Sources/MySQL/MySQL.swift | 9 +++++---- Sources/Postgres/Postgres.swift | 7 +++---- Sources/Sqlite/Sqlite.swift | 7 ++----- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/Sources/MySQL/MySQL.swift b/Sources/MySQL/MySQL.swift index 1fe2678..fa9a489 100644 --- a/Sources/MySQL/MySQL.swift +++ b/Sources/MySQL/MySQL.swift @@ -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 } @@ -16,12 +17,12 @@ struct dbDriver { func rawQuery(db: Database, query: SQLExpression) -> EventLoopFuture { 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 } } } - */ diff --git a/Sources/Postgres/Postgres.swift b/Sources/Postgres/Postgres.swift index 1aba71c..705ad10 100644 --- a/Sources/Postgres/Postgres.swift +++ b/Sources/Postgres/Postgres.swift @@ -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 { 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 diff --git a/Sources/Sqlite/Sqlite.swift b/Sources/Sqlite/Sqlite.swift index bc8330c..8022105 100644 --- a/Sources/Sqlite/Sqlite.swift +++ b/Sources/Sqlite/Sqlite.swift @@ -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 { 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