diff --git a/Source/Pjango/DB/PCDataBase.swift b/Source/Pjango/DB/PCDataBase.swift index 357b5b0..00ac442 100644 --- a/Source/Pjango/DB/PCDataBase.swift +++ b/Source/Pjango/DB/PCDataBase.swift @@ -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 diff --git a/Source/Pjango/DB/PCFileDBDataBase.swift b/Source/Pjango/DB/PCFileDBDataBase.swift index 37f7e7f..1695a12 100644 --- a/Source/Pjango/DB/PCFileDBDataBase.swift +++ b/Source/Pjango/DB/PCFileDBDataBase.swift @@ -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 { diff --git a/Source/Pjango/Model/PCModel.swift b/Source/Pjango/Model/PCModel.swift index ac5128c..fe77fab 100644 --- a/Source/Pjango/Model/PCModel.swift +++ b/Source/Pjango/Model/PCModel.swift @@ -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 diff --git a/Source/Pjango/Utility/Extension.swift b/Source/Pjango/Utility/Extension.swift index 5dbab83..8f61a86 100644 --- a/Source/Pjango/Utility/Extension.swift +++ b/Source/Pjango/Utility/Extension.swift @@ -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) } } diff --git a/Source/Pjango/Utility/PCSqlUtility.swift b/Source/Pjango/Utility/PCSqlUtility.swift index f049ea5..b39ae53 100644 --- a/Source/Pjango/Utility/PCSqlUtility.swift +++ b/Source/Pjango/Utility/PCSqlUtility.swift @@ -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))" } @@ -59,7 +59,7 @@ final public class PCSqlUtility { var updateStr = "" for i in 0..