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

Swift4 #44

Open
wants to merge 35 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
d46811e
fix isTransacting flag
Dec 5, 2018
89b2c24
fix deprecated func (deinitialize, deallocate) in swift4
Dec 5, 2018
e1cbb1e
Revert "fix deprecated func (deinitialize, deallocate) in swift4"
Dec 12, 2018
d9fbf5b
update mysql-apt-config version
Dec 12, 2018
fbd7e41
install [email protected] in macos
Dec 12, 2018
738dc21
change osx_image to xcode9.2
Dec 12, 2018
eeb4b07
modify .travis.yml
Dec 17, 2018
210c7d5
specify [email protected] in macos
Dec 17, 2018
6a5e631
fix install-mysql
Dec 17, 2018
ae57de7
modify starting [email protected]
Dec 17, 2018
2a72438
modify selection of mysq-5.7
Dec 17, 2018
a6198ef
add "export PATH" in travis.yml
Dec 17, 2018
40b018c
modify install-mysql.sh
Dec 17, 2018
95aa9f1
change trusty to xenial. add "brew link" command.
Dec 18, 2018
c86252f
modify travis.yml again
Dec 18, 2018
58a1e3a
modify "brew link" argument
Jan 24, 2019
4f00515
add "--force" option
Jan 24, 2019
bcb9d55
beautify code
Jun 4, 2019
abb59bc
set false to isClose when connection close
Jun 4, 2019
53fa5a9
change dependency package (Prorsum)
Jun 4, 2019
219beb0
put PacketStream.readHeader together in readPacket
Jun 4, 2019
57e9fbe
move func
Jun 4, 2019
2148209
implement COM_STMT_CLOSE
Jun 4, 2019
4f7ad90
modify deinitialize and deallocate
Jun 4, 2019
59e84a2
change Package.swift
Jun 4, 2019
fa2f06a
revert Package.swift (Prorsum)
Jun 5, 2019
62288ab
modify tcp read stream count in PacketStream.readStream
Jun 5, 2019
4e12916
change time format (from - to :)
Jun 6, 2019
f1cd028
update swift version to 4.1.3
Jun 6, 2019
ed28b6d
update xcode version to 9.4
Jun 6, 2019
56eb1f1
change mutex timing in ConnectionPool.getConnection
Jul 12, 2019
4cd6768
case alreadyColosed error occurred, try to reopen
Jul 12, 2019
f6a5fa9
delete unnecessary code(Logger)
Jul 12, 2019
f22348e
change to not use static DateFormatter, prettify Date extension
Jul 19, 2019
0336da1
correspond to swift5.0
Sep 10, 2019
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
16 changes: 10 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@ os:
- osx
language: generic
sudo: required
dist: trusty
osx_image: xcode9
dist: xenial
osx_image: xcode9.4
services:
- mysql

before_install:
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sh Scripts/install-mysql-5.7.sh; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew update; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew install mysql; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then mysql.server start; fi
- mysql -uroot -e 'CREATE DATABASE swift_knex_test;'
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew install [email protected]; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew services start [email protected]; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew link [email protected] --force; fi
- mysql -uroot -e 'CREATE DATABASE IF NOT EXISTS swift_knex_test;'

install:
- source Scripts/install-swift.sh

script:
- mysql --version
- swift --version
- swift build
- swift build -c release
- swift test
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:4.0
// swift-tools-version:5.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand All @@ -10,7 +10,7 @@ let package = Package(
.executable(name: "SwiftKnexMigration", targets: ["SwiftKnexMigration"]),
],
dependencies: [
.package(url: "https://github.com/noppoMan/Prorsum.git", from: "0.3.0")
.package(url: "https://github.com/tuken/Prorsum.git", from: "0.5.0"),
],
targets: [
.target(name: "Mysql", dependencies: ["Prorsum"]),
Expand Down
21 changes: 21 additions & 0 deletions [email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// swift-tools-version:4.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "SwiftKnex",
products: [
.library(name: "SwiftKnex", targets: ["SwiftKnex"]),
.executable(name: "SwiftKnexMigration", targets: ["SwiftKnexMigration"]),
],
dependencies: [
.package(url: "https://github.com/noppoMan/Prorsum.git", from: "0.3.0"),
],
targets: [
.target(name: "Mysql", dependencies: ["Prorsum"]),
.target(name: "SwiftKnex", dependencies: ["Mysql"]),
.target(name: "SwiftKnexMigration", dependencies: ["SwiftKnex", "Mysql"]),
.testTarget(name: "SwiftKnexTests", dependencies: ["SwiftKnex", "Mysql"]),
]
)
6 changes: 2 additions & 4 deletions Scripts/install-mysql-5.7.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#!/usr/bin/env bash

sudo service mysql stop || echo "mysql not stopped"
sudo stop mysql-5.6 || echo "mysql-5.6 not stopped"
wget http://dev.mysql.com/get/mysql-apt-config_0.8.11-1_all.deb
echo mysql-apt-config mysql-apt-config/select-server select mysql-5.7 | sudo debconf-set-selections
wget http://dev.mysql.com/get/mysql-apt-config_0.8.9-1_all.deb
sudo dpkg --install mysql-apt-config_0.8.9-1_all.deb
sudo dpkg -i mysql-apt-config_0.8.11-1_all.deb
sudo apt-get update -q
sudo apt-get install -q -y -o Dpkg::Options::=--force-confnew mysql-server
sudo mysql_upgrade
Expand Down
2 changes: 1 addition & 1 deletion Scripts/install-swift.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

VERSION="4.0.3"
VERSION="4.1.3"

# Determine OS
UNAME=`uname`;
Expand Down
30 changes: 21 additions & 9 deletions Sources/Mysql/Connection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@
import Foundation

public enum ConnectionError: Error {

case failedToParseHandshakeOf(String)

case wrongHandshake
}

// syncronous connection
public final class Connection: ConnectionType {

public let url: URL

public let user: String

public let password: String?

public let database: String?

private var _isClosed = true
Expand Down Expand Up @@ -50,11 +56,7 @@ public final class Connection: ConnectionType {
let (handshakeBytes, packnr) = try self.stream.readPacket()
let hp = try HandshakePacket(bytes: handshakeBytes)

let authPacket = hp.buildAuthPacket(
user: user,
password: password,
database: database
)
let authPacket = hp.buildAuthPacket(user: user, password: password, database: database)

try stream.writePacket(authPacket, packnr: packnr)
let (bytes, _) = try stream.readPacket()
Expand All @@ -65,6 +67,13 @@ public final class Connection: ConnectionType {

_isClosed = false
}

func reopen() throws {
stream.close()
isUsed = false
_isClosed = true
try self.open()
}

func write(_ cmd: Commands, query: String) throws {
try stream.writePacket([cmd.rawValue] + query.utf8, packnr: -1)
Expand All @@ -74,13 +83,13 @@ public final class Connection: ConnectionType {
try stream.writePacket([cmd.rawValue], packnr: -1)
}

func reserve(){
func reserve() {
cond.mutex.lock()
isUsed = true
cond.mutex.unlock()
}

func release(){
func release() {
cond.mutex.lock()
isUsed = false
cond.mutex.unlock()
Expand All @@ -90,11 +99,13 @@ public final class Connection: ConnectionType {
let (bytes, _) = try stream.readPacket()
if let okPacket = try OKPacket(bytes: bytes) {
return (0, okPacket)
} else {
}
else {
let (_num, n) = lenEncInt(bytes)
if let num = _num, (n - bytes.count) == 0 {
return (Int(num), nil)
} else {
}
else {
return (0, nil)
}
}
Expand All @@ -112,6 +123,7 @@ public final class Connection: ConnectionType {
public func close() throws {
try write(.quit)
stream.close()
isUsed = false
_isClosed = true
}
}
80 changes: 74 additions & 6 deletions Sources/Mysql/ConnectionPool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,55 @@ public final class ConnectionPool: ConnectionType {
self.connections = try (0..<maxPoolSize).map { _ in
return try Connection(url: url, user: user, password: password, database: database)
}

#if os(Linux)
let _ = Timer.scheduledTimer(withTimeInterval: TimeInterval(3600), repeats: true) { _ in
print("timer! \"SELECT 1\"")
do {
for con in self.connections {
if con.isUsed {
continue
}

con.reserve()
let _ = try con.query("SELECT 1")
con.release()
}
}
catch {
print("failed to \"SELECT 1\"")
}
}
#endif
}

public func query(_ sql: String, bindParams params: [Any]) throws -> QueryResult {
let con = try getConnection()
let result = try con.query(sql, bindParams: params)
var result: QueryResult
do {
result = try con.query(sql, bindParams: params)
}
catch {
if let e = error as? SocketError {
switch e {
case .alreadyClosed:
try con.reopen()
result = try con.query(sql, bindParams: params)
default: throw error
}
}
else if let e = error as? StreamError {
switch e {
case .alreadyClosed:
try con.reopen()
result = try con.query(sql, bindParams: params)
default: throw error
}
}
else {
throw error
}
}

if !con.isTransacting {
con.release()
Expand All @@ -72,7 +116,31 @@ public final class ConnectionPool: ConnectionType {

public func query(_ sql: String) throws -> QueryResult {
let con = try getConnection()
let result = try con.query(sql)
var result: QueryResult
do {
result = try con.query(sql)
}
catch {
if let e = error as? SocketError {
switch e {
case .alreadyClosed:
try con.reopen()
result = try con.query(sql)
default: throw error
}
}
else if let e = error as? StreamError {
switch e {
case .alreadyClosed:
try con.reopen()
result = try con.query(sql)
default: throw error
}
}
else {
throw error
}
}

if !con.isTransacting {
con.release()
Expand All @@ -83,10 +151,12 @@ public final class ConnectionPool: ConnectionType {

func getConnection(_ retryCount: Int = 0) throws -> Connection {
// TODO should implement timeout
if Double(retryCount) > (0.1*10)*5 {
if Double(retryCount) > (0.1 * 10) * 5 {
throw ConnectionPoolError.failedToGetConnectionFromPool
}

cond.mutex.lock()
defer { cond.mutex.unlock() }
for con in connections {
if con.isUsed {
continue
Expand All @@ -96,11 +166,9 @@ public final class ConnectionPool: ConnectionType {
return con
}

cond.mutex.lock()
_ = cond.wait(timeout: 0.1)
cond.mutex.unlock()

return try getConnection(retryCount+1)
return try getConnection(retryCount + 1)
}

public func close () throws {
Expand Down
4 changes: 2 additions & 2 deletions Sources/Mysql/Extensions/Bytes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ func arrayOfBytes<T: BinaryInteger>(_ value: T, length totalBytes: Int = MemoryL
bytes[totalBytes - 1 - j] = (bytesPointer + j).pointee
}

valuePointer.deinitialize()
valuePointer.deallocate(capacity: 1)
valuePointer.deinitialize(count: 1)
valuePointer.deallocate()

return bytes
}
Expand Down
Loading