mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-26 04:07:50 +00:00
Xcode7b6
This commit is contained in:
1 parent
13d1351a81
commit
bf2c93f9f7
5 files changed
+46
-32
No files matched your search
@@ -30,7 +30,7 @@ class Person : RowModel {
|
||||
var firstName: String?
|
||||
var lastName: String?
|
||||
var fullName: String {
|
||||
return " ".join([firstName, lastName].flatMap { $0 })
|
||||
return [firstName, lastName].flatMap { $0 }.joinWithSeparator(" ")
|
||||
}
|
||||
|
||||
init(firstName: String? = nil, lastName: String? = nil) {
|
||||
|
||||
@@ -289,9 +289,9 @@ public struct DatabaseDateComponents : DatabaseValueConvertible {
|
||||
|
||||
switch format {
|
||||
case .YMD_HM, .YMD_HMS, .YMD_HMSS:
|
||||
return .Text(" ".join([dateString, timeString].flatMap { $0 }))
|
||||
return .Text([dateString, timeString].flatMap { $0 }.joinWithSeparator(" "))
|
||||
default:
|
||||
return .Text("T".join([dateString, timeString].flatMap { $0 }))
|
||||
return .Text([dateString, timeString].flatMap { $0 }.joinWithSeparator("T"))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+5
-3
@@ -250,16 +250,16 @@ public struct Row: CollectionType {
|
||||
}
|
||||
|
||||
func databaseValue(atIndex index: Int) -> DatabaseValue {
|
||||
return databaseDictionary[advance(databaseDictionary.startIndex, index)].1
|
||||
return databaseDictionary[databaseDictionary.startIndex.advancedBy(index)].1
|
||||
}
|
||||
|
||||
func columnName(atIndex index: Int) -> String {
|
||||
return databaseDictionary[advance(databaseDictionary.startIndex, index)].0
|
||||
return databaseDictionary[databaseDictionary.startIndex.advancedBy(index)].0
|
||||
}
|
||||
|
||||
func indexForColumn(named name: String) -> Int? {
|
||||
if let index = databaseDictionary.indexForKey(name) {
|
||||
return distance(databaseDictionary.startIndex, index)
|
||||
return databaseDictionary.startIndex.distanceTo(index)
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
@@ -316,6 +316,8 @@ protocol RowImpl {
|
||||
|
||||
/// Indexes to (columnName, databaseValue) pairs in a database row.
|
||||
public struct RowIndex: ForwardIndexType, BidirectionalIndexType, RandomAccessIndexType {
|
||||
public typealias Distance = Int
|
||||
|
||||
let index: Int
|
||||
init(_ index: Int) { self.index = index }
|
||||
|
||||
|
||||
+17
-14
@@ -563,37 +563,37 @@ public class RowModel {
|
||||
|
||||
private class func insertStatement(db: Database, tableName: String, insertedColumns: [String]) throws -> UpdateStatement {
|
||||
// INSERT INTO table (id, name) VALUES (?, ?)
|
||||
let columnSQL = ",".join(insertedColumns.map { $0.quotedDatabaseIdentifier })
|
||||
let valuesSQL = ",".join([String](count: insertedColumns.count, repeatedValue: "?"))
|
||||
let columnSQL = insertedColumns.map { $0.quotedDatabaseIdentifier }.joinWithSeparator(",")
|
||||
let valuesSQL = [String](count: insertedColumns.count, repeatedValue: "?").joinWithSeparator(",")
|
||||
let sql = "INSERT INTO \(tableName.quotedDatabaseIdentifier) (\(columnSQL)) VALUES (\(valuesSQL))"
|
||||
return try db.updateStatement(sql)
|
||||
}
|
||||
|
||||
private class func updateStatement(db: Database, tableName: String, updatedColumns: [String], conditionColumns: [String]) throws -> UpdateStatement {
|
||||
// "UPDATE table SET name = ? WHERE id = ?"
|
||||
let updateSQL = ",".join(updatedColumns.map { "\($0.quotedDatabaseIdentifier)=?" })
|
||||
let conditionSQL = " AND ".join(conditionColumns.map { "\($0.quotedDatabaseIdentifier)=?" })
|
||||
let updateSQL = updatedColumns.map { "\($0.quotedDatabaseIdentifier)=?" }.joinWithSeparator(",")
|
||||
let conditionSQL = conditionColumns.map { "\($0.quotedDatabaseIdentifier)=?" }.joinWithSeparator(" AND ")
|
||||
let sql = "UPDATE \(tableName.quotedDatabaseIdentifier) SET \(updateSQL) WHERE \(conditionSQL)"
|
||||
return try db.updateStatement(sql)
|
||||
}
|
||||
|
||||
private class func deleteStatement(db: Database, tableName: String, conditionColumns: [String]) throws -> UpdateStatement {
|
||||
// "DELETE FROM table WHERE id = ?"
|
||||
let conditionSQL = " AND ".join(conditionColumns.map { "\($0.quotedDatabaseIdentifier)=?" })
|
||||
let conditionSQL = conditionColumns.map { "\($0.quotedDatabaseIdentifier)=?" }.joinWithSeparator(" AND ")
|
||||
let sql = "DELETE FROM \(tableName.quotedDatabaseIdentifier) WHERE \(conditionSQL)"
|
||||
return try db.updateStatement(sql)
|
||||
}
|
||||
|
||||
private class func existsStatement(db: Database, tableName: String, conditionColumns: [String]) -> SelectStatement {
|
||||
// "SELECT 1 FROM table WHERE id = ?"
|
||||
let conditionSQL = " AND ".join(conditionColumns.map { "\($0.quotedDatabaseIdentifier)=?" })
|
||||
let conditionSQL = conditionColumns.map { "\($0.quotedDatabaseIdentifier)=?" }.joinWithSeparator(" AND ")
|
||||
let sql = "SELECT 1 FROM \(tableName.quotedDatabaseIdentifier) WHERE \(conditionSQL)"
|
||||
return db.selectStatement(sql)
|
||||
}
|
||||
|
||||
private class func selectStatement(db: Database, tableName: String, conditionColumns: [String]) -> SelectStatement {
|
||||
// "SELECT * FROM table WHERE id = ?"
|
||||
let conditionSQL = " AND ".join(conditionColumns.map { "\($0.quotedDatabaseIdentifier)=?" })
|
||||
let conditionSQL = conditionColumns.map { "\($0.quotedDatabaseIdentifier)=?" }.joinWithSeparator(" AND ")
|
||||
let sql = "SELECT * FROM \(tableName.quotedDatabaseIdentifier) WHERE \(conditionSQL)"
|
||||
return db.selectStatement(sql)
|
||||
}
|
||||
@@ -617,12 +617,15 @@ extension RowModel : RowModelType { }
|
||||
extension RowModel : CustomStringConvertible {
|
||||
/// A textual representation of `self`.
|
||||
public var description: String {
|
||||
return "<\(self.dynamicType)" + "".join(storedDatabaseDictionary.map { (key, value) in
|
||||
if let value = value {
|
||||
return " \(key):\(String(reflecting: value))"
|
||||
} else {
|
||||
return " \(key):nil"
|
||||
}}) + ">"
|
||||
return "<\(self.dynamicType)"
|
||||
+ storedDatabaseDictionary.map { (key, value) in
|
||||
if let value = value {
|
||||
return " \(key):\(String(reflecting: value))"
|
||||
} else {
|
||||
return " \(key):nil"
|
||||
}
|
||||
}.joinWithSeparator("")
|
||||
+ ">"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -768,7 +771,7 @@ extension Database {
|
||||
fatalError("Nil Table returned from \(type).databaseTable")
|
||||
}
|
||||
|
||||
let whereSQL = " AND ".join(dictionary.keys.map { column in "\(column.quotedDatabaseIdentifier)=?" })
|
||||
let whereSQL = dictionary.keys.map { column in "\(column.quotedDatabaseIdentifier)=?" }.joinWithSeparator(" AND ")
|
||||
let sql = "SELECT * FROM \(table.name.quotedDatabaseIdentifier) WHERE \(whereSQL)"
|
||||
return selectStatement(sql).fetchOne(type, arguments: StatementArguments(dictionary.values))
|
||||
}
|
||||
|
||||
@@ -192,12 +192,17 @@ public struct StatementArguments {
|
||||
}
|
||||
|
||||
var description: String {
|
||||
return "[" + ", ".join(values.map { value in
|
||||
if let value = value {
|
||||
return String(reflecting: value)
|
||||
} else {
|
||||
return "nil"
|
||||
}}) + "]"
|
||||
return "["
|
||||
+ values
|
||||
.map { value in
|
||||
if let value = value {
|
||||
return String(reflecting: value)
|
||||
} else {
|
||||
return "nil"
|
||||
}
|
||||
}
|
||||
.joinWithSeparator(", ")
|
||||
+ "]"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -219,12 +224,16 @@ public struct StatementArguments {
|
||||
}
|
||||
|
||||
var description: String {
|
||||
return "[" + ", ".join(dictionary.map { (key, value) in
|
||||
if let value = value {
|
||||
return "\(key):\(String(reflecting: value))"
|
||||
} else {
|
||||
return "\(key):nil"
|
||||
}}) + "]"
|
||||
return "["
|
||||
+ dictionary.map { (key, value) in
|
||||
if let value = value {
|
||||
return "\(key):\(String(reflecting: value))"
|
||||
} else {
|
||||
return "\(key):nil"
|
||||
}
|
||||
}
|
||||
.joinWithSeparator(", ")
|
||||
+ "]"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user