I am doing some multi_inserts of large sets of data using mysql and
I'm going to need to support insert ignore and on duplicate key
update.
So far here is what I came up with. These are some methods I added to
a Sequel::Model class
def self.multi_insert_ignore(*args)
def dataset.multi_insert_sql(columns,values)
sql = super(columns,values)
sql[0].gsub!(/^INSERT/,'INSERT IGNORE')
end
multi_insert(*args)
end
def self.multi_insert_update(*args)
def dataset.multi_insert_sql(columns,values)
sql = super(columns,values)
dup = columns.map {|column| "#{column}=VALUES(#{column})"}.join
(', ')
sql[0] << " ON DUPLICATE KEY UPDATE #{dup}"
end
multi_insert(*args)
end
Examples
INSERT IGNORE INTO `animals` (`name`, `kind`) VALUES ('sally',
'monkey'), ('bob', 'bird')
INSERT INTO `animals` (`name`, `kind`) VALUES ('sally', 'monkey2'),
('bob', 'bird2') ON DUPLICATE KEY UPDATE name=VALUES(name), kind=VALUES
(kind)
You can see my quick usage of it and the logs with it here
http://gist.github.com/84287
Does that approach seem reasonable? If I were to fork sequel to add
those methods, what class/module does that belong in? I'm assuming it
should go somewhere in a mysql specific class.
Thanks
Dusty Doris
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sequel-talk?hl=en
-~----------~----~----~----~------~----~------~--~---