@@ -8,7 +8,6 @@ class Backend
88      DEFAULT_NAMESPACE  =  "default" 
99      PG_LARGE_OBJECT_METADATA_TABLE  =  "pg_largeobject_metadata" 
1010      READ_CHUNK_SIZE  =  3000 
11-       INIT_CONNECTION_ARG_ERROR_MSG  =  "When initializing new Refile::Postgres::Backend first argument should be an instance of PG::Connection or a lambda/proc that returns it. When using ActiveRecord it is available as ActiveRecord::Base.connection.raw_connection" 
1211
1312      def  initialize ( connection_or_proc ,  max_size : nil ,  namespace : DEFAULT_NAMESPACE ,  registry_table : DEFAULT_REGISTRY_TABLE ) 
1413        @connection_or_proc  =  connection_or_proc 
@@ -22,50 +21,43 @@ def initialize(connection_or_proc, max_size: nil, namespace: DEFAULT_NAMESPACE,
2221
2322      def  registry_table 
2423        unless  @registry_table_validated 
25-           connection . exec  %{ 
26-             SELECT count(*) from pg_catalog.pg_tables 
27-             WHERE tablename = '#{ @registry_table }  
28-           }  do  |result |
29-             unless  result [ 0 ] [ "count" ] . to_i  > 0 
30-               raise  RegistryTableDoesNotExistError . new ( %{Please create a table "#{ @registry_table }  ) 
24+           with_connection  do  |connection |
25+             connection . exec_params ( "SELECT * FROM pg_catalog.pg_tables WHERE tablename = $1::varchar;" ,  [ @registry_table ] )  do  |result |
26+               if  result . count  != 1 
27+                 raise  RegistryTableDoesNotExistError . new ( %{Please create a table "#{ @registry_table }  ) 
28+               end 
3129            end 
3230          end 
3331          @registry_table_validated  =  true 
3432        end 
3533        @registry_table 
3634      end 
3735
38-       def  connection 
39-         if  has_active_connection? 
40-           @connection 
41-         else 
42-           obtain_new_connection 
43-         end 
44-       end 
45- 
4636      verify_uploadable  def  upload ( uploadable ) 
47-         oid  =  connection . lo_creat 
48-         ensure_in_transaction  do 
49-           begin 
50-             handle  =  connection . lo_open ( oid ,  PG ::INV_WRITE ) 
51-             connection . lo_truncate ( handle ,  0 ) 
52-             buffer  =  ""  # reuse the same buffer 
53-             until  uploadable . eof? 
54-               uploadable . read ( READ_CHUNK_SIZE ,  buffer ) 
55-               connection . lo_write ( handle ,  buffer ) 
37+         with_connection  do  |connection |
38+           oid  =  connection . lo_creat 
39+           ensure_in_transaction ( connection )  do 
40+             begin 
41+               handle  =  connection . lo_open ( oid ,  PG ::INV_WRITE ) 
42+               connection . lo_truncate ( handle ,  0 ) 
43+               buffer  =  ""  # reuse the same buffer 
44+               until  uploadable . eof? 
45+                 uploadable . read ( READ_CHUNK_SIZE ,  buffer ) 
46+                 connection . lo_write ( handle ,  buffer ) 
47+               end 
48+               uploadable . close 
49+               connection . exec_params ( "INSERT INTO #{ registry_table }  ,  [ oid ,  namespace ] ) 
50+               Refile ::File . new ( self ,  oid . to_s ) 
51+             ensure 
52+               connection . lo_close ( handle ) 
5653            end 
57-             uploadable . close 
58-             connection . exec_params ( "INSERT INTO #{ registry_table }  ,  [ oid ,  namespace ] ) 
59-             Refile ::File . new ( self ,  oid . to_s ) 
60-           ensure 
61-             connection . lo_close ( handle ) 
6254          end 
6355        end 
6456      end 
6557
6658      verify_id  def  open ( id ) 
6759        if  exists? ( id ) 
68-           Reader . new ( connection ,  id ) 
60+           Reader . new ( @connection_or_proc ,  id ) 
6961        else 
7062          raise  ArgumentError . new ( "No such attachment with ID: #{ id }  ) 
7163        end 
@@ -84,14 +76,16 @@ def connection
8476      end 
8577
8678      verify_id  def  exists? ( id ) 
87-         connection . exec_params ( %{ 
88-           SELECT count(*) FROM #{ registry_table }  
89-           INNER JOIN #{ PG_LARGE_OBJECT_METADATA_TABLE }  
90-           ON #{ registry_table } #{ PG_LARGE_OBJECT_METADATA_TABLE }  
91-           WHERE #{ registry_table }  
92-           AND #{ registry_table }  
93-         } ,  [ namespace ,  id . to_s . to_i ] )  do  |result |
94-           result [ 0 ] [ "count" ] . to_i  > 0 
79+         with_connection  do  |connection |
80+           connection . exec_params ( %{ 
81+             SELECT count(*) FROM #{ registry_table }  
82+             INNER JOIN #{ PG_LARGE_OBJECT_METADATA_TABLE }  
83+             ON #{ registry_table } #{ PG_LARGE_OBJECT_METADATA_TABLE }  
84+             WHERE #{ registry_table }  
85+             AND #{ registry_table }  
86+           } ,  [ namespace ,  id . to_s . to_i ] )  do  |result |
87+             result [ 0 ] [ "count" ] . to_i  > 0 
88+           end 
9589        end 
9690      end 
9791
@@ -105,43 +99,34 @@ def connection
10599
106100      verify_id  def  delete ( id ) 
107101        if  exists? ( id ) 
108-           ensure_in_transaction  do 
109-             connection . lo_unlink ( id . to_s . to_i ) 
110-             connection . exec_params ( "DELETE FROM #{ registry_table }  ,  [ id ] ) 
102+           with_connection  do  |connection |
103+             ensure_in_transaction ( connection )  do 
104+               connection . lo_unlink ( id . to_s . to_i ) 
105+               connection . exec_params ( "DELETE FROM #{ registry_table }  ,  [ id ] ) 
106+             end 
111107          end 
112108        end 
113109      end 
114110
115111      def  clear! ( confirm  =  nil ) 
116112        raise  Refile ::Confirm  unless  confirm  == :confirm 
117113        registry_table 
118-         ensure_in_transaction  do 
119-           connection . exec_params ( %{ 
120-             SELECT * FROM #{ registry_table }  
121-             INNER JOIN #{ PG_LARGE_OBJECT_METADATA_TABLE } #{ registry_table } #{ PG_LARGE_OBJECT_METADATA_TABLE }  
122-             WHERE #{ registry_table }  
123-           } ,  [ namespace ] )  do  |result |
124-             result . each_row  do  |row |
125-               connection . lo_unlink ( row [ 0 ] . to_s . to_i ) 
114+         with_connection  do  |connection |
115+           ensure_in_transaction ( connection )  do 
116+             connection . exec_params ( %{ 
117+               SELECT * FROM #{ registry_table }  
118+               INNER JOIN #{ PG_LARGE_OBJECT_METADATA_TABLE } #{ registry_table } #{ PG_LARGE_OBJECT_METADATA_TABLE }  
119+               WHERE #{ registry_table }  
120+             } ,  [ namespace ] )  do  |result |
121+               result . each_row  do  |row |
122+                 connection . lo_unlink ( row [ 0 ] . to_s . to_i ) 
123+               end 
126124            end 
125+             connection . exec_params ( "DELETE FROM #{ registry_table }  ,  [ namespace ] ) 
127126          end 
128-           connection . exec_params ( "DELETE FROM #{ registry_table }  ,  [ namespace ] ) 
129127        end 
130128      end 
131129
132-       private 
133- 
134-       def  has_active_connection? 
135-         @connection  && !@connection . finished? 
136-       end 
137- 
138-       def  obtain_new_connection 
139-         candidate  =  @connection_or_proc . is_a? ( Proc )  ? @connection_or_proc . call  : @connection_or_proc 
140-         unless  candidate . is_a? ( PG ::Connection ) 
141-           raise  ArgumentError . new ( INIT_CONNECTION_ARG_ERROR_MSG ) 
142-         end 
143-         @connection  =  candidate 
144-       end 
145130    end 
146131  end 
147132end 
0 commit comments