Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ let package = Package(
.package(url: "https://github.com/vapor/postgresql.git", from: "1.0.0-rc"),
],
targets: [
.target(name: "FluentPostgreSQL", dependencies: ["Async", "Fluent", "PostgreSQL"]),
.target(name: "FluentPostgreSQL", dependencies: ["Async", "FluentSQL", "PostgreSQL"]),
.testTarget(name: "FluentPostgreSQLTests", dependencies: ["FluentBenchmark", "FluentPostgreSQL"]),
]
)
9 changes: 0 additions & 9 deletions Sources/FluentPostgreSQL/Deprecated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,3 @@ public protocol PostgreSQLEnumType { }
// - warning: Deprecated.
@available(*, deprecated, message: "Use custom migration instead.")
public protocol PostgreSQLType { }


extension QueryBuilder where Database == PostgreSQLDatabase {
/// - warning: Deprecated.
@available(*, deprecated, renamed: "groupBy(_:)")
public func group<T>(by field: KeyPath<Result, T>) -> Self {
return groupBy(field)
}
}
31 changes: 30 additions & 1 deletion Sources/FluentPostgreSQL/Exports.swift
Original file line number Diff line number Diff line change
@@ -1,2 +1,31 @@
@_exported import Fluent
@_exported import FluentSQL
@_exported import PostgreSQL

//extension QueryBuilder where Database == PostgreSQLDatabase {
// public func keys(_ keys: PostgreSQLQuery.Key...) -> Self {
// query.keys = keys
// return self
// }
//
// // MARK: Group By
//
// /// Adds a group by to the query builder.
// ///
// /// query.groupBy(\.name)
// ///
// /// - parameters:
// /// - field: Swift `KeyPath` to field on model to group by.
// /// - returns: Query builder for chaining.
// public func groupBy<T>(_ field: KeyPath<Result, T>) -> Self {
// return groupBy(.expression(.column(PostgreSQLDatabase.queryField(.keyPath(field))), alias: nil))
// }
//
// /// Adds a manually created group by to the query builder.
// /// - parameters:
// /// - groupBy: New `Query.GroupBy` to add.
// /// - returns: Query builder for chaining.
// public func groupBy(_ groupBy: PostgreSQLQuery.Key) -> Self {
// query.groupBy.append(groupBy)
// return self
// }
//}
19 changes: 19 additions & 0 deletions Sources/FluentPostgreSQL/Fluent+PostgreSQLUpsert.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
extension _PostgreSQLModel {
public func create(orUpdate: Bool, on conn: DatabaseConnectable) -> Future<Self> {
return Self.query(on: conn).create(orUpdate: orUpdate, self)
}
}

extension QueryBuilder where Result: _PostgreSQLModel, Result.Database == Database {
public func create(orUpdate: Bool, _ model: Result) -> Future<Result> {
if orUpdate {
let row = SQLQueryEncoder(PostgreSQLExpression.self).encode(model)
let values = row.map { row -> (PostgreSQLIdentifier, PostgreSQLExpression) in
return (.identifier(row.key), row.value)
}
self.query.upsert = .upsert([.keyPath(Result.idKey)], values)
}
return create(model)
}

}
2 changes: 1 addition & 1 deletion Sources/FluentPostgreSQL/FluentPostgreSQLProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public final class FluentPostgreSQLProvider: Provider {
}

return worker.withPooledConnection(to: .psql) { conn in
return conn.simpleQuery("SELECT current_setting('server_version') as version", decoding: Setting.self).map { rows in
return conn.select().column(.function("current_setting", [.expression(.literal(.string("server_version")))], as: .identifier("version"))).all(decoding: Setting.self).map { rows in
_serverVersion = rows[0].version
if let versionString = _serverVersion {
let pointIndex = versionString.index(of: ".") ?? versionString.endIndex
Expand Down
59 changes: 59 additions & 0 deletions Sources/FluentPostgreSQL/FluentPostgreSQLQuery.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
public enum FluentPostgreSQLQueryStatement: FluentSQLQueryStatement {
public static var insert: FluentPostgreSQLQueryStatement { return ._insert }
public static var select: FluentPostgreSQLQueryStatement { return ._select }
public static var update: FluentPostgreSQLQueryStatement { return ._update }
public static var delete: FluentPostgreSQLQueryStatement { return ._delete }

public var isInsert: Bool {
switch self {
case ._insert: return true
default: return false
}
}

case _insert
case _select
case _update
case _delete
}

public struct FluentPostgreSQLQuery: FluentSQLQuery {
public typealias Statement = FluentPostgreSQLQueryStatement
public typealias TableIdentifier = PostgreSQLTableIdentifier
public typealias Expression = PostgreSQLExpression
public typealias SelectExpression = PostgreSQLSelectExpression
public typealias Join = PostgreSQLJoin
public typealias OrderBy = PostgreSQLOrderBy
public typealias GroupBy = PostgreSQLGroupBy
public typealias Upsert = PostgreSQLUpsert

public var statement: Statement
public var table: TableIdentifier
public var keys: [SelectExpression]
public var values: [String : Expression]
public var joins: [Join]
public var predicate: Expression?
public var orderBy: [OrderBy]
public var groupBy: [GroupBy]
public var limit: Int?
public var offset: Int?
public var upsert: PostgreSQLUpsert?
public var defaultBinaryOperator: PostgreSQLBinaryOperator

public static func query(_ statement: Statement, _ table: TableIdentifier) -> FluentPostgreSQLQuery {
return .init(
statement: statement,
table: table,
keys: [],
values: [:],
joins: [],
predicate: nil,
orderBy: [],
groupBy: [],
limit: nil,
offset: nil,
upsert: nil,
defaultBinaryOperator: .and
)
}
}
34 changes: 34 additions & 0 deletions Sources/FluentPostgreSQL/FluentPostgreSQLSchema.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
public enum FluentPostgreSQLSchemaStatement: FluentSQLSchemaStatement {
public static var createTable: FluentPostgreSQLSchemaStatement { return ._createTable }
public static var alterTable: FluentPostgreSQLSchemaStatement { return ._alterTable }
public static var dropTable: FluentPostgreSQLSchemaStatement { return ._dropTable }

case _createTable
case _alterTable
case _dropTable
}

public struct FluentPostgreSQLSchema: FluentSQLSchema {
public typealias Statement = FluentPostgreSQLSchemaStatement
public typealias TableIdentifier = PostgreSQLTableIdentifier
public typealias ColumnDefinition = PostgreSQLColumnDefinition
public typealias TableConstraint = PostgreSQLTableConstraint

public var statement: Statement
public var table: TableIdentifier
public var columns: [PostgreSQLColumnDefinition]
public var deleteColumns: [PostgreSQLColumnIdentifier]
public var constraints: [PostgreSQLTableConstraint]
public var deleteConstraints: [PostgreSQLTableConstraint]

public static func schema(_ statement: Statement, _ table: TableIdentifier) -> FluentPostgreSQLSchema {
return .init(
statement: statement,
table: table,
columns: [],
deleteColumns: [],
constraints: [],
deleteConstraints: []
)
}
}
Loading