flounder 0.9.13 → 0.9.14

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9298bfc2f968429143600db83b178317276c90ee
4
- data.tar.gz: 7f8a6080dc2be42627cbf3b15a2b9af46a93e8b9
3
+ metadata.gz: 7e5d88da3b8782b8bb2fb07e95392a2123f894cd
4
+ data.tar.gz: 9a166526a02b00dcb1095f1ce7a785d5f4a32034
5
5
  SHA512:
6
- metadata.gz: e3446da6ec7625773c5ee45064646ab0d309a430bec819817e37bf9c9776dffcb31095b50e2f358f5af4bb62adb643f828ec7edb9dcc71c7360eb8564df009aa
7
- data.tar.gz: 98ca2a21ffd6e01a5b76a0d1d7f9d93562dee7ef8eab859410b4baf95a8ae9925f2073d1ea20c17e4c2595bb937b23f8a24aa180cd1755e3e516c01e46d21db7
6
+ metadata.gz: add6543b9ef0cbf7670cedf9e7f976bb45a615bbe08d80ba6ec864dfdd3e8e78a057c7e40bed41e717db414582c6c5aaa0b52450ad23478229533cf42398f702
7
+ data.tar.gz: 4eca98ff56f180af2c35b0ef5f2e563297e87b1fb05b65ebfd908e2abd8612774cf7dc283d20d56ebbdc87be0d6dadb7f3d7d9a6d5be020d58353b5aae2ae22a
data/flounder.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "flounder"
5
- s.version = '0.9.13'
5
+ s.version = '0.9.14'
6
6
  s.summary = "Flounder is a way to write SQL simply in Ruby. It deals with everything BUT object relational mapping. "
7
7
  s.email = "kaspar.schiess@technologyastronauts.ch"
8
8
  s.homepage = "https://bitbucket.org/technologyastronauts/oss_flounder"
@@ -31,33 +31,7 @@ module Flounder::Query
31
31
  # query.where(:id.noteq => 1) # ... WHERE id != 1
32
32
  #
33
33
  def where *conditions
34
- conditions = conditions.dup
35
-
36
- resolve_entity = entity
37
-
38
- # is the first argument an entity? if yes, interpret field names relative
39
- # to that entity.
40
- if conditions.size > 1 && entity_like?(conditions.first)
41
- resolve_entity = convert_to_entity(conditions.shift)
42
- end
43
-
44
- # is this a hash? extract the first element
45
- if conditions.size == 1 && conditions.first.kind_of?(Hash)
46
- conditions = conditions.first
47
-
48
- conditions.each do |k, v|
49
- manager.where(transform_tuple(resolve_entity, k, v))
50
- end
51
- return self
52
- end
53
-
54
- # maybe conditions is of the second form?
55
- conditions.each do |cond_str, *values|
56
- manager.where(
57
- Arel::Nodes::SqlLiteral.new(
58
- rewrite_bind_variables(cond_str, bind_values.size, values.size)))
59
- bind_values.concat values
60
- end
34
+ parse_conditions(*conditions) { |bit| manager.where(bit) }
61
35
  end
62
36
 
63
37
  def with name, query
@@ -104,7 +78,45 @@ module Flounder::Query
104
78
  # should be overridden
105
79
  end
106
80
 
107
- # Rewrites a statement that contains bind placeholders like '$1' to
81
+ # Parses a conditions array like it is found with #where and #having and
82
+ # calls the block for each condition bit. Returns self.
83
+ #
84
+ # Example:
85
+ # parse_conditions(conditions) { |bit| manager.where(bit) }
86
+ #
87
+ def parse_conditions *conditions, &apply
88
+ conditions = conditions.dup
89
+
90
+ resolve_entity = entity
91
+
92
+ # is the first argument an entity? if yes, interpret field names relative
93
+ # to that entity.
94
+ if conditions.size > 1 && entity_like?(conditions.first)
95
+ resolve_entity = convert_to_entity(conditions.shift)
96
+ end
97
+
98
+ # is this a hash? extract the first element
99
+ if conditions.size == 1 && conditions.first.kind_of?(Hash)
100
+ conditions = conditions.first
101
+
102
+ conditions.each do |k, v|
103
+ apply.call(
104
+ transform_tuple(resolve_entity, k, v))
105
+ end
106
+ return self
107
+ end
108
+
109
+ # maybe conditions is of the second form?
110
+ conditions.each do |cond_str, *values|
111
+ apply.call(
112
+ Arel::Nodes::SqlLiteral.new(
113
+ rewrite_bind_variables(cond_str, bind_values.size, values.size)))
114
+ bind_values.concat values
115
+ end
116
+ return self
117
+ end
118
+
119
+ # Rewrites a statement that contains bind placeholders like '$1' to
108
120
  # contain placeholders starting at offset+1. Also checks that no
109
121
  # placeholder exceeds the limit.
110
122
  #
@@ -92,34 +92,7 @@ module Flounder::Query
92
92
  self
93
93
  end
94
94
  def having *conditions
95
- # TODO mostly duplicates #where!!!
96
- conditions = conditions.dup
97
-
98
- resolve_entity = entity
99
-
100
- # is the first argument an entity? if yes, interpret field names relative
101
- # to that entity.
102
- if conditions.size > 1 && entity_like?(conditions.first)
103
- resolve_entity = convert_to_entity(conditions.shift)
104
- end
105
-
106
- # is this a hash? extract the first element
107
- if conditions.size == 1 && conditions.first.kind_of?(Hash)
108
- conditions = conditions.first
109
-
110
- conditions.each do |k, v|
111
- manager.having(transform_tuple(resolve_entity, k, v))
112
- end
113
- return self
114
- end
115
-
116
- # maybe conditions is of the second form?
117
- conditions.each do |cond_str, *values|
118
- manager.where(
119
- Arel::Nodes::SqlLiteral.new(
120
- rewrite_bind_variables(cond_str, bind_values.size, values.size)))
121
- bind_values.concat values
122
- end
95
+ parse_conditions(*conditions) { |bit| manager.having(bit) }
123
96
  end
124
97
 
125
98
  # Orders by a list of field references.
data/qed/selects.md CHANGED
@@ -67,6 +67,9 @@ Sometimes you have to say it with a subquery. For example, you might want to loo
67
67
  # GROUP BY and HAVING
68
68
 
69
69
  ~~~ruby
70
- posts.group_by(:id).having(id: 0, user_id: 2).
70
+ posts.group_by(:id).having(id: 0, user_id: 2).project('id').
71
71
  assert generates_sql("SELECT [fields] FROM \"posts\" GROUP BY \"posts\".\"id\" HAVING \"posts\".\"id\" = 0 AND \"posts\".\"user_id\" = 2")
72
+
73
+ posts.group_by(:id).having('id = 1').project('id').
74
+ assert generates_sql("SELECT [fields] FROM \"posts\" GROUP BY \"posts\".\"id\" HAVING id = 1")
72
75
  ~~~
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flounder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.13
4
+ version: 0.9.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kaspar Schiess