Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add release doc blocks #94

Merged
merged 1 commit into from
Jul 25, 2018
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
6 changes: 3 additions & 3 deletions Sources/PostgreSQL/Codable/PostgreSQLDataDecoder.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
public struct PostgreSQLDataDecoder {
struct PostgreSQLDataDecoder {
/// Creates a new `PostgreSQLDataDecoder`.
public init() {}
init() {}

public func decode<D>(_ type: D.Type, from data: PostgreSQLData) throws -> D where D: Decodable {
func decode<D>(_ 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
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/PostgreSQL/Codable/PostgreSQLRowDecoder.swift
Original file line number Diff line number Diff line change
@@ -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]`.
///
Expand Down
6 changes: 3 additions & 3 deletions Sources/PostgreSQL/Codable/PostgreSQLValueEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
///
Expand All @@ -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()
}
Expand Down
1 change: 1 addition & 0 deletions Sources/PostgreSQL/Column/PostgreSQLColumn.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 42 additions & 1 deletion Sources/PostgreSQL/Column/PostgreSQLDataTypeCode.swift
Original file line number Diff line number Diff line change
@@ -1,48 +1,89 @@
/// The data type's raw object ID.
/// Use `select * from pg_type where oid = <idhere>;` 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.==`
Expand Down
14 changes: 8 additions & 6 deletions Sources/PostgreSQL/Connection/PostgreSQLConnection+Connect.swift
Original file line number Diff line number Diff line change
@@ -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<PostgreSQLConnection> {
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<PostgreSQLConnection> {
let handler = QueueHandler<PostgreSQLMessage, PostgreSQLMessage>(on: worker, onError: onError)
let handler = QueueHandler<PostgreSQLMessage, PostgreSQLMessage>(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)
Expand Down
12 changes: 12 additions & 0 deletions Sources/PostgreSQL/Connection/PostgreSQLConnection+Query.swift
Original file line number Diff line number Diff line change
@@ -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<Void> {
return self.query(query, resultFormat: .binary, onRow)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
extension PostgreSQLConnection {
/// Performs a non-parameterized (text protocol) query to PostgreSQL.
public func simpleQuery(_ query: String) -> Future<Void> {
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<Void> {
return operation { self._simpleQuery(query, onRow) }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions Sources/PostgreSQL/Database/PostgreSQLDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ public final class PostgreSQLDatabase: Database, LogSupporting {
public func newConnection(on worker: Worker) -> Future<PostgreSQLConnection> {
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,
Expand Down
5 changes: 4 additions & 1 deletion Sources/PostgreSQL/Database/PostgreSQLDatabaseConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
20 changes: 16 additions & 4 deletions Sources/PostgreSQL/SQL/PostgreSQLAlterTable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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")
Expand All @@ -75,6 +86,7 @@ public struct PostgreSQLAlterTable: SQLAlterTable {
}
}

/// Things to drop.
public var dropActions: [DropAction]

/// Creates a new `AlterTable`.
Expand Down
1 change: 1 addition & 0 deletions Sources/PostgreSQL/SQL/PostgreSQLBinaryOperator.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// PostgreSQL specific `SQLBinaryOperator`.
public enum PostgreSQLBinaryOperator: SQLBinaryOperator, Equatable {
/// See `SQLBinaryOperator`.
public static var equal: PostgreSQLBinaryOperator { return ._equal }
Expand Down
8 changes: 8 additions & 0 deletions Sources/PostgreSQL/SQL/PostgreSQLBind.swift
Original file line number Diff line number Diff line change
@@ -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<E>(_ value: E) -> PostgreSQLBind
Expand All @@ -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`.
Expand Down
4 changes: 4 additions & 0 deletions Sources/PostgreSQL/SQL/PostgreSQLBoolLiteral.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// PostgreSQL specific `SQLBoolLiteral`.
public enum PostgreSQLBoolLiteral: SQLBoolLiteral {
/// See `SQLBoolLiteral`.
public static var `true`: PostgreSQLBoolLiteral {
Expand All @@ -9,7 +10,10 @@ public enum PostgreSQLBoolLiteral: SQLBoolLiteral {
return ._false
}

/// See `SQLBoolLiteral`.
case _true

/// See `SQLBoolLiteral`.
case _false

/// See `SQLSerializable`.
Expand Down
2 changes: 2 additions & 0 deletions Sources/PostgreSQL/SQL/PostgreSQLCollation.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/// PostgreSQL specific `SQLCollation`.
public struct PostgreSQLCollation: SQLCollation {
/// See `SQLSerializable`.
public func serialize(_ binds: inout [Encodable]) -> String {
return "COLLATE"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 }
Expand Down
Loading