Skip to content

Commit 521d99a

Browse files
authored
Merge pull request #71 from vapor/sql
sql protocols 2.0
2 parents 1f74810 + 7658f62 commit 521d99a

21 files changed

+401
-896
lines changed

Diff for: Package.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ let package = Package(
1818
.package(url: "https://github.com/vapor/postgresql.git", from: "1.0.0-rc"),
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
)

Diff for: Sources/FluentPostgreSQL/Deprecated.swift

-9
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,3 @@ public protocol PostgreSQLEnumType { }
1616
// - warning: Deprecated.
1717
@available(*, deprecated, message: "Use custom migration instead.")
1818
public protocol PostgreSQLType { }
19-
20-
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-
}

Diff for: Sources/FluentPostgreSQL/Exports.swift

+30-1
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+
//}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
extension _PostgreSQLModel {
2+
public func create(orUpdate: Bool, on conn: DatabaseConnectable) -> Future<Self> {
3+
return Self.query(on: conn).create(orUpdate: orUpdate, self)
4+
}
5+
}
6+
7+
extension QueryBuilder where Result: _PostgreSQLModel, Result.Database == Database {
8+
public func create(orUpdate: Bool, _ model: Result) -> Future<Result> {
9+
if orUpdate {
10+
let row = SQLQueryEncoder(PostgreSQLExpression.self).encode(model)
11+
let values = row.map { row -> (PostgreSQLIdentifier, PostgreSQLExpression) in
12+
return (.identifier(row.key), row.value)
13+
}
14+
self.query.upsert = .upsert([.keyPath(Result.idKey)], values)
15+
}
16+
return create(model)
17+
}
18+
19+
}

Diff for: Sources/FluentPostgreSQL/FluentPostgreSQLProvider.swift

+1-1
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

Diff for: Sources/FluentPostgreSQL/FluentPostgreSQLQuery.swift

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
public typealias GroupBy = PostgreSQLGroupBy
28+
public typealias Upsert = PostgreSQLUpsert
29+
30+
public var statement: Statement
31+
public var table: TableIdentifier
32+
public var keys: [SelectExpression]
33+
public var values: [String : Expression]
34+
public var joins: [Join]
35+
public var predicate: Expression?
36+
public var orderBy: [OrderBy]
37+
public var groupBy: [GroupBy]
38+
public var limit: Int?
39+
public var offset: Int?
40+
public var upsert: PostgreSQLUpsert?
41+
public var defaultBinaryOperator: PostgreSQLBinaryOperator
42+
43+
public static func query(_ statement: Statement, _ table: TableIdentifier) -> FluentPostgreSQLQuery {
44+
return .init(
45+
statement: statement,
46+
table: table,
47+
keys: [],
48+
values: [:],
49+
joins: [],
50+
predicate: nil,
51+
orderBy: [],
52+
groupBy: [],
53+
limit: nil,
54+
offset: nil,
55+
upsert: nil,
56+
defaultBinaryOperator: .and
57+
)
58+
}
59+
}
+34
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)