1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mindplay\sql\postgres; |
4
|
|
|
|
5
|
|
|
use mindplay\sql\model\Database; |
6
|
|
|
use mindplay\sql\model\DatabaseContainer; |
7
|
|
|
use mindplay\sql\model\DatabaseContainerFactory; |
8
|
|
|
use mindplay\sql\model\Driver; |
9
|
|
|
use mindplay\sql\model\schema\Table; |
10
|
|
|
use mindplay\sql\model\types\BoolType; |
11
|
|
|
use mindplay\sql\model\types\FloatType; |
12
|
|
|
use PDO; |
13
|
|
|
|
14
|
|
|
class PostgresDatabase extends Database implements Driver |
15
|
|
|
{ |
16
|
1 |
|
protected function bootstrap(DatabaseContainerFactory $factory): void |
17
|
|
|
{ |
18
|
1 |
|
$factory->set(Driver::class, $this); |
19
|
|
|
|
20
|
1 |
|
$factory->register(BoolType::class, function () { |
21
|
1 |
|
return BoolType::get(true, false); |
22
|
1 |
|
}); |
23
|
|
|
|
24
|
1 |
|
$factory->alias("scalar.boolean", BoolType::class); |
25
|
|
|
|
26
|
1 |
|
$factory->register("scalar.double", FloatType::class); |
27
|
|
|
} |
28
|
|
|
|
29
|
1 |
|
public function createConnection(PDO $pdo): PostgresConnection |
30
|
|
|
{ |
31
|
1 |
|
return $this->container->create(PostgresConnection::class, ['pdo' => $pdo]); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @inheritdoc |
36
|
|
|
*/ |
37
|
1 |
|
public function quoteName(string $name): string |
38
|
|
|
{ |
39
|
1 |
|
return '"' . $name . '"'; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @inheritdoc |
44
|
|
|
*/ |
45
|
1 |
|
public function quoteTableName(string|null $schema, string $table): string |
46
|
|
|
{ |
47
|
1 |
|
return $schema |
48
|
1 |
|
? '"' . $schema . '"."' . $table . '"' |
49
|
1 |
|
: '"' . $table . '"'; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param Table $from |
54
|
|
|
* |
55
|
|
|
* @return PostgresSelectQuery |
56
|
|
|
*/ |
57
|
|
|
public function select(Table $from) |
58
|
|
|
{ |
59
|
|
|
return $this->container->create(PostgresSelectQuery::class, [Table::class => $from]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param Table $into |
64
|
|
|
* |
65
|
|
|
* @return PostgresInsertQuery |
66
|
|
|
*/ |
67
|
1 |
|
public function insert(Table $into) |
68
|
|
|
{ |
69
|
1 |
|
return $this->container->create(PostgresInsertQuery::class, [Table::class => $into]); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param Table $table |
74
|
|
|
* |
75
|
|
|
* @return PostgresUpdateQuery |
76
|
|
|
*/ |
77
|
1 |
|
public function update(Table $table) |
78
|
|
|
{ |
79
|
1 |
|
return $this->container->create(PostgresUpdateQuery::class, [Table::class => $table]); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param Table $table |
84
|
|
|
* |
85
|
|
|
* @return PostgresDeleteQuery |
86
|
|
|
*/ |
87
|
1 |
|
public function delete(Table $table) |
88
|
|
|
{ |
89
|
1 |
|
return $this->container->create(PostgresDeleteQuery::class, [Table::class => $table]); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|