@@ -12,18 +12,32 @@ def self.patch_persistence
12
12
Persistence . module_eval do
13
13
private
14
14
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
15
30
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 )
18
32
19
33
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 ) )
21
35
end
22
36
23
- self . id ||= new_id if self . class . primary_key
37
+ self . id ||= new_id if @ primary_key
24
38
25
39
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 )
27
41
end
28
42
29
43
@new_record = false
@@ -33,8 +47,22 @@ def _create_record(attribute_names = self.attribute_names)
33
47
id
34
48
end
35
49
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
36
65
def _update_record ( attribute_names = self . attribute_names )
37
- attribute_names &= self . class . column_names
38
66
attribute_names = attributes_for_update ( attribute_names )
39
67
40
68
if attribute_names . empty?
@@ -46,7 +74,7 @@ def _update_record(attribute_names = self.attribute_names)
46
74
end
47
75
48
76
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 )
50
78
end
51
79
52
80
@_trigger_update_callback = affected_rows == 1
@@ -61,8 +89,12 @@ def _update_record(attribute_names = self.attribute_names)
61
89
62
90
def self . patch_database_statements
63
91
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
64
96
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 )
66
98
67
99
exec_query ( sql , name , binds ) . tap do |result |
68
100
if @_returning_attributes . present?
@@ -71,6 +103,10 @@ def exec_insert(sql, name = nil, binds = [], pk = nil, sequence_name = nil)
71
103
end
72
104
end
73
105
106
+ # def update(arel, name = nil, binds = [])
107
+ # sql, binds = to_sql_and_binds(arel, binds)
108
+ # exec_update(sql, name, binds)
109
+ # end
74
110
def update ( arel , name = nil , binds = [ ] )
75
111
sql , binds = to_sql_and_binds ( arel , binds )
76
112
@@ -89,7 +125,20 @@ def self.patch_postgresql_database_statements
89
125
ConnectionAdapters ::PostgreSQL ::DatabaseStatements . module_eval do
90
126
private
91
127
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 )
93
142
if pk . nil?
94
143
# Extract the table from the insert sql. Yuck.
95
144
table_ref = extract_table_ref_from_insert_sql ( sql )
@@ -117,7 +166,27 @@ def self.patch_postgresql_adapter
117
166
118
167
private
119
168
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
120
185
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
+
121
190
if without_prepared_statement? ( binds )
122
191
result = exec_no_cache ( sql , name , [ ] )
123
192
elsif !prepare
0 commit comments