@@ -3643,6 +3643,14 @@ pgconn_send_flush_request(VALUE self)
3643
3643
* LARGE OBJECT SUPPORT
3644
3644
**************************************************************************/
3645
3645
3646
+ #define BLOCKING_BEGIN (conn ) do { \
3647
+ int old_nonblocking = PQisnonblocking(conn); \
3648
+ PQsetnonblocking(conn, 0);
3649
+
3650
+ #define BLOCKING_END (th ) \
3651
+ PQsetnonblocking(conn, old_nonblocking); \
3652
+ } while(0);
3653
+
3646
3654
/*
3647
3655
* call-seq:
3648
3656
* conn.lo_creat( [mode] ) -> Integer
@@ -3663,7 +3671,10 @@ pgconn_locreat(int argc, VALUE *argv, VALUE self)
3663
3671
else
3664
3672
mode = NUM2INT (nmode );
3665
3673
3666
- lo_oid = lo_creat (conn , mode );
3674
+ BLOCKING_BEGIN (conn )
3675
+ lo_oid = lo_creat (conn , mode );
3676
+ BLOCKING_END (conn )
3677
+
3667
3678
if (lo_oid == 0 )
3668
3679
pg_raise_conn_error ( rb_ePGerror , self , "lo_creat failed ");
3669
3680
@@ -3708,7 +3719,10 @@ pgconn_loimport(VALUE self, VALUE filename)
3708
3719
3709
3720
Check_Type (filename , T_STRING );
3710
3721
3711
- lo_oid = lo_import (conn , StringValueCStr (filename ));
3722
+ BLOCKING_BEGIN (conn )
3723
+ lo_oid = lo_import (conn , StringValueCStr (filename ));
3724
+ BLOCKING_END (conn )
3725
+
3712
3726
if (lo_oid == 0 ) {
3713
3727
pg_raise_conn_error ( rb_ePGerror , self , "%s" , PQerrorMessage (conn ));
3714
3728
}
@@ -3726,11 +3740,16 @@ pgconn_loexport(VALUE self, VALUE lo_oid, VALUE filename)
3726
3740
{
3727
3741
PGconn * conn = pg_get_pgconn (self );
3728
3742
Oid oid ;
3743
+ int ret ;
3729
3744
Check_Type (filename , T_STRING );
3730
3745
3731
3746
oid = NUM2UINT (lo_oid );
3732
3747
3733
- if (lo_export (conn , oid , StringValueCStr (filename )) < 0 ) {
3748
+ BLOCKING_BEGIN (conn )
3749
+ ret = lo_export (conn , oid , StringValueCStr (filename ));
3750
+ BLOCKING_END (conn )
3751
+
3752
+ if (ret < 0 ) {
3734
3753
pg_raise_conn_error ( rb_ePGerror , self , "%s" , PQerrorMessage (conn ));
3735
3754
}
3736
3755
return Qnil ;
@@ -3761,7 +3780,11 @@ pgconn_loopen(int argc, VALUE *argv, VALUE self)
3761
3780
else
3762
3781
mode = NUM2INT (nmode );
3763
3782
3764
- if ((fd = lo_open (conn , lo_oid , mode )) < 0 ) {
3783
+ BLOCKING_BEGIN (conn )
3784
+ fd = lo_open (conn , lo_oid , mode );
3785
+ BLOCKING_END (conn )
3786
+
3787
+ if (fd < 0 ) {
3765
3788
pg_raise_conn_error ( rb_ePGerror , self , "can't open large object: %s" , PQerrorMessage (conn ));
3766
3789
}
3767
3790
return INT2FIX (fd );
@@ -3786,8 +3809,12 @@ pgconn_lowrite(VALUE self, VALUE in_lo_desc, VALUE buffer)
3786
3809
if ( RSTRING_LEN (buffer ) < 0 ) {
3787
3810
pg_raise_conn_error ( rb_ePGerror , self , "write buffer zero string" );
3788
3811
}
3789
- if ((n = lo_write (conn , fd , StringValuePtr (buffer ),
3790
- RSTRING_LEN (buffer ))) < 0 ) {
3812
+ BLOCKING_BEGIN (conn )
3813
+ n = lo_write (conn , fd , StringValuePtr (buffer ),
3814
+ RSTRING_LEN (buffer ));
3815
+ BLOCKING_END (conn )
3816
+
3817
+ if (n < 0 ) {
3791
3818
pg_raise_conn_error ( rb_ePGerror , self , "lo_write failed: %s" , PQerrorMessage (conn ));
3792
3819
}
3793
3820
@@ -3815,7 +3842,12 @@ pgconn_loread(VALUE self, VALUE in_lo_desc, VALUE in_len)
3815
3842
pg_raise_conn_error ( rb_ePGerror , self , "negative length %d given" , len );
3816
3843
3817
3844
buffer = ALLOC_N (char , len );
3818
- if ((ret = lo_read (conn , lo_desc , buffer , len )) < 0 )
3845
+
3846
+ BLOCKING_BEGIN (conn )
3847
+ ret = lo_read (conn , lo_desc , buffer , len );
3848
+ BLOCKING_END (conn )
3849
+
3850
+ if (ret < 0 )
3819
3851
pg_raise_conn_error ( rb_ePGerror , self , "lo_read failed ");
3820
3852
3821
3853
if (ret == 0 ) {
@@ -3845,7 +3877,11 @@ pgconn_lolseek(VALUE self, VALUE in_lo_desc, VALUE offset, VALUE whence)
3845
3877
int lo_desc = NUM2INT (in_lo_desc );
3846
3878
int ret ;
3847
3879
3848
- if ((ret = lo_lseek (conn , lo_desc , NUM2INT (offset ), NUM2INT (whence ))) < 0 ) {
3880
+ BLOCKING_BEGIN (conn )
3881
+ ret = lo_lseek (conn , lo_desc , NUM2INT (offset ), NUM2INT (whence ));
3882
+ BLOCKING_END (conn )
3883
+
3884
+ if (ret < 0 ) {
3849
3885
pg_raise_conn_error ( rb_ePGerror , self , "lo_lseek failed" );
3850
3886
}
3851
3887
@@ -3865,7 +3901,11 @@ pgconn_lotell(VALUE self, VALUE in_lo_desc)
3865
3901
PGconn * conn = pg_get_pgconn (self );
3866
3902
int lo_desc = NUM2INT (in_lo_desc );
3867
3903
3868
- if ((position = lo_tell (conn , lo_desc )) < 0 )
3904
+ BLOCKING_BEGIN (conn )
3905
+ position = lo_tell (conn , lo_desc );
3906
+ BLOCKING_END (conn )
3907
+
3908
+ if (position < 0 )
3869
3909
pg_raise_conn_error ( rb_ePGerror , self , "lo_tell failed ");
3870
3910
3871
3911
return INT2FIX (position );
@@ -3883,8 +3923,13 @@ pgconn_lotruncate(VALUE self, VALUE in_lo_desc, VALUE in_len)
3883
3923
PGconn * conn = pg_get_pgconn (self );
3884
3924
int lo_desc = NUM2INT (in_lo_desc );
3885
3925
size_t len = NUM2INT (in_len );
3926
+ int ret ;
3927
+
3928
+ BLOCKING_BEGIN (conn )
3929
+ ret = lo_truncate (conn ,lo_desc ,len );
3930
+ BLOCKING_END (conn )
3886
3931
3887
- if (lo_truncate ( conn , lo_desc , len ) < 0 )
3932
+ if (ret < 0 )
3888
3933
pg_raise_conn_error ( rb_ePGerror , self , "lo_truncate failed ");
3889
3934
3890
3935
return Qnil ;
@@ -3901,8 +3946,13 @@ pgconn_loclose(VALUE self, VALUE in_lo_desc)
3901
3946
{
3902
3947
PGconn * conn = pg_get_pgconn (self );
3903
3948
int lo_desc = NUM2INT (in_lo_desc );
3949
+ int ret ;
3950
+
3951
+ BLOCKING_BEGIN (conn )
3952
+ ret = lo_close (conn ,lo_desc );
3953
+ BLOCKING_END (conn )
3904
3954
3905
- if (lo_close ( conn , lo_desc ) < 0 )
3955
+ if (ret < 0 )
3906
3956
pg_raise_conn_error ( rb_ePGerror , self , "lo_close failed ");
3907
3957
3908
3958
return Qnil ;
@@ -3919,8 +3969,13 @@ pgconn_lounlink(VALUE self, VALUE in_oid)
3919
3969
{
3920
3970
PGconn * conn = pg_get_pgconn (self );
3921
3971
Oid oid = NUM2UINT (in_oid );
3972
+ int ret ;
3973
+
3974
+ BLOCKING_BEGIN (conn )
3975
+ ret = lo_unlink (conn ,oid );
3976
+ BLOCKING_END (conn )
3922
3977
3923
- if (lo_unlink ( conn , oid ) < 0 )
3978
+ if (ret < 0 )
3924
3979
pg_raise_conn_error ( rb_ePGerror , self , "lo_unlink failed ");
3925
3980
3926
3981
return Qnil ;
0 commit comments