MikeSofaer-saxual-replication 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/saxual-replication.rb
CHANGED
|
@@ -39,6 +39,10 @@ module SAXualReplication
|
|
|
39
39
|
@tag = value
|
|
40
40
|
end
|
|
41
41
|
|
|
42
|
+
def key_column(value)
|
|
43
|
+
@key_column = value
|
|
44
|
+
end
|
|
45
|
+
|
|
42
46
|
def datamapper_class
|
|
43
47
|
klass = self.dup
|
|
44
48
|
klass.send(:include, DataMapper::Resource)
|
|
@@ -60,8 +64,10 @@ module SAXualReplication
|
|
|
60
64
|
end
|
|
61
65
|
|
|
62
66
|
def sql(rows)
|
|
63
|
-
"INSERT INTO "+ @table_name + "(" + column_names.join(', ') + ", created_at, updated_at) VALUES " +
|
|
67
|
+
_sql = "INSERT INTO "+ @table_name + "(" + column_names.join(', ') + ", created_at, updated_at) VALUES " +
|
|
64
68
|
([("(" + (["?"] * (column_names.size + 2)).join(',') + ")")] * rows.size).join(',')
|
|
69
|
+
_sql << duplicate_key_clause if @key_column
|
|
70
|
+
_sql
|
|
65
71
|
end
|
|
66
72
|
def bind_values(rows)
|
|
67
73
|
names = column_names
|
|
@@ -70,17 +76,20 @@ module SAXualReplication
|
|
|
70
76
|
rows.each{|row| row.add_bind_values!(names, array, datetime)}
|
|
71
77
|
array
|
|
72
78
|
end
|
|
79
|
+
def duplicate_key_clause
|
|
80
|
+
" ON DUPLICATE KEY UPDATE " + (column_names - [:created_at, @key_column]).map {|c| c.to_s + "=VALUES(" + c.to_s + ")"}.join(', ')
|
|
81
|
+
end
|
|
73
82
|
|
|
74
83
|
def save(rows)
|
|
75
84
|
connection.execute sql(rows), *bind_values(rows)
|
|
76
85
|
end
|
|
77
86
|
end
|
|
78
87
|
|
|
79
|
-
|
|
80
|
-
#+ " ON DUPLICATE KEY UPDATE " + update_keys.join(', ')
|
|
81
|
-
|
|
82
88
|
def add_bind_values!(column_names, bind_array, datetime)
|
|
83
|
-
column_names.each
|
|
89
|
+
column_names.each do |c|
|
|
90
|
+
val = self.send(c)
|
|
91
|
+
bind_array << ((self.class.data_class(c) == DateTime && val) ? (DateTime.parse(val)) : val)
|
|
92
|
+
end
|
|
84
93
|
bind_array << datetime << datetime
|
|
85
94
|
end
|
|
86
95
|
|
|
@@ -71,6 +71,19 @@ describe "SAXualReplication" do
|
|
|
71
71
|
@klass.datamapper_class.all[1].title.should == "Someone's Cat"
|
|
72
72
|
end
|
|
73
73
|
end
|
|
74
|
+
describe "replication" do
|
|
75
|
+
it "should update fields on rows with a repeated primary key" do
|
|
76
|
+
@klass.key_column :written_on
|
|
77
|
+
@adapter.execute "create unique index key_column on documents(written_on)"
|
|
78
|
+
t = DateTime.now.to_s
|
|
79
|
+
xml1 = "<document><title>Hello, Everyone!</title><written_on>#{t}</written_on></document>"
|
|
80
|
+
xml2 = "<document><title>Someone's Cat</title><written_on>#{t}</written_on></document>"
|
|
81
|
+
@klass.save [@klass.parse(xml1)]
|
|
82
|
+
@klass.save [@klass.parse(xml2)]
|
|
83
|
+
@klass.datamapper_class.all.size.should == 1
|
|
84
|
+
@klass.datamapper_class.all[0].title.should == "Someone's Cat"
|
|
85
|
+
end
|
|
86
|
+
end
|
|
74
87
|
end
|
|
75
88
|
end
|
|
76
89
|
end
|