-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathPgsqlAdapter.php
More file actions
157 lines (135 loc) · 4.33 KB
/
PgsqlAdapter.php
File metadata and controls
157 lines (135 loc) · 4.33 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/**
* @package ActiveRecord
*/
namespace ActiveRecord\Adapter;
use ActiveRecord\Column;
use ActiveRecord\Connection;
use ActiveRecord\Inflector;
/**
* Adapter for Postgres (not completed yet)
*/
class PgsqlAdapter extends Connection
{
public static string $QUOTE_CHARACTER = '"';
public static int $DEFAULT_PORT = 5432;
public function supports_sequences(): bool
{
return true;
}
public function get_sequence_name(string $table, string $column_name): string
{
return "{$table}_{$column_name}_seq";
}
public function next_sequence_value(string $sequence_name): ?string
{
return "nextval('" . str_replace("'", "\\'", $sequence_name) . "')";
}
public function limit(string $sql, int $offset = 0, int $limit = 0): string
{
return $sql . ' LIMIT ' . $limit . ' OFFSET ' . $offset;
}
public function query_column_info(string $table): \PDOStatement
{
$sql = <<<SQL
SELECT
a.attname AS field,
a.attlen,
REPLACE(pg_catalog.format_type(a.atttypid, a.atttypmod), 'character varying', 'varchar') AS type,
a.attnotnull AS not_nullable,
(SELECT 't'
FROM pg_index
WHERE c.oid = pg_index.indrelid
AND a.attnum = ANY (pg_index.indkey)
AND pg_index.indisprimary = 't'
) IS NOT NULL AS pk,
REGEXP_REPLACE(
REGEXP_REPLACE(
REGEXP_REPLACE(
pg_get_expr(pg_attrdef.adbin, pg_attrdef.adrelid),
'::[a-z_ ]+', ''
),
'''$',''
),
'^''',''
) AS "default"
FROM pg_attribute a
JOIN pg_class c ON a.attrelid = c.oid
JOIN pg_type t ON a.atttypid = t.oid
LEFT JOIN pg_attrdef ON c.oid = pg_attrdef.adrelid AND a.attnum = pg_attrdef.adnum
WHERE c.relname = ?
AND a.attnum > 0
ORDER BY a.attnum;
SQL;
$values = [$table];
return $this->query($sql, $values);
}
public function query_for_tables(): \PDOStatement
{
return $this->query("SELECT tablename FROM pg_tables WHERE schemaname NOT IN('information_schema','pg_catalog')");
}
/**
* @param array{
* field: string,
* attlen: int,
* type: string,
* not_nullable: bool,
* pk: bool,
* default: string
* } $column
*/
public function create_column(array $column): Column
{
$c = new Column();
$c->inflected_name = Inflector::variablize($column['field']);
$c->name = $column['field'];
$c->nullable = ($column['not_nullable'] ? false : true);
$c->pk = ($column['pk'] ? true : false);
$c->auto_increment = false;
if ('timestamp' == substr($column['type'], 0, 9)) {
$c->raw_type = 'datetime';
$c->length = 19;
} elseif ('date' == $column['type']) {
$c->raw_type = 'date';
$c->length = 10;
} else {
preg_match('/^([A-Za-z0-9_]+)(\(([0-9]+(,[0-9]+)?)\))?/', $column['type'], $matches);
$c->raw_type = (count($matches) > 0 ? $matches[1] : $column['type']);
$c->length = count($matches) >= 4 ? intval($matches[3]) : intval($column['attlen']);
if ($c->length < 0) {
$c->length = null;
}
}
$c->map_raw_type();
if ($column['default']) {
preg_match("/^nextval\('(.*)'\)$/", $column['default'], $matches);
if (2 == count($matches)) {
$c->sequence = $matches[1];
} else {
$c->default = $c->cast($column['default'], $this);
}
}
return $c;
}
public function set_encoding(string $charset): void
{
$this->query("SET NAMES '$charset'");
}
/**
* @see Connection::escapeColumns()
*
* @param string $expression The where clause to be escaped
* @param list<string> $columns The columns of the table
*/
public function escapeColumns(string $expression, array $columns): string
{
static $quotedNames = [];
foreach ($columns as $column) {
if ($column !== strtolower($column)) {
$quotedNames[$column] ??= $this->quote_name($column);
$expression = str_replace($column, $this->quote_name($column), $expression);
}
}
return $expression;
}
}