Skip to content
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
67 changes: 32 additions & 35 deletions Sources/MySQLStORM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -265,42 +265,39 @@ open class MySQLStORM: StORM, StORMProtocol {
var createStatement = str
if str.characters.count == 0 {
var opt = [String]()
var keyName = ""
for child in Mirror(reflecting: self).children {
guard let key = child.label else {
continue
}
var verbage = ""
if !key.hasPrefix("internal_") && !key.hasPrefix("_") {
verbage = "`\(key)` "
if child.value is Int && opt.count == 0 {
verbage += "int"
} else if child.value is Int {
verbage += "int"
} else if child.value is Bool {
verbage += "int" // MySQL has no bool type
} else if child.value is Double {
verbage += "float"
} else if child.value is UInt || child.value is UInt8 || child.value is UInt16 || child.value is UInt32 || child.value is UInt64 {
verbage += "blob"
} else {
verbage += "text"
}
if opt.count == 0 {
if child.value is Int {
verbage = "`\(key)` int NOT NULL AUTO_INCREMENT"
} else {
verbage = "`\(key)` varchar(255) NOT NULL"
}
keyName = key
}
opt.append(verbage)
}
let mirrorData = StORMMirrorData.mirror(mirror: Mirror(reflecting: self))
var verbage = ""
for child in mirrorData.childs {
verbage = "`\(child.label)` "
if child.value is Int && opt.count == 0 {
verbage += "int"
} else if child.value is Int {
verbage += "int"
} else if child.value is Bool {
verbage += "int" // MySQL has no bool type
} else if child.value is Double {
verbage += "float"
} else if child.value is UInt || child.value is UInt8 || child.value is UInt16 || child.value is UInt32 || child.value is UInt64 {
verbage += "blob"
} else {
verbage += "text"
}

opt.append(verbage)
}
let keyComponent = ", PRIMARY KEY (`\(keyName)`)"

createStatement = "CREATE TABLE IF NOT EXISTS \(table()) (\(opt.joined(separator: ", "))\(keyComponent));"
if StORMdebug { LogFile.info("createStatement: \(createStatement)", logFile: "./StORMlog.txt") }
if let primary = mirrorData.primary {
if primary.value is Int || primary.value is Int32 {
verbage = "`\(primary.label)` int NOT NULL AUTO_INCREMENT"
} else {
verbage = "`\(primary.label)` varchar(255) NOT NULL"
}
opt.insert(verbage, at: 0)
let keyComponent = ", PRIMARY KEY (`\(primary.label)`)"
createStatement = "CREATE TABLE IF NOT EXISTS \(table()) (\(opt.joined(separator: ", "))\(keyComponent));"
if StORMdebug { LogFile.info("createStatement: \(createStatement)", logFile: "./StORMlog.txt") }
} else {
LogFile.error("Con't find Primary Key")
}
}
do {
try sql(createStatement, params: [])
Expand Down