querybuilder 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,8 @@
1
+ == 1.1.1 2012-06-27
2
+
3
+ * Major enhancements
4
+ * Fixed bug with default scope (should only be applied on last context).
5
+
1
6
  == 1.1.0 2011-11-01
2
7
 
3
8
  * Major enhancements
@@ -1,3 +1,3 @@
1
1
  module QueryBuilder
2
- VERSION = '1.1.0'
2
+ VERSION = '1.1.1'
3
3
  end
@@ -202,6 +202,15 @@ module QueryBuilder
202
202
  def default(key)
203
203
  @default[key] || self.class.defaults[key]
204
204
  end
205
+
206
+ # The passed :default scope is only applied on last context.
207
+ def default_scope(context)
208
+ if context[:last]
209
+ default(:scope)
210
+ else
211
+ self.class.defaults[:scope]
212
+ end
213
+ end
205
214
 
206
215
  def process(sxp)
207
216
  return sxp if sxp.kind_of?(String)
@@ -800,7 +809,7 @@ module QueryBuilder
800
809
  context[:scope_type] = nil
801
810
  # post scope
802
811
  @query.add_table(use_name, table_name, avoid_alias)
803
- apply_scope(context[:scope] || default(:scope))
812
+ apply_scope(context[:scope] || default_scope(context))
804
813
  else
805
814
  # scope already applied / skip
806
815
  @query.add_table(use_name, table_name, avoid_alias)
data/querybuilder.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{querybuilder}
8
- s.version = "1.1.0"
8
+ s.version = "1.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Gaspard Bucher"]
12
- s.date = %q{2011-11-01}
12
+ s.date = %q{2012-06-27}
13
13
  s.description = %q{QueryBuilder is an interpreter for the "pseudo sql" language. This language
14
14
  can be used for two purposes:
15
15
 
@@ -90,6 +90,9 @@ class DummyProcessor < QueryBuilder::Processor
90
90
  when 'coalesce'
91
91
  args = [arg] + args.map{|a| process(a)}
92
92
  "COALESCE(#{args.join(',')})"
93
+ when 'min'
94
+ args = [arg] + args.map{|a| process(a)}
95
+ "MIN(#{args.join(',')})"
93
96
  else
94
97
  super
95
98
  end
@@ -100,3 +100,8 @@ function_with_multiple_args:
100
100
  src: "objects where name.coalesce(id,'foo') = 'foo'"
101
101
  sxp: '[:query, [:filter, [:relation, "objects"], [:"=", [:function, [:field, "name"], [:method, "coalesce"], [:field, "id"], [:string, "foo"]], [:string, "foo"]]]]'
102
102
  res: "[%Q{SELECT objects.* FROM objects WHERE COALESCE(objects.name,objects.id,'foo') = 'foo' AND objects.parent_id = ?}, id]"
103
+
104
+ function_in_order:
105
+ src: "objects order by event_at.coalesce(0)"
106
+ sxp: '[:query, [:order, [:relation, "objects"], [:function, [:field, "event_at"], [:method, "coalesce"], [:integer, "0"]]]]'
107
+ res: "[%Q{SELECT objects.* FROM objects WHERE objects.parent_id = ? ORDER BY COALESCE(objects.event_at,0)}, id]"
@@ -138,6 +138,11 @@ functions:
138
138
  sxp: '[:query, [:filter, [:relation, "objects"], [:"=", [:function, [:field, "event_at"], [:method, "year"]], [:function, [:field, "REF_DATE"], [:method, "year"]]]]]'
139
139
  res: "[%Q{SELECT objects.* FROM objects WHERE strftime('%Y',objects.event_at) = strftime('%Y',now()) AND objects.parent_id = ?}, id]"
140
140
 
141
+ nested_functions:
142
+ src: "objects where event_at.year.min = 2006 group by project_id"
143
+ sxp: '[:query, [:group, [:filter, [:relation, "objects"], [:"=", [:function, [:function, [:field, "event_at"], [:method, "year"]], [:method, "min"]], [:integer, "2006"]]], [:field, "project_id"]]]'
144
+ res: "[%Q{SELECT objects.* FROM objects WHERE MIN(strftime('%Y',objects.event_at)) = 2006 AND objects.parent_id = ? GROUP BY objects.project_id}, id]"
145
+
141
146
  filter_empty_literal:
142
147
  src: "objects where \"\" = ''"
143
148
  sxp: '[:query, [:filter, [:relation, "objects"], [:"=", [:dstring, ""], [:string, ""]]]]'
@@ -37,6 +37,10 @@ class DummyQueryBuilder < Test::Unit::TestCase
37
37
  assert_equal '[%Q{SELECT objects.* FROM objects WHERE objects.project_id = ?}, project_id]', subject.new('objects', :default => {:scope => 'project'}).query.to_s
38
38
  assert_equal '[%Q{SELECT objects.* FROM objects WHERE objects.parent_id = ?}, id]', subject.new('objects').query.to_s
39
39
  end
40
+
41
+ should 'not overwrite defaults before last' do
42
+ assert_equal '%Q{SELECT objects.* FROM objects,objects AS ob1 WHERE objects.parent_id = ob1.id GROUP BY objects.id}', subject.new('objects from objects', :default => {:scope => 'site'}).query.to_s
43
+ end
40
44
  end
41
45
 
42
46
  context 'Including QueryBuilder' do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: querybuilder
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 1
9
- - 0
10
- version: 1.1.0
9
+ - 1
10
+ version: 1.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Gaspard Bucher
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-11-01 00:00:00 +01:00
18
+ date: 2012-06-27 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency