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