|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ORM\Dbal; |
|
4
|
|
|
|
|
5
|
|
|
use ORM\Entity; |
|
6
|
|
|
use ORM\Exception; |
|
7
|
|
|
use ORM\QueryBuilder\QueryBuilder; |
|
8
|
|
|
use PDO; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Database abstraction for PostgreSQL databases |
|
12
|
|
|
* |
|
13
|
|
|
* @package ORM\Dbal |
|
14
|
|
|
* @author Thomas Flori <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class Pgsql extends Dbal |
|
17
|
|
|
{ |
|
18
|
|
|
protected static $typeMapping = [ |
|
19
|
|
|
'integer' => Type\Number::class, |
|
20
|
|
|
'smallint' => Type\Number::class, |
|
21
|
|
|
'bigint' => Type\Number::class, |
|
22
|
|
|
'numeric' => Type\Number::class, |
|
23
|
|
|
'real' => Type\Number::class, |
|
24
|
|
|
'double precision' => Type\Number::class, |
|
25
|
|
|
'money' => Type\Number::class, |
|
26
|
|
|
|
|
27
|
|
|
'character varying' => Type\VarChar::class, |
|
28
|
|
|
'character' => Type\VarChar::class, |
|
29
|
|
|
|
|
30
|
|
|
'text' => Type\Text::class, |
|
31
|
|
|
|
|
32
|
|
|
'date' => Type\DateTime::class, |
|
33
|
|
|
'timestamp without time zone' => Type\DateTime::class, |
|
34
|
|
|
'timestamp with time zone' => Type\DateTime::class, |
|
35
|
|
|
'time without time zone' => Type\Time::class, |
|
36
|
|
|
'time with time zone' => Type\Time::class, |
|
37
|
|
|
|
|
38
|
|
|
'json' => Type\Json::class, |
|
39
|
|
|
'boolean' => Type\Boolean::class, |
|
40
|
|
|
]; |
|
41
|
|
|
|
|
42
|
|
|
protected $booleanTrue = 'true'; |
|
43
|
|
|
protected $booleanFalse = 'false'; |
|
44
|
|
|
|
|
45
|
2 |
|
public function insertAndSyncWithAutoInc(Entity ...$entities) |
|
46
|
|
|
{ |
|
47
|
2 |
|
if (count($entities) === 0) { |
|
48
|
2 |
|
return false; |
|
49
|
|
|
} |
|
50
|
2 |
|
static::assertSameType($entities); |
|
51
|
1 |
|
|
|
52
|
1 |
|
$entity = reset($entities); |
|
53
|
1 |
|
$pdo = $this->entityManager->getConnection(); |
|
54
|
|
|
$col = $entity::getColumnName($entity::getPrimaryKeyVars()[0]); |
|
55
|
1 |
|
$result = $pdo->query($this->buildInsertStatement(...$entities) . ' RETURNING ' . $col); |
|
56
|
|
|
while ($id = $result->fetchColumn()) { |
|
57
|
|
|
$this->updateAutoincrement($entity, $id); |
|
58
|
2 |
|
$entity = next($entity); |
|
59
|
|
|
} |
|
60
|
|
|
$this->syncInserted(...$entities); |
|
61
|
26 |
|
return true; |
|
62
|
|
|
} |
|
63
|
26 |
|
|
|
64
|
26 |
|
public function describe($schemaTable) |
|
65
|
|
|
{ |
|
66
|
26 |
|
$table = explode($this->identifierDivider, $schemaTable); |
|
67
|
26 |
|
list($schema, $table) = count($table) === 2 ? $table : [ 'public', $table[0] ]; |
|
68
|
26 |
|
|
|
69
|
26 |
|
$query = new QueryBuilder('INFORMATION_SCHEMA.COLUMNS', '', $this->entityManager); |
|
70
|
|
|
$query->where('table_name', $table)->andWhere('table_schema', $schema); |
|
71
|
|
|
$query->columns([ |
|
72
|
|
|
'column_name', 'column_default', 'data_type', 'is_nullable', 'character_maximum_length', |
|
73
|
26 |
|
'datetime_precision' |
|
74
|
26 |
|
]); |
|
75
|
26 |
|
|
|
76
|
2 |
|
$result = $this->entityManager->getConnection()->query($query->getQuery()); |
|
77
|
|
|
$rawColumns = $result->fetchAll(PDO::FETCH_ASSOC); |
|
78
|
|
|
if (count($rawColumns) === 0) { |
|
79
|
24 |
|
throw new Exception('Unknown table ' . $schemaTable); |
|
80
|
24 |
|
} |
|
81
|
24 |
|
|
|
82
|
17 |
|
$cols = array_map( |
|
83
|
|
|
function ($columnDefinition) { |
|
84
|
24 |
|
if (isset(static::$typeMapping[$columnDefinition['data_type']])) { |
|
85
|
24 |
|
$columnDefinition['type'] = static::$typeMapping[$columnDefinition['data_type']]; |
|
86
|
|
|
} |
|
87
|
|
|
return new Column($this, $columnDefinition); |
|
88
|
|
|
}, |
|
89
|
|
|
$rawColumns |
|
90
|
|
|
); |
|
91
|
|
|
|
|
92
|
|
|
return new Table($cols); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|