diff --git a/Sources/PostgreSQL/Codable/PostgreSQLDataDecoder.swift b/Sources/PostgreSQL/Codable/PostgreSQLDataDecoder.swift index 8d0de475..53735a25 100644 --- a/Sources/PostgreSQL/Codable/PostgreSQLDataDecoder.swift +++ b/Sources/PostgreSQL/Codable/PostgreSQLDataDecoder.swift @@ -1,8 +1,8 @@ -public struct PostgreSQLDataDecoder { +struct PostgreSQLDataDecoder { /// Creates a new `PostgreSQLDataDecoder`. - public init() {} + init() {} - public func decode(_ type: D.Type, from data: PostgreSQLData) throws -> D where D: Decodable { + func decode(_ type: D.Type, from data: PostgreSQLData) throws -> D where D: Decodable { if let convertible = type as? PostgreSQLDataConvertible.Type { return try convertible.convertFromPostgreSQLData(data) as! D } diff --git a/Sources/PostgreSQL/Codable/PostgreSQLRowDecoder.swift b/Sources/PostgreSQL/Codable/PostgreSQLRowDecoder.swift index e6d2fc09..54c287a7 100644 --- a/Sources/PostgreSQL/Codable/PostgreSQLRowDecoder.swift +++ b/Sources/PostgreSQL/Codable/PostgreSQLRowDecoder.swift @@ -1,7 +1,7 @@ /// Decodes `Decodable` types from PostgreSQL row data. -public struct PostgreSQLRowDecoder { +struct PostgreSQLRowDecoder { /// Creates a new `PostgreSQLRowDecoder`. - public init() { } + init() { } /// Decodes a `Decodable` object from `[DataColumn: PostgreSQLData]`. /// diff --git a/Sources/PostgreSQL/Codable/PostgreSQLValueEncoder.swift b/Sources/PostgreSQL/Codable/PostgreSQLValueEncoder.swift index 422b4bb0..e6d96a0f 100644 --- a/Sources/PostgreSQL/Codable/PostgreSQLValueEncoder.swift +++ b/Sources/PostgreSQL/Codable/PostgreSQLValueEncoder.swift @@ -3,9 +3,9 @@ /// let data = try PostgreSQLDataEncoder().encode("hello") /// print(data) // PostgreSQLData /// -public struct PostgreSQLDataEncoder { +struct PostgreSQLDataEncoder { /// Creates a new `PostgreSQLDataEncoder`. - public init() { } + init() { } /// Encodes the supplied `Encodable` object to `PostgreSQLData`. /// @@ -15,7 +15,7 @@ public struct PostgreSQLDataEncoder { /// - parameters: /// - encodable: `Encodable` object to encode. /// - returns: Encoded `PostgreSQLData`. - public func encode(_ encodable: Encodable) throws -> PostgreSQLData { + func encode(_ encodable: Encodable) throws -> PostgreSQLData { if let convertible = encodable as? PostgreSQLDataConvertible { return try convertible.convertToPostgreSQLData() } diff --git a/Sources/PostgreSQL/Column/PostgreSQLColumn.swift b/Sources/PostgreSQL/Column/PostgreSQLColumn.swift index 22d17117..b81a20a8 100644 --- a/Sources/PostgreSQL/Column/PostgreSQLColumn.swift +++ b/Sources/PostgreSQL/Column/PostgreSQLColumn.swift @@ -6,6 +6,7 @@ public struct PostgreSQLColumn: Hashable, Equatable { /// The column's name. public var name: String + /// Creates a new `PostgreSQLColumn`. public init(tableOID: UInt32 = 0, name: String) { self.tableOID = tableOID self.name = name diff --git a/Sources/PostgreSQL/Column/PostgreSQLDataTypeCode.swift b/Sources/PostgreSQL/Column/PostgreSQLDataTypeCode.swift index 895d128f..6918c2ed 100644 --- a/Sources/PostgreSQL/Column/PostgreSQLDataTypeCode.swift +++ b/Sources/PostgreSQL/Column/PostgreSQLDataTypeCode.swift @@ -1,48 +1,89 @@ /// The data type's raw object ID. /// Use `select * from pg_type where oid = ;` to lookup more information. public struct PostgreSQLDataFormat: Codable, Equatable, ExpressibleByIntegerLiteral { - /// Recognized types + /// `0` public static let null = PostgreSQLDataFormat(0) + /// `16` public static let bool = PostgreSQLDataFormat(16) + /// `17` public static let bytea = PostgreSQLDataFormat(17) + /// `18` public static let char = PostgreSQLDataFormat(18) + /// `19` public static let name = PostgreSQLDataFormat(19) + /// `20` public static let int8 = PostgreSQLDataFormat(20) + /// `21` public static let int2 = PostgreSQLDataFormat(21) + /// `23` public static let int4 = PostgreSQLDataFormat(23) + /// `24` public static let regproc = PostgreSQLDataFormat(24) + /// `25` public static let text = PostgreSQLDataFormat(25) + /// `26` public static let oid = PostgreSQLDataFormat(26) + /// `114` public static let json = PostgreSQLDataFormat(114) + /// `194` public static let pg_node_tree = PostgreSQLDataFormat(194) + /// `600` public static let point = PostgreSQLDataFormat(600) + /// `700` public static let float4 = PostgreSQLDataFormat(700) + /// `701` public static let float8 = PostgreSQLDataFormat(701) + /// `1000` public static let _bool = PostgreSQLDataFormat(1000) + /// `1001` public static let _bytea = PostgreSQLDataFormat(1001) + /// `1002` public static let _char = PostgreSQLDataFormat(1002) + /// `1003` public static let _name = PostgreSQLDataFormat(1003) + /// `1005` public static let _int2 = PostgreSQLDataFormat(1005) + /// `1007` public static let _int4 = PostgreSQLDataFormat(1007) + /// `1009` public static let _text = PostgreSQLDataFormat(1009) + /// `1016` public static let _int8 = PostgreSQLDataFormat(1016) + /// `1017` public static let _point = PostgreSQLDataFormat(1017) + /// `1021` public static let _float4 = PostgreSQLDataFormat(1021) + /// `1022` public static let _float8 = PostgreSQLDataFormat(1022) + /// `1034` public static let _aclitem = PostgreSQLDataFormat(1034) + /// `1042` public static let bpchar = PostgreSQLDataFormat(1042) + /// `1043` public static let varchar = PostgreSQLDataFormat(1043) + /// `1082` public static let date = PostgreSQLDataFormat(1082) + /// `1083` public static let time = PostgreSQLDataFormat(1083) + /// `1114` public static let timestamp = PostgreSQLDataFormat(1114) + /// `1115` public static let _timestamp = PostgreSQLDataFormat(1115) + /// `1184` public static let timestamptz = PostgreSQLDataFormat(1184) + /// `1266` public static let timetz = PostgreSQLDataFormat(1266) + /// `1700` public static let numeric = PostgreSQLDataFormat(1700) + /// `2278` public static let void = PostgreSQLDataFormat(2278) + /// `2950` public static let uuid = PostgreSQLDataFormat(2950) + /// `2951` public static let _uuid = PostgreSQLDataFormat(2951) + /// `3802` public static let jsonb = PostgreSQLDataFormat(3802) + /// `3807` public static let _jsonb = PostgreSQLDataFormat(3807) /// See `Equatable.==` diff --git a/Sources/PostgreSQL/Connection/PostgreSQLConnection+Connect.swift b/Sources/PostgreSQL/Connection/PostgreSQLConnection+Connect.swift index 4c6608a9..1afe82cf 100644 --- a/Sources/PostgreSQL/Connection/PostgreSQLConnection+Connect.swift +++ b/Sources/PostgreSQL/Connection/PostgreSQLConnection+Connect.swift @@ -1,21 +1,23 @@ extension PostgreSQLConnection { + /// Connects to PostgreSQL server via TCP. public static func connect( hostname: String = "localhost", port: Int = 5432, transport: TransportConfig = .cleartext, - on worker: Worker, - onError: @escaping (Error) -> () + on worker: Worker ) throws -> Future { - return try connect(to: .tcp(hostname: hostname, port: port), transport: transport, on: worker, onError: onError) + return try connect(to: .tcp(hostname: hostname, port: port), transport: transport, on: worker) } + /// Connects to PostgreSQL server specified by a `ServerAddress`. public static func connect( to serverAddress: ServerAddress, transport: TransportConfig = .cleartext, - on worker: Worker, - onError: @escaping (Error) -> () + on worker: Worker ) throws -> Future { - let handler = QueueHandler(on: worker, onError: onError) + let handler = QueueHandler(on: worker) { error in + ERROR(error.localizedDescription) + } let bootstrap = ClientBootstrap(group: worker.eventLoop) // Enable SO_REUSEADDR. .channelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) diff --git a/Sources/PostgreSQL/Connection/PostgreSQLConnection+Query.swift b/Sources/PostgreSQL/Connection/PostgreSQLConnection+Query.swift index 9b74fcb4..8853e6d4 100644 --- a/Sources/PostgreSQL/Connection/PostgreSQLConnection+Query.swift +++ b/Sources/PostgreSQL/Connection/PostgreSQLConnection+Query.swift @@ -1,4 +1,16 @@ extension PostgreSQLConnection { + /// Runs a query, returning each row to the supplied handler. + /// + /// try conn.query(.select(.all, from: "users")) { row in + /// print(row) + /// } + /// + /// Any values bound to the `DataQuery` as placeholders will be sent as query parameters. + /// + /// - parameters: + /// - query: `Query` to execute. + /// - onRow: PostgreSQL row accepting closure to handle results, if any. + /// - returns: A future that signals query completion. public func query(_ query: PostgreSQLQuery, _ onRow: @escaping ([PostgreSQLColumn: PostgreSQLData]) throws -> ()) -> Future { return self.query(query, resultFormat: .binary, onRow) } diff --git a/Sources/PostgreSQL/Connection/PostgreSQLConnection+ServerAddress.swift b/Sources/PostgreSQL/Connection/PostgreSQLConnection+ServerAddress.swift index d0c6299c..df3134f9 100644 --- a/Sources/PostgreSQL/Connection/PostgreSQLConnection+ServerAddress.swift +++ b/Sources/PostgreSQL/Connection/PostgreSQLConnection+ServerAddress.swift @@ -1,22 +1,27 @@ extension PostgreSQLConnection { /// Specifies how to connect to a PostgreSQL server. public struct ServerAddress { + /// Default PostgreSQL server address and port. public static var `default`: ServerAddress { return .tcp(hostname: "localhost", port: 5432) } + /// Default PostgreSQL socket file. public static var socketDefault: ServerAddress { return .unixSocket(path: "/tmp/.s.PGSQL.5432") } + /// TCP PostgreSQL address. public static func tcp(hostname: String, port: Int) -> ServerAddress { return .init(.tcp(hostname: hostname, port: port)) } + /// Unix socket PostgreSQL address. public static func unixSocket(path: String) -> ServerAddress { return .init(.unixSocket(path: path)) } + /// Custom PostgreSQL socket address. public static func socketAddress(_ socketAddress: SocketAddress) -> ServerAddress { return .init(.socketAddress(socketAddress)) } diff --git a/Sources/PostgreSQL/Connection/PostgreSQLConnection+SimpleQuery.swift b/Sources/PostgreSQL/Connection/PostgreSQLConnection+SimpleQuery.swift index b1100d03..fe29d66b 100644 --- a/Sources/PostgreSQL/Connection/PostgreSQLConnection+SimpleQuery.swift +++ b/Sources/PostgreSQL/Connection/PostgreSQLConnection+SimpleQuery.swift @@ -1,8 +1,10 @@ extension PostgreSQLConnection { + /// Performs a non-parameterized (text protocol) query to PostgreSQL. public func simpleQuery(_ query: String) -> Future { return operation { self._simpleQuery(query) { _ in }} } + /// Performs a non-parameterized (text protocol) query to PostgreSQL. public func simpleQuery(_ query: String, _ onRow: @escaping ([PostgreSQLColumn: PostgreSQLData]) throws -> ()) -> Future { return operation { self._simpleQuery(query, onRow) } } diff --git a/Sources/PostgreSQL/Connection/PostgreSQLConnection+TransportConfig.swift b/Sources/PostgreSQL/Connection/PostgreSQLConnection+TransportConfig.swift index 1e6f31a8..dc6bdc51 100644 --- a/Sources/PostgreSQL/Connection/PostgreSQLConnection+TransportConfig.swift +++ b/Sources/PostgreSQL/Connection/PostgreSQLConnection+TransportConfig.swift @@ -1,6 +1,7 @@ import NIOOpenSSL extension PostgreSQLConnection { + /// Transport-layer security configuration for the PostgreSQL connection. public struct TransportConfig { /// Does not attempt to enable TLS (this is the default). public static var cleartext: TransportConfig { diff --git a/Sources/PostgreSQL/Database/PostgreSQLDatabase.swift b/Sources/PostgreSQL/Database/PostgreSQLDatabase.swift index 5170aeb6..879bbb30 100644 --- a/Sources/PostgreSQL/Database/PostgreSQLDatabase.swift +++ b/Sources/PostgreSQL/Database/PostgreSQLDatabase.swift @@ -17,9 +17,7 @@ public final class PostgreSQLDatabase: Database, LogSupporting { public func newConnection(on worker: Worker) -> Future { let config = self.config do { - return try PostgreSQLConnection.connect(to: config.serverAddress, transport: config.transportConfig, on: worker) { error in - ERROR(error.localizedDescription) - }.flatMap { client in + return try PostgreSQLConnection.connect(to: config.serverAddress, transport: config.transportConfig, on: worker).flatMap { client in return client.authenticate( username: config.username, database: config.database, diff --git a/Sources/PostgreSQL/Database/PostgreSQLDatabaseConfig.swift b/Sources/PostgreSQL/Database/PostgreSQLDatabaseConfig.swift index e2540e3d..a9086f51 100644 --- a/Sources/PostgreSQL/Database/PostgreSQLDatabaseConfig.swift +++ b/Sources/PostgreSQL/Database/PostgreSQLDatabaseConfig.swift @@ -25,10 +25,12 @@ public struct PostgreSQLDatabaseConfig { /// See `PostgreSQLTransportConfig` for more info public let transportConfig: PostgreSQLConnection.TransportConfig + /// Creates a new `PostgreSQLDatabaseConfig`. public init(hostname: String, port: Int = 5432, username: String, database: String? = nil, password: String? = nil, transport: PostgreSQLConnection.TransportConfig = .cleartext) { self.init(serverAddress: .tcp(hostname: hostname, port: port), username: username, database: database, password: password, transport: transport) } + /// Creates a new `PostgreSQLDatabaseConfig`. public init(serverAddress: PostgreSQLConnection.ServerAddress, username: String, database: String? = nil, password: String? = nil, transport: PostgreSQLConnection.TransportConfig = .cleartext) { self.username = username self.database = database @@ -37,7 +39,8 @@ public struct PostgreSQLDatabaseConfig { self.transportConfig = transport } - public init?(url urlString: String, transport: PostgreSQLConnection.TransportConfig = .cleartext) throws { + /// Creates a new `PostgreSQLDatabaseConfig`. + public init?(url urlString: String, transport: PostgreSQLConnection.TransportConfig = .cleartext) { guard let url = URL(string: urlString) else { return nil } diff --git a/Sources/PostgreSQL/SQL/PostgreSQLAlterTable.swift b/Sources/PostgreSQL/SQL/PostgreSQLAlterTable.swift index 49301f6c..5f1b2eb5 100644 --- a/Sources/PostgreSQL/SQL/PostgreSQLAlterTable.swift +++ b/Sources/PostgreSQL/SQL/PostgreSQLAlterTable.swift @@ -23,33 +23,44 @@ public struct PostgreSQLAlterTable: SQLAlterTable { /// DROP [ COLUMN ] [ IF EXISTS ] column_name [ RESTRICT | CASCADE ] /// DROP CONSTRAINT [ IF EXISTS ] constraint_name [ RESTRICT | CASCADE ] public struct DropAction: SQLSerializable { + /// Algorithm to use while dropping. public enum Method { + /// `RESTRICT` case restrict + /// `CASCADE` case cascade } + /// Type of entity to drop. public enum Kind { + /// Table column. case column + /// Table constraint. case constraint } + /// See `Kind`. public var kind: Kind + /// If `true`, no error will be thrown if the entity does not exist. public var ifExists: Bool - public var column: PostgreSQLIdentifier + /// Name of the constraint or column to drop. + public var identifier: PostgreSQLIdentifier + /// Algorithm to use. public var method: Method? + /// Creates a new `DropAction`. public init( _ kind: Kind, ifExists: Bool = false, - _ column: PostgreSQLIdentifier, + _ identifier: PostgreSQLIdentifier, _ method: Method? = nil ) { self.kind = kind self.ifExists = ifExists - self.column = column + self.identifier = identifier self.method = method } @@ -64,7 +75,7 @@ public struct PostgreSQLAlterTable: SQLAlterTable { if ifExists { sql.append("IF EXISTS") } - sql.append(column.serialize(&binds)) + sql.append(identifier.serialize(&binds)) if let method = method { switch method { case .cascade: sql.append("CASCADE") @@ -75,6 +86,7 @@ public struct PostgreSQLAlterTable: SQLAlterTable { } } + /// Things to drop. public var dropActions: [DropAction] /// Creates a new `AlterTable`. diff --git a/Sources/PostgreSQL/SQL/PostgreSQLBinaryOperator.swift b/Sources/PostgreSQL/SQL/PostgreSQLBinaryOperator.swift index 4f13961e..dbd27eeb 100644 --- a/Sources/PostgreSQL/SQL/PostgreSQLBinaryOperator.swift +++ b/Sources/PostgreSQL/SQL/PostgreSQLBinaryOperator.swift @@ -1,3 +1,4 @@ +/// PostgreSQL specific `SQLBinaryOperator`. public enum PostgreSQLBinaryOperator: SQLBinaryOperator, Equatable { /// See `SQLBinaryOperator`. public static var equal: PostgreSQLBinaryOperator { return ._equal } diff --git a/Sources/PostgreSQL/SQL/PostgreSQLBind.swift b/Sources/PostgreSQL/SQL/PostgreSQLBind.swift index 1801c2a2..3722915a 100644 --- a/Sources/PostgreSQL/SQL/PostgreSQLBind.swift +++ b/Sources/PostgreSQL/SQL/PostgreSQLBind.swift @@ -1,7 +1,10 @@ +/// Representable as a `PostgreSQLExpression`. public protocol PostgreSQLExpressionRepresentable { + /// Self converted to a `PostgreSQLExpression`. var postgreSQLExpression: PostgreSQLExpression { get } } +/// PostgreSQL specific `SQLBind`. public struct PostgreSQLBind: SQLBind { /// See `SQLBind`. public static func encodable(_ value: E) -> PostgreSQLBind @@ -14,11 +17,16 @@ public struct PostgreSQLBind: SQLBind { } } + /// Specific type of bind. public enum Value { + /// A `PostgreSQLExpression`. case expression(PostgreSQLExpression) + + /// A bound `Encodable` type. case encodable(Encodable) } + /// Bind value. public var value: Value /// See `SQLSerializable`. diff --git a/Sources/PostgreSQL/SQL/PostgreSQLBoolLiteral.swift b/Sources/PostgreSQL/SQL/PostgreSQLBoolLiteral.swift index 2ac7bb6c..266518cb 100644 --- a/Sources/PostgreSQL/SQL/PostgreSQLBoolLiteral.swift +++ b/Sources/PostgreSQL/SQL/PostgreSQLBoolLiteral.swift @@ -1,3 +1,4 @@ +/// PostgreSQL specific `SQLBoolLiteral`. public enum PostgreSQLBoolLiteral: SQLBoolLiteral { /// See `SQLBoolLiteral`. public static var `true`: PostgreSQLBoolLiteral { @@ -9,7 +10,10 @@ public enum PostgreSQLBoolLiteral: SQLBoolLiteral { return ._false } + /// See `SQLBoolLiteral`. case _true + + /// See `SQLBoolLiteral`. case _false /// See `SQLSerializable`. diff --git a/Sources/PostgreSQL/SQL/PostgreSQLCollation.swift b/Sources/PostgreSQL/SQL/PostgreSQLCollation.swift index 975c5b53..2a298274 100644 --- a/Sources/PostgreSQL/SQL/PostgreSQLCollation.swift +++ b/Sources/PostgreSQL/SQL/PostgreSQLCollation.swift @@ -1,4 +1,6 @@ +/// PostgreSQL specific `SQLCollation`. public struct PostgreSQLCollation: SQLCollation { + /// See `SQLSerializable`. public func serialize(_ binds: inout [Encodable]) -> String { return "COLLATE" } diff --git a/Sources/PostgreSQL/SQL/PostgreSQLDataTypeStaticRepresentable.swift b/Sources/PostgreSQL/SQL/PostgreSQLDataTypeStaticRepresentable.swift index fe20d747..5e762c56 100644 --- a/Sources/PostgreSQL/SQL/PostgreSQLDataTypeStaticRepresentable.swift +++ b/Sources/PostgreSQL/SQL/PostgreSQLDataTypeStaticRepresentable.swift @@ -1,3 +1,4 @@ +/// Statically representable as a `PostgreSQLDataType`. public protocol PostgreSQLDataTypeStaticRepresentable { /// Appropriate PostgreSQL column type for storing this type. static var postgreSQLDataType: PostgreSQLDataType { get } diff --git a/Sources/PostgreSQL/SQL/PostgreSQLDefault.swift b/Sources/PostgreSQL/SQL/PostgreSQLDefault.swift index a0a711ce..3828024c 100644 --- a/Sources/PostgreSQL/SQL/PostgreSQLDefault.swift +++ b/Sources/PostgreSQL/SQL/PostgreSQLDefault.swift @@ -1,3 +1,4 @@ +/// PostgreSQL specific `SQLDefaultLiteral`. public struct PostgreSQLDefaultLiteral: SQLDefaultLiteral { /// See `SQLDefaultLiteral`. public static var `default`: PostgreSQLDefaultLiteral { diff --git a/Sources/PostgreSQL/SQL/PostgreSQLDropIndex.swift b/Sources/PostgreSQL/SQL/PostgreSQLDropIndex.swift index 40697be2..5209880d 100644 --- a/Sources/PostgreSQL/SQL/PostgreSQLDropIndex.swift +++ b/Sources/PostgreSQL/SQL/PostgreSQLDropIndex.swift @@ -1,4 +1,6 @@ +/// PostgreSQL specific `SQLDropIndex`. public struct PostgreSQLDropIndex: SQLDropIndex { + /// See `SQLDropIndex`. public var identifier: PostgreSQLIdentifier /// See `SQLSerializable`. @@ -10,6 +12,7 @@ public struct PostgreSQLDropIndex: SQLDropIndex { } } +/// Builds `PostgreSQLDropIndex` queries. public final class PostgreSQLDropIndexBuilder: SQLQueryBuilder where Connection: SQLConnection, Connection.Query == PostgreSQLQuery { @@ -33,6 +36,7 @@ public final class PostgreSQLDropIndexBuilder: SQLQueryBuilder extension SQLConnection where Query == PostgreSQLQuery { + /// Creates a `PostgreSQLDropIndexBuilder` for this connection. public func drop(index identifier: PostgreSQLIdentifier) -> PostgreSQLDropIndexBuilder { return .init(PostgreSQLDropIndex(identifier: identifier), on: self) } diff --git a/Sources/PostgreSQL/SQL/PostgreSQLFunction.swift b/Sources/PostgreSQL/SQL/PostgreSQLFunction.swift index 9d33cadf..a41ea93c 100644 --- a/Sources/PostgreSQL/SQL/PostgreSQLFunction.swift +++ b/Sources/PostgreSQL/SQL/PostgreSQLFunction.swift @@ -1,13 +1,20 @@ +/// PostgreSQL specific `SQLFunction`. public struct PostgreSQLFunction: SQLFunction { + /// See `SQLFunction`. public typealias Argument = GenericSQLFunctionArgument + /// See `SQLFunction`. public static func function(_ name: String, _ args: [Argument]) -> PostgreSQLFunction { return .init(name: name, arguments: args) } + /// See `SQLFunction`. public let name: String + + /// See `SQLFunction`. public let arguments: [Argument] + /// See `SQLSerializable`. public func serialize(_ binds: inout [Encodable]) -> String { return name + "(" + arguments.map { $0.serialize(&binds) }.joined(separator: ", ") + ")" } diff --git a/Sources/PostgreSQL/SQL/PostgreSQLInsert.swift b/Sources/PostgreSQL/SQL/PostgreSQLInsert.swift index 9ffc4a1d..fd3ee24e 100644 --- a/Sources/PostgreSQL/SQL/PostgreSQLInsert.swift +++ b/Sources/PostgreSQL/SQL/PostgreSQLInsert.swift @@ -1,3 +1,4 @@ +/// PostgreSQL specific `SQLInsert`. public struct PostgreSQLInsert: SQLInsert { /// See `SQLInsert`. public static func insert(_ table: PostgreSQLTableIdentifier) -> PostgreSQLInsert { @@ -50,6 +51,7 @@ public struct PostgreSQLInsert: SQLInsert { } extension SQLInsertBuilder where Connection.Query.Insert == PostgreSQLInsert { + /// Adds a `RETURNING` expression to the insert query. public func returning(_ exprs: PostgreSQLSelectExpression...) -> Self { insert.returning += exprs return self diff --git a/Sources/PostgreSQL/SQL/PostgreSQLPrimaryKey.swift b/Sources/PostgreSQL/SQL/PostgreSQLPrimaryKey.swift index 08dbb62c..e4916756 100644 --- a/Sources/PostgreSQL/SQL/PostgreSQLPrimaryKey.swift +++ b/Sources/PostgreSQL/SQL/PostgreSQLPrimaryKey.swift @@ -1,14 +1,22 @@ +/// PosgreSQL specific `PostgreSQLPrimaryKey.swift`. public enum PostgreSQLPrimaryKeyDefault: SQLPrimaryKeyDefault { + /// `GENERATED BY` methods. public enum Generated { + /// `BY DEFAULT` case byDefault + /// `ALWAYS` case always } + + /// `GENERATED BY` case generated(Generated) + /// Default primary key value. public static var `default`: PostgreSQLPrimaryKeyDefault { return .generated(.byDefault) } + /// See `SQLSerializable`. public func serialize(_ binds: inout [Encodable]) -> String { switch self { case .generated(let generated): diff --git a/Sources/PostgreSQL/SQL/PostgreSQLQuery+DataType.swift b/Sources/PostgreSQL/SQL/PostgreSQLQuery+DataType.swift index e4e6c437..94b507c3 100644 --- a/Sources/PostgreSQL/SQL/PostgreSQLQuery+DataType.swift +++ b/Sources/PostgreSQL/SQL/PostgreSQLQuery+DataType.swift @@ -1,3 +1,4 @@ +/// PostgreSQL specific `SQLDataType`. public struct PostgreSQLDataType: SQLDataType, Equatable { /// See `Equatable`. public static func == (lhs: PostgreSQLDataType, rhs: PostgreSQLDataType) -> Bool { @@ -334,6 +335,7 @@ public struct PostgreSQLDataType: SQLDataType, Equatable { return .init(.custom(name)) } + /// Creates an array type from a `PostgreSQLDataType`. public static func array(_ dataType: PostgreSQLDataType) -> PostgreSQLDataType { return .init(dataType.primitive, isArray: true) } diff --git a/Sources/PostgreSQL/SQL/PostgreSQLQuery.swift b/Sources/PostgreSQL/SQL/PostgreSQLQuery.swift index 22b04048..3306e407 100644 --- a/Sources/PostgreSQL/SQL/PostgreSQLQuery.swift +++ b/Sources/PostgreSQL/SQL/PostgreSQLQuery.swift @@ -1,3 +1,4 @@ +/// PostgreSQL specific `SQLQuery`. public enum PostgreSQLQuery: SQLQuery { /// See `SQLQuery`. public typealias AlterTable = PostgreSQLAlterTable @@ -26,9 +27,6 @@ public enum PostgreSQLQuery: SQLQuery { /// See `SQLQuery`. public typealias Update = PostgreSQLUpdate - /// See `SQLQuery`. - public typealias RowDecoder = PostgreSQLRowDecoder - /// See `SQLQuery`. public static func alterTable(_ alterTable: AlterTable) -> PostgreSQLQuery { return ._alterTable(alterTable) @@ -129,6 +127,7 @@ public enum PostgreSQLQuery: SQLQuery { } extension PostgreSQLQuery: ExpressibleByStringLiteral { + /// See `ExpressibleByStringLiteral`. public init(stringLiteral value: String) { self = ._raw(value, []) } diff --git a/Sources/PostgreSQL/SQL/PostgreSQLTable.swift b/Sources/PostgreSQL/SQL/PostgreSQLTable.swift index 95942f57..9d179b9c 100644 --- a/Sources/PostgreSQL/SQL/PostgreSQLTable.swift +++ b/Sources/PostgreSQL/SQL/PostgreSQLTable.swift @@ -1 +1,2 @@ +/// PostgreSQL specific `SQLTable`. public protocol PostgreSQLTable: SQLTable { } diff --git a/Sources/PostgreSQL/SQL/PostgreSQLUpsert.swift b/Sources/PostgreSQL/SQL/PostgreSQLUpsert.swift index 4cbc3491..34705d7b 100644 --- a/Sources/PostgreSQL/SQL/PostgreSQLUpsert.swift +++ b/Sources/PostgreSQL/SQL/PostgreSQLUpsert.swift @@ -1,3 +1,4 @@ +/// `ON CONFLICT ... DO UPDATE SET` clause. public struct PostgreSQLUpsert: SQLSerializable { /// See `SQLUpsert`. public typealias Identifier = PostgreSQLIdentifier @@ -28,6 +29,7 @@ public struct PostgreSQLUpsert: SQLSerializable { } extension SQLInsertBuilder where Connection.Query.Insert == PostgreSQLInsert { + /// Adds an `ON CONFLICT ... DO UPDATE SET` clause to the insert. public func onConflict(_ key: KeyPath, set value: E) -> Self where T: PostgreSQLTable, E: Encodable { diff --git a/Sources/PostgreSQL/Utilities/Utilities.swift b/Sources/PostgreSQL/Utilities/Utilities.swift index ba459fe5..8c4e3e82 100644 --- a/Sources/PostgreSQL/Utilities/Utilities.swift +++ b/Sources/PostgreSQL/Utilities/Utilities.swift @@ -67,14 +67,14 @@ extension ByteBuffer { } } -public enum Regproc: PostgreSQLDataConvertible, Codable { - public func convertToPostgreSQLData() throws -> PostgreSQLData { +enum Regproc: PostgreSQLDataConvertible, Codable { + func convertToPostgreSQLData() throws -> PostgreSQLData { switch self { case .function(let string): return try string.convertToPostgreSQLData() case .functionID(let id): return try id.convertToPostgreSQLData() } } - public static func convertFromPostgreSQLData(_ data: PostgreSQLData) throws -> Regproc { + static func convertFromPostgreSQLData(_ data: PostgreSQLData) throws -> Regproc { if data.binary != nil { return try .functionID(.convertFromPostgreSQLData(data)) } else { @@ -82,12 +82,12 @@ public enum Regproc: PostgreSQLDataConvertible, Codable { } } - public init(from decoder: Decoder) throws { + init(from decoder: Decoder) throws { let single = try decoder.singleValueContainer() self = try .function(single.decode(String.self)) } - public func encode(to encoder: Encoder) throws { + func encode(to encoder: Encoder) throws { var single = encoder.singleValueContainer() switch self { case .function(let string): try single.encode(string) diff --git a/Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift b/Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift index 888bb8b5..ab5db0aa 100644 --- a/Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift +++ b/Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift @@ -1,4 +1,4 @@ -import PostgreSQL +@testable import PostgreSQL import SQLBenchmark import XCTest @@ -579,9 +579,7 @@ extension PostgreSQLConnection { #endif let group = MultiThreadedEventLoopGroup(numberOfThreads: 1) - let client = try PostgreSQLConnection.connect(hostname: hostname, port: 5432, transport: transport, on: group) { error in - XCTFail("\(error)") - }.wait() + let client = try PostgreSQLConnection.connect(hostname: hostname, port: 5432, transport: transport, on: group).wait() _ = try client.authenticate(username: "vapor_username", database: "vapor_database", password: "vapor_password").wait() client.logger = DatabaseLogger(database: .psql, handler: PrintLogHandler()) return client