Skip to content

Commit 789ba49

Browse files
committed
update to sql protocols 2.0
1 parent 150bfae commit 789ba49

16 files changed

+349
-789
lines changed

Package.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ let package = Package(
1212
.package(url: "https://github.com/vapor/core.git", from: "3.0.0"),
1313

1414
// Swift ORM framework (queries, models, and relations) for building NoSQL and SQL database integrations.
15-
.package(url: "https://github.com/vapor/fluent.git", from: "3.0.0-rc"),
15+
.package(url: "https://github.com/vapor/fluent.git", .branch("sql")),
1616

1717
// 🐘 Non-blocking, event-driven Swift client for PostgreSQL.
18-
.package(url: "https://github.com/vapor/postgresql.git", from: "1.0.0-rc"),
18+
.package(url: "https://github.com/vapor/postgresql.git", .branch("sql")),
1919
],
2020
targets: [
21-
.target(name: "FluentPostgreSQL", dependencies: ["Async", "Fluent", "PostgreSQL"]),
21+
.target(name: "FluentPostgreSQL", dependencies: ["Async", "FluentSQL", "PostgreSQL"]),
2222
.testTarget(name: "FluentPostgreSQLTests", dependencies: ["FluentBenchmark", "FluentPostgreSQL"]),
2323
]
2424
)

Sources/FluentPostgreSQL/Deprecated.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ public protocol PostgreSQLEnumType { }
1818
public protocol PostgreSQLType { }
1919

2020

21-
extension QueryBuilder where Database == PostgreSQLDatabase {
22-
/// - warning: Deprecated.
23-
@available(*, deprecated, renamed: "groupBy(_:)")
24-
public func group<T>(by field: KeyPath<Result, T>) -> Self {
25-
return groupBy(field)
26-
}
27-
}
21+
//extension QueryBuilder where Database == PostgreSQLDatabase {
22+
// /// - warning: Deprecated.
23+
// @available(*, deprecated, renamed: "groupBy(_:)")
24+
// public func group<T>(by field: KeyPath<Result, T>) -> Self {
25+
// return groupBy(field)
26+
// }
27+
//}
Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,31 @@
1-
@_exported import Fluent
1+
@_exported import FluentSQL
22
@_exported import PostgreSQL
3+
4+
//extension QueryBuilder where Database == PostgreSQLDatabase {
5+
// public func keys(_ keys: PostgreSQLQuery.Key...) -> Self {
6+
// query.keys = keys
7+
// return self
8+
// }
9+
//
10+
// // MARK: Group By
11+
//
12+
// /// Adds a group by to the query builder.
13+
// ///
14+
// /// query.groupBy(\.name)
15+
// ///
16+
// /// - parameters:
17+
// /// - field: Swift `KeyPath` to field on model to group by.
18+
// /// - returns: Query builder for chaining.
19+
// public func groupBy<T>(_ field: KeyPath<Result, T>) -> Self {
20+
// return groupBy(.expression(.column(PostgreSQLDatabase.queryField(.keyPath(field))), alias: nil))
21+
// }
22+
//
23+
// /// Adds a manually created group by to the query builder.
24+
// /// - parameters:
25+
// /// - groupBy: New `Query.GroupBy` to add.
26+
// /// - returns: Query builder for chaining.
27+
// public func groupBy(_ groupBy: PostgreSQLQuery.Key) -> Self {
28+
// query.groupBy.append(groupBy)
29+
// return self
30+
// }
31+
//}

Sources/FluentPostgreSQL/FluentPostgreSQLProvider.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public final class FluentPostgreSQLProvider: Provider {
2323
}
2424

2525
return worker.withPooledConnection(to: .psql) { conn in
26-
return conn.simpleQuery("SELECT current_setting('server_version') as version", decoding: Setting.self).map { rows in
26+
return conn.select().column(.function("current_setting", [.expression(.literal(.string("server_version")))], as: .identifier("version"))).all(decoding: Setting.self).map { rows in
2727
_serverVersion = rows[0].version
2828
if let versionString = _serverVersion {
2929
let pointIndex = versionString.index(of: ".") ?? versionString.endIndex
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
public enum FluentPostgreSQLQueryStatement: FluentSQLQueryStatement {
2+
public static var insert: FluentPostgreSQLQueryStatement { return ._insert }
3+
public static var select: FluentPostgreSQLQueryStatement { return ._select }
4+
public static var update: FluentPostgreSQLQueryStatement { return ._update }
5+
public static var delete: FluentPostgreSQLQueryStatement { return ._delete }
6+
7+
public var isInsert: Bool {
8+
switch self {
9+
case ._insert: return true
10+
default: return false
11+
}
12+
}
13+
14+
case _insert
15+
case _select
16+
case _update
17+
case _delete
18+
}
19+
20+
public struct FluentPostgreSQLQuery: FluentSQLQuery {
21+
public typealias Statement = FluentPostgreSQLQueryStatement
22+
public typealias TableIdentifier = PostgreSQLTableIdentifier
23+
public typealias Expression = PostgreSQLExpression
24+
public typealias SelectExpression = PostgreSQLSelectExpression
25+
public typealias Join = PostgreSQLJoin
26+
public typealias OrderBy = PostgreSQLOrderBy
27+
28+
public var statement: Statement
29+
public var table: TableIdentifier
30+
public var keys: [SelectExpression]
31+
public var values: [String : Expression]
32+
public var joins: [Join]
33+
public var predicate: Expression?
34+
public var orderBy: [OrderBy]
35+
public var limit: Int?
36+
public var offset: Int?
37+
public var defaultBinaryOperator: GenericSQLBinaryOperator
38+
39+
public static func query(_ statement: Statement, _ table: TableIdentifier) -> FluentPostgreSQLQuery {
40+
return .init(
41+
statement: statement,
42+
table: table,
43+
keys: [],
44+
values: [:],
45+
joins: [],
46+
predicate: nil,
47+
orderBy: [],
48+
limit: nil,
49+
offset: nil,
50+
defaultBinaryOperator: .and
51+
)
52+
}
53+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
public enum FluentPostgreSQLSchemaStatement: FluentSQLSchemaStatement {
2+
public static var createTable: FluentPostgreSQLSchemaStatement { return ._createTable }
3+
public static var alterTable: FluentPostgreSQLSchemaStatement { return ._alterTable }
4+
public static var dropTable: FluentPostgreSQLSchemaStatement { return ._dropTable }
5+
6+
case _createTable
7+
case _alterTable
8+
case _dropTable
9+
}
10+
11+
public struct FluentPostgreSQLSchema: FluentSQLSchema {
12+
public typealias Statement = FluentPostgreSQLSchemaStatement
13+
public typealias TableIdentifier = PostgreSQLTableIdentifier
14+
public typealias ColumnDefinition = PostgreSQLColumnDefinition
15+
public typealias TableConstraint = PostgreSQLTableConstraint
16+
17+
public var statement: Statement
18+
public var table: TableIdentifier
19+
public var columns: [PostgreSQLColumnDefinition]
20+
public var deleteColumns: [PostgreSQLColumnIdentifier]
21+
public var constraints: [PostgreSQLTableConstraint]
22+
public var deleteConstraints: [PostgreSQLTableConstraint]
23+
24+
public static func schema(_ statement: Statement, _ table: TableIdentifier) -> FluentPostgreSQLSchema {
25+
return .init(
26+
statement: statement,
27+
table: table,
28+
columns: [],
29+
deleteColumns: [],
30+
constraints: [],
31+
deleteConstraints: []
32+
)
33+
}
34+
}

0 commit comments

Comments
 (0)