rapids 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
@@ -6,9 +6,10 @@ module Rapids
|
|
6
6
|
class CreateTrigger
|
7
7
|
include ModelExtensions
|
8
8
|
|
9
|
-
def initialize(model,batch_definition)
|
9
|
+
def initialize(model,batch_definition,options = {})
|
10
10
|
@model = model
|
11
11
|
@batch = batch_definition
|
12
|
+
@options = options
|
12
13
|
end
|
13
14
|
|
14
15
|
def to_sql
|
@@ -29,7 +30,7 @@ module Rapids
|
|
29
30
|
|
30
31
|
#{find_or_create_sql(@model,@batch.find_or_creates)}
|
31
32
|
|
32
|
-
insert into `#{@model.table_name}` (#{insert_header.join(",")})
|
33
|
+
#{@options[:replace] ? "replace" : "insert"} into `#{@model.table_name}` (#{insert_header.join(",")})
|
33
34
|
values (#{insert_values.join(",")});
|
34
35
|
end
|
35
36
|
TRIGGER_SQL
|
@@ -6,10 +6,11 @@ module Rapids
|
|
6
6
|
class InsertInto
|
7
7
|
include ModelExtensions
|
8
8
|
|
9
|
-
def initialize(model,batch_definition,values)
|
9
|
+
def initialize(model,batch_definition,values,options = {})
|
10
10
|
@model = model
|
11
11
|
@batch = batch_definition
|
12
12
|
@values = values
|
13
|
+
@options = options
|
13
14
|
end
|
14
15
|
|
15
16
|
def to_sql
|
data/lib/rapids/batch.rb
CHANGED
@@ -17,14 +17,14 @@ module Rapids
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
def batch_create(collection)
|
20
|
+
def batch_create(collection,options = {})
|
21
21
|
drop_batch_table_if_exists
|
22
22
|
|
23
23
|
create_batch_table
|
24
24
|
|
25
|
-
create_batch_trigger
|
25
|
+
create_batch_trigger(options)
|
26
26
|
|
27
|
-
batch_insert(collection)
|
27
|
+
batch_insert(collection,options)
|
28
28
|
end
|
29
29
|
|
30
30
|
def batch(&block)
|
@@ -44,14 +44,14 @@ module Rapids
|
|
44
44
|
connection.execute(create_table.to_sql)
|
45
45
|
end
|
46
46
|
|
47
|
-
def create_batch_trigger
|
48
|
-
create_trigger = CreateTrigger.new(self,@batch)
|
49
|
-
|
47
|
+
def create_batch_trigger(options)
|
48
|
+
create_trigger = CreateTrigger.new(self,@batch,options)
|
49
|
+
|
50
50
|
connection.execute(create_trigger.to_sql)
|
51
51
|
end
|
52
52
|
|
53
|
-
def batch_insert(values)
|
54
|
-
insert_into = InsertInto.new(self,@batch,values)
|
53
|
+
def batch_insert(values,options)
|
54
|
+
insert_into = InsertInto.new(self,@batch,values,options)
|
55
55
|
|
56
56
|
connection.execute(insert_into.to_sql)
|
57
57
|
end
|
data/lib/rapids/version.rb
CHANGED
@@ -32,5 +32,12 @@ module Rapids::Batch
|
|
32
32
|
create_trigger = CreateTrigger.new(Post,batch)
|
33
33
|
clean_sql(create_trigger.to_sql).should == "create trigger `$posts_batch_trigger` after insert on `$posts_batch` for each row\nbegin\ndeclare find_or_create$AltCategory integer;\nselect id from `alt_categories` where `name` <=> new.`foc$AltCategory$name` into find_or_create$AltCategory;\nif find_or_create$AltCategory is null then\ninsert into alt_categories (`name`)\nvalues (new.`foc$AltCategory$name`);\nend if;\ninsert into `posts` (`author_id`,`category`,`name`)\nvalues (new.`author_id`,new.`category`,new.`name`);\nend"
|
34
34
|
end
|
35
|
+
|
36
|
+
it "should generate sql for a replace insert instead" do
|
37
|
+
batch = Rapids::Batch::DefineBatch.new
|
38
|
+
|
39
|
+
create_trigger = CreateTrigger.new(Category,batch,:replace => true)
|
40
|
+
clean_sql(create_trigger.to_sql).should == "create trigger `$categories_batch_trigger` after insert on `$categories_batch` for each row\nbegin\nreplace into `categories` (`category`)\nvalues (new.`category`);\nend"
|
41
|
+
end
|
35
42
|
end
|
36
43
|
end
|
@@ -39,6 +39,16 @@ module Rapids::Batch
|
|
39
39
|
clean_sql(insert_into.to_sql).should == "INSERT INTO `$posts_batch` (`foc$AltCategory$name`,`author_id`,`category`,`name`) VALUES ('food',NULL,'food','Dining at 323 Butter St')"
|
40
40
|
end
|
41
41
|
|
42
|
+
it "should generate sql and ignore the replace option" do
|
43
|
+
batch = Rapids::Batch::DefineBatch.new
|
44
|
+
collection = %w{food politics}.map do |category_name|
|
45
|
+
Category.new(:category => category_name)
|
46
|
+
end
|
47
|
+
|
48
|
+
insert_into = InsertInto.new(Category,batch,collection,:replace => true)
|
49
|
+
clean_sql(insert_into.to_sql).should == "INSERT INTO `$categories_batch` (`category`) VALUES ('food'),('politics')"
|
50
|
+
end
|
51
|
+
|
42
52
|
describe "Without model objects" do
|
43
53
|
it "should generate sql for a simple batch definition" do
|
44
54
|
batch = Rapids::Batch::DefineBatch.new
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rapids
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 29
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 3
|
10
|
+
version: 0.1.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- James Smith
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-08-
|
18
|
+
date: 2011-08-26 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|