0001    class MemoryDriver: Driver {
0002    	var memory
MemoryDriver.swift:38
			if let data = self.memory[table]?[id] {
MemoryDriver.swift:49
		if let data = self.memory[table] {
MemoryDriver.swift:69
			self.memory[table] = [
MemoryDriver.swift:76
			self.memory[table]?.removeValueForKey(id)
MemoryDriver.swift:103
		if let data = self.memory[table] {
MemoryDriver.swift:121
		if let data = self.memory[table] {
: [ 0003 String: [ //table 0004 String: //id 0005 [ //entity 0006 String: String 0007 ] 0008 ] 0009 ] = [ "users": 0010 [ 0011 "metadata": [ 0012 "increment": "1" 0013 ], 0014 "1": [ 0015 "id": "1", 0016 "first_name": "Tanner", 0017 "last_name": "Nelson", 0018 "email": "me@tanner.xyz" 0019 ] 0020 ] 0021 ] 0022 0023 0024 func fetchOne(table table: String, filters: [Filter]) -> [String: String]? { 0025 print("fetch one \(filters.count) filters on \(table)") 0026 0027 var id: String? 0028 0029 for filter in filters { 0030 if let filter = filter as? CompareFilter { 0031 if filter.key == "id" { //only working for id 0032 id = filter.value 0033 } 0034 } 0035 } 0036 0037 if let id = id { 0038 if let data = self.memory[table]?[id] { 0039 return data 0040 } 0041 } 0042 0043 return nil 0044 } 0045 0046 func fetch(table table: String, filters: [Filter]) -> [[String: String]] { 0047 print("fetch \(filters.count) filters on \(table)") 0048 0049 if let data = self.memory[table] { 0050 var all: [[String: String]] = [] 0051 0052 for (key, entity) in data { 0053 if key != "metadata" { //hack 0054 all.append(entity) 0055 } 0056 } 0057 0058 return all 0059 } 0060 0061 return [] 0062 } 0063 0064 func delete(table table: String, filters: [Filter]) { 0065 print("delete \(filters.count) filters on \(table)") 0066 0067 if filters.count == 0 { 0068 //truncate 0069 self.memory[table] = [ 0070 "metadata": [ 0071 "increment": "0" 0072 ] 0073 ] 0074 } else { 0075 let id = "1" //hack 0076 self.memory[table]?.removeValueForKey(id) 0077 } 0078 } 0079 0080 func update(table table: String, filters: [Filter], data: [String: String]) { 0081 print("update \(filters.count) filters \(data.count) data points on \(table)") 0082 0083 //implement me 0084 } 0085 0086 func insert(table table: String, items: [[String: String]]) { 0087 print("insert \(items.count) items into \(table)") 0088 0089 //implement me 0090 } 0091 0092 func upsert(table table: String, items: [[String: String]]) { 0093 //check if object exists 0094 // if does - update 0095 // if not - insert 0096 0097 //implement me 0098 } 0099 0100 func exists(table table: String, filters: [Filter]) -> Bool { 0101 print("exists \(filters.count) filters on \(table)") 0102 0103 if let data = self.memory[table] { 0104 for (key, _) in data { 0105 //implement filtering 0106 0107 if key != "metadata" { //hack 0108 return true 0109 } 0110 } 0111 } 0112 0113 return false 0114 } 0115 0116 func count(table table: String, filters: [Filter]) -> Int { 0117 print("count \(filters.count) filters on \(table)") 0118 0119 var count = 0 0120 0121 if let data = self.memory[table] { 0122 for (key, _) in data { 0123 //implement filtering 0124 0125 if key != "metadata" { //hack 0126 count += 1 0127 } 0128 } 0129 } 0130 0131 return count 0132 } 0133 0134 /*func query(query: Query) -> Any? { 0135 print("Query") 0136 0137 switch query.operation { 0138 case .Get: 0139 print("Get") 0140 if let id = query.id { 0141 print("id = \(id)") 0142 if let data = self.memory[query.table]?[id] { 0143 return data 0144 } 0145 } else { 0146 print("all") 0147 var data = self.memory[query.table] 0148 data?.removeValueForKey("metadata") 0149 return data 0150 } 0151 case .Save: 0152 print("Save") 0153 if let data = query.data { 0154 if let id = query.id { 0155 print("id = \(id)") 0156 self.memory[query.table]?[id] = data 0157 } else { 0158 if 0159 let incString = self.memory[query.table]?["metadata"]?["increment"], 0160 let inc = Int(incString) 0161 { 0162 let next = "\(inc + 1)" 0163 print("next = \(next)") 0164 self.memory[query.table]?["metadata"]?["increment"] = next 0165 self.memory[query.table]?[next] = data 0166 self.memory[query.table]?[next]?["id"] = next 0167 } 0168 } 0169 } 0170 case .Delete: 0171 print("Delete") 0172 if let id = query.id { 0173 print("id = \(id)") 0174 self.memory[query.table]?.removeValueForKey(id) 0175 } 0176 } 0177 0178 return nil 0179 }*/ 0180 0181 }