cassie 1.0.0.alpha.4 → 1.0.0.alpha.5

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: b525f6072394101c053af5d655136f44cfda52a1
4
- data.tar.gz: b70d7576dfd3b36ee4958f5e75dbfee6ce9c16ed
3
+ metadata.gz: d925ecc0efe8b9f80a3db861e19e0600aefafccc
4
+ data.tar.gz: a691902f688cabb92e861b5e8117d620d6e783d0
5
5
  SHA512:
6
- metadata.gz: 3b345d2bb1febc085c96b5deca61fcd61862242d8ea02e17c8ce7ddc2015c8cfa95af8d51ccb52b7714ed9b1ac200f678194d7b2c1a9200ba056992b0126c88a
7
- data.tar.gz: d12cda51a8148b416963326eaebad65b67df3e096fc95e887ec6ff15802a36f052b42d0147e49f53515e9934ba2d842add0720521230ef704ce4b059dcc8c4eb
6
+ metadata.gz: a7d29330368b459d759110f461589776c2552c06e496e2ba2be20bbd7e5f841abbbe617b26cdec7fcd4b919a979998bde14f596ba3f9c06930794cac0b6672a4
7
+ data.tar.gz: a54572302c88f2fb902800481df8ee2b06dc4de63ab988e51f1e5575b561cc37cb851207f2fd81c9c87d28d0ecd7f2119e59accc4348ddde6bf7baca8b670feb
@@ -0,0 +1,47 @@
1
+ require_relative 'assignment'
2
+
3
+ module Cassie::Queries::Statement
4
+ module Conditions
5
+ extend ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+ def if_not_exists(opts={})
9
+ condition = "NOT EXISTS"
10
+ opts.delete(:value)
11
+
12
+ conditions[condition] = opts
13
+ end
14
+
15
+ def if_exists(opts={})
16
+ condition = "EXISTS"
17
+ opts.delete(:value)
18
+
19
+ conditions[condition] = opts
20
+ end
21
+
22
+ def conditions
23
+ @conditions ||= {}
24
+ end
25
+ end
26
+
27
+ def conditions
28
+ self.class.conditions
29
+ end
30
+
31
+ def build_condition_and_bindings
32
+ condition_strings = []
33
+ bindings = []
34
+
35
+ conditions.each do |condition, opts|
36
+ if eval_if_opt?(opts[:if])
37
+ condition_strings << condition.to_s
38
+ bindings << eval_value_opt(opts[:value]) if opts.has_key?(:value)
39
+ end
40
+ end
41
+
42
+ cql = "IF #{condition_strings.join(' AND ')}" unless condition_strings.empty?
43
+
44
+ [cql , bindings]
45
+ end
46
+ end
47
+ end
@@ -1,4 +1,6 @@
1
1
  require_relative 'relations'
2
+ require_relative 'conditions'
3
+ require_relative 'mapping'
2
4
 
3
5
  module Cassie::Queries::Statement
4
6
  module Deleting
@@ -12,6 +14,8 @@ module Cassie::Queries::Statement
12
14
  # end
13
15
  def delete(table)
14
16
  include Relations
17
+ include Conditions
18
+ include Mapping
15
19
 
16
20
  self.table = table
17
21
  self.identifier = :delete
@@ -33,15 +37,17 @@ module Cassie::Queries::Statement
33
37
  protected
34
38
 
35
39
  def build_delete_cql_and_bindings
36
- where_str, bindings = build_where_and_bindings
40
+ where_str, where_bindings = build_where_and_bindings
41
+ condition_str, condition_bindings = build_condition_and_bindings
37
42
 
38
43
  cql = %(
39
44
  DELETE #{build_delete_clause}
40
45
  FROM #{table}
41
46
  #{where_str}
47
+ #{condition_str}
42
48
  ).squish + ";"
43
49
 
44
- [cql, bindings]
50
+ [cql, where_bindings + condition_bindings]
45
51
  end
46
52
 
47
53
  # a select clause is built up of selectors
@@ -1,4 +1,5 @@
1
1
  require_relative 'assignments'
2
+ require_relative 'conditions'
2
3
 
3
4
  module Cassie::Queries::Statement
4
5
  module Inserting
@@ -7,6 +8,7 @@ module Cassie::Queries::Statement
7
8
  module ClassMethods
8
9
  def insert(table)
9
10
  include Assignments
11
+ include Conditions
10
12
 
11
13
  self.table = table
12
14
  self.identifier = :insert
@@ -23,15 +25,17 @@ module Cassie::Queries::Statement
23
25
  protected
24
26
 
25
27
  def build_insert_cql_and_bindings
26
- identifiers_str, terms_str, bindings = build_insert_and_bindings
28
+ identifiers_str, terms_str, value_bindings = build_insert_and_bindings
29
+ condition_str, condition_bindings = build_condition_and_bindings
27
30
 
28
31
  cql = %(
29
32
  INSERT INTO #{table}
30
33
  (#{identifiers_str})
31
34
  VALUES (#{terms_str})
35
+ #{condition_str}
32
36
  ).squish + ";"
33
37
 
34
- [cql, bindings]
38
+ [cql, value_bindings + condition_bindings]
35
39
  end
36
40
  end
37
41
  end
@@ -3,7 +3,15 @@ module Cassie::Queries::Statement
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  def fetch(args={})
6
- rows = super(args)
6
+ build_resources(super(args))
7
+ end
8
+
9
+ protected
10
+
11
+ # Default implementation assumes
12
+ # 1 row per resource, clients
13
+ # may override if more complex
14
+ def build_resources(rows)
7
15
  rows.map {|r| build_resource(r) }
8
16
  end
9
17
 
@@ -5,13 +5,19 @@ module Cassie::Queries::Statement
5
5
  MAPPED_METHODS = [:insert, :update, :delete].freeze
6
6
 
7
7
  included do
8
+ attr_accessor :_resource
9
+
8
10
  MAPPED_METHODS.each do |method|
9
11
  next unless method_defined?(method)
10
12
 
11
13
  define_method(method) do |resource=nil, opts={}|
12
- return super(opts) if _resource.nil? && resource.nil?
13
-
14
- self._resource = resource
14
+ if resource.nil?
15
+ # if no mapping is taking place, keep previously
16
+ # defined behavior/return value
17
+ return super(opts) if _resource.nil?
18
+ else
19
+ self._resource = resource
20
+ end
15
21
 
16
22
  if super(opts)
17
23
  _resource
@@ -1,3 +1,6 @@
1
+ # for pluralization
2
+ require 'active_support/core_ext/string'
3
+
1
4
  module Cassie::Queries::Statement
2
5
  #
3
6
  #
@@ -14,6 +17,7 @@ module Cassie::Queries::Statement
14
17
  lteq: "<=",
15
18
  gt: ">",
16
19
  gteq: ">=",
20
+ in: "IN",
17
21
  contains: "CONTAINS",
18
22
  contains_key: "CONTAINS KEY",
19
23
  }
@@ -27,6 +31,7 @@ module Cassie::Queries::Statement
27
31
  # `relation "username = ?", value: :username`
28
32
 
29
33
  # swap the 2nd arg that sucked in options hash
34
+ # to keep consistent feeling interface
30
35
  opts.merge!(op_type)
31
36
 
32
37
  @cql = identifier
@@ -45,6 +50,20 @@ module Cassie::Queries::Statement
45
50
  cql
46
51
  end
47
52
 
53
+ def multiple_term?
54
+ op_type == :in
55
+ end
56
+
57
+ def implied_value_method
58
+ method = if multiple_term?
59
+ identifier.to_s.pluralize
60
+ else
61
+ identifier
62
+ end
63
+
64
+ method.to_sym
65
+ end
66
+
48
67
  protected
49
68
 
50
69
  def cql
@@ -11,7 +11,7 @@ module Cassie::Queries::Statement
11
11
  def where(identifier, op, opts={})
12
12
  relation = Relation.new(identifier, op, opts)
13
13
 
14
- opts[:value] ||= identifier.to_sym
14
+ opts[:value] ||= relation.implied_value_method
15
15
 
16
16
  if Symbol === opts[:value]
17
17
  define_term_methods(opts[:value])
@@ -25,7 +25,7 @@ module Cassie::Queries::Statement
25
25
  @relations ||= {}
26
26
  end
27
27
 
28
- private
28
+ protected
29
29
 
30
30
  def define_term_methods(name)
31
31
  #TODO: this should probably only raise
@@ -44,6 +44,8 @@ module Cassie::Queries::Statement
44
44
  self.class.relations
45
45
  end
46
46
 
47
+ protected
48
+
47
49
  def build_where_and_bindings
48
50
  cql = ""
49
51
  bindings = []
@@ -1,4 +1,5 @@
1
1
  require_relative 'assignments'
2
+ require_relative 'conditions'
2
3
 
3
4
  module Cassie::Queries::Statement
4
5
  module Updating
@@ -8,6 +9,7 @@ module Cassie::Queries::Statement
8
9
  def update(table)
9
10
  include Relations
10
11
  include Assignments
12
+ include Conditions
11
13
 
12
14
  self.table = table
13
15
  self.identifier = :update
@@ -26,14 +28,16 @@ module Cassie::Queries::Statement
26
28
  def build_update_cql_and_bindings
27
29
  assignment_str, update_bindings = build_update_and_bindings
28
30
  where_str, where_bindings = build_where_and_bindings
31
+ condition_str, condition_bindings = build_condition_and_bindings
29
32
 
30
33
  cql = %(
31
34
  UPDATE #{table}
32
35
  SET #{assignment_str}
33
36
  #{where_str}
37
+ #{condition_str}
34
38
  ).squish + ";"
35
39
 
36
- [cql, update_bindings + where_bindings]
40
+ [cql, update_bindings + where_bindings + condition_bindings]
37
41
  end
38
42
  end
39
43
  end
data/lib/cassie/query.rb CHANGED
@@ -24,13 +24,12 @@ module Cassie
24
24
  include Queries::Instrumentation
25
25
  include Queries::Logging
26
26
 
27
- def initialize(*args)
28
- value = super(*args)
29
- after_initialize(*args)
30
- value
31
- end
27
+ def initialize(params={})
28
+ params.each do |attr, value|
29
+ self.public_send("#{attr}=", value)
30
+ end
32
31
 
33
- def after_initialize(*args)
32
+ super()
34
33
  end
35
34
  end
36
35
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cassie
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.alpha.4
4
+ version: 1.0.0.alpha.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evan Prothro
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-04 00:00:00.000000000 Z
11
+ date: 2016-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cassandra-driver
@@ -102,6 +102,7 @@ files:
102
102
  - lib/cassie/queries/statement.rb
103
103
  - lib/cassie/queries/statement/assignment.rb
104
104
  - lib/cassie/queries/statement/assignments.rb
105
+ - lib/cassie/queries/statement/conditions.rb
105
106
  - lib/cassie/queries/statement/deleting.rb
106
107
  - lib/cassie/queries/statement/fetching.rb
107
108
  - lib/cassie/queries/statement/inserting.rb