-
Notifications
You must be signed in to change notification settings - Fork 503
/
Dataset.php
83 lines (63 loc) · 2.52 KB
/
Dataset.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
<?php
namespace Ushahidi\Tests\Integration\Bootstrap\Database;
use Ushahidi\Tests\Integration\Bootstrap\Database\Dataset\AbstractDataset;
use Ushahidi\Tests\Integration\Bootstrap\Database\Dataset\DefaultTableMetadata;
class Dataset extends AbstractDataset
{
protected $tables = [];
protected $databaseConnection;
public static function buildTableSelect($tableMetaData, $databaseConnection = null)
{
if ($tableMetaData->getTableName() == '') {
$e = new \Exception('Empty Table Name');
print $e->getTraceAsString();
throw $e;
}
$columns = $tableMetaData->getColumns();
if ($databaseConnection) {
$columns = \array_map([$databaseConnection, 'quoteSchemaObject'], $columns);
}
$columnList = \implode(', ', $columns);
if ($databaseConnection) {
$tableName = $databaseConnection->quoteSchemaObject($tableMetaData->getTableName());
} else {
$tableName = $tableMetaData->getTableName();
}
$primaryKeys = $tableMetaData->getPrimaryKeys();
if ($databaseConnection) {
$primaryKeys = \array_map([$databaseConnection, 'quoteSchemaObject'], $primaryKeys);
}
if (\count($primaryKeys)) {
$orderBy = 'ORDER BY ' . \implode(' ASC, ', $primaryKeys) . ' ASC';
} else {
$orderBy = '';
}
return "SELECT {$columnList} FROM {$tableName} {$orderBy}";
}
public function __construct($databaseConnection)
{
$this->databaseConnection = $databaseConnection;
}
public function getTable($tableName)
{
if (!\in_array($tableName, $this->getTableNames())) {
throw new \Exception("$tableName is not a table in the current database.");
}
if (empty($this->tables[$tableName])) {
$this->tables[$tableName] = new Table($this->getTableMetaData($tableName), $this->databaseConnection);
}
return $this->tables[$tableName];
}
public function getTableMetaData($tableName)
{
return new DefaultTableMetadata($tableName, $this->databaseConnection->getMetaData()->getTableColumns($tableName), $this->databaseConnection->getMetaData()->getTablePrimaryKeys($tableName));
}
public function getTableNames()
{
return $this->databaseConnection->getMetaData()->getTableNames();
}
protected function createIterator($reverse = false)
{
return new TableIterator($this->getTableNames(), $this, $reverse);
}
}