aquel 0.0.3 → 0.0.4

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: feb46cbd8f0d7f2f1f69b1621c633f4e8a5dec9a
4
- data.tar.gz: 363185ab282a158a3c7cb8e33ace18562d28a24a
3
+ metadata.gz: 1236cbde36dfec9820fad7685dbecb610d6d5a83
4
+ data.tar.gz: d1912cfcec42567a72f9a9eba7388af5b10bdf2f
5
5
  SHA512:
6
- metadata.gz: cd0b27fe23bdfb2b6301b37bdbb9375253fea21c2d15ccecff4a486b8902096d99ecd4e9d2901aefa50dedc33f2dcb41f2cba663d5479713222dde5a3d53da08
7
- data.tar.gz: 04213723470351bbff63269c721fc194cfff1eee00510dd9ee08920196e6c79cc93d6cedc7ec9e2a70d37e1e08c98c2fcb2625e0e4114fff8538767d2916e9bf
6
+ metadata.gz: 0e4227298993b13b88673e96f5684920382884b2b7e02ab1febe60467bc324d0de4a3443e577586b5027d8eebe2f00b13e3e2c2f1f46e29b3f1abc1a78bffa19
7
+ data.tar.gz: e6b08b20f10bcb1c29386248ed1ff6092a50c1409e826b48cdbd8690fc10276be4d828d52baae5838d4978d61ba4a89147ee9b8537695c73a681c9a857a76cb0
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # aquel
1
+ # aquel [![Build Status](https://travis-ci.org/youpy/aquel.svg)](https://travis-ci.org/youpy/aquel)
2
2
 
3
3
  **a**(nything) **que**(ry) **l**(anguage)
4
4
 
data/Rakefile CHANGED
@@ -1,2 +1,8 @@
1
1
  require "bundler/gem_tasks"
2
2
 
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec) do |spec|
5
+ spec.pattern = FileList['spec/**/*_spec.rb']
6
+ end
7
+
8
+ task :default => :spec
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["youpy"]
10
10
  spec.email = ["youpy@buycheapviagraonlinenow.com"]
11
11
  spec.summary = %q{a(nything) que(ry) l(anguage)}
12
- spec.homepage = ""
12
+ spec.homepage = "https://github.com/youpy/aquel"
13
13
  spec.license = "MIT"
14
14
 
15
15
  spec.files = `git ls-files -z`.split("\x0")
@@ -21,4 +21,5 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency "bundler", "~> 1.7"
22
22
  spec.add_development_dependency "rake", "~> 10.0"
23
23
  spec.add_development_dependency "nokogiri"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
24
25
  end
@@ -1,3 +1,4 @@
1
+ require "aquel/attribute"
1
2
  require "aquel/attributes"
2
3
  require "aquel/context"
3
4
  require "aquel/executor"
@@ -0,0 +1,21 @@
1
+ module Aquel
2
+ class Attribute
3
+ attr_reader :name, :value
4
+
5
+ def initialize(options)
6
+ @name = options[:name]
7
+ @value = options[:value]
8
+ end
9
+
10
+ def operate(target)
11
+ case name
12
+ when '='
13
+ target == value
14
+ when '<>'
15
+ target != value
16
+ else
17
+ false
18
+ end
19
+ end
20
+ end
21
+ end
@@ -3,10 +3,10 @@ module Aquel
3
3
  def equal_values
4
4
  self.class[
5
5
  *(self.find_all do |k, v|
6
- v[:name] == '='
7
- end.map do |k, v|
8
- [k, v[:value]]
9
- end.flatten)
6
+ v.name == '='
7
+ end.map do |k, v|
8
+ [k, v.value]
9
+ end.flatten)
10
10
  ]
11
11
  end
12
12
  end
@@ -5,10 +5,48 @@ module Aquel
5
5
  def initialize(block)
6
6
  @block = block
7
7
  @find_by_block = {}
8
+ @header = false
8
9
  end
9
10
 
10
- def execute!
11
+ def execute(attributes)
11
12
  instance_eval(&@block)
13
+
14
+ items = []
15
+ document = document_block.call(attributes.equal_values)
16
+
17
+ if items_block
18
+ items_block.call(document).each do |item|
19
+ value = split_block.call(item)
20
+ items << (value.kind_of?(Array) ? value : [value])
21
+ end
22
+ elsif find_by_block.size > 0
23
+ find_by_block.each do |k, v|
24
+ v.call(attributes.equal_values[k], document).each do |item|
25
+ value = split_block.call(item)
26
+ items << (value.kind_of?(Array) ? value : [value])
27
+ end
28
+ end
29
+ else
30
+ while item = item_block.call(document)
31
+ items << split_block.call(item)
32
+ end
33
+ end
34
+
35
+ if @header
36
+ header = items.shift.map(&:downcase)
37
+ else
38
+ header = (1..(items.first.size)).to_a
39
+ end
40
+
41
+ items.map do |itm|
42
+ item = {}
43
+ itm.each_with_index { |v, i| item[header[i]] = v }
44
+ item
45
+ end
46
+ ensure
47
+ if document.respond_to?(:close)
48
+ document.close
49
+ end
12
50
  end
13
51
 
14
52
  def document(&block)
@@ -30,5 +68,9 @@ module Aquel
30
68
  def find_by(name, &block)
31
69
  @find_by_block[name] = block
32
70
  end
71
+
72
+ def has_header
73
+ @header = true
74
+ end
33
75
  end
34
76
  end
@@ -18,45 +18,29 @@ module Aquel
18
18
  ast = parser.parse(sql).parsetree.first
19
19
  type = ast['SELECT']['fromClause'][0]['RANGEVAR']['relname']
20
20
 
21
- context = @contexts[type]
22
- context.execute!
23
-
24
21
  items = []
25
22
  attributes = Attributes.new
26
23
 
27
24
  walk(ast['SELECT']['whereClause'], attributes)
28
- document = context.document_block.call(attributes.equal_values)
29
-
30
- if context.items_block
31
- context.items_block.call(document).each do |item|
32
- value = context.split_block.call(item)
33
- items << (value.kind_of?(Array) ? value : [value])
34
- end
35
- elsif context.find_by_block.size > 0
36
- context.find_by_block.each do |k, v|
37
- v.call(attributes.equal_values[k], document).each do |item|
38
- value = context.split_block.call(item)
39
- items << (value.kind_of?(Array) ? value : [value])
40
- end
41
- end
42
- else
43
- while item = context.item_block.call(document)
44
- items << context.split_block.call(item)
45
- end
46
- end
47
-
25
+ context = @contexts[type]
26
+ items = context.execute(attributes)
48
27
  items = filter(items, attributes)
49
28
  items = colum_filter(items, ast['SELECT']['targetList'])
50
29
  end
51
30
 
52
31
  def filter(items, attributes)
53
32
  attributes.each do |k, v|
54
- if k.kind_of?(Fixnum)
33
+ case k
34
+ when Fixnum
35
+ items = items.find_all do |item|
36
+ v.operate(item[k])
37
+ end
38
+ when String
55
39
  items = items.find_all do |item|
56
- if v[:name] == '='
57
- item[k-1] == v[:value]
58
- elsif v[:name] == '<>'
59
- item[k-1] != v[:value]
40
+ if item[k]
41
+ v.operate(item[k])
42
+ else
43
+ true
60
44
  end
61
45
  end
62
46
  end
@@ -67,7 +51,7 @@ module Aquel
67
51
 
68
52
  def colum_filter(items, target_list)
69
53
  items.map do |item|
70
- result = []
54
+ result = {}
71
55
 
72
56
  target_list.each do |target|
73
57
  val = expr_value(target['RESTARGET']['val'])
@@ -76,7 +60,9 @@ module Aquel
76
60
  when {"A_STAR"=>{}}
77
61
  result = item
78
62
  when Fixnum
79
- result << item[val-1]
63
+ result[val] = item[val]
64
+ when String
65
+ result[val] = item[val]
80
66
  end
81
67
  end
82
68
 
@@ -88,7 +74,7 @@ module Aquel
88
74
  if aexpr = node['AEXPR']
89
75
  k = expr_value(aexpr['lexpr'])
90
76
  v = expr_value(aexpr['rexpr'])
91
- attributes[k] = { :value => v, :name => aexpr['name'][0] }
77
+ attributes[k] = Attribute.new(:value => v, :name => aexpr['name'][0])
92
78
  elsif aexpr = node['AEXPR AND']
93
79
  walk(aexpr['lexpr'], attributes)
94
80
  walk(aexpr['rexpr'], attributes)
@@ -1,3 +1,3 @@
1
1
  module Aquel
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -10,6 +10,8 @@ describe Aquel do
10
10
 
11
11
  let(:aquel) {
12
12
  Aquel.define 'tsv' do
13
+ has_header
14
+
13
15
  document do |attributes|
14
16
  open(attributes['path'])
15
17
  end
@@ -30,32 +32,32 @@ describe Aquel do
30
32
  items = aquel.execute("select * from tsv where path = '#{tsv_path}'")
31
33
 
32
34
  expect(items.size).to eql(2)
33
- expect(items.first).to eql(%w/foo1 bar1 baz1/)
35
+ expect(items.first).to eql({"col1"=>"foo1", "col2"=>"bar1", "col3"=>"baz1"})
34
36
 
35
- items = aquel.execute("select 1,3 from tsv where path = '#{tsv_path}'")
37
+ items = aquel.execute("select COL1,col3 from tsv where path = '#{tsv_path}'")
36
38
 
37
39
  expect(items.size).to eql(2)
38
- expect(items.first).to eql(%w/foo1 baz1/)
40
+ expect(items.first).to eql({"col1"=>"foo1", "col3"=>"baz1"})
39
41
  end
40
42
  end
41
43
 
42
44
  context 'filter query' do
43
45
  it 'finds matching line' do
44
46
  # TODO: support prepared statement
45
- items = aquel.execute("select * from tsv where path = '#{tsv_path}' and 1 = 'foo2'")
47
+ items = aquel.execute("select * from tsv where path = '#{tsv_path}' and col1 = 'foo2'")
46
48
 
47
49
  expect(items.size).to eql(1)
48
- expect(items.first).to eql(%w/foo2 bar2 baz2/)
50
+ expect(items.first).to eql({"col1"=>"foo2", "col2"=>"bar2", "col3"=>"baz2"})
49
51
 
50
- items = aquel.execute("select 1,3 from tsv where path = '#{tsv_path}' and 1 = 'foo1'")
52
+ items = aquel.execute("select col1,col3 from tsv where path = '#{tsv_path}' and col1 = 'foo1'")
51
53
 
52
54
  expect(items.size).to eql(1)
53
- expect(items.first).to eql(%w/foo1 baz1/)
55
+ expect(items.first).to eql({"col1"=>"foo1", "col3"=>"baz1"})
54
56
 
55
- items = aquel.execute("select 1,3 from tsv where path = '#{tsv_path}' and 1 != 'foo1'")
57
+ items = aquel.execute("select col1,COL3 from tsv where path = '#{tsv_path}' and col1 != 'foo1'")
56
58
 
57
59
  expect(items.size).to eql(1)
58
- expect(items.first).to eql(%w/foo2 baz2/)
60
+ expect(items.first).to eql({"col1"=>"foo2", "col3"=>"baz2"})
59
61
  end
60
62
  end
61
63
  end
@@ -87,7 +89,7 @@ describe Aquel do
87
89
  items = aquel.execute("select * from html where path = '#{html_path}'")
88
90
 
89
91
  expect(items.size).to eql(2)
90
- expect(items.first).to eql(['a'])
92
+ expect(items.first).to eql({1 => 'a'})
91
93
  end
92
94
  end
93
95
  end
@@ -114,7 +116,7 @@ describe Aquel do
114
116
  items = aquel.execute("select * from html where path = '#{html_path}' and css = 'div.foo'")
115
117
 
116
118
  expect(items.size).to eql(2)
117
- expect(items.first).to eql(['a'])
119
+ expect(items.first).to eql({1 => 'a'})
118
120
  end
119
121
  end
120
122
  end
@@ -1,2 +1,3 @@
1
+ COL1 COL2 COL3
1
2
  foo1 bar1 baz1
2
3
  foo2 bar2 baz2
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aquel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - youpy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-23 00:00:00.000000000 Z
11
+ date: 2015-04-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg_query
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
69
83
  description:
70
84
  email:
71
85
  - youpy@buycheapviagraonlinenow.com
@@ -81,6 +95,7 @@ files:
81
95
  - Rakefile
82
96
  - aquel.gemspec
83
97
  - lib/aquel.rb
98
+ - lib/aquel/attribute.rb
84
99
  - lib/aquel/attributes.rb
85
100
  - lib/aquel/context.rb
86
101
  - lib/aquel/executor.rb
@@ -89,7 +104,7 @@ files:
89
104
  - spec/spec_helper.rb
90
105
  - spec/test.html
91
106
  - spec/test.tsv
92
- homepage: ''
107
+ homepage: https://github.com/youpy/aquel
93
108
  licenses:
94
109
  - MIT
95
110
  metadata: {}