cassandra_db 0.1.1 → 1.0.0

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
  SHA256:
3
- metadata.gz: 361482a345f38fb4bf81842cf9042d8b701e9be9fafc49882a56d04d2e004437
4
- data.tar.gz: 94c351a9a73e97a358ff0bc04133602d2239fd6fef965f37e59aa4876334ab43
3
+ metadata.gz: ed2d4c361a64638cb1efd26d311f63a7867bcafcb5e7efdea0c72e72bddce90c
4
+ data.tar.gz: 5376a2d931db76bd7a1e7d71217c3351c8298cc185dc181901c20ccf433c2ac3
5
5
  SHA512:
6
- metadata.gz: 9aec4396f0c99714d314d9c31df7849cddbb07c9e9dfc4338d2a729ee8b685b4342477b64a64ddf1e0aae3253f397b0755fdfc6aafef8d9b22314e914b092e17
7
- data.tar.gz: 7524878056971f8f493d9dd85ca5b149915b42a2764ddfc75d78f9e7b44f35a3c7d59b966d4613352a2c0bc39059a7d3cd5f94613cd84aea979f8658ffa6c313
6
+ metadata.gz: 8bdc20dc8bcfa76967847f32327a5f42f334063ec4a1a424ecce8c44e69a559fb0b77940a4e4e5d81e1cf55ed5ef77ce7fa62e4cdec277cb56877df9fe1734a4
7
+ data.tar.gz: 3c0624835af96bf2b1c92a54052612303e280a320c3a9cf21afe54d4c5af1853073a9aa30969b48a0d5fa25db29183f27313cdead32bbbf6f8f913c006bc010e
data/README.md CHANGED
@@ -70,6 +70,15 @@ dataset.map
70
70
  dataset.all
71
71
  ```
72
72
 
73
+ ### Insert, Update, Delete
74
+ ```ruby
75
+ dataset.insert id: 1, name: 'Name 1'
76
+
77
+ dataset.where(id: 1).update name: 'Name 2'
78
+
79
+ dataset.where(id: 1).delete
80
+ ```
81
+
73
82
  ## Contributing
74
83
 
75
84
  Bug reports and pull requests are welcome on GitHub at https://github.com/gabynaiman/cassandra_db.
data/cassandra_db.gemspec CHANGED
@@ -28,6 +28,7 @@ Gem::Specification.new do |spec|
28
28
  spec.add_development_dependency 'minitest', '~> 5.0', '< 5.11'
29
29
  spec.add_development_dependency 'minitest-colorin', '~> 0.1'
30
30
  spec.add_development_dependency 'minitest-line', '~> 0.6'
31
+ spec.add_development_dependency 'minitest-great_expectations', '~> 0.0', '>= 0.0.5'
31
32
  spec.add_development_dependency 'simplecov', '~> 0.14'
32
33
  spec.add_development_dependency 'coveralls', '~> 0.8'
33
34
  spec.add_development_dependency 'pry-nav', '~> 0.2'
@@ -5,6 +5,8 @@ module CassandraDB
5
5
  keyspace: DEFAULT_KEYSPACE
6
6
  }.freeze
7
7
 
8
+ DEFAULT_LOGGER = Logger.new '/dev/null'
9
+
8
10
  def initialize(options={})
9
11
  @options = DEFAULTS.merge(options)
10
12
  keyspace = @options.delete :keyspace
@@ -31,7 +33,7 @@ module CassandraDB
31
33
  end
32
34
 
33
35
  def from(table)
34
- Dataset.new self, from: table
36
+ Dataset.new self, table: table
35
37
  end
36
38
  alias_method :[], :from
37
39
 
@@ -48,8 +50,9 @@ module CassandraDB
48
50
  execute "DROP KEYSPACE \"#{name}\";"
49
51
  end
50
52
 
51
- def execute(*args)
52
- session.execute(*args)
53
+ def execute(cql, options={})
54
+ logger.debug(self.class) { "#{cql} #{options.any? ? options : ''}".strip }
55
+ session.execute cql, options
53
56
  end
54
57
 
55
58
  def inspect
@@ -60,5 +63,9 @@ module CassandraDB
60
63
 
61
64
  attr_reader :cluster, :session, :options
62
65
 
66
+ def logger
67
+ @logger ||= options.fetch(:logger, DEFAULT_LOGGER)
68
+ end
69
+
63
70
  end
64
71
  end
@@ -3,22 +3,18 @@ module CassandraDB
3
3
 
4
4
  include Enumerable
5
5
 
6
- def initialize(db, from:, where:[])
6
+ def initialize(db, table:, conditions:[])
7
7
  @db = db
8
- @table = from
9
- @conditions = where
8
+ @table = table
9
+ @conditions = conditions
10
10
  end
11
11
 
12
12
  def where(filters)
13
13
  new_conditions = filters.map do |field, value|
14
- {
15
- field: field,
16
- operator: value.is_a?(Array) ? 'IN' : '=',
17
- value: value
18
- }
14
+ QueryCondition.new field, value
19
15
  end
20
16
 
21
- Dataset.new db, from: table, where: conditions + new_conditions
17
+ Dataset.new db, table: table, conditions: conditions + new_conditions
22
18
  end
23
19
 
24
20
  def each(&block)
@@ -26,27 +22,34 @@ module CassandraDB
26
22
  end
27
23
 
28
24
  def all
29
- db.execute(*cql).to_a
25
+ statement = Statements::Select.new table: table, conditions: conditions
26
+ result = db.execute statement.cql, statement.options
27
+ result.to_a
30
28
  end
31
29
 
32
- def cql
33
- select = 'SELECT *'
34
- from = "FROM #{table}"
35
- where = conditions.any? ? "WHERE #{conditions.map { |c| "#{c[:field]} #{c[:operator]} :#{c[:field]}" }.join(' AND ')}" : ''
36
- arguments = conditions.each_with_object({}) { |c,h| h[c[:field]] = c[:value] }
30
+ def insert(attributes)
31
+ statement = Statements::Insert.new table: table, attributes: attributes
32
+ db.execute statement.cql, statement.options
33
+ end
34
+
35
+ def update(attributes)
36
+ statement = Statements::Update.new table: table, attributes: attributes, conditions: conditions
37
+ db.execute statement.cql, statement.options
38
+ end
37
39
 
38
- [
39
- "#{select} #{from} #{where}".strip,
40
- {arguments: arguments}
41
- ]
40
+ def delete
41
+ statement = Statements::Delete.new table: table, conditions: conditions
42
+ db.execute statement.cql, statement.options
42
43
  end
43
44
 
44
45
  def inspect
45
- "#<#{self.class.name}: #{cql.join(', ')}>"
46
+ statement = Statements::Select.new table: table, conditions: conditions
47
+ "#<#{self.class.name}: #{statement.to_s}>"
46
48
  end
47
49
 
48
50
  private
49
51
 
50
52
  attr_reader :db, :table, :conditions
53
+
51
54
  end
52
55
  end
@@ -0,0 +1,21 @@
1
+ module CassandraDB
2
+ class QueryCondition
3
+
4
+ attr_reader :field, :value, :operator
5
+
6
+ def initialize(field, value)
7
+ @field = field
8
+ @value = value
9
+ @operator = value.is_a?(Array) ? 'IN' : '='
10
+ end
11
+
12
+ def to_cql
13
+ "#{field} #{operator} :#{field}"
14
+ end
15
+
16
+ def to_argument
17
+ {field => value}
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,121 @@
1
+ module CassandraDB
2
+ module Statements
3
+ class Base
4
+
5
+ def options
6
+ {arguments: arguments}
7
+ end
8
+
9
+ def to_s
10
+ "#{cql} #{options}"
11
+ end
12
+
13
+ private
14
+
15
+ def where_cql
16
+ conditions.any? ? "WHERE #{conditions.map(&:to_cql).join(' AND ')}" : ''
17
+ end
18
+
19
+ def conditions_agruments
20
+ conditions.each_with_object({}) do |condition, agruments|
21
+ agruments.merge! condition.to_argument
22
+ end
23
+ end
24
+
25
+ end
26
+
27
+ class Select < Base
28
+
29
+ attr_reader :table, :conditions
30
+
31
+ def initialize(table:, conditions:[])
32
+ @table = table
33
+ @conditions = conditions
34
+ end
35
+
36
+ def cql
37
+ @cql ||= "SELECT * FROM #{table} #{where_cql}".strip
38
+ end
39
+
40
+ def arguments
41
+ @arguments ||= conditions_agruments
42
+ end
43
+
44
+ end
45
+
46
+ class Insert < Base
47
+
48
+ attr_reader :table, :attributes
49
+
50
+ def initialize(table:, attributes:)
51
+ @table = table
52
+ @attributes = attributes
53
+ end
54
+
55
+ def cql
56
+ @cql ||= "INSERT INTO #{table} (#{field_names}) VALUES (#{field_references})"
57
+ end
58
+
59
+ def arguments
60
+ attributes
61
+ end
62
+
63
+ private
64
+
65
+ def field_names
66
+ attributes.keys.join(', ')
67
+ end
68
+
69
+ def field_references
70
+ attributes.keys.map { |f| ":#{f}" }.join(', ')
71
+ end
72
+
73
+ end
74
+
75
+ class Update < Base
76
+
77
+ attr_reader :table, :attributes, :conditions
78
+
79
+ def initialize(table:, attributes:, conditions:)
80
+ @table = table
81
+ @attributes = attributes
82
+ @conditions = conditions
83
+ end
84
+
85
+ def cql
86
+ @cql ||= "UPDATE #{table} SET #{filed_mappings} #{where_cql}"
87
+ end
88
+
89
+ def arguments
90
+ @arguments ||= attributes.merge(conditions_agruments)
91
+ end
92
+
93
+ private
94
+
95
+ def filed_mappings
96
+ attributes.keys.map { |f| "#{f} = :#{f}" }.join(', ')
97
+ end
98
+
99
+ end
100
+
101
+ class Delete < Base
102
+
103
+ attr_reader :table, :conditions
104
+
105
+ def initialize(table:, conditions:)
106
+ @table = table
107
+ @conditions = conditions
108
+ end
109
+
110
+ def cql
111
+ @cql ||= "DELETE FROM #{table} #{where_cql}"
112
+ end
113
+
114
+ def arguments
115
+ @arguments ||= conditions_agruments
116
+ end
117
+
118
+ end
119
+
120
+ end
121
+ end
@@ -1,3 +1,3 @@
1
1
  module CassandraDB
2
- VERSION = '0.1.1'
2
+ VERSION = '1.0.0'
3
3
  end
data/lib/cassandra_db.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'cassandra'
2
2
  require 'json'
3
+ require 'logger'
3
4
 
4
5
  module CassandraDB
5
6
 
@@ -18,4 +19,6 @@ end
18
19
 
19
20
  require_relative 'cassandra_db/version'
20
21
  require_relative 'cassandra_db/database'
21
- require_relative 'cassandra_db/dataset'
22
+ require_relative 'cassandra_db/dataset'
23
+ require_relative 'cassandra_db/statements'
24
+ require_relative 'cassandra_db/query_condition'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cassandra_db
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriel Naiman
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-09-25 00:00:00.000000000 Z
11
+ date: 2024-04-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cassandra-driver
@@ -86,6 +86,26 @@ dependencies:
86
86
  - - "~>"
87
87
  - !ruby/object:Gem::Version
88
88
  version: '0.6'
89
+ - !ruby/object:Gem::Dependency
90
+ name: minitest-great_expectations
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '0.0'
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: 0.0.5
99
+ type: :development
100
+ prerelease: false
101
+ version_requirements: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - "~>"
104
+ - !ruby/object:Gem::Version
105
+ version: '0.0'
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: 0.0.5
89
109
  - !ruby/object:Gem::Dependency
90
110
  name: simplecov
91
111
  requirement: !ruby/object:Gem::Requirement
@@ -148,6 +168,8 @@ files:
148
168
  - lib/cassandra_db.rb
149
169
  - lib/cassandra_db/database.rb
150
170
  - lib/cassandra_db/dataset.rb
171
+ - lib/cassandra_db/query_condition.rb
172
+ - lib/cassandra_db/statements.rb
151
173
  - lib/cassandra_db/version.rb
152
174
  homepage: https://github.com/gabynaiman/cassandra_db
153
175
  licenses: