-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathfunctions.php
134 lines (114 loc) · 3.5 KB
/
functions.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
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
<?php
namespace Amp\Postgres;
use Amp\Loop;
use Amp\Promise;
use Amp\Sql\Common\ConnectionPool;
use Amp\Sql\ConnectionConfig as SqlConnectionConfig;
use Amp\Sql\Connector;
const LOOP_CONNECTOR_IDENTIFIER = Connector::class . "\\Postgres";
function connector(?Connector $connector = null): Connector
{
if ($connector === null) {
$connector = Loop::getState(LOOP_CONNECTOR_IDENTIFIER);
if ($connector) {
return $connector;
}
$connector = new TimeoutConnector;
}
Loop::setState(LOOP_CONNECTOR_IDENTIFIER, $connector);
return $connector;
}
/**
* Create a connection using the global Connector instance.
*
* @param SqlConnectionConfig $config
*
* @return Promise<Connection>
*
* @throws \Amp\Sql\FailureException If connecting fails.
*
* @throws \Error If neither ext-pgsql or pecl-pq is loaded.
*
* @codeCoverageIgnore
*/
function connect(SqlConnectionConfig $config): Promise
{
return connector()->connect($config);
}
/**
* Create a pool using the global Connector instance.
*
* @param SqlConnectionConfig $config
* @param int $maxConnections
* @param int $idleTimeout
* @param bool $resetConnections
*
* @return Pool
*/
function pool(
SqlConnectionConfig $config,
int $maxConnections = ConnectionPool::DEFAULT_MAX_CONNECTIONS,
int $idleTimeout = ConnectionPool::DEFAULT_IDLE_TIMEOUT,
bool $resetConnections = true
): Pool {
return new Pool($config, $maxConnections, $idleTimeout, $resetConnections, connector());
}
/**
* Casts a PHP value to a representation that is understood by Postgres, including encoding arrays.
*
* @param mixed $value
*
* @return string|int|float|null
*
* @throws \Error If $value is an object without a __toString() method, a resource, or an unknown type.
*/
function cast($value)
{
switch ($type = \gettype($value)) {
case "NULL":
case "integer":
case "double":
case "string":
return $value; // No casting necessary for numerics, strings, and null.
case "boolean":
return $value ? 't' : 'f';
case "array":
return encode($value);
case "object":
if (!\method_exists($value, "__toString")) {
throw new \Error("Object without a __toString() method included in parameter values");
}
return (string) $value;
default:
throw new \Error("Invalid value type '$type' in parameter values");
}
}
/**
* Encodes an array into a PostgreSQL representation of the array.
*
* @param array $array
*
* @return string The serialized representation of the array.
*
* @throws \Error If $array contains an object without a __toString() method, a resource, or an unknown type.
*/
function encode(array $array): string
{
$array = \array_map(function ($value) {
switch (\gettype($value)) {
case "NULL":
return "NULL";
case "object":
if (!\method_exists($value, "__toString")) {
throw new \Error("Object without a __toString() method in array");
}
$value = (string) $value;
// no break
case "string":
return '"' . \str_replace(['\\', '"'], ['\\\\', '\\"'], $value) . '"';
default:
return cast($value); // Recursively encodes arrays and errors on invalid values.
}
}, $array);
return '{' . \implode(',', $array) . '}';
}