-
Notifications
You must be signed in to change notification settings - Fork 638
/
PdoHelper.php
49 lines (47 loc) · 1.57 KB
/
PdoHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
namespace TYPO3\CMS\Core\Database;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
/**
* A helper class for handling PDO databases
*/
class PdoHelper
{
/**
* Pumps the SQL into the database. Use for DDL only.
*
* Important: key definitions with length specifiers (needed for MySQL) must
* be given as "field"(xyz) - no space between double quote and parenthesis -
* so they can be removed automatically.
*
* @param \PDO $databaseHandle
* @param string $pdoDriver
* @param string $pathAndFilename
*/
public static function importSql(\PDO $databaseHandle, $pdoDriver, $pathAndFilename)
{
$sql = file($pathAndFilename, FILE_IGNORE_NEW_LINES & FILE_SKIP_EMPTY_LINES);
// Remove MySQL style key length delimiters (yuck!) if we are not setting up a MySQL db
if (substr($pdoDriver, 0, 5) !== 'mysql') {
$sql = preg_replace('/"\\([0-9]+\\)/', '"', $sql);
}
$statement = '';
foreach ($sql as $line) {
$statement .= ' ' . trim($line);
if (substr($statement, -1) === ';') {
$databaseHandle->exec($statement);
$statement = '';
}
}
}
}