Completed
Pull Request — master (#7554)
by Morris
26:41 queued 12:26
created
lib/private/legacy/db.php 2 patches
Indentation   +194 added lines, -194 removed lines patch added patch discarded remove patch
@@ -34,210 +34,210 @@
 block discarded – undo
34 34
  */
35 35
 class OC_DB {
36 36
 
37
-	/**
38
-	 * get MDB2 schema manager
39
-	 *
40
-	 * @return \OC\DB\MDB2SchemaManager
41
-	 */
42
-	private static function getMDB2SchemaManager() {
43
-		return new \OC\DB\MDB2SchemaManager(\OC::$server->getDatabaseConnection());
44
-	}
37
+    /**
38
+     * get MDB2 schema manager
39
+     *
40
+     * @return \OC\DB\MDB2SchemaManager
41
+     */
42
+    private static function getMDB2SchemaManager() {
43
+        return new \OC\DB\MDB2SchemaManager(\OC::$server->getDatabaseConnection());
44
+    }
45 45
 
46
-	/**
47
-	 * Prepare a SQL query
48
-	 * @param string $query Query string
49
-	 * @param int|null $limit
50
-	 * @param int|null $offset
51
-	 * @param bool|null $isManipulation
52
-	 * @throws \OC\DatabaseException
53
-	 * @return OC_DB_StatementWrapper prepared SQL query
54
-	 *
55
-	 * SQL query via Doctrine prepare(), needs to be execute()'d!
56
-	 */
57
-	static public function prepare( $query , $limit = null, $offset = null, $isManipulation = null) {
58
-		$connection = \OC::$server->getDatabaseConnection();
46
+    /**
47
+     * Prepare a SQL query
48
+     * @param string $query Query string
49
+     * @param int|null $limit
50
+     * @param int|null $offset
51
+     * @param bool|null $isManipulation
52
+     * @throws \OC\DatabaseException
53
+     * @return OC_DB_StatementWrapper prepared SQL query
54
+     *
55
+     * SQL query via Doctrine prepare(), needs to be execute()'d!
56
+     */
57
+    static public function prepare( $query , $limit = null, $offset = null, $isManipulation = null) {
58
+        $connection = \OC::$server->getDatabaseConnection();
59 59
 
60
-		if ($isManipulation === null) {
61
-			//try to guess, so we return the number of rows on manipulations
62
-			$isManipulation = self::isManipulation($query);
63
-		}
60
+        if ($isManipulation === null) {
61
+            //try to guess, so we return the number of rows on manipulations
62
+            $isManipulation = self::isManipulation($query);
63
+        }
64 64
 
65
-		// return the result
66
-		try {
67
-			$result =$connection->prepare($query, $limit, $offset);
68
-		} catch (\Doctrine\DBAL\DBALException $e) {
69
-			throw new \OC\DatabaseException($e->getMessage());
70
-		}
71
-		// differentiate between query and manipulation
72
-		$result = new OC_DB_StatementWrapper($result, $isManipulation);
73
-		return $result;
74
-	}
65
+        // return the result
66
+        try {
67
+            $result =$connection->prepare($query, $limit, $offset);
68
+        } catch (\Doctrine\DBAL\DBALException $e) {
69
+            throw new \OC\DatabaseException($e->getMessage());
70
+        }
71
+        // differentiate between query and manipulation
72
+        $result = new OC_DB_StatementWrapper($result, $isManipulation);
73
+        return $result;
74
+    }
75 75
 
76
-	/**
77
-	 * tries to guess the type of statement based on the first 10 characters
78
-	 * the current check allows some whitespace but does not work with IF EXISTS or other more complex statements
79
-	 *
80
-	 * @param string $sql
81
-	 * @return bool
82
-	 */
83
-	static public function isManipulation( $sql ) {
84
-		$selectOccurrence = stripos($sql, 'SELECT');
85
-		if ($selectOccurrence !== false && $selectOccurrence < 10) {
86
-			return false;
87
-		}
88
-		$insertOccurrence = stripos($sql, 'INSERT');
89
-		if ($insertOccurrence !== false && $insertOccurrence < 10) {
90
-			return true;
91
-		}
92
-		$updateOccurrence = stripos($sql, 'UPDATE');
93
-		if ($updateOccurrence !== false && $updateOccurrence < 10) {
94
-			return true;
95
-		}
96
-		$deleteOccurrence = stripos($sql, 'DELETE');
97
-		if ($deleteOccurrence !== false && $deleteOccurrence < 10) {
98
-			return true;
99
-		}
100
-		return false;
101
-	}
76
+    /**
77
+     * tries to guess the type of statement based on the first 10 characters
78
+     * the current check allows some whitespace but does not work with IF EXISTS or other more complex statements
79
+     *
80
+     * @param string $sql
81
+     * @return bool
82
+     */
83
+    static public function isManipulation( $sql ) {
84
+        $selectOccurrence = stripos($sql, 'SELECT');
85
+        if ($selectOccurrence !== false && $selectOccurrence < 10) {
86
+            return false;
87
+        }
88
+        $insertOccurrence = stripos($sql, 'INSERT');
89
+        if ($insertOccurrence !== false && $insertOccurrence < 10) {
90
+            return true;
91
+        }
92
+        $updateOccurrence = stripos($sql, 'UPDATE');
93
+        if ($updateOccurrence !== false && $updateOccurrence < 10) {
94
+            return true;
95
+        }
96
+        $deleteOccurrence = stripos($sql, 'DELETE');
97
+        if ($deleteOccurrence !== false && $deleteOccurrence < 10) {
98
+            return true;
99
+        }
100
+        return false;
101
+    }
102 102
 
103
-	/**
104
-	 * execute a prepared statement, on error write log and throw exception
105
-	 * @param mixed $stmt OC_DB_StatementWrapper,
106
-	 *					  an array with 'sql' and optionally 'limit' and 'offset' keys
107
-	 *					.. or a simple sql query string
108
-	 * @param array $parameters
109
-	 * @return OC_DB_StatementWrapper
110
-	 * @throws \OC\DatabaseException
111
-	 */
112
-	static public function executeAudited( $stmt, array $parameters = []) {
113
-		if (is_string($stmt)) {
114
-			// convert to an array with 'sql'
115
-			if (stripos($stmt, 'LIMIT') !== false) { //OFFSET requires LIMIT, so we only need to check for LIMIT
116
-				// TODO try to convert LIMIT OFFSET notation to parameters
117
-				$message = 'LIMIT and OFFSET are forbidden for portability reasons,'
118
-						 . ' pass an array with \'limit\' and \'offset\' instead';
119
-				throw new \OC\DatabaseException($message);
120
-			}
121
-			$stmt = array('sql' => $stmt, 'limit' => null, 'offset' => null);
122
-		}
123
-		if (is_array($stmt)) {
124
-			// convert to prepared statement
125
-			if ( ! array_key_exists('sql', $stmt) ) {
126
-				$message = 'statement array must at least contain key \'sql\'';
127
-				throw new \OC\DatabaseException($message);
128
-			}
129
-			if ( ! array_key_exists('limit', $stmt) ) {
130
-				$stmt['limit'] = null;
131
-			}
132
-			if ( ! array_key_exists('limit', $stmt) ) {
133
-				$stmt['offset'] = null;
134
-			}
135
-			$stmt = self::prepare($stmt['sql'], $stmt['limit'], $stmt['offset']);
136
-		}
137
-		self::raiseExceptionOnError($stmt, 'Could not prepare statement');
138
-		if ($stmt instanceof OC_DB_StatementWrapper) {
139
-			$result = $stmt->execute($parameters);
140
-			self::raiseExceptionOnError($result, 'Could not execute statement');
141
-		} else {
142
-			if (is_object($stmt)) {
143
-				$message = 'Expected a prepared statement or array got ' . get_class($stmt);
144
-			} else {
145
-				$message = 'Expected a prepared statement or array got ' . gettype($stmt);
146
-			}
147
-			throw new \OC\DatabaseException($message);
148
-		}
149
-		return $result;
150
-	}
103
+    /**
104
+     * execute a prepared statement, on error write log and throw exception
105
+     * @param mixed $stmt OC_DB_StatementWrapper,
106
+     *					  an array with 'sql' and optionally 'limit' and 'offset' keys
107
+     *					.. or a simple sql query string
108
+     * @param array $parameters
109
+     * @return OC_DB_StatementWrapper
110
+     * @throws \OC\DatabaseException
111
+     */
112
+    static public function executeAudited( $stmt, array $parameters = []) {
113
+        if (is_string($stmt)) {
114
+            // convert to an array with 'sql'
115
+            if (stripos($stmt, 'LIMIT') !== false) { //OFFSET requires LIMIT, so we only need to check for LIMIT
116
+                // TODO try to convert LIMIT OFFSET notation to parameters
117
+                $message = 'LIMIT and OFFSET are forbidden for portability reasons,'
118
+                            . ' pass an array with \'limit\' and \'offset\' instead';
119
+                throw new \OC\DatabaseException($message);
120
+            }
121
+            $stmt = array('sql' => $stmt, 'limit' => null, 'offset' => null);
122
+        }
123
+        if (is_array($stmt)) {
124
+            // convert to prepared statement
125
+            if ( ! array_key_exists('sql', $stmt) ) {
126
+                $message = 'statement array must at least contain key \'sql\'';
127
+                throw new \OC\DatabaseException($message);
128
+            }
129
+            if ( ! array_key_exists('limit', $stmt) ) {
130
+                $stmt['limit'] = null;
131
+            }
132
+            if ( ! array_key_exists('limit', $stmt) ) {
133
+                $stmt['offset'] = null;
134
+            }
135
+            $stmt = self::prepare($stmt['sql'], $stmt['limit'], $stmt['offset']);
136
+        }
137
+        self::raiseExceptionOnError($stmt, 'Could not prepare statement');
138
+        if ($stmt instanceof OC_DB_StatementWrapper) {
139
+            $result = $stmt->execute($parameters);
140
+            self::raiseExceptionOnError($result, 'Could not execute statement');
141
+        } else {
142
+            if (is_object($stmt)) {
143
+                $message = 'Expected a prepared statement or array got ' . get_class($stmt);
144
+            } else {
145
+                $message = 'Expected a prepared statement or array got ' . gettype($stmt);
146
+            }
147
+            throw new \OC\DatabaseException($message);
148
+        }
149
+        return $result;
150
+    }
151 151
 
152
-	/**
153
-	 * saves database schema to xml file
154
-	 * @param string $file name of file
155
-	 * @return bool
156
-	 *
157
-	 * TODO: write more documentation
158
-	 */
159
-	public static function getDbStructure($file) {
160
-		$schemaManager = self::getMDB2SchemaManager();
161
-		return $schemaManager->getDbStructure($file);
162
-	}
152
+    /**
153
+     * saves database schema to xml file
154
+     * @param string $file name of file
155
+     * @return bool
156
+     *
157
+     * TODO: write more documentation
158
+     */
159
+    public static function getDbStructure($file) {
160
+        $schemaManager = self::getMDB2SchemaManager();
161
+        return $schemaManager->getDbStructure($file);
162
+    }
163 163
 
164
-	/**
165
-	 * Creates tables from XML file
166
-	 * @param string $file file to read structure from
167
-	 * @return bool
168
-	 *
169
-	 * TODO: write more documentation
170
-	 */
171
-	public static function createDbFromStructure( $file ) {
172
-		$schemaManager = self::getMDB2SchemaManager();
173
-		$result = $schemaManager->createDbFromStructure($file);
174
-		return $result;
175
-	}
164
+    /**
165
+     * Creates tables from XML file
166
+     * @param string $file file to read structure from
167
+     * @return bool
168
+     *
169
+     * TODO: write more documentation
170
+     */
171
+    public static function createDbFromStructure( $file ) {
172
+        $schemaManager = self::getMDB2SchemaManager();
173
+        $result = $schemaManager->createDbFromStructure($file);
174
+        return $result;
175
+    }
176 176
 
177
-	/**
178
-	 * update the database schema
179
-	 * @param string $file file to read structure from
180
-	 * @throws Exception
181
-	 * @return string|boolean
182
-	 * @suppress PhanDeprecatedFunction
183
-	 */
184
-	public static function updateDbFromStructure($file) {
185
-		$schemaManager = self::getMDB2SchemaManager();
186
-		try {
187
-			$result = $schemaManager->updateDbFromStructure($file);
188
-		} catch (Exception $e) {
189
-			\OCP\Util::writeLog('core', 'Failed to update database structure ('.$e.')', \OCP\Util::FATAL);
190
-			throw $e;
191
-		}
192
-		return $result;
193
-	}
177
+    /**
178
+     * update the database schema
179
+     * @param string $file file to read structure from
180
+     * @throws Exception
181
+     * @return string|boolean
182
+     * @suppress PhanDeprecatedFunction
183
+     */
184
+    public static function updateDbFromStructure($file) {
185
+        $schemaManager = self::getMDB2SchemaManager();
186
+        try {
187
+            $result = $schemaManager->updateDbFromStructure($file);
188
+        } catch (Exception $e) {
189
+            \OCP\Util::writeLog('core', 'Failed to update database structure ('.$e.')', \OCP\Util::FATAL);
190
+            throw $e;
191
+        }
192
+        return $result;
193
+    }
194 194
 
195
-	/**
196
-	 * remove all tables defined in a database structure xml file
197
-	 * @param string $file the xml file describing the tables
198
-	 */
199
-	public static function removeDBStructure($file) {
200
-		$schemaManager = self::getMDB2SchemaManager();
201
-		$schemaManager->removeDBStructure($file);
202
-	}
195
+    /**
196
+     * remove all tables defined in a database structure xml file
197
+     * @param string $file the xml file describing the tables
198
+     */
199
+    public static function removeDBStructure($file) {
200
+        $schemaManager = self::getMDB2SchemaManager();
201
+        $schemaManager->removeDBStructure($file);
202
+    }
203 203
 
204
-	/**
205
-	 * check if a result is an error and throws an exception, works with \Doctrine\DBAL\DBALException
206
-	 * @param mixed $result
207
-	 * @param string $message
208
-	 * @return void
209
-	 * @throws \OC\DatabaseException
210
-	 */
211
-	public static function raiseExceptionOnError($result, $message = null) {
212
-		if($result === false) {
213
-			if ($message === null) {
214
-				$message = self::getErrorMessage();
215
-			} else {
216
-				$message .= ', Root cause:' . self::getErrorMessage();
217
-			}
218
-			throw new \OC\DatabaseException($message);
219
-		}
220
-	}
204
+    /**
205
+     * check if a result is an error and throws an exception, works with \Doctrine\DBAL\DBALException
206
+     * @param mixed $result
207
+     * @param string $message
208
+     * @return void
209
+     * @throws \OC\DatabaseException
210
+     */
211
+    public static function raiseExceptionOnError($result, $message = null) {
212
+        if($result === false) {
213
+            if ($message === null) {
214
+                $message = self::getErrorMessage();
215
+            } else {
216
+                $message .= ', Root cause:' . self::getErrorMessage();
217
+            }
218
+            throw new \OC\DatabaseException($message);
219
+        }
220
+    }
221 221
 
222
-	/**
223
-	 * returns the error code and message as a string for logging
224
-	 * works with DoctrineException
225
-	 * @return string
226
-	 */
227
-	public static function getErrorMessage() {
228
-		$connection = \OC::$server->getDatabaseConnection();
229
-		return $connection->getError();
230
-	}
222
+    /**
223
+     * returns the error code and message as a string for logging
224
+     * works with DoctrineException
225
+     * @return string
226
+     */
227
+    public static function getErrorMessage() {
228
+        $connection = \OC::$server->getDatabaseConnection();
229
+        return $connection->getError();
230
+    }
231 231
 
232
-	/**
233
-	 * Checks if a table exists in the database - the database prefix will be prepended
234
-	 *
235
-	 * @param string $table
236
-	 * @return bool
237
-	 * @throws \OC\DatabaseException
238
-	 */
239
-	public static function tableExists($table) {
240
-		$connection = \OC::$server->getDatabaseConnection();
241
-		return $connection->tableExists($table);
242
-	}
232
+    /**
233
+     * Checks if a table exists in the database - the database prefix will be prepended
234
+     *
235
+     * @param string $table
236
+     * @return bool
237
+     * @throws \OC\DatabaseException
238
+     */
239
+    public static function tableExists($table) {
240
+        $connection = \OC::$server->getDatabaseConnection();
241
+        return $connection->tableExists($table);
242
+    }
243 243
 }
Please login to merge, or discard this patch.
Spacing   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -54,7 +54,7 @@  discard block
 block discarded – undo
54 54
 	 *
55 55
 	 * SQL query via Doctrine prepare(), needs to be execute()'d!
56 56
 	 */
57
-	static public function prepare( $query , $limit = null, $offset = null, $isManipulation = null) {
57
+	static public function prepare($query, $limit = null, $offset = null, $isManipulation = null) {
58 58
 		$connection = \OC::$server->getDatabaseConnection();
59 59
 
60 60
 		if ($isManipulation === null) {
@@ -64,7 +64,7 @@  discard block
 block discarded – undo
64 64
 
65 65
 		// return the result
66 66
 		try {
67
-			$result =$connection->prepare($query, $limit, $offset);
67
+			$result = $connection->prepare($query, $limit, $offset);
68 68
 		} catch (\Doctrine\DBAL\DBALException $e) {
69 69
 			throw new \OC\DatabaseException($e->getMessage());
70 70
 		}
@@ -80,7 +80,7 @@  discard block
 block discarded – undo
80 80
 	 * @param string $sql
81 81
 	 * @return bool
82 82
 	 */
83
-	static public function isManipulation( $sql ) {
83
+	static public function isManipulation($sql) {
84 84
 		$selectOccurrence = stripos($sql, 'SELECT');
85 85
 		if ($selectOccurrence !== false && $selectOccurrence < 10) {
86 86
 			return false;
@@ -109,7 +109,7 @@  discard block
 block discarded – undo
109 109
 	 * @return OC_DB_StatementWrapper
110 110
 	 * @throws \OC\DatabaseException
111 111
 	 */
112
-	static public function executeAudited( $stmt, array $parameters = []) {
112
+	static public function executeAudited($stmt, array $parameters = []) {
113 113
 		if (is_string($stmt)) {
114 114
 			// convert to an array with 'sql'
115 115
 			if (stripos($stmt, 'LIMIT') !== false) { //OFFSET requires LIMIT, so we only need to check for LIMIT
@@ -122,14 +122,14 @@  discard block
 block discarded – undo
122 122
 		}
123 123
 		if (is_array($stmt)) {
124 124
 			// convert to prepared statement
125
-			if ( ! array_key_exists('sql', $stmt) ) {
125
+			if (!array_key_exists('sql', $stmt)) {
126 126
 				$message = 'statement array must at least contain key \'sql\'';
127 127
 				throw new \OC\DatabaseException($message);
128 128
 			}
129
-			if ( ! array_key_exists('limit', $stmt) ) {
129
+			if (!array_key_exists('limit', $stmt)) {
130 130
 				$stmt['limit'] = null;
131 131
 			}
132
-			if ( ! array_key_exists('limit', $stmt) ) {
132
+			if (!array_key_exists('limit', $stmt)) {
133 133
 				$stmt['offset'] = null;
134 134
 			}
135 135
 			$stmt = self::prepare($stmt['sql'], $stmt['limit'], $stmt['offset']);
@@ -140,9 +140,9 @@  discard block
 block discarded – undo
140 140
 			self::raiseExceptionOnError($result, 'Could not execute statement');
141 141
 		} else {
142 142
 			if (is_object($stmt)) {
143
-				$message = 'Expected a prepared statement or array got ' . get_class($stmt);
143
+				$message = 'Expected a prepared statement or array got '.get_class($stmt);
144 144
 			} else {
145
-				$message = 'Expected a prepared statement or array got ' . gettype($stmt);
145
+				$message = 'Expected a prepared statement or array got '.gettype($stmt);
146 146
 			}
147 147
 			throw new \OC\DatabaseException($message);
148 148
 		}
@@ -168,7 +168,7 @@  discard block
 block discarded – undo
168 168
 	 *
169 169
 	 * TODO: write more documentation
170 170
 	 */
171
-	public static function createDbFromStructure( $file ) {
171
+	public static function createDbFromStructure($file) {
172 172
 		$schemaManager = self::getMDB2SchemaManager();
173 173
 		$result = $schemaManager->createDbFromStructure($file);
174 174
 		return $result;
@@ -209,11 +209,11 @@  discard block
 block discarded – undo
209 209
 	 * @throws \OC\DatabaseException
210 210
 	 */
211 211
 	public static function raiseExceptionOnError($result, $message = null) {
212
-		if($result === false) {
212
+		if ($result === false) {
213 213
 			if ($message === null) {
214 214
 				$message = self::getErrorMessage();
215 215
 			} else {
216
-				$message .= ', Root cause:' . self::getErrorMessage();
216
+				$message .= ', Root cause:'.self::getErrorMessage();
217 217
 			}
218 218
 			throw new \OC\DatabaseException($message);
219 219
 		}
Please login to merge, or discard this patch.