aquel 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5cab421bf60cbe9a1db710ef05330cdb68efc011
4
+ data.tar.gz: 61ac189a192ef2199e02953b734c9104035e152e
5
+ SHA512:
6
+ metadata.gz: 48cacbfc062b88aa76ad03796d44d1d00ff1ef78be30c4ff31640a3f951ce8b90b063b002b9e337699f2f17e3b9f6699d30d5d58e9dff3fa477627b25860a484
7
+ data.tar.gz: 101ffd59ef8bdb6b772993b6ce3dd02d6e87bffb3f3fca752d316d2c7e39b4a2ca85ed59c6b6816bd8ac34b42a8a1a024bdf47147c439bc5b371e5b7536ede38
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in aquel.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 youpy
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,67 @@
1
+ # aquel
2
+
3
+ a(nything) que(ry) l(anguage)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'aquel'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install aquel
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ aquel = Aquel.define 'tsv' do
25
+ document do |attributes|
26
+ open(attributes['path'])
27
+ end
28
+
29
+ item do |document|
30
+ document.gets
31
+ end
32
+
33
+ split do |item|
34
+ item.chomp.split(/\t/)
35
+ end
36
+ end
37
+
38
+ items = aquel.execute("select 1,3 from tsv where path = '/path/to/file.tsv' and 1 = 'foo'")
39
+ ```
40
+
41
+ ```ruby
42
+ require 'open-uri'
43
+
44
+ aquel = Aquel.define 'html' do
45
+ document do |attributes|
46
+ Nokogiri::HTML(open(attributes['url']))
47
+ end
48
+
49
+ find_by('css') do |css, document|
50
+ document.css(css)
51
+ end
52
+
53
+ split do |item|
54
+ item.text
55
+ end
56
+ end
57
+
58
+ items = aquel.execute("select * from html where url = 'http://example.com/foo' and css = 'div.bar'")
59
+ ```
60
+
61
+ ## Contributing
62
+
63
+ 1. Fork it ( https://github.com/youpy/aquel/fork )
64
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
65
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
66
+ 4. Push to the branch (`git push origin my-new-feature`)
67
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'aquel/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "aquel"
8
+ spec.version = Aquel::VERSION
9
+ spec.authors = ["youpy"]
10
+ spec.email = ["youpy@buycheapviagraonlinenow.com"]
11
+ spec.summary = %q{A(nything) Q(uery) L(anguage)}
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "pg_query"
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "nokogiri"
24
+ end
@@ -0,0 +1,11 @@
1
+ require "aquel/context"
2
+ require "aquel/executor"
3
+ require "aquel/version"
4
+
5
+ module Aquel
6
+ def define(name, &block)
7
+ Executor.new.define(name, &block)
8
+ end
9
+
10
+ module_function :define
11
+ end
@@ -0,0 +1,34 @@
1
+ module Aquel
2
+ class Context
3
+ attr_reader :document_block, :item_block, :items_block, :split_block, :find_by_block
4
+
5
+ def initialize(block)
6
+ @block = block
7
+ @find_by_block = {}
8
+ end
9
+
10
+ def execute!
11
+ instance_eval(&@block)
12
+ end
13
+
14
+ def document(&block)
15
+ @document_block = block
16
+ end
17
+
18
+ def item(&block)
19
+ @item_block = block
20
+ end
21
+
22
+ def split(&block)
23
+ @split_block = block
24
+ end
25
+
26
+ def items(&block)
27
+ @items_block = block
28
+ end
29
+
30
+ def find_by(name, &block)
31
+ @find_by_block[name] = block
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,108 @@
1
+ require 'pg_query'
2
+
3
+ module Aquel
4
+ class Executor
5
+ attr_reader :context
6
+
7
+ def initialize
8
+ @contexts = {}
9
+ end
10
+
11
+ def define(name, &block)
12
+ @contexts[name] = Context.new(block)
13
+
14
+ self
15
+ end
16
+
17
+ def execute(sql)
18
+ ast = parser.parse(sql).parsetree.first
19
+ type = ast['SELECT']['fromClause'][0]['RANGEVAR']['relname']
20
+
21
+ context = @contexts[type]
22
+ context.execute!
23
+
24
+ items = []
25
+ attributes = {}
26
+
27
+ walk(ast['SELECT']['whereClause'], attributes)
28
+ document = context.document_block.call(attributes)
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[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
+
48
+ items = filter(items, attributes)
49
+ items = colum_filter(items, ast['SELECT']['targetList'])
50
+ end
51
+
52
+ def filter(items, attributes)
53
+ attributes.each do |k, v|
54
+ if k.kind_of?(Fixnum)
55
+ items = items.find_all do |item|
56
+ item[k-1] == v
57
+ end
58
+ end
59
+ end
60
+
61
+ items
62
+ end
63
+
64
+ def colum_filter(items, target_list)
65
+ items.map do |item|
66
+ result = []
67
+
68
+ target_list.each do |target|
69
+ val = expr_value(target['RESTARGET']['val'])
70
+
71
+ case val
72
+ when {"A_STAR"=>{}}
73
+ result = item
74
+ when Fixnum
75
+ result << item[val-1]
76
+ end
77
+ end
78
+
79
+ result
80
+ end
81
+ end
82
+
83
+ def walk(aexpr, attributes)
84
+ if aexpr['AEXPR']
85
+ k = expr_value(aexpr['AEXPR']['lexpr'])
86
+ v = expr_value(aexpr['AEXPR']['rexpr'])
87
+ attributes[k] = v
88
+ elsif aexpr['AEXPR AND']
89
+ walk(aexpr['AEXPR AND']['lexpr'], attributes)
90
+ walk(aexpr['AEXPR AND']['rexpr'], attributes)
91
+ elsif aexpr['AEXPR OR']
92
+ raise 'OR clauses are not supported yet'
93
+ end
94
+ end
95
+
96
+ def expr_value(expr)
97
+ if expr['COLUMNREF']
98
+ expr['COLUMNREF']['fields'][0]
99
+ elsif expr['A_CONST']
100
+ expr['A_CONST']['val']
101
+ end
102
+ end
103
+
104
+ def parser
105
+ @parser ||= PgQuery
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,3 @@
1
+ module Aquel
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,117 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ require 'nokogiri'
4
+
5
+ describe Aquel do
6
+ describe 'TSV parser' do
7
+ let(:tsv_path) {
8
+ File.dirname(__FILE__) + '/test.tsv'
9
+ }
10
+
11
+ let(:aquel) {
12
+ Aquel.define 'tsv' do
13
+ document do |attributes|
14
+ open(attributes['path'])
15
+ end
16
+
17
+ item do |document|
18
+ document.gets
19
+ end
20
+
21
+ split do |item|
22
+ item.chomp.split(/\t/)
23
+ end
24
+ end
25
+ }
26
+
27
+ context 'simple query' do
28
+ it 'finds matching line' do
29
+ # TODO: support prepared statement
30
+ items = aquel.execute("select * from tsv where path = '#{tsv_path}'")
31
+
32
+ expect(items.size).to eql(2)
33
+ expect(items.first).to eql(%w/foo1 bar1 baz1/)
34
+
35
+ items = aquel.execute("select 1,3 from tsv where path = '#{tsv_path}'")
36
+
37
+ expect(items.size).to eql(2)
38
+ expect(items.first).to eql(%w/foo1 baz1/)
39
+ end
40
+ end
41
+
42
+ context 'filter query' do
43
+ it 'finds matching line' do
44
+ # TODO: support prepared statement
45
+ items = aquel.execute("select * from tsv where path = '#{tsv_path}' and 1 = 'foo2'")
46
+
47
+ expect(items.size).to eql(1)
48
+ expect(items.first).to eql(%w/foo2 bar2 baz2/)
49
+
50
+ items = aquel.execute("select 1,3 from tsv where path = '#{tsv_path}' and 1 = 'foo1'")
51
+
52
+ expect(items.size).to eql(1)
53
+ expect(items.first).to eql(%w/foo1 baz1/)
54
+ end
55
+ end
56
+ end
57
+
58
+ describe 'HTML parser' do
59
+ let(:html_path) {
60
+ File.dirname(__FILE__) + '/test.html'
61
+ }
62
+
63
+ context 'items' do
64
+ let(:aquel) {
65
+ Aquel.define 'html' do
66
+ document do |attributes|
67
+ Nokogiri::HTML(open(attributes['path']))
68
+ end
69
+
70
+ items do |document|
71
+ document.css('div.foo')
72
+ end
73
+
74
+ split do |item|
75
+ item.text
76
+ end
77
+ end
78
+ }
79
+
80
+ context 'simple query' do
81
+ it 'finds matching line' do
82
+ items = aquel.execute("select * from html where path = '#{html_path}'")
83
+
84
+ expect(items.size).to eql(2)
85
+ expect(items.first).to eql(['a'])
86
+ end
87
+ end
88
+ end
89
+
90
+ context 'find_by' do
91
+ let(:aquel) {
92
+ Aquel.define 'html' do
93
+ document do |attributes|
94
+ Nokogiri::HTML(open(attributes['path']))
95
+ end
96
+
97
+ find_by('css') do |css, document|
98
+ document.css(css)
99
+ end
100
+
101
+ split do |item|
102
+ item.text
103
+ end
104
+ end
105
+ }
106
+
107
+ context 'simple query' do
108
+ it 'finds matching line' do
109
+ items = aquel.execute("select * from html where path = '#{html_path}' and css = 'div.foo'")
110
+
111
+ expect(items.size).to eql(2)
112
+ expect(items.first).to eql(['a'])
113
+ end
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,91 @@
1
+ require 'aquel'
2
+
3
+ # This file was generated by the `rspec --init` command. Conventionally, all
4
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
6
+ # file to always be loaded, without a need to explicitly require it in any files.
7
+ #
8
+ # Given that it is always loaded, you are encouraged to keep this file as
9
+ # light-weight as possible. Requiring heavyweight dependencies from this file
10
+ # will add to the boot time of your test suite on EVERY test run, even for an
11
+ # individual file that may not need all of that loaded. Instead, consider making
12
+ # a separate helper file that requires the additional dependencies and performs
13
+ # the additional setup, and require it from the spec files that actually need it.
14
+ #
15
+ # The `.rspec` file also contains a few flags that are not defaults but that
16
+ # users commonly want.
17
+ #
18
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19
+ RSpec.configure do |config|
20
+ # rspec-expectations config goes here. You can use an alternate
21
+ # assertion/expectation library such as wrong or the stdlib/minitest
22
+ # assertions if you prefer.
23
+ config.expect_with :rspec do |expectations|
24
+ # This option will default to `true` in RSpec 4. It makes the `description`
25
+ # and `failure_message` of custom matchers include text for helper methods
26
+ # defined using `chain`, e.g.:
27
+ # be_bigger_than(2).and_smaller_than(4).description
28
+ # # => "be bigger than 2 and smaller than 4"
29
+ # ...rather than:
30
+ # # => "be bigger than 2"
31
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
32
+ end
33
+
34
+ # rspec-mocks config goes here. You can use an alternate test double
35
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
36
+ config.mock_with :rspec do |mocks|
37
+ # Prevents you from mocking or stubbing a method that does not exist on
38
+ # a real object. This is generally recommended, and will default to
39
+ # `true` in RSpec 4.
40
+ mocks.verify_partial_doubles = true
41
+ end
42
+
43
+ # The settings below are suggested to provide a good initial experience
44
+ # with RSpec, but feel free to customize to your heart's content.
45
+ =begin
46
+ # These two settings work together to allow you to limit a spec run
47
+ # to individual examples or groups you care about by tagging them with
48
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
49
+ # get run.
50
+ config.filter_run :focus
51
+ config.run_all_when_everything_filtered = true
52
+
53
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
54
+ # For more details, see:
55
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
56
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
57
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
58
+ config.disable_monkey_patching!
59
+
60
+ # This setting enables warnings. It's recommended, but in some cases may
61
+ # be too noisy due to issues in dependencies.
62
+ config.warnings = true
63
+
64
+ # Many RSpec users commonly either run the entire suite or an individual
65
+ # file, and it's useful to allow more verbose output when running an
66
+ # individual spec file.
67
+ if config.files_to_run.one?
68
+ # Use the documentation formatter for detailed output,
69
+ # unless a formatter has already been configured
70
+ # (e.g. via a command-line flag).
71
+ config.default_formatter = 'doc'
72
+ end
73
+
74
+ # Print the 10 slowest examples and example groups at the
75
+ # end of the spec run, to help surface which specs are running
76
+ # particularly slow.
77
+ config.profile_examples = 10
78
+
79
+ # Run specs in random order to surface order dependencies. If you find an
80
+ # order dependency and want to debug it, you can fix the order by providing
81
+ # the seed, which is printed after each run.
82
+ # --seed 1234
83
+ config.order = :random
84
+
85
+ # Seed global randomization in this process using the `--seed` CLI option.
86
+ # Setting this allows you to use `--seed` to deterministically reproduce
87
+ # test failures related to randomization by passing the same `--seed` value
88
+ # as the one that triggered the failure.
89
+ Kernel.srand config.seed
90
+ =end
91
+ end
@@ -0,0 +1,6 @@
1
+ <html>
2
+ <body>
3
+ <div class="foo">a</div>
4
+ <div class="foo">b</div>
5
+ </body>
6
+ </html>
@@ -0,0 +1,2 @@
1
+ foo1 bar1 baz1
2
+ foo2 bar2 baz2
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aquel
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - youpy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pg_query
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: nokogiri
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - youpy@buycheapviagraonlinenow.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - aquel.gemspec
83
+ - lib/aquel.rb
84
+ - lib/aquel/context.rb
85
+ - lib/aquel/executor.rb
86
+ - lib/aquel/version.rb
87
+ - spec/aquel_spec.rb
88
+ - spec/spec_helper.rb
89
+ - spec/test.html
90
+ - spec/test.tsv
91
+ homepage: ''
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.2.0.rc.1
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: A(nything) Q(uery) L(anguage)
115
+ test_files:
116
+ - spec/aquel_spec.rb
117
+ - spec/spec_helper.rb
118
+ - spec/test.html
119
+ - spec/test.tsv