flounder 0.9.7 → 0.9.8

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: 25d788049bf3f82973544e20b826280ebfd41411
4
- data.tar.gz: eb21cf7b82f279de89f198e1badc243a1d727ae3
3
+ metadata.gz: 0b671fc956d5fcd73817cdc07ec5e9dbd5b03751
4
+ data.tar.gz: 1a6cf5f585030c036c5bd57abf4450d25103e529
5
5
  SHA512:
6
- metadata.gz: 00b05e0b4cbbc339bdd90c52c9b9eed8f1ec300a724bce7f587f888e360ed6368fbba2fdc561e0b28aba58da9dd311cbb6c415040fa196c1cc64742a32846cad
7
- data.tar.gz: e2e1eac6ce014003b11f009792ad9c4a0a702e485da0730e537df37e55f5abcc3603669a675655ffc2e65a9981e85079de1a15f75dfd8a1f7603c5015b99b7e4
6
+ metadata.gz: 7803a9083fa04f1f3c0cef3ac9a781f699c55194676e4f362f610a38ff663f3893d07dc1de5da525eaafada13935bbc34c44f56e40218962096790c96e776d69
7
+ data.tar.gz: 409cd4865b56744ae81129d6cc3eee861fb050c37bcda5943acad18885dca809e1d6c5fb779ce2bc220d33ebcd3171b1e8c84bf81367d92bbf6db827c82221f4
data/HISTORY CHANGED
@@ -5,19 +5,10 @@
5
5
  + You now need to call `#kick` when performing an insert or an update
6
6
  to get it to really happen. Please see our documentation in qed/*.
7
7
  + `domain.transaction do |conn| ... end`
8
-
9
- 0.9.1
10
8
  + `search_path` argument for `Flounder.connect`.
11
-
12
- 0.9.2
13
9
  + bind variables on INSERT/UPDATE.
14
-
15
- 0.9.3
16
10
  + DELETE FROM - entity#delete
17
-
18
- 0.9.4
19
11
  + nested transactions
20
-
21
- 0.9.5
22
12
  + subquery support
23
-
13
+ + localized where as in where(entity, :field => value)
14
+ + db.where(entity, hash) form, where all symbols are looked up in entity.
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.7'
5
+ s.version = '0.9.8'
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,12 +31,22 @@ 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 && conditions.first.kind_of?(Flounder::Entity)
41
+ resolve_entity = conditions.shift
42
+ end
43
+
34
44
  # is this a hash? extract the first element
35
45
  if conditions.size == 1 && conditions.first.kind_of?(Hash)
36
46
  conditions = conditions.first
37
47
 
38
48
  conditions.each do |k, v|
39
- manager.where(transform_tuple(k, v))
49
+ manager.where(transform_tuple(resolve_entity, k, v))
40
50
  end
41
51
  return self
42
52
  end
@@ -125,21 +135,31 @@ module Flounder::Query
125
135
  # * #where
126
136
  # * #on
127
137
  #
128
- def transform_tuple field, value
138
+ def transform_tuple entity, field, value
129
139
  if value.kind_of? Flounder::Field
130
140
  value = value.arel_field
131
141
  end
132
142
 
143
+ if defined?(DataMapper) &&
144
+ defined?(DataMapper::Query::Operator) &&
145
+ field.kind_of?(DataMapper::Query::Operator)
146
+
147
+ # interop: datamapper hijacks some of our operators, let's reverse this
148
+ field = Flounder::SymbolExtensions::Modifier.new(
149
+ field.target, field.operator)
150
+ end
151
+
133
152
  case field
134
153
  # covers: :field_a => ...
135
154
  when Symbol
136
- join_and_condition_part(entity[field].arel_field, value)
155
+ join_and_condition_part(entity, entity[field].arel_field, value)
137
156
  # covers: entity[:field] => ...
138
157
  when Flounder::Field
139
- join_and_condition_part(field.arel_field, value)
158
+ join_and_condition_part(entity, field.arel_field, value)
140
159
  # covers: :field_a.noteq => ...
141
160
  when Flounder::SymbolExtensions::Modifier
142
161
  join_and_condition_part(
162
+ entity,
143
163
  field.to_arel_field(entity),
144
164
  value,
145
165
  field.kind)
@@ -147,7 +167,7 @@ module Flounder::Query
147
167
  fail "Could not transform condition part. (#{field.inspect}, #{value.inspect})"
148
168
  end
149
169
  end
150
- def join_and_condition_part arel_field, value, kind=:eq
170
+ def join_and_condition_part entity, arel_field, value, kind=:eq
151
171
  case value
152
172
  # covers subselects
153
173
  when Flounder::Query::Base
@@ -31,10 +31,6 @@ module Flounder::Query
31
31
  attr_reader :projection_prefixes
32
32
 
33
33
  def _join join_node, entity
34
- if entity.kind_of?(Symbol)
35
- entity = domain[entity]
36
- end
37
-
38
34
  @last_join = entity
39
35
 
40
36
  table = entity.table
@@ -70,7 +66,7 @@ module Flounder::Query
70
66
  def on join_conditions
71
67
  join_conditions.each do |k, v|
72
68
  manager.on(
73
- transform_tuple(k, join_field(v)))
69
+ transform_tuple(entity, k, join_field(v)))
74
70
  end
75
71
  self
76
72
  end
@@ -16,8 +16,7 @@ module Flounder
16
16
  # NOTE mixing comparison ops with asc and desc is nasty, but not really
17
17
  # relevant. Errors will get raised later on - we're not in the business of
18
18
  # checking your SQL for validity.
19
- [:not_eq, :lt, :gt, :gteq, :lteq, :matches, :in, :not_in,
20
- :asc, :desc].each do |kind|
19
+ [:not_eq, :lt, :gt, :gteq, :lteq, :matches, :asc, :desc].each do |kind|
21
20
  define_method kind do
22
21
  Modifier.new(self, kind)
23
22
  end
data/lib/flounder.rb CHANGED
@@ -31,6 +31,11 @@ module_function
31
31
  def domain connection, &block
32
32
  Domain.new(connection).tap { |d| yield d if block_given? }
33
33
  end
34
+
35
+ def literal str
36
+ Arel::Nodes::SqlLiteral.new(str)
37
+ end
38
+ module_function :literal
34
39
  end
35
40
 
36
41
  Symbol.send(:include, Flounder::SymbolExtensions)
@@ -22,6 +22,8 @@ end
22
22
  #
23
23
  # One fine day we will use transactional features.
24
24
  #
25
- File.open File.expand_path '../../qed/flounder.sql' do |file|
25
+ require 'pathname'
26
+ sql = Pathname.new(__FILE__).dirname.join('../..').join('qed/flounder.sql')
27
+ File.open sql do |file|
26
28
  Flounder::Engine.new(domain.connection_pool).exec file.read
27
29
  end
data/qed/index.md CHANGED
@@ -46,10 +46,6 @@ Also, several conditions work as one would expect from DataMapper.
46
46
  "SELECT [fields] FROM \"users\" WHERE \"users\".\"id\" >= 10")
47
47
  domain[:users].where(:id.not_eq => 10).assert generates_sql(
48
48
  "SELECT [fields] FROM \"users\" WHERE \"users\".\"id\" != 10")
49
- domain[:users].where(:id.in => [1,2,3]).assert generates_sql(
50
- "SELECT [fields] FROM \"users\" WHERE \"users\".\"id\" IN (1, 2, 3)")
51
- domain[:users].where(:id.not_in => [1,2,3]).assert generates_sql(
52
- "SELECT [fields] FROM \"users\" WHERE \"users\".\"id\" NOT IN (1, 2, 3)")
53
49
  ~~~
54
50
 
55
51
  Fields can be used fully qualified by going through the entity.
@@ -67,6 +63,13 @@ Fields can be used fully qualified by going through the entity.
67
63
  assert generates_sql("SELECT [fields] FROM \"users\" WHERE \"users\".\"user_id\" = \"users\".\"approver_id\"")
68
64
  ~~~
69
65
 
66
+ By default, symbols are interpreted as field names in the entity that you start your query with. But you can localize your `#where` clause to any other entity.
67
+
68
+ ~~~ruby
69
+ users.where(posts, :id => 1).
70
+ assert generates_sql("SELECT [fields] FROM \"users\" WHERE \"posts\".\"id\" = 1")
71
+ ~~~
72
+
70
73
  # Some JOINs
71
74
 
72
75
  Here are some non-crazy joins that also work.
@@ -79,16 +82,6 @@ Here are some non-crazy joins that also work.
79
82
  assert generates_sql(%Q(SELECT [fields] FROM "users" LEFT OUTER JOIN "posts" ON "users"."id" = "posts"."user_id"))
80
83
  ~~~
81
84
 
82
- Join also accepts symbols as argument - these will be turned into entities.
83
-
84
- ~~~ruby
85
- domain[:users].join(:posts).on(:id => :user_id).
86
- assert generates_sql(%Q(SELECT [fields] FROM "users" INNER JOIN "posts" ON "users"."id" = "posts"."user_id"))
87
-
88
- domain[:users].outer_join(:posts).on(:id => :user_id).
89
- assert generates_sql(%Q(SELECT [fields] FROM "users" LEFT OUTER JOIN "posts" ON "users"."id" = "posts"."user_id"))
90
- ~~~
91
-
92
85
  Joining presents an interesting dilemma. There are two ways of joining things together, given three tables. The sequence A.B.C might mean to join A to B and C; it might also be interpreted to mean to join A to B and B to C. Here's how we solve this.
93
86
 
94
87
  ~~~ruby
data/qed/selects.md CHANGED
@@ -56,7 +56,7 @@ Sometimes you have to say it with a subquery. For example, you might want to loo
56
56
  where(:approver_id => domain[:users][:id]).limit(1)
57
57
 
58
58
  query = domain[:users].
59
- join(posts).on(:id => :user_id, posts[:id] => last_post)
59
+ join(posts).on(posts[:id] => last_post)
60
60
 
61
61
  query.assert generates_sql(%Q(SELECT [fields] FROM "users" INNER JOIN "posts" ON "posts"."id" = (SELECT "posts"."id" FROM "posts" WHERE "posts"."approver_id" = "users"."id" ORDER BY "posts"."id" DESC LIMIT 1)))
62
62
 
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.7
4
+ version: 0.9.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kaspar Schiess
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-08-20 00:00:00.000000000 Z
12
+ date: 2014-08-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: arel
@@ -88,12 +88,10 @@ extensions: []
88
88
  extra_rdoc_files: []
89
89
  files:
90
90
  - Gemfile
91
- - Gemfile.lock
92
91
  - HACKING
93
92
  - HISTORY
94
93
  - LICENSE
95
94
  - README
96
- - flounder-0.9.6.gem
97
95
  - flounder.gemspec
98
96
  - lib/flounder.rb
99
97
  - lib/flounder/connection.rb
data/Gemfile.lock DELETED
@@ -1,33 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- flounder (0.8.1)
5
- arel (~> 5, > 5.0.1)
6
- connection_pool (~> 2)
7
- hashie (~> 3, >= 3.2)
8
- pg (> 0.17)
9
-
10
- GEM
11
- remote: https://rubygems.org/
12
- specs:
13
- ae (1.8.2)
14
- ansi
15
- ansi (1.4.3)
16
- arel (5.0.1.20140414130214)
17
- brass (1.2.1)
18
- connection_pool (2.0.0)
19
- facets (2.9.3)
20
- hashie (3.2.0)
21
- pg (0.17.1)
22
- qed (2.9.1)
23
- ansi
24
- brass
25
- facets (>= 2.8)
26
-
27
- PLATFORMS
28
- ruby
29
-
30
- DEPENDENCIES
31
- ae
32
- flounder!
33
- qed
data/flounder-0.9.6.gem DELETED
Binary file