-
Notifications
You must be signed in to change notification settings - Fork 88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bulk import #168
Bulk import #168
Changes from 1 commit
814de94
d1b2134
93b6e67
6435d63
85c36a0
73e7de5
1a03145
54dd5b7
c74d126
e0e1be4
84364d9
2d15769
bbd2f7f
2425ad2
87009d5
acc4a6e
4b10e27
87268a0
f6dcf2a
e3e525a
371d0b4
9fe4bcb
5b31b80
027be38
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,6 +76,31 @@ class Granite::Adapter::Mysql < Granite::Adapter::Base | |
end | ||
end | ||
|
||
def import(table_name, primary_name, primary_auto, fields, model_array) | ||
statement = String.build do |stmt| | ||
stmt << "INSERT INTO #{quote(table_name)} (" | ||
stmt << "#{quote(primary_name)}, " unless primary_auto | ||
stmt << fields.map { |field| quote(field) }.join(", ") | ||
stmt << ") VALUES " | ||
|
||
model_array.each do |model| | ||
if model.responds_to? :to_h | ||
next unless model.valid? | ||
stmt << "(" | ||
stmt << model.to_h[primary_name].to_s + ", " unless primary_auto | ||
stmt << fields.map { |field| model.to_h[field].is_a?(String) ? "'" + model.to_h[field].to_s + "'" : model.to_h[field] }.join(", ") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not a fan of how I'm handling the quoting of String type fields. Any better ideas? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The adapters now provide a quote method but the functionality is has some bugs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The quoting method I do use for column/table names. However, since it quotes with the backtick that couldn't be used for string values afaik? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I didn't see that you are quoting values. A parameterized query is better for that, and I believe the adapters automatically do what they need to with values in parameterized queries. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That was my initial thought, however when I tried it I kept getting like I'll mess with it again and see if I can get the params working prob this weekend. |
||
stmt << ")," | ||
end | ||
end | ||
end.chomp(",") | ||
|
||
log statement | ||
|
||
open do |db| | ||
db.exec statement | ||
end | ||
end | ||
|
||
private def last_val | ||
return "SELECT LAST_INSERT_ID()" | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,21 @@ module Granite::ORM::Transactions | |
@updated_at : Time? | ||
@created_at : Time? | ||
|
||
# The import class method will run a batch INSERT statement for each model in the array | ||
# the array must contain only one model class | ||
# invalid model records will be skipped | ||
def self.import(model_array) | ||
raise ArgumentError.new("Model class mismatch: expected array of only #{self} models.") unless model_array.all? { |model| model.class == self } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this definition be changed so that the compiler will enforce the type constraint instead of a runtime error? First guess, something like this might work: def self.import(model_array : Array(self)) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ooo yes! I will do that tonight (still getting used to static typed land :P) |
||
begin | ||
@@adapter.import(table_name, primary_name, {{primary_auto}}, fields, model_array) | ||
rescue err | ||
raise DB::Error.new(err.message) | ||
end | ||
end | ||
|
||
# The save method will check to see if the primary exists yet. If it does it | ||
# will call the update method, otherwise it will call the create method. | ||
# This will update the timestamps apropriately. | ||
# This will update the timestamps appropriately. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🥇 |
||
def save | ||
return false unless valid? | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, this is an editor thing. You should setup and ignore your editor files in a system wide global gitignore file instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Blacksmoke16 interesting, Are you coding with Intellij Idea? just curious, What plugin are you using to have crystal support? 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@faustinoaq I am yes :) Using Rubymine with https://github.com/intellij-crystal/intellij-crystal
Is pretty meh in regards to full native support like Ruby in Rubymine, but it provides syntax highlighting and that's good enough for me atm.