flounder 0.9.1 → 0.9.2

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: b7f0215b9a32d26575d6a27982baf1e3557df937
4
- data.tar.gz: b0b7cb0356ea83e8af672a0f91b025f567891d03
3
+ metadata.gz: 0f322c0fdd9247c24a821bce8f602d025f961d19
4
+ data.tar.gz: d27d58604c817b088add6384ebd9022e38d3d650
5
5
  SHA512:
6
- metadata.gz: ff873c40c360913978fd6d9138b704e8362c26e577a15f042c062f2be74731e63ab0d06ec49931892b1a98f729eab67823b8e23da8cf2fd41f09841ccaa9fa01
7
- data.tar.gz: 4ab1e4048e43d99ce94ea224e026e4e6bd9b36ca591d600df5777f9bcf4666ddcc47eaa93afcb44fc5a74751e98790ec1be3f68e440dc3c43ba0747926efdbe8
6
+ metadata.gz: 338d6f3a5a574b442375846b94af3c17b7104aeae5519fea3b7b2d8c4d7a2e167841021b78ad68117a90840bc056e348f1168487f82dac21f622afdfb4704b01
7
+ data.tar.gz: e0a72ca40089dcd6221589c2c989d7ea9a6dfac60d809e165e1265328694b7a803974f95ce3c697da961d1546454bf119bf7b77c94705650763746bf47ca0890
data/HISTORY CHANGED
@@ -7,4 +7,7 @@
7
7
  + `domain.transaction do |conn| ... end`
8
8
 
9
9
  0.9.1
10
- + `search_path` argument for `Flounder.connect`.
10
+ + `search_path` argument for `Flounder.connect`.
11
+
12
+ 0.9.2
13
+ + bind variables on INSERT/UPDATE.
Binary file
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.1'
5
+ s.version = '0.9.2'
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"
@@ -16,13 +16,15 @@ module Flounder
16
16
 
17
17
  def typecast type_oid, value
18
18
  return nil unless value
19
+
20
+ # assert: value is not nil
19
21
  case type_oid
20
22
  when OID_TIMESTAMP
21
- value && DateTime.parse(value)
23
+ DateTime.parse(value)
22
24
  when OID_DATE
23
- value && Date.parse(value)
25
+ Date.parse(value)
24
26
  when OID_TIME
25
- value && Time.parse(value)
27
+ Time.parse(value)
26
28
  when OID_INTEGER, OID_SMALLINT
27
29
  value.to_i
28
30
  when OID_FLOAT
@@ -110,6 +110,14 @@ module Flounder::Query
110
110
  end
111
111
  end
112
112
 
113
+ # Adds a binding
114
+ #
115
+ def add_binding value
116
+ bind_values << value
117
+ # is 1-based - hence the postop
118
+ Arel::Nodes::SqlLiteral.new("$#{bind_values.size}")
119
+ end
120
+
113
121
  # Called on each key/value pair of a
114
122
  # * condition
115
123
  # * join
@@ -153,5 +161,20 @@ module Flounder::Query
153
161
  end
154
162
  end
155
163
 
164
+ # Called on each key/value pair of an update clause, this returns a
165
+ # hash that can be passed to Arel #update.
166
+ #
167
+ def transform_tuple_for_set field, value
168
+ placeholder = add_binding(value)
169
+
170
+ case field
171
+ when Symbol, String
172
+ [entity[field.to_sym].arel_field, placeholder]
173
+ when Flounder::Field
174
+ [field.arel_field, placeholder]
175
+ else
176
+ fail "Could not transform condition part. (#{field.inspect}, #{value.inspect})"
177
+ end
178
+ end
156
179
  end
157
180
  end
@@ -1,4 +1,4 @@
1
-
1
+
2
2
  require_relative 'base'
3
3
  require_relative 'returning'
4
4
 
@@ -18,24 +18,9 @@ module Flounder::Query
18
18
  def row fields
19
19
  manager.insert(
20
20
  fields.map { |k, v|
21
- transform_values(k, v) })
21
+ transform_tuple_for_set(k, v) })
22
22
  end
23
23
 
24
24
  include Returning
25
- private
26
-
27
- # Called on each key/value pair of an insert clause, this returns a
28
- # hash that can be passed to Arel #insert.
29
- #
30
- def transform_values field, value
31
- case field
32
- when Symbol, String
33
- [entity[field.to_sym].arel_field, value]
34
- when Flounder::Field
35
- [field.arel_field, value]
36
- else
37
- fail "Could not transform value. (#{field.inspect}, #{value.inspect})"
38
- end
39
- end
40
25
  end # class
41
26
  end # module Flounder
@@ -17,29 +17,10 @@ module Flounder::Query
17
17
  def set fields
18
18
  manager.set(
19
19
  fields.map { |k, v|
20
- transform_attributes(k, v) })
20
+ transform_tuple_for_set(k, v) })
21
21
  end
22
22
 
23
23
  include Returning
24
24
 
25
- private
26
-
27
- # Called on each key/value pair of an update clause, this returns a
28
- # hash that can be passed to Arel #update.
29
- #
30
- def transform_attributes field, value
31
- if value.kind_of? Flounder::Field
32
- value = value.arel_field
33
- end
34
-
35
- case field
36
- when Symbol, String
37
- [entity[field.to_sym].arel_field, value]
38
- when Flounder::Field
39
- [field.arel_field, value]
40
- else
41
- fail "Could not transform condition part. (#{field.inspect}, #{value.inspect})"
42
- end
43
- end
44
25
  end # class
45
26
  end # module Flounder
data/qed/inserts.md CHANGED
@@ -3,7 +3,7 @@ An insert creates a state from which SQL can be extracted.
3
3
  ~~~ruby
4
4
  sql = users.insert(:name => 'Mr. Insert SQL').to_sql
5
5
 
6
- sql.assert == "INSERT INTO \"users\" (\"name\") VALUES ('Mr. Insert SQL') RETURNING *"
6
+ sql.assert == "INSERT INTO \"users\" (\"name\") VALUES ($1) RETURNING *"
7
7
  ~~~
8
8
 
9
9
  Using returning will return the inserted object.
data/qed/updates.md CHANGED
@@ -5,10 +5,10 @@ An update creates a state from which SQL can be extracted.
5
5
 
6
6
  sql = posts.update(:title => 'Update SQL').where(:id => post.id).to_sql
7
7
 
8
- sql.assert == 'UPDATE "posts" SET "title" = \'Update SQL\' WHERE "posts"."id" = 1 RETURNING *'
8
+ sql.assert == 'UPDATE "posts" SET "title" = $1 WHERE "posts"."id" = 1 RETURNING *'
9
9
  ~~~
10
10
 
11
- Flounder fields are ok.
11
+ Flounder fields can be used to identify the values to be set.
12
12
 
13
13
  ~~~ruby
14
14
  post = posts.first
@@ -17,10 +17,10 @@ Flounder fields are ok.
17
17
  update(posts[:title] => 'Update Flounder SQL').
18
18
  where(:id => post.id).to_sql
19
19
 
20
- sql.assert == 'UPDATE "posts" SET "title" = \'Update Flounder SQL\' WHERE "posts"."id" = 1 RETURNING *'
20
+ sql.assert == 'UPDATE "posts" SET "title" = $1 WHERE "posts"."id" = 1 RETURNING *'
21
21
  ~~~
22
22
 
23
- It can update a single field.
23
+ Using `where`, you can limit the records that are updated.
24
24
 
25
25
  ~~~ruby
26
26
  post_id = posts.first.id
@@ -31,26 +31,20 @@ It can update a single field.
31
31
  post.title.assert == 'Update Field'
32
32
  ~~~
33
33
 
34
- Flounder fields are ok here too.
35
-
36
- ~~~ruby
37
- post = posts.first
38
-
39
- post = posts.
40
- update(posts[:title] => 'Update Flounder Field').
41
- where(:id => post.id).kick.first
42
-
43
- post.title.assert == 'Update Flounder Field'
44
- ~~~
45
-
46
34
  An update can take multiple fields.
47
35
 
48
36
  ~~~ruby
49
- post = posts.first
37
+ post_id = 1
50
38
 
51
- sql = posts.update(:title => 'Update SQL', :text => 'Update Multiple Fields Text').where(:id => post.id).to_sql
39
+ statement = posts.update(
40
+ title: 'Update SQL',
41
+ text: 'Update Multiple Fields Text',
42
+ approver_id: nil).
43
+ where(:id => post_id)
52
44
 
53
- sql.assert == 'UPDATE "posts" SET "title" = \'Update SQL\', "text" = \'Update Multiple Fields Text\' WHERE "posts"."id" = 1 RETURNING *'
45
+ statement.to_sql.assert == %Q(UPDATE "posts" SET "title" = $1, "text" = $2, "approver_id" = $3 WHERE "posts"."id" = 1 RETURNING *)
46
+
47
+ statement.kick
54
48
  ~~~
55
49
 
56
50
  Updating a single row is possible.
@@ -70,4 +64,4 @@ Updating multiple rows is possible.
70
64
  updated = users.update(:name => 'Update Multiple Rows').where(:name.not_eq => nil).kick
71
65
 
72
66
  updated.map(&:name).assert == ['Update Multiple Rows']*6
73
- ~~~
67
+ ~~~
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.1
4
+ version: 0.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kaspar Schiess
@@ -90,6 +90,7 @@ files:
90
90
  - HISTORY
91
91
  - LICENSE
92
92
  - README
93
+ - flounder-0.9.1.gem
93
94
  - flounder.gemspec
94
95
  - lib/flounder.rb
95
96
  - lib/flounder/connection.rb