Skip to content

Commit

Permalink
Add ext param in PCModel query
Browse files Browse the repository at this point in the history
  • Loading branch information
enums committed Jan 21, 2018
1 parent 59c8076 commit 63e541a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 17 deletions.
8 changes: 4 additions & 4 deletions Source/Pjango/DB/PCDataBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ open class PCDataBase {
_pjango_core_log.info("Success on droping table \(PCSqlUtility.schemaAndTableToStr(schema, table))")
}

open func selectTable(model: PCMetaModel) -> [PCDataBaseRecord]? {
return selectTable(table: model.tableName)
open func selectTable(model: PCMetaModel, ext: String? = nil) -> [PCDataBaseRecord]? {
return selectTable(table: model.tableName, ext: ext)
}

open func selectTable(table: String) -> [PCDataBaseRecord]? {
return query(PCSqlUtility.selectTable(schema, table))
open func selectTable(table: String, ext: String? = nil) -> [PCDataBaseRecord]? {
return query(PCSqlUtility.selectTable(schema, table, ext: ext))
}

@discardableResult
Expand Down
6 changes: 3 additions & 3 deletions Source/Pjango/DB/PCFileDBDataBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ open class PCFileDBDataBase: PCDataBase {
}
}

open override func selectTable(model: PCMetaModel) -> [PCDataBaseRecord]? {
return selectTable(table: model.tableName)
open override func selectTable(model: PCMetaModel, ext: String? = nil) -> [PCDataBaseRecord]? {
return selectTable(table: model.tableName, ext: ext)
}

open override func selectTable(table: String) -> [PCDataBaseRecord]? {
open override func selectTable(table: String, ext: String? = nil) -> [PCDataBaseRecord]? {
return _pjango_filedb_doWithLocked {
let file = File.init(PCFileDBUtility.filePathForTable(path: path, schema: schema!, table: table))
guard file.exists else {
Expand Down
7 changes: 4 additions & 3 deletions Source/Pjango/Model/PCModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,20 @@ open class PCModel: PCObject, PCViewable {
return nil
}

open class func queryObjects() -> [PCModel]? {
open class func queryObjects(ext: (useCache: Bool, param: String)? = nil) -> [PCModel]? {
guard let meta = PjangoRuntime._pjango_runtime_models_name2meta[_pjango_core_class_name] else {
return nil
}
let nowTime = Date.init()
let records: [PCDataBaseRecord]
if let cacheTime = cacheTime,
if ext?.useCache == false,
let cacheTime = cacheTime,
let cache = _pjango_core_model_cache[_pjango_core_class_name],
let lastCacheTime = _pjango_core_model_cache_time[_pjango_core_class_name],
nowTime.timeIntervalSince1970 - lastCacheTime <= cacheTime {
records = cache
} else {
guard let recordsFromDB = PjangoRuntime._pjango_runtime_database.selectTable(model: meta) else {
guard let recordsFromDB = PjangoRuntime._pjango_runtime_database.selectTable(model: meta, ext: ext?.param) else {
return nil
}
_pjango_core_model_cache[_pjango_core_class_name] = recordsFromDB
Expand Down
10 changes: 7 additions & 3 deletions Source/Pjango/Utility/Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ fileprivate var dateFormatter = { () -> DateFormatter in

public extension Date {
var stringValue: String {
get {
return dateFormatter.string(from: self)
}
return dateFormatter.string(from: self)
}
}

public extension String {
var dateValue: Date? {
return dateFormatter.date(from: self)
}
}

Expand Down
8 changes: 4 additions & 4 deletions Source/Pjango/Utility/PCSqlUtility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ final public class PCSqlUtility {
return "DROP TABLE \(schemaAndTableToStr(schema, table));"
}

public static func selectTable(_ schema: String? = nil, _ table: String, _ fields: String = "*") -> PCSqlStatement {
return "SELECT \(fields) FROM \(schemaAndTableToStr(schema, table));"
public static func selectTable(_ schema: String? = nil, _ table: String, _ fields: String = "*", ext: String? = nil) -> PCSqlStatement {
return "SELECT \(fields) FROM \(schemaAndTableToStr(schema, table)) \(ext ?? "");"
}

public static func insertRecord(_ schema: String? = nil, _ table: String, _ record: PCDataBaseRecord) -> PCSqlStatement {
let recordStr = record.reduce("'0'") {
"\($0), '\(($1))'"
"\($0), '\(($1.replacingOccurrences(of: "'", with: "\\'")))'"
}
return "INSERT INTO \(schemaAndTableToStr(schema, table)) VALUES (\(recordStr))"
}
Expand All @@ -59,7 +59,7 @@ final public class PCSqlUtility {
var updateStr = ""
for i in 0..<fields.count {
let field = fields[i]
let value = record[i]
let value = record[i]
updateStr += "`\(field)`='\(value)'"
if i < fields.count - 1 {
updateStr += ","
Expand Down

0 comments on commit 63e541a

Please sign in to comment.