Skip to content

Commit

Permalink
upgrading to Swift 5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
RockfordWei committed Mar 29, 2019
1 parent e19bd37 commit e98eff6
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -5,7 +5,7 @@
## Build generated
build/
DerivedData

.vscode/
## Various settings
*.pbxuser
!default.pbxuser
Expand Down
33 changes: 20 additions & 13 deletions Package.swift
@@ -1,3 +1,4 @@
// swift-tools-version:4.2
//
// Package.swift
// Perfect-MariaDB
Expand All @@ -19,22 +20,28 @@

import PackageDescription

#if os(OSX)
let package = Package(
name: "MariaDB",
targets: [],
dependencies: [
.Package(url: "https://github.com/PerfectlySoft/Perfect-mariadbclient.git", majorVersion: 2)
products: [
.library(
name: "MariaDB",
targets: ["MariaDB"]),
],
exclude: []
)
#else
let package = Package(
name: "MariaDB",
targets: [],
dependencies: [
.Package(url: "https://github.com/PerfectlySoft/Perfect-mariadbclient-Linux.git", majorVersion: 2)
],
exclude: []
targets: [
.systemLibrary(name: "mariadbclient",
pkgConfig: "mariadb",
providers: [
.apt(["libmariadb-client-lgpl-dev"]),
.brew(["mariadb-connector-c"])
]
),
.target(
name: "MariaDB",
dependencies: ["mariadbclient"]),
.testTarget(
name: "MariaDBTests",
dependencies: ["MariaDB"]),
]
)
#endif
57 changes: 27 additions & 30 deletions Sources/MariaDB/MariaDB.swift
Expand Up @@ -19,12 +19,10 @@
//

#if os(Linux)
import SwiftGlibc
#else
import Darwin
import Glibc
#endif
import mariadbclient

import Foundation
/// This class permits an UnsafeMutablePointer to be used as a IteratorProtocol
struct GenerateFromPointer<T> : IteratorProtocol {

Expand Down Expand Up @@ -227,7 +225,7 @@ public final class MySQL {
func cleanConvertedString(_ pair: (UnsafeMutablePointer<Int8>?, Int)) {
if let p0 = pair.0 , pair.1 > 0 {
p0.deinitialize(count: pair.1)
p0.deallocate(capacity: pair.1)
p0.deallocate()
}
}

Expand Down Expand Up @@ -742,29 +740,30 @@ public final class MySQLStmt {

switch bind.buffer_type.rawValue {
case MYSQL_TYPE_DOUBLE.rawValue:
bind.buffer.assumingMemoryBound(to: Double.self).deallocate(capacity: 1)
bind.buffer.assumingMemoryBound(to: Double.self).deallocate()

case MYSQL_TYPE_LONGLONG.rawValue:
if bind.is_unsigned == 1 {
bind.buffer.assumingMemoryBound(to: UInt64.self).deallocate(capacity: 1)
bind.buffer.assumingMemoryBound(to: UInt64.self).deallocate()
} else {
bind.buffer.assumingMemoryBound(to: Int64.self).deallocate(capacity: 1)
bind.buffer.assumingMemoryBound(to: Int64.self).deallocate()
}
case MYSQL_TYPE_VAR_STRING.rawValue,
MYSQL_TYPE_DATE.rawValue,
MYSQL_TYPE_DATETIME.rawValue:
bind.buffer.assumingMemoryBound(to: Int8.self).deallocate(capacity: Int(bind.buffer_length))
bind.buffer.assumingMemoryBound(to: Int8.self).deallocate()
case MYSQL_TYPE_LONG_BLOB.rawValue:
()
default:
assertionFailure("Unhandled MySQL type \(bind.buffer_type)")
}

if let len = bind.length {
len.deallocate(capacity: 1)
}
if let len = bind.length {
len.deallocate()
}
}
paramBinds.deinitialize(count: count)
paramBinds.deallocate(capacity: count)
paramBinds.deallocate()
self.paramBinds = nil
self.paramBindsOffset = 0
}
Expand Down Expand Up @@ -1010,12 +1009,10 @@ public final class MySQLStmt {
public func close() {
if let meta = self.meta {
mysql_free_result(meta)
binds.deallocate()

binds.deallocate(capacity: numFields)

lengthBuffers.deallocate(capacity: numFields)
isNullBuffers.deallocate(capacity: numFields)

lengthBuffers.deallocate()
isNullBuffers.deallocate()
self.meta = nil
}
}
Expand Down Expand Up @@ -1157,34 +1154,34 @@ public final class MySQLStmt {
case .Double:
switch bind.buffer_type {
case MYSQL_TYPE_FLOAT:
bind.buffer.assumingMemoryBound(to: Float.self).deallocate(capacity: 1)
bind.buffer.assumingMemoryBound(to: Float.self).deallocate()
case MYSQL_TYPE_DOUBLE:
bind.buffer.assumingMemoryBound(to: Double.self).deallocate(capacity: 1)
bind.buffer.assumingMemoryBound(to: Double.self).deallocate()
default: break
}
case .Integer:
if bind.is_unsigned == 1 {
switch bind.buffer_type {
case MYSQL_TYPE_LONGLONG:
bind.buffer.assumingMemoryBound(to: CUnsignedLongLong.self).deallocate(capacity: 1)
bind.buffer.assumingMemoryBound(to: CUnsignedLongLong.self).deallocate()
case MYSQL_TYPE_LONG, MYSQL_TYPE_INT24:
bind.buffer.assumingMemoryBound(to: CUnsignedInt.self).deallocate(capacity: 1)
bind.buffer.assumingMemoryBound(to: CUnsignedInt.self).deallocate()
case MYSQL_TYPE_SHORT:
bind.buffer.assumingMemoryBound(to: CUnsignedShort.self).deallocate(capacity: 1)
bind.buffer.assumingMemoryBound(to: CUnsignedShort.self).deallocate()
case MYSQL_TYPE_TINY:
bind.buffer.assumingMemoryBound(to: CUnsignedChar.self).deallocate(capacity: 1)
bind.buffer.assumingMemoryBound(to: CUnsignedChar.self).deallocate()
default: break
}
} else {
switch bind.buffer_type {
case MYSQL_TYPE_LONGLONG:
bind.buffer.assumingMemoryBound(to: CLongLong.self).deallocate(capacity: 1)
bind.buffer.assumingMemoryBound(to: CLongLong.self).deallocate()
case MYSQL_TYPE_LONG, MYSQL_TYPE_INT24:
bind.buffer.assumingMemoryBound(to: CInt.self).deallocate(capacity: 1)
bind.buffer.assumingMemoryBound(to: CInt.self).deallocate()
case MYSQL_TYPE_SHORT:
bind.buffer.assumingMemoryBound(to: CShort.self).deallocate(capacity: 1)
bind.buffer.assumingMemoryBound(to: CShort.self).deallocate()
case MYSQL_TYPE_TINY:
bind.buffer.assumingMemoryBound(to: CChar.self).deallocate(capacity: 1)
bind.buffer.assumingMemoryBound(to: CChar.self).deallocate()
default: break
}
}
Expand Down Expand Up @@ -1269,7 +1266,7 @@ public final class MySQLStmt {

let raw = UnsafeMutablePointer<UInt8>.allocate(capacity: length)
defer {
raw.deallocate(capacity: length)
raw.deallocate()
}
bind.buffer = UnsafeMutableRawPointer(raw)
bind.buffer_length = UInt(length)
Expand All @@ -1290,7 +1287,7 @@ public final class MySQLStmt {

let raw = UnsafeMutablePointer<UInt8>.allocate(capacity: length)
defer {
raw.deallocate(capacity: length)
raw.deallocate()
}
bind.buffer = UnsafeMutableRawPointer(raw)
bind.buffer_length = UInt(length)
Expand Down
8 changes: 8 additions & 0 deletions Sources/mariadbclient/mariadb.h
@@ -0,0 +1,8 @@
#ifndef __PERFECT_MARIADB__
#define __PERFECT_MARIADB__
#ifdef __APPLE__
#include "/usr/local/include/mariadb/mysql.h"
#else
#include "/usr/include/mariadb/mysql.h"
#endif
#endif
4 changes: 4 additions & 0 deletions Sources/mariadbclient/module.modulemap
@@ -0,0 +1,4 @@
module mariadbclient {
header "mariadb.h"
export *
}
4 changes: 4 additions & 0 deletions Tests/MariaDBTests/MariaDBTests.swift
Expand Up @@ -22,7 +22,11 @@
import XCTest
@testable import MariaDB

#if os(Linux)
let testHost = "172.23.0.2"
#else
let testHost = "127.0.0.1"
#endif
let testUser = "root"
// PLEASE change to whatever your actual password is before running these tests
let testPassword = "123"//testpassword"
Expand Down
8 changes: 6 additions & 2 deletions docker-compose.yml
@@ -1,12 +1,16 @@
version: '3.1'

services:

db:
image: mariadb
restart: always
ports:
- 3306:3306
networks:
- maria-net
environment:
MYSQL_ROOT_PASSWORD: 123
MYSQL_DATABASE: test
MYSQL_DATABASE: test
networks:
maria-net:
driver: bridge

0 comments on commit e98eff6

Please sign in to comment.