Skip to content

Commit

Permalink
Merge pull request #14 from qutheory/updates
Browse files Browse the repository at this point in the history
updated node + datetime
  • Loading branch information
tanner0101 authored Jul 30, 2016
2 parents 052d1d0 + afaaa97 commit 2421723
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ let package = Package(
.Package(url: "https://github.com/qutheory/cmysql.git", majorVersion: 0, minor: 2),

// Data structure for converting between multiple representations
.Package(url: "https://github.com/qutheory/node.git", majorVersion: 0, minor: 2),
.Package(url: "https://github.com/qutheory/node.git", majorVersion: 0, minor: 3),

// Core extensions, type-aliases, and functions that facilitate common tasks
.Package(url: "https://github.com/qutheory/core.git", majorVersion: 0, minor: 3),

// JSON parsing and serialization for storing arrays and objects in MySQL
.Package(url: "https://github.com/qutheory/json.git", majorVersion: 0, minor: 2)
.Package(url: "https://github.com/qutheory/json.git", majorVersion: 0, minor: 3)
]
)
25 changes: 25 additions & 0 deletions Sources/MySQL/Bind+Node.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ extension Bind {
case MYSQL_TYPE_DOUBLE:
let double = unwrap(buffer, Double.self)
return .number(.double(double))
case MYSQL_TYPE_DATE:
let time = unwrap(buffer, MYSQL_TIME.self)
return .string("\(time.year.pad(4))-\(time.month.pad(2))-\(time.day.pad(2))")
case MYSQL_TYPE_DATETIME:
let time = unwrap(buffer, MYSQL_TIME.self)
return .string("\(time.year.pad(4))-\(time.month.pad(2))-\(time.day.pad(2)) \(time.hour.pad(2)):\(time.minute.pad(2)):\(time.second.pad(2))")
case MYSQL_TYPE_TIME:
let time = unwrap(buffer, MYSQL_TIME.self)
return .string("\(time.hour.pad(2)):\(time.minute.pad(2)):\(time.second.pad(2))")
default:
print("[MySQL] Unsupported type: \(variant).")
return .null
Expand All @@ -92,6 +101,22 @@ extension Bind {
}
}

extension UInt32 {
func pad(_ n: Int) -> String {
var string = description

if string.characters.count >= n {
return string
}

for _ in 0..<(n - string.characters.count) {
string = "0" + string
}

return string
}
}

extension Sequence where Iterator.Element == UInt8 {
var string: String {
var utf = UTF8()
Expand Down
4 changes: 2 additions & 2 deletions Sources/MySQL/Bind.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ extension Node {
case .array(let array):
var bytes: Bytes = []
do {
bytes = try JSON(array).makeBytes()
bytes = try JSON(node: array).makeBytes()
} catch {
print("[MySQL] Could not convert array to JSON.")
}
Expand All @@ -208,7 +208,7 @@ extension Node {
case .object(let object):
var bytes: Bytes = []
do {
bytes = try JSON(object).makeBytes()
bytes = try JSON(node: object).makeBytes()
} catch {
print("[MySQL] Could not convert object to JSON.")
}
Expand Down
26 changes: 25 additions & 1 deletion Tests/MySQL/MySQLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class MySQLTests: XCTestCase {
try mysql.execute("DROP TABLE IF EXISTS json")
try mysql.execute("CREATE TABLE json (i INT, b VARCHAR(64), j JSON)")

let json = try JSON([
let json = try JSON(node: [
"string": "hello, world",
"int": 42
])
Expand All @@ -144,6 +144,30 @@ class MySQLTests: XCTestCase {
}
}

func testTimestamps() {
do {

try mysql.execute("DROP TABLE IF EXISTS times")
try mysql.execute("CREATE TABLE times (i INT, d DATE, t TIME)")


try mysql.execute("INSERT INTO times VALUES (?, ?, ?)", [
1,
"2050-05-12",
"13:42"
])


if let result = try mysql.execute("SELECT * FROM times").first {
print(result)
} else {
XCTFail("No results")
}
} catch {
XCTFail("Testing tables failed: \(error)")
}
}

func testError() {
do {
try mysql.execute("error")
Expand Down

0 comments on commit 2421723

Please sign in to comment.