Skip to content

Commit

Permalink
Merge pull request #42 from vapor/sql
Browse files Browse the repository at this point in the history
update to sql 2.0
  • Loading branch information
tanner0101 authored Jun 19, 2018
2 parents 7670865 + b3c47b8 commit 2f04c08
Show file tree
Hide file tree
Showing 62 changed files with 841 additions and 2,417 deletions.
11 changes: 7 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@ let package = Package(
.package(url: "https://github.com/vapor/core.git", from: "3.0.0"),

// 🗄 Core services for creating database integrations.
.package(url: "https://github.com/vapor/database-kit.git", from: "1.0.0"),
.package(url: "https://github.com/vapor/database-kit.git", from: "1.2.0"),

// *️⃣ Build SQL queries in Swift. Extensible, protocol-based design that supports DQL, DML, and DDL.
.package(url: "https://github.com/vapor/sql.git", from: "2.0.0-beta"),
],
targets: [
.testTarget(name: "SQLiteTests", dependencies: ["SQLite"]),
.testTarget(name: "SQLiteTests", dependencies: ["SQLite", "SQLBenchmark"]),
]
)

#if os(Linux)
package.targets.append(.target(name: "CSQLite"))
package.targets.append(.target(name: "SQLite", dependencies: ["Async", "Bits", "Core", "CSQLite", "DatabaseKit", "Debugging"]))
package.targets.append(.target(name: "SQLite", dependencies: ["Async", "Bits", "Core", "CSQLite", "DatabaseKit", "Debugging", "SQL"]))
#else
package.targets.append(.target(name: "SQLite", dependencies: ["Async", "Bits", "Core", "DatabaseKit", "Debugging"]))
package.targets.append(.target(name: "SQLite", dependencies: ["Async", "Bits", "Core", "DatabaseKit", "Debugging", "SQL"]))
#endif
19 changes: 17 additions & 2 deletions Sources/SQLite/Codable/SQLiteDataDecoder.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
struct SQLiteDataDecoder {
init() { }
/// Decodes `Decodable` types from `SQLiteData`.
///
/// let string = try SQLiteDecoder().decode(String.self, from: .text("Hello"))
/// print(string) // "Hello"
///
public struct SQLiteDataDecoder {
/// Creates a new `SQLiteDataDecoder`.
public init() { }

/// Decodes `Decodable` types from `SQLiteData`.
///
/// let string = try SQLiteDecoder().decode(String.self, from: .text("Hello"))
/// print(string) // "Hello"
///
/// - parameters:
/// - type: `Decodable` type to decode.
/// - data: `SQLiteData` to decode.
/// - returns: Instance of decoded type.
public func decode<D>(_ type: D.Type, from data: SQLiteData) throws -> D where D: Decodable {
if let convertible = type as? SQLiteDataConvertible.Type {
return try convertible.convertFromSQLiteData(data) as! D
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,42 @@
public protocol SQLiteQueryExpressionRepresentable {
var sqliteQueryExpression: SQLiteQuery.Expression { get }
}

struct SQLiteQueryExpressionEncoder {
init() { }
/// Encodes `Encodable` values to `SQLiteData`.
///
/// let expr = try SQLiteDataEncoder().encode("Hello")
/// print(expr) // .text("Hello")
///
/// Conform your types to `SQLiteDataConvertible` to
/// customize how they are encoded.
public struct SQLiteDataEncoder {
/// Creates a new `SQLiteDataEncoder`.
public init() { }

func encode<E>(_ value: E) throws -> SQLiteQuery.Expression
where E: Encodable
{
if let sqlite = value as? SQLiteQueryExpressionRepresentable {
return sqlite.sqliteQueryExpression
} else if let value = value as? SQLiteDataConvertible {
return try .data(value.convertToSQLiteData())
/// Encodes `Encodable` values to `SQLiteData`.
///
/// let expr = try SQLiteDataEncoder().encode("Hello")
/// print(expr) // .text("Hello")
///
/// - parameters:
/// - value: `Encodable` value to encode.
/// - returns: `SQLiteData` representing the encoded data.
public func encode(_ value: Encodable) throws -> SQLiteData {
if let value = value as? SQLiteDataConvertible {
return try value.convertToSQLiteData()
} else {
let encoder = _Encoder()
do {
try value.encode(to: encoder)
return encoder.data!
} catch is _DoJSONError {
let json = try JSONEncoder().encode(value)
return .data(.blob(json))
struct AnyEncodable: Encodable {
var encodable: Encodable
init(_ encodable: Encodable) {
self.encodable = encodable
}
func encode(to encoder: Encoder) throws {
try encodable.encode(to: encoder)
}
}
let json = try JSONEncoder().encode(AnyEncodable(value))
return .blob(json)
}
}
}
Expand All @@ -29,7 +46,7 @@ struct SQLiteQueryExpressionEncoder {
private final class _Encoder: Encoder {
let codingPath: [CodingKey] = []
let userInfo: [CodingUserInfoKey: Any] = [:]
var data: SQLiteQuery.Expression?
var data: SQLiteData?

init() {
self.data = nil
Expand Down Expand Up @@ -60,15 +77,12 @@ struct SQLiteQueryExpressionEncoder {
}

mutating func encodeNil() throws {
encoder.data = .literal(.null)
encoder.data = .null
}

mutating func encode<T>(_ value: T) throws where T : Encodable {
if let sqlite = value as? SQLiteQueryExpressionRepresentable {
encoder.data = sqlite.sqliteQueryExpression
return
} else if let convertible = value as? SQLiteDataConvertible {
encoder.data = try .data(convertible.convertToSQLiteData())
if let convertible = value as? SQLiteDataConvertible {
encoder.data = try convertible.convertToSQLiteData()
return
}
try value.encode(to: encoder)
Expand Down
90 changes: 0 additions & 90 deletions Sources/SQLite/Codable/SQLiteQueryEncoder.swift

This file was deleted.

29 changes: 27 additions & 2 deletions Sources/SQLite/Codable/SQLiteRowDecoder.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
/// Decodes `Decodable` types from SQLite rows (`[SQLiteColumn: SQLiteData]`).
///
/// struct User: Codable {
/// var name: String
/// }
///
/// let row: [SQLiteColumn: SQLiteData] ...
/// let user = try SQLiteRowDecoder().decode(User.self, from: row, table: "users")
///
/// Uses `SQLiteDataDecoder` internally to decode each column. Use `SQLiteDataConvertible` to
/// customize how your types are decoded.
public struct SQLiteRowDecoder {
/// Creates a new `SQLiteRowDecoder`.
public init() { }

public func decode<D>(_ type: D.Type, from row: [SQLiteColumn: SQLiteData], table: String? = nil) throws -> D
/// Decodes `Decodable` types from SQLite rows (`[SQLiteColumn: SQLiteData]`).
///
/// struct User: Codable {
/// var name: String
/// }
///
/// let row: [SQLiteColumn: SQLiteData] ...
/// let user = try SQLiteRowDecoder().decode(User.self, from: row, table: "users")
///
/// - parameters:
/// - type: `Decodable` type to decode.
/// - data: SQLite row (`[SQLiteColumn: SQLiteData]`) to decode.
/// - returns: Instance of decoded type.
public func decode<D>(_ type: D.Type, from row: [SQLiteColumn: SQLiteData], table: SQLiteTableIdentifier? = nil) throws -> D
where D: Decodable
{
return try D(from: _Decoder(row: row, table: table))
return try D(from: _Decoder(row: row, table: table?.identifier.string))
}

// MARK: Private
Expand Down
6 changes: 6 additions & 0 deletions Sources/SQLite/Database/DatabaseIdentifier+SQLite.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
extension DatabaseIdentifier {
/// Default `DatabaseIdentifier` for SQLite databases.
public static var sqlite: DatabaseIdentifier<SQLiteDatabase> {
return "sqlite"
}
}
Loading

0 comments on commit 2f04c08

Please sign in to comment.