27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
# File 'lib/mover.rb', line 27
def move_to(to_class, options={})
from_class = self
conditions = options[:conditions] || '1'
conditions = self.sanitize_sql(conditions)
where = "WHERE #{conditions}"
magic = options[:magic] || 'moved_at'
from = {
:columns => from_class.column_names,
:table => from_class.table_name
}
to = {
:columns => to_class.column_names,
:table => to_class.table_name
}
insert = (from[:columns] & to[:columns]).inject({}) do |hash, column|
if column == magic && !options[:migrate]
hash[column] = Time.now.utc
else
hash[column] = column
end
hash
end
if to[:columns].include?(magic) && !from[:columns].include?(magic)
insert[magic] = Time.now.utc
end
insert = insert.inject({}) do |hash, (column, value)|
if value.is_a?(::Time)
hash[connection.quote_column_name(column)] = connection.quote(value)
else
hash[connection.quote_column_name(column)] = connection.quote_column_name(value)
end
hash
end
collector = lambda do |(classes, block)|
classes.collect! { |c| eval(c.to_s) }
block if classes.include?(to_class) || classes.empty?
end
before = (@before_move || []).collect(&collector).compact
after = (@after_move || []).collect(&collector).compact
instances =
if options[:instance]
[ options[:instance] ]
elsif before.empty? && after.empty?
[]
else
self.find(:all, :conditions => conditions)
end
options.delete(:instance)
exec_callbacks = lambda do |callbacks|
callbacks.each do |block|
instances.each do |instance|
instance.move_options = options
instance.instance_eval(&block)
instance.move_options = nil
end
end
end
transaction do
exec_callbacks.call before
if options[:quick]
connection.execute(" INSERT INTO \#{to[:table]} (\#{insert.keys.join(', ')})\n SELECT \#{insert.values.join(', ')}\n FROM \#{from[:table]}\n \#{where}\n SQL\n elsif !options[:generic] && connection.class.to_s.include?('Mysql')\n update = insert.collect do |column, value|\n if value.include?(\"'\")\n \"\#{to[:table]}.\#{column} = \#{value}\"\n else\n \"\#{to[:table]}.\#{column} = \#{from[:table]}.\#{value}\"\n end\n end\n \n connection.execute(<<-SQL)\n INSERT INTO \#{to[:table]} (\#{insert.keys.join(', ')})\n SELECT \#{insert.values.join(', ')}\n FROM \#{from[:table]}\n \#{where}\n ON DUPLICATE KEY\n UPDATE \#{update.join(', ')};\n SQL\n else\n conditions.gsub!(to[:table], 't')\n conditions.gsub!(from[:table], 'f')\n conditions.gsub!(/\\\"id\\\"/,'f.id') if connection.class.to_s.include?('PostgreSQL')\n \n select = insert.values.collect { |i| i.include?(\"'\") ? i : \"f.\#{i}\" }\n set = insert.collect do |column, value|\n prefix = 't.' unless connection.class.to_s.include?('PostgreSQL')\n if value.include?(\"'\")\n \"\#{prefix}\#{column} = \#{value}\"\n else\n \"\#{prefix}\#{column} = f.\#{value}\"\n end\n end\n \n if connection.class.to_s.include?('PostgreSQL')\n connection.execute(<<-SQL)\n UPDATE \#{to[:table]}\n AS t\n SET \#{set.join(', ')}\n FROM \#{from[:table]}\n AS f\n WHERE f.id = t.id\n AND \#{conditions}\n SQL\n else\n connection.execute(<<-SQL)\n UPDATE \#{to[:table]}\n AS t\n INNER JOIN \#{from[:table]}\n AS f\n ON f.id = t.id\n AND \#{conditions}\n SET \#{set.join(', ')}\n SQL\n end\n \n connection.execute(<<-SQL)\n INSERT INTO \#{to[:table]} (\#{insert.keys.join(', ')})\n SELECT \#{select.join(', ')}\n FROM \#{from[:table]}\n AS f\n LEFT OUTER JOIN \#{to[:table]}\n AS t\n ON f.id = t.id\n WHERE (\n t.id IS NULL\n AND \#{conditions}\n )\n SQL\n end\n \n unless options[:copy]\n connection.execute(\"DELETE FROM \#{from[:table]} \#{where}\")\n end\n \n exec_callbacks.call after\n end\nend\n")
|