2017-07-17 04:36:38 +02:00
|
|
|
//
|
|
|
|
// DatabaseTable.swift
|
|
|
|
// RSDatabase
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 7/16/17.
|
|
|
|
// Copyright © 2017 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
2017-07-17 05:51:08 +02:00
|
|
|
public protocol DatabaseTable {
|
|
|
|
|
2017-07-29 21:13:38 +02:00
|
|
|
var name: String {get}
|
|
|
|
var queue: RSDatabaseQueue {get}
|
2017-07-17 05:51:08 +02:00
|
|
|
|
2017-07-29 21:13:38 +02:00
|
|
|
init(name: String, queue: RSDatabaseQueue)
|
2017-07-17 05:51:08 +02:00
|
|
|
}
|
2017-07-17 04:36:38 +02:00
|
|
|
|
2017-07-17 05:51:08 +02:00
|
|
|
extension DatabaseTable {
|
|
|
|
|
2017-07-17 04:36:38 +02:00
|
|
|
public func selectRowsWhere(key: String, equals value: Any, in database: FMDatabase) -> FMResultSet? {
|
2017-07-17 05:51:08 +02:00
|
|
|
|
2017-07-17 04:36:38 +02:00
|
|
|
return database.rs_selectRowsWhereKey(key, equalsValue: value, tableName: self.name)
|
2017-07-17 05:51:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public func deleteRowsWhere(key: String, equalsAnyValue values: [Any], in database: FMDatabase) {
|
|
|
|
|
|
|
|
if values.isEmpty {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
database.rs_deleteRowsWhereKey(key, inValues: values, tableName: name)
|
|
|
|
}
|
2017-07-29 21:29:05 +02:00
|
|
|
|
|
|
|
// MARK: Counts
|
|
|
|
|
|
|
|
func numberWithCountResultSet(_ resultSet: FMResultSet?) -> Int {
|
|
|
|
|
|
|
|
if let resultSet = resultSet, resultSet.next() {
|
|
|
|
return Int(resultSet.int(forColumnIndex: 0))
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func numberWithSQLAndParameters(_ sql: String, _ parameters: [Any], in database: FMDatabase) -> Int {
|
|
|
|
|
|
|
|
let resultSet = database.executeQuery(sql, withArgumentsIn: parameters)
|
|
|
|
return numberWithCountResultSet(resultSet)
|
|
|
|
}
|
2017-07-17 04:36:38 +02:00
|
|
|
}
|
2017-07-29 21:29:05 +02:00
|
|
|
|