Completed
Push — master ( 384d23...c6058f )
by Tom
42s queued 10s
created
maphper/datasource/sqliteadapter.php 1 patch
Braces   +60 added lines, -29 removed lines patch added patch discarded remove patch
@@ -15,33 +15,48 @@  discard block
 block discarded – undo
15 15
 		
16 16
 	public function query(\Maphper\Lib\Query $query) {
17 17
 		$queryId = md5($query->getSql());
18
-		if (isset($this->queryCache[$queryId])) $stmt = $this->queryCache[$queryId];
19
-		else {
18
+		if (isset($this->queryCache[$queryId])) {
19
+			$stmt = $this->queryCache[$queryId];
20
+		} else {
20 21
 			$stmt = $this->pdo->prepare($query->getSql(), [\PDO::ATTR_CURSOR => \PDO::CURSOR_FWDONLY]);
21
-			if ($stmt) $this->queryCache[$queryId] = $stmt;
22
+			if ($stmt) {
23
+				$this->queryCache[$queryId] = $stmt;
24
+			}
22 25
 		}
23 26
 		
24 27
 		$args = $query->getArgs();
25
-		foreach ($args as &$arg) if ($arg instanceof \DateTime) {
28
+		foreach ($args as &$arg) {
29
+			if ($arg instanceof \DateTime) {
26 30
 			if ($arg->format('H:i:s')  == '00:00:00') $arg = $arg->format('Y-m-d');
27
-			else $arg = $arg->format('Y-m-d H:i:s');
31
+		}
32
+			else {
33
+				$arg = $arg->format('Y-m-d H:i:s');
34
+			}
28 35
 		}
29 36
 				
30 37
 		if ($stmt !== false) {
31 38
 			try {
32
-				if (count($args) > 0) $res = $stmt->execute($args);
33
-				else $res = $stmt->execute();
34
-				if ($stmt->errorCode() !== '00000') throw new \Exception('Invalid query');
35
-				if (substr($query->getSql(), 0, 6) === 'SELECT') return $stmt->fetchAll(\PDO::FETCH_OBJ);
36
-				else return $stmt;
37
-			}
38
-			catch (\Exception $e) {
39
+				if (count($args) > 0) {
40
+					$res = $stmt->execute($args);
41
+				} else {
42
+					$res = $stmt->execute();
43
+				}
44
+				if ($stmt->errorCode() !== '00000') {
45
+					throw new \Exception('Invalid query');
46
+				}
47
+				if (substr($query->getSql(), 0, 6) === 'SELECT') {
48
+					return $stmt->fetchAll(\PDO::FETCH_OBJ);
49
+				} else {
50
+					return $stmt;
51
+				}
52
+			} catch (\Exception $e) {
39 53
 				//SQLite causes an error if when the DB schema changes, rebuild $stmt and try again.
40 54
 				if ($stmt->errorInfo()[2] == 'database schema has changed') {
41 55
 					unset($this->queryCache[$queryId]);
42 56
 					return $this->query($query);	
57
+				} else {
58
+					return $stmt;
43 59
 				}
44
-				else return $stmt;				
45 60
 			}
46 61
 		}
47 62
 		//Handle SQLite when PDO_ERRMODE is set to SILENT
@@ -55,12 +70,19 @@  discard block
 block discarded – undo
55 70
 	}
56 71
 	
57 72
 	private function getType($val) {
58
-		if ($val instanceof \DateTime) return 'DATETIME';
59
-		else if (is_int($val)) return  'INTEGER';
60
-		else if (is_double($val)) return 'DECIMAL(9,' . strlen($val) - strrpos($val, '.') - 1 . ')';
61
-		else if (is_string($val) && strlen($val) < 256) return 'VARCHAR(255)';
62
-		else if (is_string($val) && strlen($val) > 256) return 'LONGBLOG';
63
-		else return 'VARCHAR(255)';		
73
+		if ($val instanceof \DateTime) {
74
+			return 'DATETIME';
75
+		} else if (is_int($val)) {
76
+			return  'INTEGER';
77
+		} else if (is_double($val)) {
78
+			return 'DECIMAL(9,' . strlen($val) - strrpos($val, '.') - 1 . ')';
79
+		} else if (is_string($val) && strlen($val) < 256) {
80
+			return 'VARCHAR(255)';
81
+		} else if (is_string($val) && strlen($val) > 256) {
82
+			return 'LONGBLOG';
83
+		} else {
84
+			return 'VARCHAR(255)';
85
+		}
64 86
 	}
65 87
 
66 88
 	private function tableExists($name) {
@@ -94,8 +116,7 @@  discard block
 block discarded – undo
94 116
 				$this->pdo->query('INSERT INTO ' . $this->quote($table . $affix) . '(' . $columns . ') SELECT ' . $columns . ' FROM ' . $this->quote($table));
95 117
 				$this->pdo->query('DROP TABLE IF EXISTS ' . $table );
96 118
 			}
97
-		}
98
-		catch (\PDOException $e) {
119
+		} catch (\PDOException $e) {
99 120
 			// No data to copy
100 121
 			echo $e->getMessage();
101 122
 		}
@@ -109,8 +130,11 @@  discard block
 block discarded – undo
109 130
 		$parts = [];
110 131
 		foreach ($primaryKey as $key) {
111 132
 			$pk = $data->$key;
112
-			if ($pk == null) $parts[] = $key . ' INTEGER'; 
113
-			else $parts[] = $key . ' ' . $this->getType($pk) . ' NOT NULL';					
133
+			if ($pk == null) {
134
+				$parts[] = $key . ' INTEGER';
135
+			} else {
136
+				$parts[] = $key . ' ' . $this->getType($pk) . ' NOT NULL';
137
+			}
114 138
 		}
115 139
 		
116 140
 		$pkField = implode(', ', $parts) . ', PRIMARY KEY(' . implode(', ', $primaryKey) . ')';
@@ -119,8 +143,12 @@  discard block
 block discarded – undo
119 143
 		$this->pdo->query('CREATE TABLE ' . $table . ' (' . $pkField . ')');
120 144
 					
121 145
 		foreach ($data as $key => $value) {
122
-			if (is_array($value) || (is_object($value) && !($value instanceof \DateTime))) continue;
123
-			if (in_array($key, $primaryKey)) continue;
146
+			if (is_array($value) || (is_object($value) && !($value instanceof \DateTime))) {
147
+				continue;
148
+			}
149
+			if (in_array($key, $primaryKey)) {
150
+				continue;
151
+			}
124 152
 
125 153
 			$type = $this->getType($value);
126 154
 		
@@ -130,10 +158,14 @@  discard block
 block discarded – undo
130 158
 	
131 159
 
132 160
 	public function addIndex($table, array $fields) {
133
-		if (empty($fields)) return false;
161
+		if (empty($fields)) {
162
+			return false;
163
+		}
134 164
 		
135 165
 		//SQLite doesn't support ASC/DESC indexes, remove the keywords
136
-		foreach ($fields as &$field) $field = str_ireplace([' desc', ' asc'], '', $field);
166
+		foreach ($fields as &$field) {
167
+			$field = str_ireplace([' desc', ' asc'], '', $field);
168
+		}
137 169
 		sort($fields);
138 170
 		$fields = array_map('strtolower', $fields);
139 171
 		$fields = array_map('trim', $fields);
@@ -142,8 +174,7 @@  discard block
 block discarded – undo
142 174
 		
143 175
 		try {
144 176
 			$this->pdo->query('CREATE INDEX IF NOT EXISTS  ' . $keyName . ' ON ' . $table . ' (' . implode(', ', $fields) . ')');
145
-		}
146
-		catch (\Exception $e) {
177
+		} catch (\Exception $e) {
147 178
 			
148 179
 		}
149 180
 	}
Please login to merge, or discard this patch.