@@ -12,18 +12,32 @@ def self.patch_persistence
1212 Persistence . module_eval do
1313 private
1414
15+ # def _create_record(attribute_names = self.attribute_names)
16+ # attribute_names = attributes_for_create(attribute_names)
17+
18+ # new_id = self.class._insert_record(
19+ # attributes_with_values(attribute_names)
20+ # )
21+
22+ # self.id ||= new_id if @primary_key
23+
24+ # @new_record = false
25+
26+ # yield(self) if block_given?
27+
28+ # id
29+ # end
1530 def _create_record ( attribute_names = self . attribute_names )
16- attribute_names &= self . class . column_names
17- attributes_values = attributes_with_values_for_create ( attribute_names )
31+ attribute_names = attributes_for_create ( attribute_names )
1832
1933 new_id , returned_attributes = self . class . connection . with_returning_attributes ( returning_attributes ) do
20- self . class . _insert_record ( attributes_values )
34+ self . class . _insert_record ( attributes_with_values ( attribute_names ) )
2135 end
2236
23- self . id ||= new_id if self . class . primary_key
37+ self . id ||= new_id if @ primary_key
2438
2539 returning_attributes . each do |attribute |
26- write_attribute ( attribute , returned_attributes [ attribute . to_s ] ) if returned_attributes . key? ( attribute . to_s )
40+ write_attribute ( attribute , returned_attributes [ attribute ] ) if returned_attributes . key? ( attribute )
2741 end
2842
2943 @new_record = false
@@ -33,8 +47,22 @@ def _create_record(attribute_names = self.attribute_names)
3347 id
3448 end
3549
50+ # def _update_record(attribute_names = self.attribute_names)
51+ # attribute_names = attributes_for_update(attribute_names)
52+
53+ # if attribute_names.empty?
54+ # affected_rows = 0
55+ # @_trigger_update_callback = true
56+ # else
57+ # affected_rows = _update_row(attribute_names)
58+ # @_trigger_update_callback = affected_rows == 1
59+ # end
60+
61+ # yield(self) if block_given?
62+
63+ # affected_rows
64+ # end
3665 def _update_record ( attribute_names = self . attribute_names )
37- attribute_names &= self . class . column_names
3866 attribute_names = attributes_for_update ( attribute_names )
3967
4068 if attribute_names . empty?
@@ -46,7 +74,7 @@ def _update_record(attribute_names = self.attribute_names)
4674 end
4775
4876 returning_attributes . each do |attribute |
49- write_attribute ( attribute , returned_attributes [ attribute . to_s ] ) if returned_attributes . key? ( attribute . to_s )
77+ write_attribute ( attribute , returned_attributes [ attribute ] ) if returned_attributes . key? ( attribute )
5078 end
5179
5280 @_trigger_update_callback = affected_rows == 1
@@ -61,8 +89,12 @@ def _update_record(attribute_names = self.attribute_names)
6189
6290 def self . patch_database_statements
6391 ConnectionAdapters ::DatabaseStatements . module_eval do
92+ # def exec_insert(sql, name = nil, binds = [], pk = nil, sequence_name = nil)
93+ # sql, binds = sql_for_insert(sql, pk, binds)
94+ # exec_query(sql, name, binds)
95+ # end
6496 def exec_insert ( sql , name = nil , binds = [ ] , pk = nil , sequence_name = nil )
65- sql , binds = sql_for_insert ( sql , pk , nil , sequence_name , binds )
97+ sql , binds = sql_for_insert ( sql , pk , binds )
6698
6799 exec_query ( sql , name , binds ) . tap do |result |
68100 if @_returning_attributes . present?
@@ -71,6 +103,10 @@ def exec_insert(sql, name = nil, binds = [], pk = nil, sequence_name = nil)
71103 end
72104 end
73105
106+ # def update(arel, name = nil, binds = [])
107+ # sql, binds = to_sql_and_binds(arel, binds)
108+ # exec_update(sql, name, binds)
109+ # end
74110 def update ( arel , name = nil , binds = [ ] )
75111 sql , binds = to_sql_and_binds ( arel , binds )
76112
@@ -89,7 +125,20 @@ def self.patch_postgresql_database_statements
89125 ConnectionAdapters ::PostgreSQL ::DatabaseStatements . module_eval do
90126 private
91127
92- def sql_for_insert ( sql , pk , id_value , sequence_name , binds )
128+ # def sql_for_insert(sql, pk, binds) # :nodoc:
129+ # if pk.nil?
130+ # # Extract the table from the insert sql. Yuck.
131+ # table_ref = extract_table_ref_from_insert_sql(sql)
132+ # pk = primary_key(table_ref) if table_ref
133+ # end
134+
135+ # if pk = suppress_composite_primary_key(pk)
136+ # sql = "#{sql} RETURNING #{quote_column_name(pk)}"
137+ # end
138+
139+ # super
140+ # end
141+ def sql_for_insert ( sql , pk , binds )
93142 if pk . nil?
94143 # Extract the table from the insert sql. Yuck.
95144 table_ref = extract_table_ref_from_insert_sql ( sql )
@@ -117,7 +166,27 @@ def self.patch_postgresql_adapter
117166
118167 private
119168
169+ # def execute_and_clear(sql, name, binds, prepare: false)
170+ # if preventing_writes? && write_query?(sql)
171+ # raise ActiveRecord::ReadOnlyError, "Write query attempted while in readonly mode: #{sql}"
172+ # end
173+
174+ # if without_prepared_statement?(binds)
175+ # result = exec_no_cache(sql, name, [])
176+ # elsif !prepare
177+ # result = exec_no_cache(sql, name, binds)
178+ # else
179+ # result = exec_cache(sql, name, binds)
180+ # end
181+ # ret = yield result
182+ # result.clear
183+ # ret
184+ # end
120185 def execute_and_clear ( sql , name , binds , prepare : false )
186+ if preventing_writes? && write_query? ( sql )
187+ raise ActiveRecord ::ReadOnlyError , "Write query attempted while in readonly mode: #{ sql } "
188+ end
189+
121190 if without_prepared_statement? ( binds )
122191 result = exec_no_cache ( sql , name , [ ] )
123192 elsif !prepare
0 commit comments