Skip to content

Commit 7a3f971

Browse files
committed
Fix pg_lo_read() and make tests useful
1 parent ffe06b1 commit 7a3f971

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

ext/pgsql/pgsql.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2562,31 +2562,31 @@ PHP_FUNCTION(pg_lo_close)
25622562
PHP_FUNCTION(pg_lo_read)
25632563
{
25642564
zval *pgsql_id;
2565-
zend_long len = 0;
2566-
size_t buf_len = PGSQL_LO_READ_BUF_SIZE;
2565+
zend_long buffer_length = PGSQL_LO_READ_BUF_SIZE;
25672566
int nbytes;
25682567
zend_string *buf;
25692568
pgLofp *pgsql;
25702569

2571-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &pgsql_id, &len) == FAILURE) {
2570+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|l", &pgsql_id, &buffer_length) == FAILURE) {
25722571
RETURN_THROWS();
25732572
}
25742573

25752574
if ((pgsql = (pgLofp *)zend_fetch_resource(Z_RES_P(pgsql_id), "PostgreSQL large object", le_lofp)) == NULL) {
25762575
RETURN_THROWS();
25772576
}
25782577

2579-
if (len < 0) {
2578+
if (buffer_length < 0) {
25802579
zend_argument_value_error(2, "must be greater or equal than 0");
25812580
RETURN_THROWS();
25822581
}
25832582

2584-
buf = zend_string_alloc(buf_len, 0);
2583+
buf = zend_string_alloc(buffer_length, 0);
25852584
if ((nbytes = lo_read((PGconn *)pgsql->conn, pgsql->lofd, ZSTR_VAL(buf), ZSTR_LEN(buf)))<0) {
25862585
zend_string_efree(buf);
25872586
RETURN_FALSE;
25882587
}
25892588

2589+
/* TODO Use truncate API? */
25902590
ZSTR_LEN(buf) = nbytes;
25912591
ZSTR_VAL(buf)[ZSTR_LEN(buf)] = '\0';
25922592
RETURN_NEW_STR(buf);

ext/pgsql/tests/05large_object.phpt

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,32 @@ $oid = pg_lo_create ($db);
1515
if (!$oid) echo ("pg_lo_create() error\n");
1616
$handle = pg_lo_open ($db, $oid, "w");
1717
if (!$handle) echo ("pg_lo_open() error\n");
18-
pg_lo_write ($handle, "large object data\n");
18+
pg_lo_write ($handle, "large object data");
1919
pg_lo_close ($handle);
2020
pg_exec ($db, "commit");
2121

2222
echo "open/read/tell/seek/close LO\n";
2323
pg_exec ($db, "begin");
2424
$handle = pg_lo_open ($db, $oid, "w");
25-
pg_lo_read($handle, 100);
26-
pg_lo_tell($handle);
27-
pg_lo_seek($handle, 2);
25+
var_dump(pg_lo_read($handle, 5));
26+
var_dump(pg_lo_tell($handle));
27+
var_dump(pg_lo_seek($handle, 2, /* PGSQL_SEEK_CUR */)); // This is the default so move cursor from 5
28+
var_dump(pg_lo_read($handle, 100)); // Read to the end because chunk is larger then remaining content
29+
var_dump(pg_lo_tell($handle));
30+
var_dump(pg_lo_seek($handle, 0, PGSQL_SEEK_SET)); /* Reset cursor to beginning */
31+
var_dump(pg_lo_read($handle));
32+
var_dump(pg_lo_seek($handle, -4, PGSQL_SEEK_END)); /* Seek from the end */
33+
var_dump(pg_lo_read($handle));
2834
pg_lo_close($handle);
2935
pg_exec ($db, "commit");
3036

3137
echo "open/read_all/close LO\n";
3238
pg_exec ($db, "begin");
3339
$handle = pg_lo_open ($db, $oid, "w");
34-
pg_lo_read_all($handle);
40+
/* Will write to stdout */
41+
$bytesWritten = pg_lo_read_all($handle);
42+
echo "\n";
43+
var_dump($bytesWritten);
3544
if (pg_last_error()) echo "pg_lo_read_all() error\n".pg_last_error();
3645
pg_lo_close($handle);
3746
pg_exec ($db, "commit");
@@ -95,8 +104,18 @@ echo "OK";
95104
--EXPECT--
96105
create/write/close LO
97106
open/read/tell/seek/close LO
107+
string(5) "large"
108+
int(5)
109+
bool(true)
110+
string(10) "bject data"
111+
int(17)
112+
bool(true)
113+
string(17) "large object data"
114+
bool(true)
115+
string(4) "data"
98116
open/read_all/close LO
99117
large object data
118+
int(17)
100119
unlink LO
101120
Test without connection
102121
Test with string oid value

0 commit comments

Comments
 (0)