summaryrefslogtreecommitdiff
path: root/it_dbi_postgres.class
blob: e96a46de31493e25eeb6c81a03d47db5e002d224 (plain)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
/*
**	Copyright (C) 1995-2021 by the ITools Authors.
**	This file is part of ITools - the Internet Tools Library
**
**	ITools is free software; you can redistribute it and/or modify
**	it under the terms of the GNU General Public License as published by
**	the Free Software Foundation; either version 3 of the License, or
**	(at your option) any later version.
**
**	ITools is distributed in the hope that it will be useful,
**	but WITHOUT ANY WARRANTY; without even the implied warranty of
**	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**	GNU General Public License for more details.
**
**	You should have received a copy of the GNU General Public License
**	along with this program.  If not, see <http://www.gnu.org/licenses/>.
**
**	dbi.class - UltraFlexible Database Interface 3000
*/

class it_dbi_postgres extends it_dbi
{

static $_global_key = 'it_dbi_postgres'; # Override base class to get our own singleton

function _where($params)
{
	if ($params['LIMIT'] && ($m = it::match('^\s*(\d+)\s*,\s*(\d+)\s*$', $params['LIMIT'])))
	{
		unset($params['LIMIT']);
		$params[] = " OFFSET $m[0] LIMIT $m[1]";
	}

	return parent::_where($params);
}

function replace($tags = [])
{
	foreach (array_keys($this->_fields) as $k)
	{
		$escaped = $this->escape_name($k);
		$strings[] = $escaped . '= EXCLUDED.' . $escaped;
	}

	$this->insert(array_merge($tags, [' ON CONFLICT (' . $this->escape_name($this->_p['keyfield']) . ') DO UPDATE SET ' . implode(', ', $strings)]));
}

function _tables($p = array())
{
	for ($qr = $this->query('SELECT table_name FROM information_schema.tables ' . $this->_where(['table_catalog' => $this->_p['db'], 'table_schema' => 'public'], $p), $p); $row = $this->_fetch_assoc($qr);)
		$result[] = $row['table_name'];

	return (array)$result;
}

static function _convertbool($v)
{
	return $v == 't' ? 1 : 0;
}

function _get_field_defs()
{
	list($table_name, $table_schema) = array_reverse(explode('.', $this->_p['table'], 2));
	$where = $this->_where(['t.table_name' => $table_name, 't.table_catalog' => $this->_p['db']] + ($table_schema ? ['t.table_schema' => $table_schema] : []));

	# Recreate Key column of mysql show columns
	$res = $this->query('SELECT column_name,constraint_type,ordinal_position FROM information_schema.table_constraints AS t JOIN information_schema.key_column_usage USING (constraint_name, constraint_schema, constraint_catalog) ' . $where);
	while ($res && ($row = $this->_fetch_assoc($res)))
	{
		if ($row['constraint_type'] == 'PRIMARY KEY')
			$keys[$row['column_name']] = 'PRI';
		else if ($row['ordinal_position'] == 1)
			$keys[$row['column_name']] = $row['constraint_type'] == 'UNIQUE' ? 'UNI' : 'MUL';
	}

	$res = $this->query('SELECT * FROM information_schema.columns AS t ' . $where);
	while ($res && ($field = $this->_fetch_assoc($res)))
	{
		$result[$field['column_name']] = ['Field' => $field['column_name'], 'Type' => $field['data_type'], 'Extra' => it::match('^nextval\(', $field['column_default']) ? 'auto_increment' : '', 'Key' => $keys[$field['column_name']]];
		if ($field['data_type'] == 'boolean')
			$result[$field['column_name']] += ['_convertfunc' => 'it_dbi_postgres::_convertbool', '_escapefunc' => 'it_dbi::escape_bool'];
	}

	return $result;
}

function _escape_string($str)
{
	return pg_escape_literal($this->_link, $str);
}

function _escape_name($str)
{
	return pg_escape_identifier($this->_link, $str);
}

function _connect_db($p) {
	$result = @pg_connect("host=$p[server] user=$p[user] dbname=$p[db] password=$p[pw]", PGSQL_CONNECT_FORCE_NEW);

	if ($result)
	{
		# set charset used for this connection
		if ($p['charset'])
			if (!pg_set_client_encoding($result, $p['charset']))
				$this->_fatal("_connect(): can't set charset \"{$p['charset']}\"");
	}

	return [$result, $result ? '' : 'Could not connect'];
}

function __query($query, $p)
{
	if ($p['timeout'] || $this->_p['interruptible_queries'])
	{
		$starttime = microtime(true);
		pg_send_query($this->_link, $query);
		while (pg_connection_busy($this->_link)) {
			if ($p['timeout'] && (microtime(true) - $starttime) > $p['timeout'])
				return false;
			$read = $error = [pg_socket($this->_link)];
			$write = [];
			stream_select($read, $write, $error, 1, 0);
		}

		while ($newresult = @pg_get_result($this->_link))
			$result = $newresult;
		if ($result && pg_result_error($result))
			$result = false;
		return $result;
	}
	else
	{
		return @pg_query($this->_link, $query);
	}
}

function _query($query, $p)
{
	if ($this->_p['keyfield'] && it::match('^INSERT ', $query))
	{
		$isinsert = true;
		$query .= ' RETURNING ' . $this->_escape_name($this->_p['keyfield']);
	}

	if (!($result = $this->__query($query, $p)) && $p['safety'])
	{
		if (($p['safety'] < 2) && it::match('duplicate key value', pg_last_error($this->_link))) # Duplicate entry
			return null;

		/* TODO
		if ($errno == 2006) # mysql server has gone away: retry
		{
			it::log('sqllog', "it_dbi(): reconnecting mysqli_connect {$p['server']}, {$p['db']}");
			$this->_connect(array('reconnect' => true));
			$result = $this->__query($query, $p);
		}
		*/
	}

	if ($result)
	{
		$this->_affectedrows = pg_affected_rows($result);
		$this->_insertid = $isinsert ? $this->_fetch_assoc($result)[$this->_p['keyfield']] : 0;
		/* TODO probably PGSQl_NOTICE_ALL
		if (($warning = $this->_link->get_warnings()))
		{
			do {
				if (!it::match(trim($this->_p['ignored_warnings'] . "|1364|1261|1051|1062", "|"), $warning->errno))
					$messages[] = $warning->message . " [error $warning->errno]";
			} while ($warning->next() && ++$checked < 20);

			if ($messages)
				it::error(['title' => "Mysql warning: " . $messages[0], 'body' => "$query\n\n" . implode("\n", $messages) . "\n"]);
		}
		*/
	}

	return $result;
}

function _json_extract($col, $field)
{
	return "$col->>" . $this->escape_string($field);
}

function _json_object($tags)
{
	foreach ((array)$tags as $f => $v)
		$strings[] = $this->escape_string($f) . ', ' . $v;
	return "JSONB_BUILD_OBJECT(" . implode(', ', $strings) . ")";
}

function _json_set($source, $tags)
{
	return "$source || " . $this->_json_object($tags);
}

function _json_remove($source, $fields)
{
	foreach ((array)$fields as $f)
		$strings[] = $this->escape_string($f);
	return "($source - " . implode(" - ", $strings) . ")";
}

function _fetch_assoc($res)
{
	return pg_fetch_assoc($res);
}

function _num_rows($res)
{
	return pg_num_rows($res);
}


function _seek($res, $offset)
{
	pg_result_seek($res, $offset);
}

function _error($text)
{
	$text = get_class($this).'::'.$text;

	if ($this->_link && ($errstr = pg_last_error($this->_link)))
		$text = "\"$errstr\" in $text";

	return $text;
}

}