| 1 |
<?php |
| 2 |
|
| 3 |
namespace Safe; |
| 4 |
|
| 5 |
use Safe\Exceptions\PgsqlException; |
| 6 |
|
| 7 |
/** |
| 8 |
* pg_cancel_query cancels an asynchronous query sent with |
| 9 |
* pg_send_query, pg_send_query_params |
| 10 |
* or pg_send_execute. You cannot cancel a query executed using |
| 11 |
* pg_query. |
| 12 |
* |
| 13 |
* @param resource $connection PostgreSQL database connection resource. |
| 14 |
* @throws PgsqlException |
| 15 |
* |
| 16 |
*/ |
| 17 |
function pg_cancel_query($connection): void |
| 18 |
{ |
| 19 |
error_clear_last(); |
| 20 |
$result = \pg_cancel_query($connection); |
| 21 |
if ($result === false) { |
| 22 |
throw PgsqlException::createFromPhpError(); |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
/** |
| 28 |
* PostgreSQL supports automatic character set conversion between |
| 29 |
* server and client for certain character sets. |
| 30 |
* pg_client_encoding returns the client |
| 31 |
* encoding as a string. The returned string will be one of the |
| 32 |
* standard PostgreSQL encoding identifiers. |
| 33 |
* |
| 34 |
* @param resource $connection PostgreSQL database connection resource. When |
| 35 |
* connection is not present, the default connection |
| 36 |
* is used. The default connection is the last connection made by |
| 37 |
* pg_connect or pg_pconnect. |
| 38 |
* @return string The client encoding. |
| 39 |
* @throws PgsqlException |
| 40 |
* |
| 41 |
*/ |
| 42 |
function pg_client_encoding($connection = null): string |
| 43 |
{ |
| 44 |
error_clear_last(); |
| 45 |
if ($connection !== null) { |
| 46 |
$result = \pg_client_encoding($connection); |
| 47 |
} else { |
| 48 |
$result = \pg_client_encoding(); |
| 49 |
} |
| 50 |
if ($result === false) { |
| 51 |
throw PgsqlException::createFromPhpError(); |
| 52 |
} |
| 53 |
return $result; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
/** |
| 58 |
* pg_close closes the non-persistent |
| 59 |
* connection to a PostgreSQL database associated with the given |
| 60 |
* connection resource. |
| 61 |
* |
| 62 |
* If there is open large object resource on the connection, do not |
| 63 |
* close the connection before closing all large object resources. |
| 64 |
* |
| 65 |
* @param resource $connection PostgreSQL database connection resource. When |
| 66 |
* connection is not present, the default connection |
| 67 |
* is used. The default connection is the last connection made by |
| 68 |
* pg_connect or pg_pconnect. |
| 69 |
* @throws PgsqlException |
| 70 |
* |
| 71 |
*/ |
| 72 |
function pg_close($connection = null): void |
| 73 |
{ |
| 74 |
error_clear_last(); |
| 75 |
if ($connection !== null) { |
| 76 |
$result = \pg_close($connection); |
| 77 |
} else { |
| 78 |
$result = \pg_close(); |
| 79 |
} |
| 80 |
if ($result === false) { |
| 81 |
throw PgsqlException::createFromPhpError(); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
/** |
| 87 |
* pg_connect opens a connection to a |
| 88 |
* PostgreSQL database specified by the |
| 89 |
* connection_string. |
| 90 |
* |
| 91 |
* If a second call is made to pg_connect with |
| 92 |
* the same connection_string as an existing connection, the |
| 93 |
* existing connection will be returned unless you pass |
| 94 |
* PGSQL_CONNECT_FORCE_NEW as |
| 95 |
* connect_type. |
| 96 |
* |
| 97 |
* The old syntax with multiple parameters |
| 98 |
* $conn = pg_connect("host", "port", "options", "tty", "dbname") |
| 99 |
* has been deprecated. |
| 100 |
* |
| 101 |
* @param string $connection_string The connection_string can be empty to use all default parameters, or it |
| 102 |
* can contain one or more parameter settings separated by whitespace. |
| 103 |
* Each parameter setting is in the form keyword = value. Spaces around |
| 104 |
* the equal sign are optional. To write an empty value or a value |
| 105 |
* containing spaces, surround it with single quotes, e.g., keyword = |
| 106 |
* 'a value'. Single quotes and backslashes within the value must be |
| 107 |
* escaped with a backslash, i.e., \' and \\. |
| 108 |
* |
| 109 |
* The currently recognized parameter keywords are: |
| 110 |
* host, hostaddr, port, |
| 111 |
* dbname (defaults to value of user), |
| 112 |
* user, |
| 113 |
* password, connect_timeout, |
| 114 |
* options, tty (ignored), sslmode, |
| 115 |
* requiressl (deprecated in favor of sslmode), and |
| 116 |
* service. Which of these arguments exist depends |
| 117 |
* on your PostgreSQL version. |
| 118 |
* |
| 119 |
* The options parameter can be used to set command line parameters |
| 120 |
* to be invoked by the server. |
| 121 |
* @param int|null $connect_type If PGSQL_CONNECT_FORCE_NEW is passed, then a new connection |
| 122 |
* is created, even if the connection_string is identical to |
| 123 |
* an existing connection. |
| 124 |
* |
| 125 |
* If PGSQL_CONNECT_ASYNC is given, then the |
| 126 |
* connection is established asynchronously. The state of the connection |
| 127 |
* can then be checked via pg_connect_poll or |
| 128 |
* pg_connection_status. |
| 129 |
* |
| 130 |
* @return resource PostgreSQL connection resource on success, FALSE on failure. |
| 131 |
* @throws PgsqlException |
| 132 |
* |
| 133 |
*/ |
| 134 |
function pg_connect(string $connection_string, ?int $connect_type = null) |
| 135 |
{ |
| 136 |
error_clear_last(); |
| 137 |
if ($connect_type !== null) { |
| 138 |
$result = \pg_connect($connection_string, $connect_type); |
| 139 |
} else { |
| 140 |
$result = \pg_connect($connection_string); |
| 141 |
} |
| 142 |
if ($result === false) { |
| 143 |
throw PgsqlException::createFromPhpError(); |
| 144 |
} |
| 145 |
return $result; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
/** |
| 150 |
* pg_connection_reset resets the connection. |
| 151 |
* It is useful for error recovery. |
| 152 |
* |
| 153 |
* @param resource $connection PostgreSQL database connection resource. |
| 154 |
* @throws PgsqlException |
| 155 |
* |
| 156 |
*/ |
| 157 |
function pg_connection_reset($connection): void |
| 158 |
{ |
| 159 |
error_clear_last(); |
| 160 |
$result = \pg_connection_reset($connection); |
| 161 |
if ($result === false) { |
| 162 |
throw PgsqlException::createFromPhpError(); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
/** |
| 168 |
* pg_convert checks and converts the values in |
| 169 |
* assoc_array into suitable values for use in an SQL |
| 170 |
* statement. Precondition for pg_convert is the |
| 171 |
* existence of a table table_name which has at least |
| 172 |
* as many columns as assoc_array has elements. The |
| 173 |
* fieldnames in table_name must match the indices in |
| 174 |
* assoc_array and the corresponding datatypes must be |
| 175 |
* compatible. Returns an array with the converted values on success, FALSE |
| 176 |
* otherwise. |
| 177 |
* |
| 178 |
* @param resource $connection PostgreSQL database connection resource. |
| 179 |
* @param string $table_name Name of the table against which to convert types. |
| 180 |
* @param array $assoc_array Data to be converted. |
| 181 |
* @param int $options Any number of PGSQL_CONV_IGNORE_DEFAULT, |
| 182 |
* PGSQL_CONV_FORCE_NULL or |
| 183 |
* PGSQL_CONV_IGNORE_NOT_NULL, combined. |
| 184 |
* @return array An array of converted values. |
| 185 |
* @throws PgsqlException |
| 186 |
* |
| 187 |
*/ |
| 188 |
function pg_convert($connection, string $table_name, array $assoc_array, int $options = 0): array |
| 189 |
{ |
| 190 |
error_clear_last(); |
| 191 |
$result = \pg_convert($connection, $table_name, $assoc_array, $options); |
| 192 |
if ($result === false) { |
| 193 |
throw PgsqlException::createFromPhpError(); |
| 194 |
} |
| 195 |
return $result; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
/** |
| 200 |
* pg_copy_from inserts records into a table from |
| 201 |
* rows. It issues a COPY FROM SQL command |
| 202 |
* internally to insert records. |
| 203 |
* |
| 204 |
* @param resource $connection PostgreSQL database connection resource. |
| 205 |
* @param string $table_name Name of the table into which to copy the rows. |
| 206 |
* @param array $rows An array of data to be copied into table_name. |
| 207 |
* Each value in rows becomes a row in table_name. |
| 208 |
* Each value in rows should be a delimited string of the values |
| 209 |
* to insert into each field. Values should be linefeed terminated. |
| 210 |
* @param string|null $delimiter The token that separates values for each field in each element of |
| 211 |
* rows. Default is TAB. |
| 212 |
* @param string|null $null_as How SQL NULL values are represented in the |
| 213 |
* rows. Default is \N ("\\N"). |
| 214 |
* |
| 215 |
* @throws PgsqlException |
| 216 |
* |
| 217 |
*/ |
| 218 |
function pg_copy_from($connection, string $table_name, array $rows, ?string $delimiter = null, ?string $null_as = null): void |
| 219 |
{ |
| 220 |
error_clear_last(); |
| 221 |
if ($null_as !== null) { |
| 222 |
$result = \pg_copy_from($connection, $table_name, $rows, $delimiter, $null_as); |
| 223 |
} elseif ($delimiter !== null) { |
| 224 |
$result = \pg_copy_from($connection, $table_name, $rows, $delimiter); |
| 225 |
} else { |
| 226 |
$result = \pg_copy_from($connection, $table_name, $rows); |
| 227 |
} |
| 228 |
if ($result === false) { |
| 229 |
throw PgsqlException::createFromPhpError(); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
/** |
| 235 |
* pg_copy_to copies a table to an array. It |
| 236 |
* issues COPY TO SQL command internally to |
| 237 |
* retrieve records. |
| 238 |
* |
| 239 |
* @param resource $connection PostgreSQL database connection resource. |
| 240 |
* @param string $table_name Name of the table from which to copy the data into rows. |
| 241 |
* @param string|null $delimiter The token that separates values for each field in each element of |
| 242 |
* rows. Default is TAB. |
| 243 |
* @param string|null $null_as How SQL NULL values are represented in the |
| 244 |
* rows. Default is \N ("\\N"). |
| 245 |
* |
| 246 |
* @return array An array with one element for each line of COPY data. |
| 247 |
* It returns FALSE on failure. |
| 248 |
* @throws PgsqlException |
| 249 |
* |
| 250 |
*/ |
| 251 |
function pg_copy_to($connection, string $table_name, ?string $delimiter = null, ?string $null_as = null): array |
| 252 |
{ |
| 253 |
error_clear_last(); |
| 254 |
if ($null_as !== null) { |
| 255 |
$result = \pg_copy_to($connection, $table_name, $delimiter, $null_as); |
| 256 |
} elseif ($delimiter !== null) { |
| 257 |
$result = \pg_copy_to($connection, $table_name, $delimiter); |
| 258 |
} else { |
| 259 |
$result = \pg_copy_to($connection, $table_name); |
| 260 |
} |
| 261 |
if ($result === false) { |
| 262 |
throw PgsqlException::createFromPhpError(); |
| 263 |
} |
| 264 |
return $result; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
/** |
| 269 |
* pg_dbname returns the name of the database |
| 270 |
* that the given PostgreSQL connection |
| 271 |
* resource. |
| 272 |
* |
| 273 |
* @param resource $connection PostgreSQL database connection resource. When |
| 274 |
* connection is not present, the default connection |
| 275 |
* is used. The default connection is the last connection made by |
| 276 |
* pg_connect or pg_pconnect. |
| 277 |
* @return string A string containing the name of the database the |
| 278 |
* connection is to. |
| 279 |
* @throws PgsqlException |
| 280 |
* |
| 281 |
*/ |
| 282 |
function pg_dbname($connection = null): string |
| 283 |
{ |
| 284 |
error_clear_last(); |
| 285 |
if ($connection !== null) { |
| 286 |
$result = \pg_dbname($connection); |
| 287 |
} else { |
| 288 |
$result = \pg_dbname(); |
| 289 |
} |
| 290 |
if ($result === false) { |
| 291 |
throw PgsqlException::createFromPhpError(); |
| 292 |
} |
| 293 |
return $result; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
/** |
| 298 |
* pg_delete deletes records from a table |
| 299 |
* specified by the keys and values |
| 300 |
* in assoc_array. If options |
| 301 |
* is specified, pg_convert is applied |
| 302 |
* to assoc_array with the specified options. |
| 303 |
* |
| 304 |
* If options is specified, |
| 305 |
* pg_convert is applied to |
| 306 |
* assoc_array with the specified flags. |
| 307 |
* |
| 308 |
* By default pg_delete passes raw values. Values |
| 309 |
* must be escaped or PGSQL_DML_ESCAPE option must be |
| 310 |
* specified. PGSQL_DML_ESCAPE quotes and escapes |
| 311 |
* parameters/identifiers. Therefore, table/column names became case |
| 312 |
* sensitive. |
| 313 |
* |
| 314 |
* Note that neither escape nor prepared query can protect LIKE query, |
| 315 |
* JSON, Array, Regex, etc. These parameters should be handled |
| 316 |
* according to their contexts. i.e. Escape/validate values. |
| 317 |
* |
| 318 |
* @param resource $connection PostgreSQL database connection resource. |
| 319 |
* @param string $table_name Name of the table from which to delete rows. |
| 320 |
* @param array $assoc_array An array whose keys are field names in the table table_name, |
| 321 |
* and whose values are the values of those fields that are to be deleted. |
| 322 |
* @param int $options Any number of PGSQL_CONV_FORCE_NULL, |
| 323 |
* PGSQL_DML_NO_CONV, |
| 324 |
* PGSQL_DML_ESCAPE, |
| 325 |
* PGSQL_DML_EXEC, |
| 326 |
* PGSQL_DML_ASYNC or |
| 327 |
* PGSQL_DML_STRING combined. If PGSQL_DML_STRING is part of the |
| 328 |
* options then query string is returned. When PGSQL_DML_NO_CONV |
| 329 |
* or PGSQL_DML_ESCAPE is set, it does not call pg_convert internally. |
| 330 |
* @return mixed Returns TRUE on success. Returns string if PGSQL_DML_STRING is passed |
| 331 |
* via options. |
| 332 |
* @throws PgsqlException |
| 333 |
* |
| 334 |
*/ |
| 335 |
function pg_delete($connection, string $table_name, array $assoc_array, int $options = PGSQL_DML_EXEC) |
| 336 |
{ |
| 337 |
error_clear_last(); |
| 338 |
$result = \pg_delete($connection, $table_name, $assoc_array, $options); |
| 339 |
if ($result === false) { |
| 340 |
throw PgsqlException::createFromPhpError(); |
| 341 |
} |
| 342 |
return $result; |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
/** |
| 347 |
* pg_end_copy syncs the PostgreSQL frontend |
| 348 |
* (usually a web server process) with the PostgreSQL server after |
| 349 |
* doing a copy operation performed by |
| 350 |
* pg_put_line. pg_end_copy |
| 351 |
* must be issued, otherwise the PostgreSQL server may get out of |
| 352 |
* sync with the frontend and will report an error. |
| 353 |
* |
| 354 |
* @param resource $connection PostgreSQL database connection resource. When |
| 355 |
* connection is not present, the default connection |
| 356 |
* is used. The default connection is the last connection made by |
| 357 |
* pg_connect or pg_pconnect. |
| 358 |
* @throws PgsqlException |
| 359 |
* |
| 360 |
*/ |
| 361 |
function pg_end_copy($connection = null): void |
| 362 |
{ |
| 363 |
error_clear_last(); |
| 364 |
if ($connection !== null) { |
| 365 |
$result = \pg_end_copy($connection); |
| 366 |
} else { |
| 367 |
$result = \pg_end_copy(); |
| 368 |
} |
| 369 |
if ($result === false) { |
| 370 |
throw PgsqlException::createFromPhpError(); |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
/** |
| 376 |
* Sends a request to execute a prepared statement with given parameters, and |
| 377 |
* waits for the result. |
| 378 |
* |
| 379 |
* pg_execute is like pg_query_params, |
| 380 |
* but the command to be executed is |
| 381 |
* specified by naming a previously-prepared statement, instead of giving a |
| 382 |
* query string. This feature allows commands that will be used repeatedly to |
| 383 |
* be parsed and planned just once, rather than each time they are executed. |
| 384 |
* The statement must have been prepared previously in the current session. |
| 385 |
* pg_execute is supported only against PostgreSQL 7.4 or |
| 386 |
* higher connections; it will fail when using earlier versions. |
| 387 |
* |
| 388 |
* The parameters are identical to pg_query_params, except that the name of a |
| 389 |
* prepared statement is given instead of a query string. |
| 390 |
* |
| 391 |
* @param resource $connection PostgreSQL database connection resource. When |
| 392 |
* connection is not present, the default connection |
| 393 |
* is used. The default connection is the last connection made by |
| 394 |
* pg_connect or pg_pconnect. |
| 395 |
* @param string|null $stmtname The name of the prepared statement to execute. if |
| 396 |
* "" is specified, then the unnamed statement is executed. The name must have |
| 397 |
* been previously prepared using pg_prepare, |
| 398 |
* pg_send_prepare or a PREPARE SQL |
| 399 |
* command. |
| 400 |
* @param array|null $params An array of parameter values to substitute for the $1, $2, etc. placeholders |
| 401 |
* in the original prepared query string. The number of elements in the array |
| 402 |
* must match the number of placeholders. |
| 403 |
* |
| 404 |
* Elements are converted to strings by calling this function. |
| 405 |
* |
| 406 |
* @return resource A query result resource on success. |
| 407 |
* @throws PgsqlException |
| 408 |
* |
| 409 |
*/ |
| 410 |
function pg_execute($connection = null, ?string $stmtname = null, ?array $params = null) |
| 411 |
{ |
| 412 |
error_clear_last(); |
| 413 |
if ($params !== null) { |
| 414 |
$result = \pg_execute($connection, $stmtname, $params); |
| 415 |
} elseif ($stmtname !== null) { |
| 416 |
$result = \pg_execute($connection, $stmtname); |
| 417 |
} elseif ($connection !== null) { |
| 418 |
$result = \pg_execute($connection); |
| 419 |
} else { |
| 420 |
$result = \pg_execute(); |
| 421 |
} |
| 422 |
if ($result === false) { |
| 423 |
throw PgsqlException::createFromPhpError(); |
| 424 |
} |
| 425 |
return $result; |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
/** |
| 430 |
* pg_field_name returns the name of the field |
| 431 |
* occupying the given field_number in the |
| 432 |
* given PostgreSQL result resource. Field |
| 433 |
* numbering starts from 0. |
| 434 |
* |
| 435 |
* @param resource $result PostgreSQL query result resource, returned by pg_query, |
| 436 |
* pg_query_params or pg_execute |
| 437 |
* (among others). |
| 438 |
* @param int $field_number Field number, starting from 0. |
| 439 |
* @return string The field name. |
| 440 |
* @throws PgsqlException |
| 441 |
* |
| 442 |
*/ |
| 443 |
function pg_field_name($result, int $field_number): string |
| 444 |
{ |
| 445 |
error_clear_last(); |
| 446 |
$result = \pg_field_name($result, $field_number); |
| 447 |
if ($result === false) { |
| 448 |
throw PgsqlException::createFromPhpError(); |
| 449 |
} |
| 450 |
return $result; |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
/** |
| 455 |
* pg_field_table returns the name of the table that field |
| 456 |
* belongs to, or the table's oid if oid_only is TRUE. |
| 457 |
* |
| 458 |
* @param resource $result PostgreSQL query result resource, returned by pg_query, |
| 459 |
* pg_query_params or pg_execute |
| 460 |
* (among others). |
| 461 |
* @param int $field_number Field number, starting from 0. |
| 462 |
* @param bool $oid_only By default the tables name that field belongs to is returned but |
| 463 |
* if oid_only is set to TRUE, then the |
| 464 |
* oid will instead be returned. |
| 465 |
* @return mixed On success either the fields table name or oid. Or, FALSE on failure. |
| 466 |
* @throws PgsqlException |
| 467 |
* |
| 468 |
*/ |
| 469 |
function pg_field_table($result, int $field_number, bool $oid_only = false) |
| 470 |
{ |
| 471 |
error_clear_last(); |
| 472 |
$result = \pg_field_table($result, $field_number, $oid_only); |
| 473 |
if ($result === false) { |
| 474 |
throw PgsqlException::createFromPhpError(); |
| 475 |
} |
| 476 |
return $result; |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
/** |
| 481 |
* pg_field_type returns a string containing the |
| 482 |
* base type name of the given field_number in the |
| 483 |
* given PostgreSQL result resource. |
| 484 |
* |
| 485 |
* @param resource $result PostgreSQL query result resource, returned by pg_query, |
| 486 |
* pg_query_params or pg_execute |
| 487 |
* (among others). |
| 488 |
* @param int $field_number Field number, starting from 0. |
| 489 |
* @return string A string containing the base name of the field's type. |
| 490 |
* @throws PgsqlException |
| 491 |
* |
| 492 |
*/ |
| 493 |
function pg_field_type($result, int $field_number): string |
| 494 |
{ |
| 495 |
error_clear_last(); |
| 496 |
$result = \pg_field_type($result, $field_number); |
| 497 |
if ($result === false) { |
| 498 |
throw PgsqlException::createFromPhpError(); |
| 499 |
} |
| 500 |
return $result; |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
/** |
| 505 |
* pg_flush flushes any outbound query data waiting to be |
| 506 |
* sent on the connection. |
| 507 |
* |
| 508 |
* @param resource $connection PostgreSQL database connection resource. |
| 509 |
* @return mixed Returns TRUE if the flush was successful or no data was waiting to be |
| 510 |
* flushed, 0 if part of the pending data was flushed but |
| 511 |
* more remains. |
| 512 |
* @throws PgsqlException |
| 513 |
* |
| 514 |
*/ |
| 515 |
function pg_flush($connection) |
| 516 |
{ |
| 517 |
error_clear_last(); |
| 518 |
$result = \pg_flush($connection); |
| 519 |
if ($result === false) { |
| 520 |
throw PgsqlException::createFromPhpError(); |
| 521 |
} |
| 522 |
return $result; |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
/** |
| 527 |
* pg_free_result frees the memory and data associated with the |
| 528 |
* specified PostgreSQL query result resource. |
| 529 |
* |
| 530 |
* This function need only be called if memory |
| 531 |
* consumption during script execution is a problem. Otherwise, all result memory will |
| 532 |
* be automatically freed when the script ends. |
| 533 |
* |
| 534 |
* @param resource $result PostgreSQL query result resource, returned by pg_query, |
| 535 |
* pg_query_params or pg_execute |
| 536 |
* (among others). |
| 537 |
* @throws PgsqlException |
| 538 |
* |
| 539 |
*/ |
| 540 |
function pg_free_result($result): void |
| 541 |
{ |
| 542 |
error_clear_last(); |
| 543 |
$result = \pg_free_result($result); |
| 544 |
if ($result === false) { |
| 545 |
throw PgsqlException::createFromPhpError(); |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
/** |
| 551 |
* pg_host returns the host name of the given |
| 552 |
* PostgreSQL connection resource is |
| 553 |
* connected to. |
| 554 |
* |
| 555 |
* @param resource $connection PostgreSQL database connection resource. When |
| 556 |
* connection is not present, the default connection |
| 557 |
* is used. The default connection is the last connection made by |
| 558 |
* pg_connect or pg_pconnect. |
| 559 |
* @return string A string containing the name of the host the |
| 560 |
* connection is to. |
| 561 |
* @throws PgsqlException |
| 562 |
* |
| 563 |
*/ |
| 564 |
function pg_host($connection = null): string |
| 565 |
{ |
| 566 |
error_clear_last(); |
| 567 |
if ($connection !== null) { |
| 568 |
$result = \pg_host($connection); |
| 569 |
} else { |
| 570 |
$result = \pg_host(); |
| 571 |
} |
| 572 |
if ($result === false) { |
| 573 |
throw PgsqlException::createFromPhpError(); |
| 574 |
} |
| 575 |
return $result; |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
/** |
| 580 |
* pg_insert inserts the values |
| 581 |
* of assoc_array into the table specified |
| 582 |
* by table_name. If options |
| 583 |
* is specified, pg_convert is applied |
| 584 |
* to assoc_array with the specified options. |
| 585 |
* |
| 586 |
* If options is specified, |
| 587 |
* pg_convert is applied to |
| 588 |
* assoc_array with the specified flags. |
| 589 |
* |
| 590 |
* By default pg_insert passes raw values. Values |
| 591 |
* must be escaped or PGSQL_DML_ESCAPE option must be |
| 592 |
* specified. PGSQL_DML_ESCAPE quotes and escapes |
| 593 |
* parameters/identifiers. Therefore, table/column names became case |
| 594 |
* sensitive. |
| 595 |
* |
| 596 |
* Note that neither escape nor prepared query can protect LIKE query, |
| 597 |
* JSON, Array, Regex, etc. These parameters should be handled |
| 598 |
* according to their contexts. i.e. Escape/validate values. |
| 599 |
* |
| 600 |
* @param resource $connection PostgreSQL database connection resource. |
| 601 |
* @param string $table_name Name of the table into which to insert rows. The table table_name must at least |
| 602 |
* have as many columns as assoc_array has elements. |
| 603 |
* @param array $assoc_array An array whose keys are field names in the table table_name, |
| 604 |
* and whose values are the values of those fields that are to be inserted. |
| 605 |
* @param int $options Any number of PGSQL_CONV_OPTS, |
| 606 |
* PGSQL_DML_NO_CONV, |
| 607 |
* PGSQL_DML_ESCAPE, |
| 608 |
* PGSQL_DML_EXEC, |
| 609 |
* PGSQL_DML_ASYNC or |
| 610 |
* PGSQL_DML_STRING combined. If PGSQL_DML_STRING is part of the |
| 611 |
* options then query string is returned. When PGSQL_DML_NO_CONV |
| 612 |
* or PGSQL_DML_ESCAPE is set, it does not call pg_convert internally. |
| 613 |
* @return mixed Returns the connection resource on success. Returns string if PGSQL_DML_STRING is passed |
| 614 |
* via options. |
| 615 |
* @throws PgsqlException |
| 616 |
* |
| 617 |
*/ |
| 618 |
function pg_insert($connection, string $table_name, array $assoc_array, int $options = PGSQL_DML_EXEC) |
| 619 |
{ |
| 620 |
error_clear_last(); |
| 621 |
$result = \pg_insert($connection, $table_name, $assoc_array, $options); |
| 622 |
if ($result === false) { |
| 623 |
throw PgsqlException::createFromPhpError(); |
| 624 |
} |
| 625 |
return $result; |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
/** |
| 630 |
* pg_last_error returns the last error message |
| 631 |
* for a given connection. |
| 632 |
* |
| 633 |
* Error messages may be overwritten by internal PostgreSQL (libpq) |
| 634 |
* function calls. It may not return an appropriate error message if |
| 635 |
* multiple errors occur inside a PostgreSQL module function. |
| 636 |
* |
| 637 |
* Use pg_result_error, pg_result_error_field, |
| 638 |
* pg_result_status and |
| 639 |
* pg_connection_status for better error handling. |
| 640 |
* |
| 641 |
* @param resource $connection PostgreSQL database connection resource. When |
| 642 |
* connection is not present, the default connection |
| 643 |
* is used. The default connection is the last connection made by |
| 644 |
* pg_connect or pg_pconnect. |
| 645 |
* @return string A string containing the last error message on the |
| 646 |
* given connection. |
| 647 |
* @throws PgsqlException |
| 648 |
* |
| 649 |
*/ |
| 650 |
function pg_last_error($connection = null): string |
| 651 |
{ |
| 652 |
error_clear_last(); |
| 653 |
if ($connection !== null) { |
| 654 |
$result = \pg_last_error($connection); |
| 655 |
} else { |
| 656 |
$result = \pg_last_error(); |
| 657 |
} |
| 658 |
if ($result === false) { |
| 659 |
throw PgsqlException::createFromPhpError(); |
| 660 |
} |
| 661 |
return $result; |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
/** |
| 666 |
* pg_last_notice returns the last notice |
| 667 |
* message from the PostgreSQL server on the specified |
| 668 |
* connection. The PostgreSQL server sends notice |
| 669 |
* messages in several cases, for instance when creating a SERIAL |
| 670 |
* column in a table. |
| 671 |
* |
| 672 |
* With pg_last_notice, you can avoid issuing useless |
| 673 |
* queries by checking whether or not the notice is related to your transaction. |
| 674 |
* |
| 675 |
* Notice message tracking can be set to optional by setting 1 for |
| 676 |
* pgsql.ignore_notice in php.ini. |
| 677 |
* |
| 678 |
* Notice message logging can be set to optional by setting 0 for |
| 679 |
* pgsql.log_notice in php.ini. |
| 680 |
* Unless pgsql.ignore_notice is set |
| 681 |
* to 0, notice message cannot be logged. |
| 682 |
* |
| 683 |
* @param resource $connection PostgreSQL database connection resource. |
| 684 |
* @param int $option One of PGSQL_NOTICE_LAST (to return last notice), |
| 685 |
* PGSQL_NOTICE_ALL (to return all notices), |
| 686 |
* or PGSQL_NOTICE_CLEAR (to clear notices). |
| 687 |
* @return string A string containing the last notice on the |
| 688 |
* given connection with |
| 689 |
* PGSQL_NOTICE_LAST, |
| 690 |
* an array with PGSQL_NOTICE_ALL, |
| 691 |
* a boolean with PGSQL_NOTICE_CLEAR. |
| 692 |
* @throws PgsqlException |
| 693 |
* |
| 694 |
*/ |
| 695 |
function pg_last_notice($connection, int $option = PGSQL_NOTICE_LAST): string |
| 696 |
{ |
| 697 |
error_clear_last(); |
| 698 |
$result = \pg_last_notice($connection, $option); |
| 699 |
if ($result === false) { |
| 700 |
throw PgsqlException::createFromPhpError(); |
| 701 |
} |
| 702 |
return $result; |
| 703 |
} |
| 704 |
|
| 705 |
|
| 706 |
/** |
| 707 |
* pg_last_oid is used to retrieve the |
| 708 |
* OID assigned to an inserted row. |
| 709 |
* |
| 710 |
* OID field became an optional field from PostgreSQL 7.2 and will |
| 711 |
* not be present by default in PostgreSQL 8.1. When the |
| 712 |
* OID field is not present in a table, the programmer must use |
| 713 |
* pg_result_status to check for successful |
| 714 |
* insertion. |
| 715 |
* |
| 716 |
* To get the value of a SERIAL field in an inserted |
| 717 |
* row, it is necessary to use the PostgreSQL CURRVAL |
| 718 |
* function, naming the sequence whose last value is required. If the |
| 719 |
* name of the sequence is unknown, the pg_get_serial_sequence |
| 720 |
* PostgreSQL 8.0 function is necessary. |
| 721 |
* |
| 722 |
* PostgreSQL 8.1 has a function LASTVAL that returns |
| 723 |
* the value of the most recently used sequence in the session. This avoids |
| 724 |
* the need for naming the sequence, table or column altogether. |
| 725 |
* |
| 726 |
* @param resource $result PostgreSQL query result resource, returned by pg_query, |
| 727 |
* pg_query_params or pg_execute |
| 728 |
* (among others). |
| 729 |
* @return string A string containing the OID assigned to the most recently inserted |
| 730 |
* row in the specified connection or |
| 731 |
* no available OID. |
| 732 |
* @throws PgsqlException |
| 733 |
* |
| 734 |
*/ |
| 735 |
function pg_last_oid($result): string |
| 736 |
{ |
| 737 |
error_clear_last(); |
| 738 |
$result = \pg_last_oid($result); |
| 739 |
if ($result === false) { |
| 740 |
throw PgsqlException::createFromPhpError(); |
| 741 |
} |
| 742 |
return $result; |
| 743 |
} |
| 744 |
|
| 745 |
|
| 746 |
/** |
| 747 |
* pg_lo_close closes a large |
| 748 |
* object. large_object is a resource for the |
| 749 |
* large object from pg_lo_open. |
| 750 |
* |
| 751 |
* To use the large object interface, it is necessary to |
| 752 |
* enclose it within a transaction block. |
| 753 |
* |
| 754 |
* @param resource $large_object PostgreSQL large object (LOB) resource, returned by pg_lo_open. |
| 755 |
* @throws PgsqlException |
| 756 |
* |
| 757 |
*/ |
| 758 |
function pg_lo_close($large_object): void |
| 759 |
{ |
| 760 |
error_clear_last(); |
| 761 |
$result = \pg_lo_close($large_object); |
| 762 |
if ($result === false) { |
| 763 |
throw PgsqlException::createFromPhpError(); |
| 764 |
} |
| 765 |
} |
| 766 |
|
| 767 |
|
| 768 |
/** |
| 769 |
* pg_lo_export takes a large object in a |
| 770 |
* PostgreSQL database and saves its contents to a file on the local |
| 771 |
* filesystem. |
| 772 |
* |
| 773 |
* To use the large object interface, it is necessary to |
| 774 |
* enclose it within a transaction block. |
| 775 |
* |
| 776 |
* @param resource $connection PostgreSQL database connection resource. When |
| 777 |
* connection is not present, the default connection |
| 778 |
* is used. The default connection is the last connection made by |
| 779 |
* pg_connect or pg_pconnect. |
| 780 |
* @param int|null $oid The OID of the large object in the database. |
| 781 |
* @param string|null $pathname The full path and file name of the file in which to write the |
| 782 |
* large object on the client filesystem. |
| 783 |
* |
| 784 |
* @throws PgsqlException |
| 785 |
* |
| 786 |
*/ |
| 787 |
function pg_lo_export($connection = null, ?int $oid = null, ?string $pathname = null): void |
| 788 |
{ |
| 789 |
error_clear_last(); |
| 790 |
if ($pathname !== null) { |
| 791 |
$result = \pg_lo_export($connection, $oid, $pathname); |
| 792 |
} elseif ($oid !== null) { |
| 793 |
$result = \pg_lo_export($connection, $oid); |
| 794 |
} elseif ($connection !== null) { |
| 795 |
$result = \pg_lo_export($connection); |
| 796 |
} else { |
| 797 |
$result = \pg_lo_export(); |
| 798 |
} |
| 799 |
if ($result === false) { |
| 800 |
throw PgsqlException::createFromPhpError(); |
| 801 |
} |
| 802 |
} |
| 803 |
|
| 804 |
|
| 805 |
/** |
| 806 |
* pg_lo_import creates a new large object |
| 807 |
* in the database using a file on the filesystem as its data |
| 808 |
* source. |
| 809 |
* |
| 810 |
* To use the large object interface, it is necessary to |
| 811 |
* enclose it within a transaction block. |
| 812 |
* |
| 813 |
* @param resource $connection PostgreSQL database connection resource. When |
| 814 |
* connection is not present, the default connection |
| 815 |
* is used. The default connection is the last connection made by |
| 816 |
* pg_connect or pg_pconnect. |
| 817 |
* @param string|null $pathname The full path and file name of the file on the client |
| 818 |
* filesystem from which to read the large object data. |
| 819 |
* @param mixed $object_id If an object_id is given the function |
| 820 |
* will try to create a large object with this id, else a free |
| 821 |
* object id is assigned by the server. The parameter |
| 822 |
* was added in PHP 5.3 and relies on functionality that first |
| 823 |
* appeared in PostgreSQL 8.1. |
| 824 |
* |
| 825 |
* @return int The OID of the newly created large object. |
| 826 |
* @throws PgsqlException |
| 827 |
* |
| 828 |
*/ |
| 829 |
function pg_lo_import($connection = null, ?string $pathname = null, $object_id = null): int |
| 830 |
{ |
| 831 |
error_clear_last(); |
| 832 |
if ($object_id !== null) { |
| 833 |
$result = \pg_lo_import($connection, $pathname, $object_id); |
| 834 |
} elseif ($pathname !== null) { |
| 835 |
$result = \pg_lo_import($connection, $pathname); |
| 836 |
} elseif ($connection !== null) { |
| 837 |
$result = \pg_lo_import($connection); |
| 838 |
} else { |
| 839 |
$result = \pg_lo_import(); |
| 840 |
} |
| 841 |
if ($result === false) { |
| 842 |
throw PgsqlException::createFromPhpError(); |
| 843 |
} |
| 844 |
return $result; |
| 845 |
} |
| 846 |
|
| 847 |
|
| 848 |
/** |
| 849 |
* pg_lo_open opens a large object in the database |
| 850 |
* and returns large object resource so that it can be manipulated. |
| 851 |
* |
| 852 |
* To use the large object interface, it is necessary to |
| 853 |
* enclose it within a transaction block. |
| 854 |
* |
| 855 |
* @param resource $connection PostgreSQL database connection resource. When |
| 856 |
* connection is not present, the default connection |
| 857 |
* is used. The default connection is the last connection made by |
| 858 |
* pg_connect or pg_pconnect. |
| 859 |
* @param int $oid The OID of the large object in the database. |
| 860 |
* @param string $mode Can be either "r" for read-only, "w" for write only or "rw" for read and |
| 861 |
* write. |
| 862 |
* @return resource A large object resource. |
| 863 |
* @throws PgsqlException |
| 864 |
* |
| 865 |
*/ |
| 866 |
function pg_lo_open($connection, int $oid, string $mode) |
| 867 |
{ |
| 868 |
error_clear_last(); |
| 869 |
$result = \pg_lo_open($connection, $oid, $mode); |
| 870 |
if ($result === false) { |
| 871 |
throw PgsqlException::createFromPhpError(); |
| 872 |
} |
| 873 |
return $result; |
| 874 |
} |
| 875 |
|
| 876 |
|
| 877 |
/** |
| 878 |
* pg_lo_read_all reads a large object and passes |
| 879 |
* it straight through to the browser after sending all pending |
| 880 |
* headers. Mainly intended for sending binary data like images or |
| 881 |
* sound. |
| 882 |
* |
| 883 |
* To use the large object interface, it is necessary to |
| 884 |
* enclose it within a transaction block. |
| 885 |
* |
| 886 |
* @param resource $large_object PostgreSQL large object (LOB) resource, returned by pg_lo_open. |
| 887 |
* @return int Number of bytes read. |
| 888 |
* @throws PgsqlException |
| 889 |
* |
| 890 |
*/ |
| 891 |
function pg_lo_read_all($large_object): int |
| 892 |
{ |
| 893 |
error_clear_last(); |
| 894 |
$result = \pg_lo_read_all($large_object); |
| 895 |
if ($result === false) { |
| 896 |
throw PgsqlException::createFromPhpError(); |
| 897 |
} |
| 898 |
return $result; |
| 899 |
} |
| 900 |
|
| 901 |
|
| 902 |
/** |
| 903 |
* pg_lo_read reads at most |
| 904 |
* len bytes from a large object and |
| 905 |
* returns it as a string. |
| 906 |
* |
| 907 |
* To use the large object interface, it is necessary to |
| 908 |
* enclose it within a transaction block. |
| 909 |
* |
| 910 |
* @param resource $large_object PostgreSQL large object (LOB) resource, returned by pg_lo_open. |
| 911 |
* @param int $len An optional maximum number of bytes to return. |
| 912 |
* @return string A string containing len bytes from the |
| 913 |
* large object. |
| 914 |
* @throws PgsqlException |
| 915 |
* |
| 916 |
*/ |
| 917 |
function pg_lo_read($large_object, int $len = 8192): string |
| 918 |
{ |
| 919 |
error_clear_last(); |
| 920 |
$result = \pg_lo_read($large_object, $len); |
| 921 |
if ($result === false) { |
| 922 |
throw PgsqlException::createFromPhpError(); |
| 923 |
} |
| 924 |
return $result; |
| 925 |
} |
| 926 |
|
| 927 |
|
| 928 |
/** |
| 929 |
* pg_lo_seek seeks a position within a large object |
| 930 |
* resource. |
| 931 |
* |
| 932 |
* To use the large object interface, it is necessary to |
| 933 |
* enclose it within a transaction block. |
| 934 |
* |
| 935 |
* @param resource $large_object PostgreSQL large object (LOB) resource, returned by pg_lo_open. |
| 936 |
* @param int $offset The number of bytes to seek. |
| 937 |
* @param int $whence One of the constants PGSQL_SEEK_SET (seek from object start), |
| 938 |
* PGSQL_SEEK_CUR (seek from current position) |
| 939 |
* or PGSQL_SEEK_END (seek from object end) . |
| 940 |
* @throws PgsqlException |
| 941 |
* |
| 942 |
*/ |
| 943 |
function pg_lo_seek($large_object, int $offset, int $whence = PGSQL_SEEK_CUR): void |
| 944 |
{ |
| 945 |
error_clear_last(); |
| 946 |
$result = \pg_lo_seek($large_object, $offset, $whence); |
| 947 |
if ($result === false) { |
| 948 |
throw PgsqlException::createFromPhpError(); |
| 949 |
} |
| 950 |
} |
| 951 |
|
| 952 |
|
| 953 |
/** |
| 954 |
* pg_lo_truncate truncates a large object |
| 955 |
* resource. |
| 956 |
* |
| 957 |
* To use the large object interface, it is necessary to |
| 958 |
* enclose it within a transaction block. |
| 959 |
* |
| 960 |
* @param resource $large_object PostgreSQL large object (LOB) resource, returned by pg_lo_open. |
| 961 |
* @param int $size The number of bytes to truncate. |
| 962 |
* @throws PgsqlException |
| 963 |
* |
| 964 |
*/ |
| 965 |
function pg_lo_truncate($large_object, int $size): void |
| 966 |
{ |
| 967 |
error_clear_last(); |
| 968 |
$result = \pg_lo_truncate($large_object, $size); |
| 969 |
if ($result === false) { |
| 970 |
throw PgsqlException::createFromPhpError(); |
| 971 |
} |
| 972 |
} |
| 973 |
|
| 974 |
|
| 975 |
/** |
| 976 |
* pg_lo_unlink deletes a large object with the |
| 977 |
* oid. Returns TRUE on success. |
| 978 |
* |
| 979 |
* To use the large object interface, it is necessary to |
| 980 |
* enclose it within a transaction block. |
| 981 |
* |
| 982 |
* @param resource $connection PostgreSQL database connection resource. When |
| 983 |
* connection is not present, the default connection |
| 984 |
* is used. The default connection is the last connection made by |
| 985 |
* pg_connect or pg_pconnect. |
| 986 |
* @param int $oid The OID of the large object in the database. |
| 987 |
* @throws PgsqlException |
| 988 |
* |
| 989 |
*/ |
| 990 |
function pg_lo_unlink($connection, int $oid): void |
| 991 |
{ |
| 992 |
error_clear_last(); |
| 993 |
$result = \pg_lo_unlink($connection, $oid); |
| 994 |
if ($result === false) { |
| 995 |
throw PgsqlException::createFromPhpError(); |
| 996 |
} |
| 997 |
} |
| 998 |
|
| 999 |
|
| 1000 |
/** |
| 1001 |
* pg_lo_write writes data into a large object |
| 1002 |
* at the current seek position. |
| 1003 |
* |
| 1004 |
* To use the large object interface, it is necessary to |
| 1005 |
* enclose it within a transaction block. |
| 1006 |
* |
| 1007 |
* @param resource $large_object PostgreSQL large object (LOB) resource, returned by pg_lo_open. |
| 1008 |
* @param string $data The data to be written to the large object. If len is |
| 1009 |
* specified and is less than the length of data, only |
| 1010 |
* len bytes will be written. |
| 1011 |
* @param int|null $len An optional maximum number of bytes to write. Must be greater than zero |
| 1012 |
* and no greater than the length of data. Defaults to |
| 1013 |
* the length of data. |
| 1014 |
* |
| 1015 |
* @return int The number of bytes written to the large object. |
| 1016 |
* @throws PgsqlException |
| 1017 |
* |
| 1018 |
*/ |
| 1019 |
function pg_lo_write($large_object, string $data, ?int $len = null): int |
| 1020 |
{ |
| 1021 |
error_clear_last(); |
| 1022 |
if ($len !== null) { |
| 1023 |
$result = \pg_lo_write($large_object, $data, $len); |
| 1024 |
} else { |
| 1025 |
$result = \pg_lo_write($large_object, $data); |
| 1026 |
} |
| 1027 |
if ($result === false) { |
| 1028 |
throw PgsqlException::createFromPhpError(); |
| 1029 |
} |
| 1030 |
return $result; |
| 1031 |
} |
| 1032 |
|
| 1033 |
|
| 1034 |
/** |
| 1035 |
* pg_meta_data returns table definition for |
| 1036 |
* table_name as an array. |
| 1037 |
* |
| 1038 |
* @param resource $connection PostgreSQL database connection resource. |
| 1039 |
* @param string $table_name The name of the table. |
| 1040 |
* @param bool $extended Flag for returning extended meta data. Default to FALSE. |
| 1041 |
* @return array An array of the table definition. |
| 1042 |
* @throws PgsqlException |
| 1043 |
* |
| 1044 |
*/ |
| 1045 |
function pg_meta_data($connection, string $table_name, bool $extended = false): array |
| 1046 |
{ |
| 1047 |
error_clear_last(); |
| 1048 |
$result = \pg_meta_data($connection, $table_name, $extended); |
| 1049 |
if ($result === false) { |
| 1050 |
throw PgsqlException::createFromPhpError(); |
| 1051 |
} |
| 1052 |
return $result; |
| 1053 |
} |
| 1054 |
|
| 1055 |
|
| 1056 |
/** |
| 1057 |
* pg_options will return a string containing |
| 1058 |
* the options specified on the given PostgreSQL |
| 1059 |
* connection resource. |
| 1060 |
* |
| 1061 |
* @param resource $connection PostgreSQL database connection resource. When |
| 1062 |
* connection is not present, the default connection |
| 1063 |
* is used. The default connection is the last connection made by |
| 1064 |
* pg_connect or pg_pconnect. |
| 1065 |
* @return string A string containing the connection |
| 1066 |
* options. |
| 1067 |
* @throws PgsqlException |
| 1068 |
* |
| 1069 |
*/ |
| 1070 |
function pg_options($connection = null): string |
| 1071 |
{ |
| 1072 |
error_clear_last(); |
| 1073 |
if ($connection !== null) { |
| 1074 |
$result = \pg_options($connection); |
| 1075 |
} else { |
| 1076 |
$result = \pg_options(); |
| 1077 |
} |
| 1078 |
if ($result === false) { |
| 1079 |
throw PgsqlException::createFromPhpError(); |
| 1080 |
} |
| 1081 |
return $result; |
| 1082 |
} |
| 1083 |
|
| 1084 |
|
| 1085 |
/** |
| 1086 |
* Looks up a current parameter setting of the server. |
| 1087 |
* |
| 1088 |
* Certain parameter values are reported by the server automatically at |
| 1089 |
* connection startup or whenever their values change. pg_parameter_status can be |
| 1090 |
* used to interrogate these settings. It returns the current value of a |
| 1091 |
* parameter if known, or FALSE if the parameter is not known. |
| 1092 |
* |
| 1093 |
* Parameters reported as of PostgreSQL 8.0 include server_version, |
| 1094 |
* server_encoding, client_encoding, |
| 1095 |
* is_superuser, session_authorization, |
| 1096 |
* DateStyle, TimeZone, and integer_datetimes. |
| 1097 |
* (server_encoding, TimeZone, and |
| 1098 |
* integer_datetimes were not reported by releases before 8.0.) Note that |
| 1099 |
* server_version, server_encoding and integer_datetimes |
| 1100 |
* cannot change after PostgreSQL startup. |
| 1101 |
* |
| 1102 |
* PostgreSQL 7.3 or lower servers do not report parameter settings, |
| 1103 |
* pg_parameter_status |
| 1104 |
* includes logic to obtain values for server_version and |
| 1105 |
* client_encoding |
| 1106 |
* anyway. Applications are encouraged to use pg_parameter_status rather than ad |
| 1107 |
* hoc code to determine these values. |
| 1108 |
* |
| 1109 |
* @param resource $connection PostgreSQL database connection resource. When |
| 1110 |
* connection is not present, the default connection |
| 1111 |
* is used. The default connection is the last connection made by |
| 1112 |
* pg_connect or pg_pconnect. |
| 1113 |
* @param string|null $param_name Possible param_name values include server_version, |
| 1114 |
* server_encoding, client_encoding, |
| 1115 |
* is_superuser, session_authorization, |
| 1116 |
* DateStyle, TimeZone, and |
| 1117 |
* integer_datetimes. Note that this value is case-sensitive. |
| 1118 |
* |
| 1119 |
* @return string A string containing the value of the parameter, FALSE on failure or invalid |
| 1120 |
* param_name. |
| 1121 |
* @throws PgsqlException |
| 1122 |
* |
| 1123 |
*/ |
| 1124 |
function pg_parameter_status($connection = null, ?string $param_name = null): string |
| 1125 |
{ |
| 1126 |
error_clear_last(); |
| 1127 |
if ($param_name !== null) { |
| 1128 |
$result = \pg_parameter_status($connection, $param_name); |
| 1129 |
} elseif ($connection !== null) { |
| 1130 |
$result = \pg_parameter_status($connection); |
| 1131 |
} else { |
| 1132 |
$result = \pg_parameter_status(); |
| 1133 |
} |
| 1134 |
if ($result === false) { |
| 1135 |
throw PgsqlException::createFromPhpError(); |
| 1136 |
} |
| 1137 |
return $result; |
| 1138 |
} |
| 1139 |
|
| 1140 |
|
| 1141 |
/** |
| 1142 |
* pg_pconnect opens a connection to a |
| 1143 |
* PostgreSQL database. It returns a connection resource that is |
| 1144 |
* needed by other PostgreSQL functions. |
| 1145 |
* |
| 1146 |
* If a second call is made to pg_pconnect with |
| 1147 |
* the same connection_string as an existing connection, the |
| 1148 |
* existing connection will be returned unless you pass |
| 1149 |
* PGSQL_CONNECT_FORCE_NEW as |
| 1150 |
* connect_type. |
| 1151 |
* |
| 1152 |
* To enable persistent connection, the pgsql.allow_persistent |
| 1153 |
* php.ini directive must be set to "On" (which is the default). |
| 1154 |
* The maximum number of persistent connection can be defined with the pgsql.max_persistent |
| 1155 |
* php.ini directive (defaults to -1 for no limit). The total number |
| 1156 |
* of connections can be set with the pgsql.max_links |
| 1157 |
* php.ini directive. |
| 1158 |
* |
| 1159 |
* pg_close will not close persistent links |
| 1160 |
* generated by pg_pconnect. |
| 1161 |
* |
| 1162 |
* @param string $connection_string The connection_string can be empty to use all default parameters, or it |
| 1163 |
* can contain one or more parameter settings separated by whitespace. |
| 1164 |
* Each parameter setting is in the form keyword = value. Spaces around |
| 1165 |
* the equal sign are optional. To write an empty value or a value |
| 1166 |
* containing spaces, surround it with single quotes, e.g., keyword = |
| 1167 |
* 'a value'. Single quotes and backslashes within the value must be |
| 1168 |
* escaped with a backslash, i.e., \' and \\. |
| 1169 |
* |
| 1170 |
* The currently recognized parameter keywords are: |
| 1171 |
* host, hostaddr, port, |
| 1172 |
* dbname, user, |
| 1173 |
* password, connect_timeout, |
| 1174 |
* options, tty (ignored), sslmode, |
| 1175 |
* requiressl (deprecated in favor of sslmode), and |
| 1176 |
* service. Which of these arguments exist depends |
| 1177 |
* on your PostgreSQL version. |
| 1178 |
* @param int|null $connect_type If PGSQL_CONNECT_FORCE_NEW is passed, then a new connection |
| 1179 |
* is created, even if the connection_string is identical to |
| 1180 |
* an existing connection. |
| 1181 |
* |
| 1182 |
* @return resource PostgreSQL connection resource on success, FALSE on failure. |
| 1183 |
* @throws PgsqlException |
| 1184 |
* |
| 1185 |
*/ |
| 1186 |
function pg_pconnect(string $connection_string, ?int $connect_type = null) |
| 1187 |
{ |
| 1188 |
error_clear_last(); |
| 1189 |
if ($connect_type !== null) { |
| 1190 |
$result = \pg_pconnect($connection_string, $connect_type); |
| 1191 |
} else { |
| 1192 |
$result = \pg_pconnect($connection_string); |
| 1193 |
} |
| 1194 |
if ($result === false) { |
| 1195 |
throw PgsqlException::createFromPhpError(); |
| 1196 |
} |
| 1197 |
return $result; |
| 1198 |
} |
| 1199 |
|
| 1200 |
|
| 1201 |
/** |
| 1202 |
* pg_ping pings a database connection and tries to |
| 1203 |
* reconnect it if it is broken. |
| 1204 |
* |
| 1205 |
* @param resource $connection PostgreSQL database connection resource. When |
| 1206 |
* connection is not present, the default connection |
| 1207 |
* is used. The default connection is the last connection made by |
| 1208 |
* pg_connect or pg_pconnect. |
| 1209 |
* @throws PgsqlException |
| 1210 |
* |
| 1211 |
*/ |
| 1212 |
function pg_ping($connection = null): void |
| 1213 |
{ |
| 1214 |
error_clear_last(); |
| 1215 |
if ($connection !== null) { |
| 1216 |
$result = \pg_ping($connection); |
| 1217 |
} else { |
| 1218 |
$result = \pg_ping(); |
| 1219 |
} |
| 1220 |
if ($result === false) { |
| 1221 |
throw PgsqlException::createFromPhpError(); |
| 1222 |
} |
| 1223 |
} |
| 1224 |
|
| 1225 |
|
| 1226 |
/** |
| 1227 |
* pg_port returns the port number that the |
| 1228 |
* given PostgreSQL connection resource is |
| 1229 |
* connected to. |
| 1230 |
* |
| 1231 |
* @param resource $connection PostgreSQL database connection resource. When |
| 1232 |
* connection is not present, the default connection |
| 1233 |
* is used. The default connection is the last connection made by |
| 1234 |
* pg_connect or pg_pconnect. |
| 1235 |
* @return int An int containing the port number of the database |
| 1236 |
* server the connection is to. |
| 1237 |
* @throws PgsqlException |
| 1238 |
* |
| 1239 |
*/ |
| 1240 |
function pg_port($connection = null): int |
| 1241 |
{ |
| 1242 |
error_clear_last(); |
| 1243 |
if ($connection !== null) { |
| 1244 |
$result = \pg_port($connection); |
| 1245 |
} else { |
| 1246 |
$result = \pg_port(); |
| 1247 |
} |
| 1248 |
if ($result === false) { |
| 1249 |
throw PgsqlException::createFromPhpError(); |
| 1250 |
} |
| 1251 |
return $result; |
| 1252 |
} |
| 1253 |
|
| 1254 |
|
| 1255 |
/** |
| 1256 |
* pg_prepare creates a prepared statement for later execution with |
| 1257 |
* pg_execute or pg_send_execute. |
| 1258 |
* This feature allows commands that will be used repeatedly to |
| 1259 |
* be parsed and planned just once, rather than each time they are executed. |
| 1260 |
* pg_prepare is supported only against PostgreSQL 7.4 or |
| 1261 |
* higher connections; it will fail when using earlier versions. |
| 1262 |
* |
| 1263 |
* The function creates a prepared statement named stmtname from the query |
| 1264 |
* string, which must contain a single SQL command. stmtname may be "" to |
| 1265 |
* create an unnamed statement, in which case any pre-existing unnamed |
| 1266 |
* statement is automatically replaced; otherwise it is an error if the |
| 1267 |
* statement name is already defined in the current session. If any parameters |
| 1268 |
* are used, they are referred to in the query as $1, $2, etc. |
| 1269 |
* |
| 1270 |
* Prepared statements for use with pg_prepare can also be created by |
| 1271 |
* executing SQL PREPARE statements. (But pg_prepare is more flexible since it |
| 1272 |
* does not require parameter types to be pre-specified.) Also, although there |
| 1273 |
* is no PHP function for deleting a prepared statement, the SQL DEALLOCATE |
| 1274 |
* statement can be used for that purpose. |
| 1275 |
* |
| 1276 |
* @param resource $connection PostgreSQL database connection resource. When |
| 1277 |
* connection is not present, the default connection |
| 1278 |
* is used. The default connection is the last connection made by |
| 1279 |
* pg_connect or pg_pconnect. |
| 1280 |
* @param string|null $stmtname The name to give the prepared statement. Must be unique per-connection. If |
| 1281 |
* "" is specified, then an unnamed statement is created, overwriting any |
| 1282 |
* previously defined unnamed statement. |
| 1283 |
* @param string|null $query The parameterized SQL statement. Must contain only a single statement. |
| 1284 |
* (multiple statements separated by semi-colons are not allowed.) If any parameters |
| 1285 |
* are used, they are referred to as $1, $2, etc. |
| 1286 |
* |
| 1287 |
* @return resource A query result resource on success. |
| 1288 |
* @throws PgsqlException |
| 1289 |
* |
| 1290 |
*/ |
| 1291 |
function pg_prepare($connection = null, ?string $stmtname = null, ?string $query = null) |
| 1292 |
{ |
| 1293 |
error_clear_last(); |
| 1294 |
if ($query !== null) { |
| 1295 |
$result = \pg_prepare($connection, $stmtname, $query); |
| 1296 |
} elseif ($stmtname !== null) { |
| 1297 |
$result = \pg_prepare($connection, $stmtname); |
| 1298 |
} elseif ($connection !== null) { |
| 1299 |
$result = \pg_prepare($connection); |
| 1300 |
} else { |
| 1301 |
$result = \pg_prepare(); |
| 1302 |
} |
| 1303 |
if ($result === false) { |
| 1304 |
throw PgsqlException::createFromPhpError(); |
| 1305 |
} |
| 1306 |
return $result; |
| 1307 |
} |
| 1308 |
|
| 1309 |
|
| 1310 |
/** |
| 1311 |
* pg_put_line sends a NULL-terminated string |
| 1312 |
* to the PostgreSQL backend server. This is needed in conjunction |
| 1313 |
* with PostgreSQL's COPY FROM command. |
| 1314 |
* |
| 1315 |
* COPY is a high-speed data loading interface |
| 1316 |
* supported by PostgreSQL. Data is passed in without being parsed, |
| 1317 |
* and in a single transaction. |
| 1318 |
* |
| 1319 |
* An alternative to using raw pg_put_line commands |
| 1320 |
* is to use pg_copy_from. This is a far simpler |
| 1321 |
* interface. |
| 1322 |
* |
| 1323 |
* @param resource $connection PostgreSQL database connection resource. When |
| 1324 |
* connection is not present, the default connection |
| 1325 |
* is used. The default connection is the last connection made by |
| 1326 |
* pg_connect or pg_pconnect. |
| 1327 |
* @param string|null $data A line of text to be sent directly to the PostgreSQL backend. A NULL |
| 1328 |
* terminator is added automatically. |
| 1329 |
* |
| 1330 |
* @throws PgsqlException |
| 1331 |
* |
| 1332 |
*/ |
| 1333 |
function pg_put_line($connection = null, ?string $data = null): void |
| 1334 |
{ |
| 1335 |
error_clear_last(); |
| 1336 |
if ($data !== null) { |
| 1337 |
$result = \pg_put_line($connection, $data); |
| 1338 |
} elseif ($connection !== null) { |
| 1339 |
$result = \pg_put_line($connection); |
| 1340 |
} else { |
| 1341 |
$result = \pg_put_line(); |
| 1342 |
} |
| 1343 |
if ($result === false) { |
| 1344 |
throw PgsqlException::createFromPhpError(); |
| 1345 |
} |
| 1346 |
} |
| 1347 |
|
| 1348 |
|
| 1349 |
/** |
| 1350 |
* Submits a command to the server and waits for the result, with the ability |
| 1351 |
* to pass parameters separately from the SQL command text. |
| 1352 |
* |
| 1353 |
* pg_query_params is like pg_query, |
| 1354 |
* but offers additional functionality: parameter |
| 1355 |
* values can be specified separately from the command string proper. |
| 1356 |
* pg_query_params is supported only against PostgreSQL 7.4 or |
| 1357 |
* higher connections; it will fail when using earlier versions. |
| 1358 |
* |
| 1359 |
* If parameters are used, they are referred to in the |
| 1360 |
* query string as $1, $2, etc. The same parameter may |
| 1361 |
* appear more than once in the query; the same value |
| 1362 |
* will be used in that case. params specifies the |
| 1363 |
* actual values of the parameters. A NULL value in this array means the |
| 1364 |
* corresponding parameter is SQL NULL. |
| 1365 |
* |
| 1366 |
* The primary advantage of pg_query_params over pg_query |
| 1367 |
* is that parameter values |
| 1368 |
* may be separated from the query string, thus avoiding the need for tedious |
| 1369 |
* and error-prone quoting and escaping. Unlike pg_query, |
| 1370 |
* pg_query_params allows at |
| 1371 |
* most one SQL command in the given string. (There can be semicolons in it, |
| 1372 |
* but not more than one nonempty command.) |
| 1373 |
* |
| 1374 |
* @param resource $connection PostgreSQL database connection resource. When |
| 1375 |
* connection is not present, the default connection |
| 1376 |
* is used. The default connection is the last connection made by |
| 1377 |
* pg_connect or pg_pconnect. |
| 1378 |
* @param string|null $query The parameterized SQL statement. Must contain only a single statement. |
| 1379 |
* (multiple statements separated by semi-colons are not allowed.) If any parameters |
| 1380 |
* are used, they are referred to as $1, $2, etc. |
| 1381 |
* |
| 1382 |
* User-supplied values should always be passed as parameters, not |
| 1383 |
* interpolated into the query string, where they form possible |
| 1384 |
* SQL injection |
| 1385 |
* attack vectors and introduce bugs when handling data containing quotes. |
| 1386 |
* If for some reason you cannot use a parameter, ensure that interpolated |
| 1387 |
* values are properly escaped. |
| 1388 |
* @param array|null $params An array of parameter values to substitute for the $1, $2, etc. placeholders |
| 1389 |
* in the original prepared query string. The number of elements in the array |
| 1390 |
* must match the number of placeholders. |
| 1391 |
* |
| 1392 |
* Values intended for bytea fields are not supported as |
| 1393 |
* parameters. Use pg_escape_bytea instead, or use the |
| 1394 |
* large object functions. |
| 1395 |
* |
| 1396 |
* @return resource A query result resource on success. |
| 1397 |
* @throws PgsqlException |
| 1398 |
* |
| 1399 |
*/ |
| 1400 |
function pg_query_params($connection = null, ?string $query = null, ?array $params = null) |
| 1401 |
{ |
| 1402 |
error_clear_last(); |
| 1403 |
if ($params !== null) { |
| 1404 |
$result = \pg_query_params($connection, $query, $params); |
| 1405 |
} elseif ($query !== null) { |
| 1406 |
$result = \pg_query_params($connection, $query); |
| 1407 |
} elseif ($connection !== null) { |
| 1408 |
$result = \pg_query_params($connection); |
| 1409 |
} else { |
| 1410 |
$result = \pg_query_params(); |
| 1411 |
} |
| 1412 |
if ($result === false) { |
| 1413 |
throw PgsqlException::createFromPhpError(); |
| 1414 |
} |
| 1415 |
return $result; |
| 1416 |
} |
| 1417 |
|
| 1418 |
|
| 1419 |
/** |
| 1420 |
* pg_query executes the query |
| 1421 |
* on the specified database connection. |
| 1422 |
* pg_query_params should be preferred |
| 1423 |
* in most cases. |
| 1424 |
* |
| 1425 |
* If an error occurs, and FALSE is returned, details of the error can |
| 1426 |
* be retrieved using the pg_last_error |
| 1427 |
* function if the connection is valid. |
| 1428 |
* |
| 1429 |
* |
| 1430 |
* |
| 1431 |
* Although connection can be omitted, it |
| 1432 |
* is not recommended, since it can be the cause of hard to find |
| 1433 |
* bugs in scripts. |
| 1434 |
* |
| 1435 |
* |
| 1436 |
* |
| 1437 |
* @param resource $connection PostgreSQL database connection resource. When |
| 1438 |
* connection is not present, the default connection |
| 1439 |
* is used. The default connection is the last connection made by |
| 1440 |
* pg_connect or pg_pconnect. |
| 1441 |
* @param string|null $query The SQL statement or statements to be executed. When multiple statements are passed to the function, |
| 1442 |
* they are automatically executed as one transaction, unless there are explicit BEGIN/COMMIT commands |
| 1443 |
* included in the query string. However, using multiple transactions in one function call is not recommended. |
| 1444 |
* |
| 1445 |
* String interpolation of user-supplied data is extremely dangerous and is |
| 1446 |
* likely to lead to SQL |
| 1447 |
* injection vulnerabilities. In most cases |
| 1448 |
* pg_query_params should be preferred, passing |
| 1449 |
* user-supplied values as parameters rather than substituting them into |
| 1450 |
* the query string. |
| 1451 |
* |
| 1452 |
* Any user-supplied data substituted directly into a query string should |
| 1453 |
* be properly escaped. |
| 1454 |
* |
| 1455 |
* @return resource A query result resource on success. |
| 1456 |
* @throws PgsqlException |
| 1457 |
* |
| 1458 |
*/ |
| 1459 |
function pg_query($connection = null, ?string $query = null) |
| 1460 |
{ |
| 1461 |
error_clear_last(); |
| 1462 |
if ($query !== null) { |
| 1463 |
$result = \pg_query($connection, $query); |
| 1464 |
} elseif ($connection !== null) { |
| 1465 |
$result = \pg_query($connection); |
| 1466 |
} else { |
| 1467 |
$result = \pg_query(); |
| 1468 |
} |
| 1469 |
if ($result === false) { |
| 1470 |
throw PgsqlException::createFromPhpError(); |
| 1471 |
} |
| 1472 |
return $result; |
| 1473 |
} |
| 1474 |
|
| 1475 |
|
| 1476 |
/** |
| 1477 |
* pg_result_error_field returns one of the detailed error message |
| 1478 |
* fields associated with result resource. It is only available |
| 1479 |
* against a PostgreSQL 7.4 or above server. The error field is specified by |
| 1480 |
* the fieldcode. |
| 1481 |
* |
| 1482 |
* Because pg_query and pg_query_params return FALSE if the query fails, |
| 1483 |
* you must use pg_send_query and |
| 1484 |
* pg_get_result to get the result handle. |
| 1485 |
* |
| 1486 |
* If you need to get additional error information from failed pg_query queries, |
| 1487 |
* use pg_set_error_verbosity and pg_last_error |
| 1488 |
* and then parse the result. |
| 1489 |
* |
| 1490 |
* @param resource $result A PostgreSQL query result resource from a previously executed |
| 1491 |
* statement. |
| 1492 |
* @param int $fieldcode Possible fieldcode values are: PGSQL_DIAG_SEVERITY, |
| 1493 |
* PGSQL_DIAG_SQLSTATE, PGSQL_DIAG_MESSAGE_PRIMARY, |
| 1494 |
* PGSQL_DIAG_MESSAGE_DETAIL, |
| 1495 |
* PGSQL_DIAG_MESSAGE_HINT, PGSQL_DIAG_STATEMENT_POSITION, |
| 1496 |
* PGSQL_DIAG_INTERNAL_POSITION (PostgreSQL 8.0+ only), |
| 1497 |
* PGSQL_DIAG_INTERNAL_QUERY (PostgreSQL 8.0+ only), |
| 1498 |
* PGSQL_DIAG_CONTEXT, PGSQL_DIAG_SOURCE_FILE, |
| 1499 |
* PGSQL_DIAG_SOURCE_LINE or |
| 1500 |
* PGSQL_DIAG_SOURCE_FUNCTION. |
| 1501 |
* @return string|null A string containing the contents of the error field, NULL if the field does not exist. |
| 1502 |
* @throws PgsqlException |
| 1503 |
* |
| 1504 |
*/ |
| 1505 |
function pg_result_error_field($result, int $fieldcode): ?string |
| 1506 |
{ |
| 1507 |
error_clear_last(); |
| 1508 |
$result = \pg_result_error_field($result, $fieldcode); |
| 1509 |
if ($result === false) { |
| 1510 |
throw PgsqlException::createFromPhpError(); |
| 1511 |
} |
| 1512 |
return $result; |
| 1513 |
} |
| 1514 |
|
| 1515 |
|
| 1516 |
/** |
| 1517 |
* pg_result_seek sets the internal row offset in |
| 1518 |
* a result resource. |
| 1519 |
* |
| 1520 |
* @param resource $result PostgreSQL query result resource, returned by pg_query, |
| 1521 |
* pg_query_params or pg_execute |
| 1522 |
* (among others). |
| 1523 |
* @param int $offset Row to move the internal offset to in the result resource. |
| 1524 |
* Rows are numbered starting from zero. |
| 1525 |
* @throws PgsqlException |
| 1526 |
* |
| 1527 |
*/ |
| 1528 |
function pg_result_seek($result, int $offset): void |
| 1529 |
{ |
| 1530 |
error_clear_last(); |
| 1531 |
$result = \pg_result_seek($result, $offset); |
| 1532 |
if ($result === false) { |
| 1533 |
throw PgsqlException::createFromPhpError(); |
| 1534 |
} |
| 1535 |
} |
| 1536 |
|
| 1537 |
|
| 1538 |
/** |
| 1539 |
* pg_select selects records specified by |
| 1540 |
* assoc_array which has |
| 1541 |
* field=>value. For a successful query, it returns an |
| 1542 |
* array containing all records and fields that match the condition |
| 1543 |
* specified by assoc_array. |
| 1544 |
* |
| 1545 |
* If options is specified, |
| 1546 |
* pg_convert is applied to |
| 1547 |
* assoc_array with the specified flags. |
| 1548 |
* |
| 1549 |
* By default pg_select passes raw values. Values |
| 1550 |
* must be escaped or PGSQL_DML_ESCAPE option must be |
| 1551 |
* specified. PGSQL_DML_ESCAPE quotes and escapes |
| 1552 |
* parameters/identifiers. Therefore, table/column names became case |
| 1553 |
* sensitive. |
| 1554 |
* |
| 1555 |
* Note that neither escape nor prepared query can protect LIKE query, |
| 1556 |
* JSON, Array, Regex, etc. These parameters should be handled |
| 1557 |
* according to their contexts. i.e. Escape/validate values. |
| 1558 |
* |
| 1559 |
* @param resource $connection PostgreSQL database connection resource. |
| 1560 |
* @param string $table_name Name of the table from which to select rows. |
| 1561 |
* @param array $assoc_array An array whose keys are field names in the table table_name, |
| 1562 |
* and whose values are the conditions that a row must meet to be retrieved. |
| 1563 |
* @param int $options Any number of PGSQL_CONV_FORCE_NULL, |
| 1564 |
* PGSQL_DML_NO_CONV, |
| 1565 |
* PGSQL_DML_ESCAPE, |
| 1566 |
* PGSQL_DML_EXEC, |
| 1567 |
* PGSQL_DML_ASYNC or |
| 1568 |
* PGSQL_DML_STRING combined. If PGSQL_DML_STRING is part of the |
| 1569 |
* options then query string is returned. When PGSQL_DML_NO_CONV |
| 1570 |
* or PGSQL_DML_ESCAPE is set, it does not call pg_convert internally. |
| 1571 |
* @param int $result_type |
| 1572 |
* @return mixed Returns TRUE on success. Returns string if PGSQL_DML_STRING is passed |
| 1573 |
* via options. |
| 1574 |
* @throws PgsqlException |
| 1575 |
* |
| 1576 |
*/ |
| 1577 |
function pg_select($connection, string $table_name, array $assoc_array, int $options = PGSQL_DML_EXEC, int $result_type = PGSQL_ASSOC) |
| 1578 |
{ |
| 1579 |
error_clear_last(); |
| 1580 |
$result = \pg_select($connection, $table_name, $assoc_array, $options, $result_type); |
| 1581 |
if ($result === false) { |
| 1582 |
throw PgsqlException::createFromPhpError(); |
| 1583 |
} |
| 1584 |
return $result; |
| 1585 |
} |
| 1586 |
|
| 1587 |
|
| 1588 |
/** |
| 1589 |
* Sends a request to execute a prepared statement with given parameters, |
| 1590 |
* without waiting for the result(s). |
| 1591 |
* |
| 1592 |
* This is similar to pg_send_query_params, but the command to be executed is specified |
| 1593 |
* by naming a previously-prepared statement, instead of giving a query string. The |
| 1594 |
* function's parameters are handled identically to pg_execute. |
| 1595 |
* Like pg_execute, it will not work on pre-7.4 versions of |
| 1596 |
* PostgreSQL. |
| 1597 |
* |
| 1598 |
* @param resource $connection PostgreSQL database connection resource. When |
| 1599 |
* connection is not present, the default connection |
| 1600 |
* is used. The default connection is the last connection made by |
| 1601 |
* pg_connect or pg_pconnect. |
| 1602 |
* @param string $stmtname The name of the prepared statement to execute. if |
| 1603 |
* "" is specified, then the unnamed statement is executed. The name must have |
| 1604 |
* been previously prepared using pg_prepare, |
| 1605 |
* pg_send_prepare or a PREPARE SQL |
| 1606 |
* command. |
| 1607 |
* @param array $params An array of parameter values to substitute for the $1, $2, etc. placeholders |
| 1608 |
* in the original prepared query string. The number of elements in the array |
| 1609 |
* must match the number of placeholders. |
| 1610 |
* @throws PgsqlException |
| 1611 |
* |
| 1612 |
*/ |
| 1613 |
function pg_send_execute($connection, string $stmtname, array $params): void |
| 1614 |
{ |
| 1615 |
error_clear_last(); |
| 1616 |
$result = \pg_send_execute($connection, $stmtname, $params); |
| 1617 |
if ($result === false) { |
| 1618 |
throw PgsqlException::createFromPhpError(); |
| 1619 |
} |
| 1620 |
} |
| 1621 |
|
| 1622 |
|
| 1623 |
/** |
| 1624 |
* Sends a request to create a prepared statement with the given parameters, |
| 1625 |
* without waiting for completion. |
| 1626 |
* |
| 1627 |
* This is an asynchronous version of pg_prepare: it returns TRUE if it was able to |
| 1628 |
* dispatch the request, and FALSE if not. After a successful call, call |
| 1629 |
* pg_get_result to determine whether the server successfully created the |
| 1630 |
* prepared statement. The function's parameters are handled identically to |
| 1631 |
* pg_prepare. Like pg_prepare, it will not work |
| 1632 |
* on pre-7.4 versions of PostgreSQL. |
| 1633 |
* |
| 1634 |
* @param resource $connection PostgreSQL database connection resource. When |
| 1635 |
* connection is not present, the default connection |
| 1636 |
* is used. The default connection is the last connection made by |
| 1637 |
* pg_connect or pg_pconnect. |
| 1638 |
* @param string $stmtname The name to give the prepared statement. Must be unique per-connection. If |
| 1639 |
* "" is specified, then an unnamed statement is created, overwriting any |
| 1640 |
* previously defined unnamed statement. |
| 1641 |
* @param string $query The parameterized SQL statement. Must contain only a single statement. |
| 1642 |
* (multiple statements separated by semi-colons are not allowed.) If any parameters |
| 1643 |
* are used, they are referred to as $1, $2, etc. |
| 1644 |
* @throws PgsqlException |
| 1645 |
* |
| 1646 |
*/ |
| 1647 |
function pg_send_prepare($connection, string $stmtname, string $query): void |
| 1648 |
{ |
| 1649 |
error_clear_last(); |
| 1650 |
$result = \pg_send_prepare($connection, $stmtname, $query); |
| 1651 |
if ($result === false) { |
| 1652 |
throw PgsqlException::createFromPhpError(); |
| 1653 |
} |
| 1654 |
} |
| 1655 |
|
| 1656 |
|
| 1657 |
/** |
| 1658 |
* Submits a command and separate parameters to the server without |
| 1659 |
* waiting for the result(s). |
| 1660 |
* |
| 1661 |
* This is equivalent to pg_send_query except that query |
| 1662 |
* parameters can be specified separately from the |
| 1663 |
* query string. The function's parameters are |
| 1664 |
* handled identically to pg_query_params. Like |
| 1665 |
* pg_query_params, it will not work on pre-7.4 PostgreSQL |
| 1666 |
* connections, and it allows only one command in the query string. |
| 1667 |
* |
| 1668 |
* @param resource $connection PostgreSQL database connection resource. |
| 1669 |
* @param string $query The parameterized SQL statement. Must contain only a single statement. |
| 1670 |
* (multiple statements separated by semi-colons are not allowed.) If any parameters |
| 1671 |
* are used, they are referred to as $1, $2, etc. |
| 1672 |
* @param array $params An array of parameter values to substitute for the $1, $2, etc. placeholders |
| 1673 |
* in the original prepared query string. The number of elements in the array |
| 1674 |
* must match the number of placeholders. |
| 1675 |
* @throws PgsqlException |
| 1676 |
* |
| 1677 |
*/ |
| 1678 |
function pg_send_query_params($connection, string $query, array $params): void |
| 1679 |
{ |
| 1680 |
error_clear_last(); |
| 1681 |
$result = \pg_send_query_params($connection, $query, $params); |
| 1682 |
if ($result === false) { |
| 1683 |
throw PgsqlException::createFromPhpError(); |
| 1684 |
} |
| 1685 |
} |
| 1686 |
|
| 1687 |
|
| 1688 |
/** |
| 1689 |
* pg_send_query sends a query or queries asynchronously to the |
| 1690 |
* connection. Unlike |
| 1691 |
* pg_query, it can send multiple queries at once to |
| 1692 |
* PostgreSQL and get the results one by one using |
| 1693 |
* pg_get_result. |
| 1694 |
* |
| 1695 |
* Script execution is not blocked while the queries are executing. Use |
| 1696 |
* pg_connection_busy to check if the connection is |
| 1697 |
* busy (i.e. the query is executing). Queries may be cancelled using |
| 1698 |
* pg_cancel_query. |
| 1699 |
* |
| 1700 |
* Although the user can send multiple queries at once, multiple queries |
| 1701 |
* cannot be sent over a busy connection. If a query is sent while |
| 1702 |
* the connection is busy, it waits until the last query is finished and |
| 1703 |
* discards all its results. |
| 1704 |
* |
| 1705 |
* @param resource $connection PostgreSQL database connection resource. |
| 1706 |
* @param string $query The SQL statement or statements to be executed. |
| 1707 |
* |
| 1708 |
* Data inside the query should be properly escaped. |
| 1709 |
* @throws PgsqlException |
| 1710 |
* |
| 1711 |
*/ |
| 1712 |
function pg_send_query($connection, string $query): void |
| 1713 |
{ |
| 1714 |
error_clear_last(); |
| 1715 |
$result = \pg_send_query($connection, $query); |
| 1716 |
if ($result === false) { |
| 1717 |
throw PgsqlException::createFromPhpError(); |
| 1718 |
} |
| 1719 |
} |
| 1720 |
|
| 1721 |
|
| 1722 |
/** |
| 1723 |
* pg_socket returns a read only resource |
| 1724 |
* corresponding to the socket underlying the given PostgreSQL connection. |
| 1725 |
* |
| 1726 |
* @param resource $connection PostgreSQL database connection resource. |
| 1727 |
* @return resource A socket resource on success. |
| 1728 |
* @throws PgsqlException |
| 1729 |
* |
| 1730 |
*/ |
| 1731 |
function pg_socket($connection) |
| 1732 |
{ |
| 1733 |
error_clear_last(); |
| 1734 |
$result = \pg_socket($connection); |
| 1735 |
if ($result === false) { |
| 1736 |
throw PgsqlException::createFromPhpError(); |
| 1737 |
} |
| 1738 |
return $result; |
| 1739 |
} |
| 1740 |
|
| 1741 |
|
| 1742 |
/** |
| 1743 |
* pg_trace enables tracing of the PostgreSQL |
| 1744 |
* frontend/backend communication to a file. To fully understand the results, |
| 1745 |
* one needs to be familiar with the internals of PostgreSQL |
| 1746 |
* communication protocol. |
| 1747 |
* |
| 1748 |
* For those who are not, it can still be |
| 1749 |
* useful for tracing errors in queries sent to the server, you |
| 1750 |
* could do for example grep '^To backend' |
| 1751 |
* trace.log and see what queries actually were sent to the |
| 1752 |
* PostgreSQL server. For more information, refer to the |
| 1753 |
* PostgreSQL Documentation. |
| 1754 |
* |
| 1755 |
* @param string $pathname The full path and file name of the file in which to write the |
| 1756 |
* trace log. Same as in fopen. |
| 1757 |
* @param string $mode An optional file access mode, same as for fopen. |
| 1758 |
* @param resource $connection PostgreSQL database connection resource. When |
| 1759 |
* connection is not present, the default connection |
| 1760 |
* is used. The default connection is the last connection made by |
| 1761 |
* pg_connect or pg_pconnect. |
| 1762 |
* @throws PgsqlException |
| 1763 |
* |
| 1764 |
*/ |
| 1765 |
function pg_trace(string $pathname, string $mode = "w", $connection = null): void |
| 1766 |
{ |
| 1767 |
error_clear_last(); |
| 1768 |
if ($connection !== null) { |
| 1769 |
$result = \pg_trace($pathname, $mode, $connection); |
| 1770 |
} else { |
| 1771 |
$result = \pg_trace($pathname, $mode); |
| 1772 |
} |
| 1773 |
if ($result === false) { |
| 1774 |
throw PgsqlException::createFromPhpError(); |
| 1775 |
} |
| 1776 |
} |
| 1777 |
|
| 1778 |
|
| 1779 |
/** |
| 1780 |
* pg_tty returns the TTY name that server |
| 1781 |
* side debugging output is sent to on the given PostgreSQL |
| 1782 |
* connection resource. |
| 1783 |
* |
| 1784 |
* @param resource $connection PostgreSQL database connection resource. When |
| 1785 |
* connection is not present, the default connection |
| 1786 |
* is used. The default connection is the last connection made by |
| 1787 |
* pg_connect or pg_pconnect. |
| 1788 |
* @return string A string containing the debug TTY of |
| 1789 |
* the connection. |
| 1790 |
* @throws PgsqlException |
| 1791 |
* |
| 1792 |
*/ |
| 1793 |
function pg_tty($connection = null): string |
| 1794 |
{ |
| 1795 |
error_clear_last(); |
| 1796 |
if ($connection !== null) { |
| 1797 |
$result = \pg_tty($connection); |
| 1798 |
} else { |
| 1799 |
$result = \pg_tty(); |
| 1800 |
} |
| 1801 |
if ($result === false) { |
| 1802 |
throw PgsqlException::createFromPhpError(); |
| 1803 |
} |
| 1804 |
return $result; |
| 1805 |
} |
| 1806 |
|
| 1807 |
|
| 1808 |
/** |
| 1809 |
* pg_update updates records that matches |
| 1810 |
* condition with data. If |
| 1811 |
* options is specified, |
| 1812 |
* pg_convert is applied to |
| 1813 |
* data with specified options. |
| 1814 |
* |
| 1815 |
* pg_update updates records specified by |
| 1816 |
* assoc_array which has |
| 1817 |
* field=>value. |
| 1818 |
* |
| 1819 |
* If options is specified, |
| 1820 |
* pg_convert is applied to |
| 1821 |
* assoc_array with the specified flags. |
| 1822 |
* |
| 1823 |
* By default pg_update passes raw values. Values |
| 1824 |
* must be escaped or PGSQL_DML_ESCAPE option must be |
| 1825 |
* specified. PGSQL_DML_ESCAPE quotes and escapes |
| 1826 |
* parameters/identifiers. Therefore, table/column names became case |
| 1827 |
* sensitive. |
| 1828 |
* |
| 1829 |
* Note that neither escape nor prepared query can protect LIKE query, |
| 1830 |
* JSON, Array, Regex, etc. These parameters should be handled |
| 1831 |
* according to their contexts. i.e. Escape/validate values. |
| 1832 |
* |
| 1833 |
* @param resource $connection PostgreSQL database connection resource. |
| 1834 |
* @param string $table_name Name of the table into which to update rows. |
| 1835 |
* @param array $data An array whose keys are field names in the table table_name, |
| 1836 |
* and whose values are what matched rows are to be updated to. |
| 1837 |
* @param array $condition An array whose keys are field names in the table table_name, |
| 1838 |
* and whose values are the conditions that a row must meet to be updated. |
| 1839 |
* @param int $options Any number of PGSQL_CONV_FORCE_NULL, |
| 1840 |
* PGSQL_DML_NO_CONV, |
| 1841 |
* PGSQL_DML_ESCAPE, |
| 1842 |
* PGSQL_DML_EXEC, |
| 1843 |
* PGSQL_DML_ASYNC or |
| 1844 |
* PGSQL_DML_STRING combined. If PGSQL_DML_STRING is part of the |
| 1845 |
* options then query string is returned. When PGSQL_DML_NO_CONV |
| 1846 |
* or PGSQL_DML_ESCAPE is set, it does not call pg_convert internally. |
| 1847 |
* @return mixed Returns TRUE on success. Returns string if PGSQL_DML_STRING is passed |
| 1848 |
* via options. |
| 1849 |
* @throws PgsqlException |
| 1850 |
* |
| 1851 |
*/ |
| 1852 |
function pg_update($connection, string $table_name, array $data, array $condition, int $options = PGSQL_DML_EXEC) |
| 1853 |
{ |
| 1854 |
error_clear_last(); |
| 1855 |
$result = \pg_update($connection, $table_name, $data, $condition, $options); |
| 1856 |
if ($result === false) { |
| 1857 |
throw PgsqlException::createFromPhpError(); |
| 1858 |
} |
| 1859 |
return $result; |
| 1860 |
} |
| 1861 |
|
| 1862 |
|
| 1863 |
/** |
| 1864 |
* pg_version returns an array with the client, protocol |
| 1865 |
* and server version. Protocol and server versions are only available if PHP |
| 1866 |
* was compiled with PostgreSQL 7.4 or later. |
| 1867 |
* |
| 1868 |
* For more detailed server information, use pg_parameter_status. |
| 1869 |
* |
| 1870 |
* @param resource $connection PostgreSQL database connection resource. When |
| 1871 |
* connection is not present, the default connection |
| 1872 |
* is used. The default connection is the last connection made by |
| 1873 |
* pg_connect or pg_pconnect. |
| 1874 |
* @return array Returns an array with client, protocol |
| 1875 |
* and server keys and values (if available) or invalid connection. |
| 1876 |
* @throws PgsqlException |
| 1877 |
* |
| 1878 |
*/ |
| 1879 |
function pg_version($connection = null): array |
| 1880 |
{ |
| 1881 |
error_clear_last(); |
| 1882 |
if ($connection !== null) { |
| 1883 |
$result = \pg_version($connection); |
| 1884 |
} else { |
| 1885 |
$result = \pg_version(); |
| 1886 |
} |
| 1887 |
if ($result === false) { |
| 1888 |
throw PgsqlException::createFromPhpError(); |
| 1889 |
} |
| 1890 |
return $result; |
| 1891 |
} |
| 1892 |
|