Skip to content

Commit

Permalink
Merge pull request #1 from vapor/beta
Browse files Browse the repository at this point in the history
psql beta
  • Loading branch information
tanner0101 authored Feb 24, 2018
2 parents faadd6d + bdb1eaf commit 25a0435
Show file tree
Hide file tree
Showing 56 changed files with 3,168 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
Package.resolved

21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2018 Qutheory, LLC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
32 changes: 32 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// swift-tools-version:4.0
import PackageDescription

let package = Package(
name: "PostgreSQL",
products: [
.library(name: "PostgreSQL", targets: ["PostgreSQL"]),
],
dependencies: [
// ⏱ Promises and reactive-streams in Swift built for high-performance and scalability.
.package(url: "https://github.com/vapor/async.git", from: "1.0.0-rc"),

// 🌎 Utility package containing tools for byte manipulation, Codable, OS APIs, and debugging.
.package(url: "https://github.com/vapor/core.git", from: "3.0.0-rc"),

// 🔑 Hashing (BCrypt, SHA, HMAC, etc), encryption, and randomness.
.package(url: "https://github.com/vapor/crypto.git", from: "3.0.0-rc"),

// 🗄 Core services for creating database integrations.
.package(url: "https://github.com/vapor/database-kit.git", from: "1.0.0-rc"),

// 📦 Dependency injection / inversion of control framework.
.package(url: "https://github.com/vapor/service.git", from: "1.0.0-rc"),

// 🔌 Non-blocking TCP socket layer, with event-driven server and client.
.package(url: "https://github.com/vapor/sockets.git", from: "3.0.0-rc"),
],
targets: [
.target(name: "PostgreSQL", dependencies: ["Async", "Bits", "Crypto", "DatabaseKit", "Service", "TCP"]),
.testTarget(name: "PostgreSQLTests", dependencies: ["PostgreSQL"]),
]
)
25 changes: 22 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
(See [vapor-community/postgresql](https://github.com/vapor-community/postgresql/) for libpq-based version)
<p align="center">
<img src="https://user-images.githubusercontent.com/1342803/36623751-7f1f2884-18d5-11e8-9fd8-5a94c23ec7ce.png" height="64" alt="PostgreSQL">
<br>
<br>
<a href="http://docs.vapor.codes/3.0/">
<img src="http://img.shields.io/badge/read_the-docs-2196f3.svg" alt="Documentation">
</a>
<a href="http://vapor.team">
<img src="http://vapor.team/badge.svg" alt="Slack Team">
</a>
<a href="LICENSE">
<img src="http://img.shields.io/badge/license-MIT-brightgreen.svg" alt="MIT License">
</a>
<a href="https://circleci.com/gh/vapor/postgresql">
<img src="https://circleci.com/gh/vapor/postgresql.svg?style=shield" alt="Continuous Integration">
</a>
<a href="https://swift.org">
<img src="http://img.shields.io/badge/swift-4.1-brightgreen.svg" alt="Swift 4.1">
</a>
</p>

# Pure-Swift PostgreSQL Library
<hr>

Work in progress
See [vapor-community/postgresql](https://github.com/vapor-community/postgresql/) for `libpq` based version.
77 changes: 77 additions & 0 deletions Sources/PostgreSQL/Connection/PostgreSQLConnection+Query.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import Async

extension PostgreSQLConnection {
/// Sends a parameterized PostgreSQL query command, collecting the parsed results.
public func query(
_ string: String,
_ parameters: [PostgreSQLDataCustomConvertible] = []
) throws -> Future<[[String: PostgreSQLData]]> {
var rows: [[String: PostgreSQLData]] = []
return try query(string, parameters) { row in
rows.append(row)
}.map(to: [[String: PostgreSQLData]].self) {
return rows
}
}

/// Sends a parameterized PostgreSQL query command, returning the parsed results to
/// the supplied closure.
public func query(
_ string: String,
_ parameters: [PostgreSQLDataCustomConvertible] = [],
resultFormat: PostgreSQLResultFormat = .binary(),
onRow: @escaping ([String: PostgreSQLData]) -> ()
) throws -> Future<Void> {
let parameters = try parameters.map { try $0.convertToPostgreSQLData() }
logger?.log(query: string, parameters: parameters)
let parse = PostgreSQLParseRequest(
statementName: "",
query: string,
parameterTypes: parameters.map { $0.type }
)
let describe = PostgreSQLDescribeRequest(type: .statement, name: "")
var currentRow: PostgreSQLRowDescription?

return send([
.parse(parse), .describe(describe), .sync
]) { message in
switch message {
case .parseComplete: break
case .rowDescription(let row): currentRow = row
case .parameterDescription: break
case .noData: break
default: throw PostgreSQLError(identifier: "query", reason: "Unexpected message during PostgreSQLParseRequest: \(message)", source: .capture())
}
}.flatMap(to: Void.self) {
let resultFormats = resultFormat.formatCodeFactory(currentRow?.fields.map { $0.dataType } ?? [])
// cache so we don't compute twice
let bind = PostgreSQLBindRequest(
portalName: "",
statementName: "",
parameterFormatCodes: parameters.map { $0.format },
parameters: parameters.map { .init(data: $0.data) },
resultFormatCodes: resultFormats
)
let execute = PostgreSQLExecuteRequest(
portalName: "",
maxRows: 0
)
return self.send([
.bind(bind), .execute(execute), .sync
]) { message in
switch message {
case .bindComplete: break
case .dataRow(let data):
guard let row = currentRow else {
throw PostgreSQLError(identifier: "query", reason: "Unexpected PostgreSQLDataRow without preceding PostgreSQLRowDescription.", source: .capture())
}
let parsed = try row.parse(data: data, formatCodes: resultFormats)
onRow(parsed)
case .close: break
case .noData: break
default: throw PostgreSQLError(identifier: "query", reason: "Unexpected message during PostgreSQLParseRequest: \(message)", source: .capture())
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Async

extension PostgreSQLConnection {
/// Sends a simple PostgreSQL query command, collecting the parsed results.
public func simpleQuery(_ string: String) -> Future<[[String: PostgreSQLData]]> {
var rows: [[String: PostgreSQLData]] = []
return simpleQuery(string) { row in
rows.append(row)
}.map(to: [[String: PostgreSQLData]].self) {
return rows
}
}

/// Sends a simple PostgreSQL query command, returning the parsed results to
/// the supplied closure.
public func simpleQuery(_ string: String, onRow: @escaping ([String: PostgreSQLData]) -> ()) -> Future<Void> {
logger?.log(query: string, parameters: [])
var currentRow: PostgreSQLRowDescription?
let query = PostgreSQLQuery(query: string)
return send([.query(query)]) { message in
switch message {
case .rowDescription(let row):
currentRow = row
case .dataRow(let data):
guard let row = currentRow else {
throw PostgreSQLError(identifier: "simpleQuery", reason: "Unexpected PostgreSQLDataRow without preceding PostgreSQLRowDescription.", source: .capture())
}
let parsed = try row.parse(data: data, formatCodes: row.fields.map { $0.formatCode })
onRow(parsed)
case .close: break // query over, waiting for `readyForQuery`
default: throw PostgreSQLError(identifier: "simpleQuery", reason: "Unexpected message during PostgreSQLQuery: \(message)", source: .capture())
}
}
}
}
18 changes: 18 additions & 0 deletions Sources/PostgreSQL/Connection/PostgreSQLConnection+TCP.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Async
import TCP

extension PostgreSQLConnection {
/// Connects to a Redis server using a TCP socket.
public static func connect(
hostname: String = "localhost",
port: UInt16 = 5432,
on worker: Worker,
onError: @escaping TCPSocketSink.ErrorHandler
) throws -> PostgreSQLConnection {
let socket = try TCPSocket(isNonBlocking: true)
let client = try TCPClient(socket: socket)
try client.connect(hostname: hostname, port: port)
let stream = socket.stream(on: worker, onError: onError)
return PostgreSQLConnection(stream: stream, on: worker)
}
}
130 changes: 130 additions & 0 deletions Sources/PostgreSQL/Connection/PostgreSQLConnection.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import Async
import Crypto

/// A PostgreSQL frontend client.
public final class PostgreSQLConnection {
/// Handles enqueued redis commands and responses.
private let queueStream: QueueStream<PostgreSQLMessage, PostgreSQLMessage>

/// If non-nil, will log queries.
public var logger: PostgreSQLLogger?

/// Creates a new Redis client on the provided data source and sink.
init<Stream>(stream: Stream, on worker: Worker) where Stream: ByteStream {
let queueStream = QueueStream<PostgreSQLMessage, PostgreSQLMessage>()

let serializerStream = PostgreSQLMessageSerializer().stream(on: worker)
let parserStream = PostgreSQLMessageParser().stream(on: worker)

stream.stream(to: parserStream)
.stream(to: queueStream)
.stream(to: serializerStream)
.output(to: stream)

self.queueStream = queueStream
}

/// Sends `PostgreSQLMessage` to the server.
func send(_ messages: [PostgreSQLMessage], onResponse: @escaping (PostgreSQLMessage) throws -> ()) -> Future<Void> {
var error: Error?
return queueStream.enqueue(messages) { message in
switch message {
case .readyForQuery:
if let e = error { throw e }
return true
case .error(let e): error = e
case .notice(let n): print(n)
default: try onResponse(message)
}
return false // request until ready for query
}
}

/// Sends `PostgreSQLMessage` to the server.
func send(_ message: [PostgreSQLMessage]) -> Future<[PostgreSQLMessage]> {
var responses: [PostgreSQLMessage] = []
return send(message) { response in
responses.append(response)
}.map(to: [PostgreSQLMessage].self) {
return responses
}
}

/// Authenticates the `PostgreSQLClient` using a username with no password.
public func authenticate(username: String, database: String? = nil, password: String? = nil) -> Future<Void> {
let startup = PostgreSQLStartupMessage.versionThree(parameters: [
"user": username,
"database": database ?? username
])
var authRequest: PostgreSQLAuthenticationRequest?
return queueStream.enqueue([.startupMessage(startup)]) { message in
switch message {
case .authenticationRequest(let a):
authRequest = a
return true
default: throw PostgreSQLError(identifier: "auth", reason: "Unsupported message encountered during auth: \(message).", source: .capture())
}
}.flatMap(to: Void.self) {
guard let auth = authRequest else {
throw PostgreSQLError(identifier: "authRequest", reason: "No authorization request / status sent.", source: .capture())
}

let input: [PostgreSQLMessage]
switch auth {
case .ok:
guard password == nil else {
throw PostgreSQLError(identifier: "trust", reason: "No password is required", source: .capture())
}
input = []
case .plaintext:
guard let password = password else {
throw PostgreSQLError(identifier: "password", reason: "Password is required", source: .capture())
}
let passwordMessage = PostgreSQLPasswordMessage(password: password)
input = [.password(passwordMessage)]
case .md5(let salt):
guard let password = password else {
throw PostgreSQLError(identifier: "password", reason: "Password is required", source: .capture())
}
guard let passwordData = password.data(using: .utf8) else {
throw PostgreSQLError(identifier: "passwordUTF8", reason: "Could not convert password to UTF-8 encoded Data.", source: .capture())
}

guard let usernameData = username.data(using: .utf8) else {
throw PostgreSQLError(identifier: "usernameUTF8", reason: "Could not convert username to UTF-8 encoded Data.", source: .capture())
}

let hasher = MD5()
// pwdhash = md5(password + username).hexdigest()
var passwordUsernameData = passwordData + usernameData
hasher.update(sequence: &passwordUsernameData)
hasher.finalize()
guard let pwdhash = hasher.hash.hexString.data(using: .utf8) else {
throw PostgreSQLError(identifier: "hashUTF8", reason: "Could not convert password hash to UTF-8 encoded Data.", source: .capture())
}
hasher.reset()
// hash = ′ md 5′ + md 5(pwdhash + salt ).hexdigest ()
var saltedData = pwdhash + salt
hasher.update(sequence: &saltedData)
hasher.finalize()
let passwordMessage = PostgreSQLPasswordMessage(password: "md5" + hasher.hash.hexString)
input = [.password(passwordMessage)]
}

return self.queueStream.enqueue(input) { message in
switch message {
case .error(let error): throw error
case .readyForQuery: return true
case .authenticationRequest: return false
case .parameterStatus, .backendKeyData: return false
default: throw PostgreSQLError(identifier: "authenticationMessage", reason: "Unexpected authentication message: \(message)", source: .capture())
}
}
}
}

/// Closes this client.
public func close() {
queueStream.close()
}
}
Loading

0 comments on commit 25a0435

Please sign in to comment.