lazy_records 0.0.1 → 0.0.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: 1c328e28c94fbdc782c6108e9c063ce42b612cf2
4
- data.tar.gz: 4e2d4f08656d49b6873235f8b04950002ab9c06d
3
+ metadata.gz: bace4538e07a4bafa4e4e151c0cbf32526aff5ba
4
+ data.tar.gz: b74b0dc6ff4da1070ab83b485d59ad3cc56a049c
5
5
  SHA512:
6
- metadata.gz: 85993a6c79751fd3694a11d1653c5c5effe56de413fc8adc4b4f340785e6c5d98f6b17c0d1037874480b103f47d94b53be5c138e31a46ec40024c47e7f7bad11
7
- data.tar.gz: 03026dc4461c897e1984e1e1bf4dac9b46cf93c17e9fd5bb08c98414907b45d2023947829df68e7e8eecf684f1b88663f6ed7e34f6e291fd7f6864774bf53858
6
+ metadata.gz: 52a846c3b05ccb791110f59e8003c1d3c30f744c909cd9257602ab3d9e464f0fc9e373766c32f75207a49db56f6f8cd8238819babbadda842003130091682139
7
+ data.tar.gz: 16d5408c0b02e716f2840e9b2a24fc84ed6bee5bbad6b0256313a845b3fee419122d382b3bb583c5b477d920e5fba408fc0ef749dfb4e18aeb705aceac3fc209
data/Gemfile.lock CHANGED
@@ -44,7 +44,7 @@ GEM
44
44
  activesupport
45
45
  cache (>= 0.2.1)
46
46
  mini_portile (0.6.0)
47
- minitest (5.4.1)
47
+ minitest (5.4.2)
48
48
  multi_json (1.10.1)
49
49
  multi_xml (0.5.5)
50
50
  multipart-post (2.0.0)
@@ -79,7 +79,7 @@ GEM
79
79
  simplecov-html (~> 0.8.0)
80
80
  simplecov-html (0.8.0)
81
81
  thread_safe (0.3.4)
82
- totally_lazy (0.0.5)
82
+ totally_lazy (0.0.6)
83
83
  tzinfo (1.2.2)
84
84
  thread_safe (~> 0.1)
85
85
 
data/README.md CHANGED
@@ -16,7 +16,7 @@ This gem requires ruby 2.x.x
16
16
  In your bundler Gemfile
17
17
 
18
18
  ```ruby
19
- gem lazy_records, '~>0.0.2'
19
+ gem lazy_records, '~>0.0.1'
20
20
  ```
21
21
 
22
22
  Or with rubygems
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
@@ -0,0 +1,91 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+ # stub: lazy_records 0.0.2 ruby lib
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "lazy_records"
9
+ s.version = "0.0.2"
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+ s.require_paths = ["lib"]
13
+ s.authors = ["Kingsley Hendrickse"]
14
+ s.date = "2014-11-02"
15
+ s.description = "port of lazy records to ruby"
16
+ s.email = "kingsley.hendrickse@gmail.com"
17
+ s.extra_rdoc_files = [
18
+ "LICENSE.txt",
19
+ "README.md"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ ".rspec",
24
+ "Gemfile",
25
+ "Gemfile.lock",
26
+ "LICENSE.txt",
27
+ "README.md",
28
+ "Rakefile",
29
+ "VERSION",
30
+ "lazy_records.gemspec",
31
+ "lazy_store.gemspec",
32
+ "lib/adapters/mssql.rb",
33
+ "lib/adapters/mysql.rb",
34
+ "lib/adapters/sqlite3.rb",
35
+ "lib/definition.rb",
36
+ "lib/keyword.rb",
37
+ "lib/lazy_records.rb",
38
+ "lib/memory_records.rb",
39
+ "lib/predicate_to_sql.rb",
40
+ "lib/predicates.rb",
41
+ "lib/record.rb",
42
+ "lib/sql_records.rb",
43
+ "lib/sql_virtual_records.rb",
44
+ "spec/memory_records_spec.rb",
45
+ "spec/spec_helper.rb",
46
+ "spec/sql_records_spec.rb",
47
+ "spec/sql_virtual_records_spec.rb",
48
+ "spec/test_data/.keep"
49
+ ]
50
+ s.homepage = "http://github.com/kingsleyh/lazy_records"
51
+ s.licenses = ["MIT"]
52
+ s.rubygems_version = "2.2.2"
53
+ s.summary = "port of lazy records to ruby"
54
+
55
+ if s.respond_to? :specification_version then
56
+ s.specification_version = 4
57
+
58
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
59
+ s.add_runtime_dependency(%q<totally_lazy>, [">= 0"])
60
+ s.add_runtime_dependency(%q<lock_method>, [">= 0"])
61
+ s.add_runtime_dependency(%q<encrypted_strings>, [">= 0"])
62
+ s.add_runtime_dependency(%q<mysql2>, [">= 0"])
63
+ s.add_development_dependency(%q<rspec>, ["~> 3.0.0.rc1"])
64
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
65
+ s.add_development_dependency(%q<bundler>, ["~> 1.0"])
66
+ s.add_development_dependency(%q<jeweler>, ["~> 2.0.1"])
67
+ s.add_development_dependency(%q<simplecov>, [">= 0"])
68
+ else
69
+ s.add_dependency(%q<totally_lazy>, [">= 0"])
70
+ s.add_dependency(%q<lock_method>, [">= 0"])
71
+ s.add_dependency(%q<encrypted_strings>, [">= 0"])
72
+ s.add_dependency(%q<mysql2>, [">= 0"])
73
+ s.add_dependency(%q<rspec>, ["~> 3.0.0.rc1"])
74
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
75
+ s.add_dependency(%q<bundler>, ["~> 1.0"])
76
+ s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
77
+ s.add_dependency(%q<simplecov>, [">= 0"])
78
+ end
79
+ else
80
+ s.add_dependency(%q<totally_lazy>, [">= 0"])
81
+ s.add_dependency(%q<lock_method>, [">= 0"])
82
+ s.add_dependency(%q<encrypted_strings>, [">= 0"])
83
+ s.add_dependency(%q<mysql2>, [">= 0"])
84
+ s.add_dependency(%q<rspec>, ["~> 3.0.0.rc1"])
85
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
86
+ s.add_dependency(%q<bundler>, ["~> 1.0"])
87
+ s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
88
+ s.add_dependency(%q<simplecov>, [">= 0"])
89
+ end
90
+ end
91
+
@@ -0,0 +1,21 @@
1
+ require 'sqlite3'
2
+
3
+ class Sqlite3
4
+
5
+ def initialize(database,show_sql=false)
6
+ @client = SQLite3::Database.new(database)
7
+ @show_sql = show_sql
8
+ end
9
+
10
+ def query(sql)
11
+ puts sql if @show_sql
12
+ @client.execute(sql)
13
+ end
14
+
15
+ def fields(table)
16
+ query('select * from ' + table.to_s).fields
17
+ end
18
+
19
+ end
20
+
21
+
data/lib/lazy_records.rb CHANGED
@@ -8,5 +8,6 @@ require_relative 'definition'
8
8
  require_relative 'memory_records'
9
9
  require_relative 'predicate_to_sql'
10
10
  require_relative 'sql_records'
11
+ require_relative 'sql_virtual_records'
11
12
  require_relative 'predicates'
12
13
  include LazyRecords
data/lib/predicates.rb CHANGED
@@ -2,3 +2,4 @@ def like(value)
2
2
  value_predicate(:like, '=', value)
3
3
  end
4
4
 
5
+
data/lib/record.rb CHANGED
@@ -4,6 +4,10 @@ module LazyRecords
4
4
  Record.new(sequence(data).in_pairs.map { |r| {r.key.name => r.value} }.reduce({}) { |a, b| a.merge(b) })
5
5
  end
6
6
 
7
+ def vrecord(*data)
8
+ Record.new(sequence(data).in_pairs.map { |r| {r.key => r.value} }.reduce({}) { |a, b| a.merge(b) })
9
+ end
10
+
7
11
  class Record < OpenStruct
8
12
 
9
13
  def get_hash
data/lib/sql_records.rb CHANGED
@@ -18,13 +18,9 @@ module LazyRecords
18
18
  end
19
19
 
20
20
  def get(definition, selection=nil)
21
- if selection
22
- sql = 'select * from ' + definition.name.to_s + ' where ' + @predicate_to_sql.convert(selection)
23
- sequence(@c.query(sql).map { |r| Record.new(r) })
24
- else
25
- sql = 'select * from ' + definition.name.to_s
26
- sequence(@c.query(sql).map { |r| Record.new(r) })
27
- end
21
+ sql = option(selection).is_some? ? 'select * from ' + definition.name.to_s + ' where ' + @predicate_to_sql.convert(selection) :
22
+ 'select * from ' + definition.name.to_s
23
+ sequence(@c.query(sql).map { |r| Record.new(r) })
28
24
  end
29
25
 
30
26
  def set(definition, selection, *updates)
@@ -33,8 +29,8 @@ module LazyRecords
33
29
  sequence(@c.query(sql))
34
30
  end
35
31
 
36
- def remove(definition, selection)
37
- sql = 'delete from ' + definition.name.to_s + ' where ' + @predicate_to_sql.convert(selection)
32
+ def remove(definition, selection=nil)
33
+ sql = option(selection).is_some? ? 'delete from ' + definition.name.to_s + ' where ' + @predicate_to_sql.convert(selection) : 'delete from ' + definition.name.to_s
38
34
  sequence(@c.query(sql))
39
35
  end
40
36
 
@@ -0,0 +1,52 @@
1
+ module LazyRecords
2
+
3
+ class SqlVirtualRecords
4
+
5
+ def initialize(connection)
6
+ @c = connection
7
+ @predicate_to_sql = PredicateToSql.new
8
+ end
9
+
10
+ def add(table_name, records)
11
+ records.each do |r|
12
+ sql = 'insert into ' + table_name.to_s + " (#{r.get_hash.keys.to_a.join(',')}) values (#{r.get_hash.values.map(&:inspect).to_a.join(',')})"
13
+ @c.query(sql)
14
+ end
15
+ end
16
+
17
+ def get(table_name, selection=nil)
18
+ sql = option(selection).is_some? ? 'select * from ' + table_name.to_s + ' where ' + @predicate_to_sql.convert(selection) :
19
+ 'select * from ' + table_name.to_s
20
+ sequence(@c.query(sql).map { |r| Record.new(r) })
21
+ end
22
+
23
+ def set(table_name, selection, *updates)
24
+ pending_fields = Maps.merge(sequence(updates).in_pairs.map { |e| {e.key => e.value} })
25
+ sql = 'update ' + table_name.to_s + ' set ' + pending_fields.map { |k, v| "#{k}='#{v}'" }.join(',') + ' where ' + @predicate_to_sql.convert(selection)
26
+ sequence(@c.query(sql))
27
+ end
28
+
29
+ def remove(table_name, selection=nil)
30
+ sql = option(selection).is_some? ? 'delete from ' + table_name.to_s + ' where ' + @predicate_to_sql.convert(selection) : 'delete from ' + table_name.to_s
31
+ sequence(@c.query(sql))
32
+ end
33
+
34
+ def sql_query(sql)
35
+ sequence(@c.query(sql).map { |r| Record.new(r) })
36
+ end
37
+
38
+ private
39
+
40
+ def process_keywords(keyword_map, d1, d2)
41
+ keyword_map.map { |k, v| d1.name.to_s + ".#{k.name} = " + d2.name.to_s + ".#{v.name}" }.to_a.join(' and ')
42
+ end
43
+
44
+ def process_columns(d1, d2)
45
+ one = d1.columns_as_list.map{|k| " #{d1.name}.#{k} as '#{d1.name}_#{k}'"}.join(',')
46
+ two = d2.columns_as_list.map{|k| " #{d2.name}.#{k} as '#{d2.name}_#{k}'"}.join(',')
47
+ one + two
48
+ end
49
+
50
+ end
51
+
52
+ end
@@ -0,0 +1,145 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe SqlVirtualRecords do
4
+
5
+ before(:each) do
6
+ @c = double("DbConnection")
7
+ @records = SqlVirtualRecords.new(@c)
8
+ end
9
+
10
+ it 'should add records' do
11
+ allow(@c).to receive(:query).with('insert into people (name,age) values ("kostas",25)')
12
+ allow(@c).to receive(:query).with('insert into people (name,age) values ("kings",34)')
13
+ allow(@c).to receive(:query).with('select * from people') {
14
+ [vrecord(:name, 'kostas', :age, 25), vrecord(:name, 'kings', :age, 34)]
15
+ }
16
+
17
+ @records.add(:people, sequence(
18
+ vrecord(:name, 'kostas', :age, 25),
19
+ vrecord(:name, 'kings', :age, 34)))
20
+
21
+
22
+ expect(@records.get(:people).count).to eq(2)
23
+ expect(@records.get(:people).head.name).to eq('kostas')
24
+ end
25
+
26
+ it 'should update records with set' do
27
+ allow(@c).to receive(:query).with('insert into people (name,age) values ("kostas",25)')
28
+ allow(@c).to receive(:query).with('insert into people (name,age) values ("kings",34)')
29
+ allow(@c).to receive(:query).with("update people set age='35' where name = 'kostas'")
30
+ allow(@c).to receive(:query).with('select * from people') {
31
+ [vrecord(:name, 'kostas', :age, 35), vrecord(:name, 'kings', :age, 34)]
32
+ }
33
+
34
+ @records.add(:people, sequence(
35
+ vrecord(:name, 'kostas', :age, 25)))
36
+
37
+ @records.add(:people, sequence(
38
+ vrecord(:name, 'kings', :age, 34)))
39
+
40
+ @records.set(:people, where(name: equals('kostas')), :age, 35)
41
+ expect(@records.get(:people).filter(where(name: equals('kostas'))).head.age).to eq(35)
42
+ end
43
+
44
+
45
+ it 'should remove records' do
46
+ allow(@c).to receive(:query).with('insert into people (name,age) values ("andrew",25)')
47
+ allow(@c).to receive(:query).with('insert into people (name,age) values ("kostas",26)')
48
+ allow(@c).to receive(:query).with("delete from people where name = 'andrew'")
49
+ allow(@c).to receive(:query).with('select * from people') {
50
+ [vrecord(:name, 'kostas', :age, 26)]
51
+ }
52
+
53
+ @records.add(:people, sequence(
54
+ vrecord(:name, 'andrew', :age, 25),
55
+ vrecord(:name, 'kostas', :age, 26),
56
+ ))
57
+
58
+ @records.remove(:people, where(name: equals('andrew')))
59
+ expect(@records.get(:people).head).to eq(vrecord(:name, 'kostas', :age, 26))
60
+
61
+ end
62
+
63
+ it 'should filter records' do
64
+
65
+ allow(@c).to receive(:query).with('insert into people (name,age) values ("kostas",25)')
66
+ allow(@c).to receive(:query).with('insert into people (name,age) values ("andrew",26)')
67
+ allow(@c).to receive(:query).with('insert into people (name,age) values ("david",27)')
68
+ allow(@c).to receive(:query).with('insert into people (name,age) values ("mike",28)')
69
+ allow(@c).to receive(:query).with('insert into people (name,age) values ("kings",29)')
70
+
71
+ @records.add(:people, sequence(
72
+ vrecord(:name, 'kostas', :age, 25),
73
+ vrecord(:name, 'andrew', :age, 26),
74
+ vrecord(:name, 'david', :age, 27),
75
+ vrecord(:name, 'mike', :age, 28),
76
+ vrecord(:name, 'kings', :age, 29)))
77
+
78
+ allow(@c).to receive(:query).with("select * from people where name like '%k%'") {
79
+ [vrecord(:name, 'kostas', :age, 25),
80
+ vrecord(:name, 'mike', :age, 28),
81
+ vrecord(:name, 'kings', :age, 29)]
82
+ }
83
+
84
+ expect(@records.get(:people, where(name: like('%k%'))).count).to eq(3)
85
+ end
86
+
87
+ it 'should support sql query' do
88
+ data_for_joins
89
+
90
+ expected = sequence(
91
+ vrecord(:name, 'Khilan', :amount, 1560),
92
+ vrecord(:name, 'kaushik', :amount, 3000),
93
+ vrecord(:name, 'kaushik', :amount, 1500),
94
+ vrecord(:name, 'Chaitali', :amount, 2060)
95
+ )
96
+
97
+ allow(@c).to receive(:query).with("select people.name, orders.amount from people inner join orders on orders.id = people.id") {
98
+ [vrecord(:name, 'Khilan', :amount, 1560),
99
+ vrecord(:name, 'kaushik', :amount, 3000),
100
+ vrecord(:name, 'kaushik', :amount, 1500),
101
+ vrecord(:name, 'Chaitali', :amount, 2060)]
102
+ }
103
+
104
+ expect(@records.sql_query("select people.name, orders.amount from people inner join orders on orders.id = people.id").entries).to eq(expected.entries)
105
+ end
106
+
107
+
108
+ def data_for_joins
109
+ allow(@c).to receive(:query).with('insert into people (id,name,age,address,salary) values (1,"Ramesh",32,"Ahmedabad",2000.0)')
110
+ allow(@c).to receive(:query).with('insert into people (id,name,age,address,salary) values (2,"Khilan",25,"Delhi",1500.0)')
111
+ allow(@c).to receive(:query).with('insert into people (id,name,age,address,salary) values (3,"kaushik",23,"Kota",2000.0)')
112
+ allow(@c).to receive(:query).with('insert into people (id,name,age,address,salary) values (4,"Chaitali",25,"Mumbai",6500.0)')
113
+ allow(@c).to receive(:query).with('insert into people (id,name,age,address,salary) values (5,"Hardik",27,"Bhopal",8500.0)')
114
+ allow(@c).to receive(:query).with('insert into people (id,name,age,address,salary) values (6,"Komal",22,"MP",4500.0)')
115
+ allow(@c).to receive(:query).with('insert into people (id,name,age,address,salary) values (7,"Muffy",24,"Indore",10000.0)')
116
+
117
+ people_list = sequence(
118
+ vrecord(:id, 1, :name, 'Ramesh', :age, 32, :address, 'Ahmedabad', :salary, 2000.00),
119
+ vrecord(:id, 2, :name, 'Khilan', :age, 25, :address, 'Delhi', :salary, 1500.00),
120
+ vrecord(:id, 3, :name, 'kaushik', :age, 23, :address, 'Kota', :salary, 2000.00),
121
+ vrecord(:id, 4, :name, 'Chaitali', :age, 25, :address, 'Mumbai', :salary, 6500.00),
122
+ vrecord(:id, 5, :name, 'Hardik', :age, 27, :address, 'Bhopal', :salary, 8500.00),
123
+ vrecord(:id, 6, :name, 'Komal', :age, 22, :address, 'MP', :salary, 4500.00),
124
+ vrecord(:id, 7, :name, 'Muffy', :age, 24, :address, 'Indore', :salary, 10000.00)
125
+ )
126
+
127
+ @records.add(:people, people_list)
128
+
129
+ allow(@c).to receive(:query).with('insert into orders (order_id,id,date,amount) values (102,3,"2014",3000)')
130
+ allow(@c).to receive(:query).with('insert into orders (order_id,id,date,amount) values (100,3,"2013",1500)')
131
+ allow(@c).to receive(:query).with('insert into orders (order_id,id,date,amount) values (101,2,"2012",1560)')
132
+ allow(@c).to receive(:query).with('insert into orders (order_id,id,date,amount) values (103,4,"2012",2060)')
133
+
134
+ orders_list = sequence(
135
+ vrecord(:order_id, 102, :id, 3, :date, '2014', :amount, 3000),
136
+ vrecord(:order_id, 100, :id, 3, :date, '2013', :amount, 1500),
137
+ vrecord(:order_id, 101, :id, 2, :date, '2012', :amount, 1560),
138
+ vrecord(:order_id, 103, :id, 4, :date, '2012', :amount, 2060)
139
+ )
140
+
141
+ @records.add(:orders, orders_list)
142
+ end
143
+
144
+
145
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lazy_records
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kingsley Hendrickse
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-03 00:00:00.000000000 Z
11
+ date: 2014-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: totally_lazy
@@ -152,9 +152,11 @@ files:
152
152
  - README.md
153
153
  - Rakefile
154
154
  - VERSION
155
+ - lazy_records.gemspec
155
156
  - lazy_store.gemspec
156
157
  - lib/adapters/mssql.rb
157
158
  - lib/adapters/mysql.rb
159
+ - lib/adapters/sqlite3.rb
158
160
  - lib/definition.rb
159
161
  - lib/keyword.rb
160
162
  - lib/lazy_records.rb
@@ -163,9 +165,11 @@ files:
163
165
  - lib/predicates.rb
164
166
  - lib/record.rb
165
167
  - lib/sql_records.rb
168
+ - lib/sql_virtual_records.rb
166
169
  - spec/memory_records_spec.rb
167
170
  - spec/spec_helper.rb
168
171
  - spec/sql_records_spec.rb
172
+ - spec/sql_virtual_records_spec.rb
169
173
  - spec/test_data/.keep
170
174
  homepage: http://github.com/kingsleyh/lazy_records
171
175
  licenses: