Skip to content

Change Element from array to dict, extract field name #17

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
48 changes: 25 additions & 23 deletions Sources/MySQL/MySQL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ public final class MySQLStmt {
/// manage results sets for MysqlStmt
public final class Results {
let _UNSIGNED_FLAG = UInt32(UNSIGNED_FLAG)
public typealias Element = [Any?]
public typealias Element = [String: Any?]

let stmt: MySQLStmt

Expand All @@ -869,6 +869,7 @@ public final class MySQLStmt {

var meta: UnsafeMutablePointer<MYSQL_RES>? { return stmt.meta }
let binds: UnsafeMutablePointer<MYSQL_BIND>
var fieldNames = [String]()

let lengthBuffers: UnsafeMutablePointer<UInt>
let isNullBuffers: UnsafeMutablePointer<my_bool>
Expand Down Expand Up @@ -975,6 +976,7 @@ public final class MySQLStmt {
continue
}
let f: MYSQL_FIELD = field.pointee
fieldNames.append(String(cString: UnsafePointer<CChar>(f.name)))
var bind = bindField(field)
bind.length = lengthBuffers.advanced(by: i)
bind.length.initialize(to: 0)
Expand Down Expand Up @@ -1094,51 +1096,51 @@ public final class MySQLStmt {
let isNull = bind.is_null.pointee

if isNull != 0 {
row.append(nil)
row[fieldNames[i]] = nil
} else {

switch genType {
case .Double:
switch bind.buffer_type {
case MYSQL_TYPE_FLOAT:
let f = bind.buffer.assumingMemoryBound(to: Float.self).pointee
row.append(f)
row[fieldNames[i]] = f
case MYSQL_TYPE_DOUBLE:
let f = bind.buffer.assumingMemoryBound(to: Double.self).pointee
row.append(f)
row[fieldNames[i]] = f
default: break
}
case .Integer:
if bind.is_unsigned == 1 {
switch bind.buffer_type {
case MYSQL_TYPE_LONGLONG:
let i = bind.buffer.assumingMemoryBound(to: CUnsignedLongLong.self).pointee
row.append(i)
let pointee = bind.buffer.assumingMemoryBound(to: CUnsignedLongLong.self).pointee
row[fieldNames[i]] = pointee
case MYSQL_TYPE_LONG, MYSQL_TYPE_INT24:
let i = bind.buffer.assumingMemoryBound(to: CUnsignedInt.self).pointee
row.append(i)
let pointee = bind.buffer.assumingMemoryBound(to: CUnsignedInt.self).pointee
row[fieldNames[i]] = pointee
case MYSQL_TYPE_SHORT:
let i = bind.buffer.assumingMemoryBound(to: CUnsignedShort.self).pointee
row.append(i)
let pointee = bind.buffer.assumingMemoryBound(to: CUnsignedShort.self).pointee
row[fieldNames[i]] = pointee
case MYSQL_TYPE_TINY:
let i = bind.buffer.assumingMemoryBound(to: CUnsignedChar.self).pointee
row.append(i)
let pointee = bind.buffer.assumingMemoryBound(to: CUnsignedChar.self).pointee
row[fieldNames[i]] = pointee
default: break
}
} else {
switch bind.buffer_type {
case MYSQL_TYPE_LONGLONG:
let i = bind.buffer.assumingMemoryBound(to: CLongLong.self).pointee
row.append(i)
let pointee = bind.buffer.assumingMemoryBound(to: CLongLong.self).pointee
row[fieldNames[i]] = pointee
case MYSQL_TYPE_LONG, MYSQL_TYPE_INT24:
let i = bind.buffer.assumingMemoryBound(to: CInt.self).pointee
row.append(i)
let pointee = bind.buffer.assumingMemoryBound(to: CInt.self).pointee
row[fieldNames[i]] = pointee
case MYSQL_TYPE_SHORT:
let i = bind.buffer.assumingMemoryBound(to: CShort.self).pointee
row.append(i)
let pointee = bind.buffer.assumingMemoryBound(to: CShort.self).pointee
row[fieldNames[i]] = pointee
case MYSQL_TYPE_TINY:
let i = bind.buffer.assumingMemoryBound(to: CChar.self).pointee
row.append(i)
let pointee = bind.buffer.assumingMemoryBound(to: CChar.self).pointee
row[fieldNames[i]] = pointee
default: break
}
}
Expand All @@ -1161,7 +1163,7 @@ public final class MySQLStmt {
while let c = gen.next() {
a.append(c)
}
row.append(a)
row[fieldNames[i]] = a

case .String, .Date:

Expand All @@ -1178,10 +1180,10 @@ public final class MySQLStmt {
}

let s = UTF8Encoding.encode(generator: GenerateFromPointer(from: raw, count: length))
row.append(s)
row[fieldNames[i]] = s

case .Null:
row.append(nil)
row[fieldNames[i]] = nil
}
}
}
Expand Down