-
Notifications
You must be signed in to change notification settings - Fork 503
/
AbstractMetadata.php
113 lines (86 loc) · 2.89 KB
/
AbstractMetadata.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
namespace Ushahidi\Tests\Integration\Bootstrap\Database\Metadata;
use PDO;
use ReflectionClass;
abstract class AbstractMetadata
{
protected static $metaDataClassMap = [
'mysql' => MySQL::class
];
protected $pdo;
protected $schema;
protected $schemaObjectQuoteChar = '"';
protected $truncateCommand = 'TRUNCATE';
public static function createMetaData(PDO $pdo, $schema = '')
{
$driverName = $pdo->getAttribute(PDO::ATTR_DRIVER_NAME);
if (isset(self::$metaDataClassMap[$driverName])) {
$className = self::$metaDataClassMap[$driverName];
if ($className instanceof ReflectionClass) {
return $className->newInstance($pdo, $schema);
}
return self::registerClassWithDriver($className, $driverName)->newInstance($pdo, $schema);
}
throw new \Exception("Could not find a meta data driver for {$driverName} pdo driver.");
}
public static function registerClassWithDriver($className, $pdoDriver)
{
if (!\class_exists($className)) {
throw new \Exception("Specified class for {$pdoDriver} driver ({$className}) does not exist.");
}
$reflection = new ReflectionClass($className);
if ($reflection->isSubclassOf(self::class)) {
return self::$metaDataClassMap[$pdoDriver] = $reflection;
}
throw new \Exception("Specified class for {$pdoDriver} driver ({$className}) does not extend PHPUnit_Extensions_Database_DB_MetaData.");
}
final public function __construct(PDO $pdo, $schema = '')
{
$this->pdo = $pdo;
$this->schema = $schema;
}
public function getSchema()
{
return $this->schema;
}
public function quoteSchemaObject($object)
{
$parts = \explode('.', $object);
$quotedParts = [];
foreach ($parts as $part) {
$quotedParts[] = $this->schemaObjectQuoteChar .
\str_replace($this->schemaObjectQuoteChar, $this->schemaObjectQuoteChar . $this->schemaObjectQuoteChar, $part) .
$this->schemaObjectQuoteChar;
}
return \implode('.', $quotedParts);
}
public function splitTableName($fullTableName)
{
if (($dot = \strpos($fullTableName, '.')) !== false) {
return [
'schema' => \substr($fullTableName, 0, $dot),
'table' => \substr($fullTableName, $dot + 1)
];
}
return [
'schema' => null,
'table' => $fullTableName
];
}
public function getTruncateCommand()
{
return $this->truncateCommand;
}
public function allowsCascading()
{
return false;
}
public function disablePrimaryKeys($tableName): void
{
return;
}
public function enablePrimaryKeys($tableName): void
{
return;
}
}