mohair 0.0.3 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +3 -2
- data/lib/mohair.rb +16 -4
- data/lib/mohair/inserter.rb +3 -1
- data/lib/mohair/selector.rb +1 -0
- data/lib/mohair/sql/parser.rb +11 -3
- data/lib/mohair/sql/select.rb +3 -1
- data/lib/mohair/sql/tree.rb +21 -29
- data/lib/mohair/version.rb +1 -1
- data/test/plugin/sql.rb +5 -0
- metadata +4 -4
data/README.md
CHANGED
@@ -45,9 +45,10 @@ $ bundle exec bin/mohair -q "select * from sometable"
|
|
45
45
|
|
46
46
|
## TODO
|
47
47
|
|
48
|
-
- group by
|
49
48
|
- limit 10
|
50
49
|
- asc/desc
|
50
|
+
- types: timestamp, float
|
51
|
+
- recursive representation of json with dot notation
|
51
52
|
- 2i
|
52
53
|
- query optimization
|
53
54
|
|
@@ -60,5 +61,5 @@ $ bundle exec bin/mohair -q "select * from sometable"
|
|
60
61
|
5. Create new Pull Request
|
61
62
|
|
62
63
|
```
|
63
|
-
$
|
64
|
+
$ rake release
|
64
65
|
```
|
data/lib/mohair.rb
CHANGED
@@ -10,14 +10,14 @@ require "optparse"
|
|
10
10
|
require "logger"
|
11
11
|
|
12
12
|
LOG = Logger.new(STDERR)
|
13
|
-
LOG.level = Logger::
|
13
|
+
LOG.level = Logger::INFO # WARN, INFO, DEBUG, ...
|
14
14
|
|
15
15
|
module Mohair
|
16
16
|
|
17
17
|
def self.usage
|
18
18
|
print <<EOS
|
19
19
|
usage:
|
20
|
-
$ mohair -q "select foo, bar from bucket_name" [-i INDEX]
|
20
|
+
$ mohair -q "select foo, bar from bucket_name" [-i INDEX] [-s SERVER]
|
21
21
|
$ mohair_dump <bucket_name> < sample_data.json
|
22
22
|
|
23
23
|
insert, delete sentence is future work
|
@@ -30,13 +30,24 @@ EOS
|
|
30
30
|
|
31
31
|
q = nil #query!!
|
32
32
|
index = nil
|
33
|
+
host = 'localhost'
|
34
|
+
port = 8098
|
33
35
|
|
34
36
|
opt = OptionParser.new
|
37
|
+
opt.on('-h', '--help'){ usage }
|
38
|
+
opt.on('-d', '--debug'){
|
39
|
+
LOG.level = Logger::DEBUG
|
40
|
+
}
|
41
|
+
opt.on('-v', '--version'){ usage }
|
35
42
|
opt.on('-q Q'){|v| q = v}
|
36
43
|
opt.on('-i INDEX'){|v| index = v}
|
37
|
-
opt.on('-
|
44
|
+
opt.on('-s SERVER'){|v|
|
45
|
+
host, port = v.split(':')
|
46
|
+
}
|
38
47
|
|
39
48
|
opt.parse!(ARGV)
|
49
|
+
|
50
|
+
LOG.info("connecting #{host}:#{port}")
|
40
51
|
|
41
52
|
parser = Sql::Parser.new
|
42
53
|
sql_syntax_tree = parser.parse (q.strip)
|
@@ -52,7 +63,8 @@ EOS
|
|
52
63
|
LOG.debug "reducer->\n"
|
53
64
|
LOG.debug s.reducer
|
54
65
|
|
55
|
-
client = Riak::Client.new(:protocol => "http"
|
66
|
+
client = Riak::Client.new(:protocol => "http",
|
67
|
+
:nodes => [{:host => host, :http_port => port}])
|
56
68
|
bucket = Riak::MapReduce.new(client)
|
57
69
|
.add(client.bucket(s.bucket))## keyfilsters and so on here
|
58
70
|
|
data/lib/mohair/inserter.rb
CHANGED
@@ -7,7 +7,9 @@ module Mohair
|
|
7
7
|
@bucket_name = bucket_name
|
8
8
|
end
|
9
9
|
def insert_all objs
|
10
|
-
@client = Riak::Client.new(:protocol => "http"
|
10
|
+
@client = Riak::Client.new(:protocol => "http",
|
11
|
+
:nodes => [
|
12
|
+
{:http_port => 10018}])
|
11
13
|
bucket = @client.bucket(@bucket_name)
|
12
14
|
if bucket.props["allow_mult"] then
|
13
15
|
$stderr.puts "allow_mult should be false (how to handle siblilngs?)"
|
data/lib/mohair/selector.rb
CHANGED
data/lib/mohair/sql/parser.rb
CHANGED
@@ -6,7 +6,8 @@ module Mohair
|
|
6
6
|
module Sql
|
7
7
|
class Parser < Parslet::Parser
|
8
8
|
rule(:integer) { match('[0-9]').repeat(1) }
|
9
|
-
|
9
|
+
rule(:float) { integer.repeat >> str('.') >> integer.maybe }
|
10
|
+
#rule(:numeric) { integer | float | (str('-') >> numeric) }
|
10
11
|
|
11
12
|
rule(:space) { match('\s').repeat(1) }
|
12
13
|
rule(:space?) { space.maybe }
|
@@ -25,10 +26,17 @@ module Mohair
|
|
25
26
|
#rule(:like) { str('like') >> space? }
|
26
27
|
rule(:binop) { eq | neq | gt | lt | geq | leq | btw}
|
27
28
|
|
28
|
-
rule(:string) {
|
29
|
+
rule(:string) {
|
30
|
+
str('"') >>
|
31
|
+
(
|
32
|
+
str('\\') >> any |
|
33
|
+
str('"').absent? >> any
|
34
|
+
).repeat >>
|
35
|
+
str('"')
|
36
|
+
}
|
29
37
|
|
30
38
|
rule(:const) {
|
31
|
-
integer | string
|
39
|
+
integer | float | string
|
32
40
|
}
|
33
41
|
rule(:term) { const | item }
|
34
42
|
|
data/lib/mohair/sql/select.rb
CHANGED
data/lib/mohair/sql/tree.rb
CHANGED
@@ -110,43 +110,35 @@ module Mohair
|
|
110
110
|
rhs = tree[:rhs]
|
111
111
|
@op = tree[:op].to_s
|
112
112
|
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
113
|
+
@lhs = objectize lhs
|
114
|
+
@rhs = objectize rhs
|
115
|
+
end
|
116
|
+
|
117
|
+
def objectize s
|
118
|
+
if s.class == Hash then
|
119
|
+
Condition.new s
|
120
|
+
elsif (s.to_s =~ /^[0-9]+$/).nil? then
|
121
|
+
s.to_s
|
117
122
|
else
|
118
|
-
|
123
|
+
s.to_i
|
119
124
|
end
|
125
|
+
end
|
120
126
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
127
|
+
def jstify o
|
128
|
+
if o.class == Fixnum then
|
129
|
+
o
|
130
|
+
elsif o.class == Condition then
|
131
|
+
"( #{o.to_js} )"
|
132
|
+
elsif (o[0] == "\"" and o[-1] == "\"") then
|
133
|
+
o
|
125
134
|
else
|
126
|
-
|
135
|
+
"obj.#{o}"
|
127
136
|
end
|
128
137
|
end
|
129
138
|
|
130
139
|
def to_js
|
131
|
-
lhs =
|
132
|
-
|
133
|
-
rhs = @rhs
|
134
|
-
elsif @rhs.class == Condition then
|
135
|
-
rhs = "( #{@rhs.to_js} )"
|
136
|
-
elsif (@rhs[0] == "\"" and @rhs[-1] == "\"") then
|
137
|
-
rhs = @rhs
|
138
|
-
else
|
139
|
-
rhs = "obj.#{@rhs}"
|
140
|
-
end
|
141
|
-
if @lhs.class == Fixnum then
|
142
|
-
lhs = @lhs
|
143
|
-
elsif @lhs.class == Condition then
|
144
|
-
lhs = "( #{@lhs.to_js} )"
|
145
|
-
elsif (@lhs[0] == "\"" and @lhs[-1] == "\"") then
|
146
|
-
lhs = @lhs
|
147
|
-
else
|
148
|
-
lhs = "obj.#{@lhs}"
|
149
|
-
end
|
140
|
+
lhs = jstify @lhs
|
141
|
+
rhs = jstify @rhs
|
150
142
|
" (#{lhs}) #{operator2str(@op)} (#{rhs}) "
|
151
143
|
end
|
152
144
|
|
data/lib/mohair/version.rb
CHANGED
data/test/plugin/sql.rb
CHANGED
@@ -30,6 +30,7 @@ class ParserTest < MiniTest::Unit::TestCase
|
|
30
30
|
[
|
31
31
|
'select',
|
32
32
|
' select a from b',
|
33
|
+
'select a, b from c where a = "120.0, ~!@#$%^&*+_\}{\" asdfa"sf"',
|
33
34
|
].each do |bad_sql|
|
34
35
|
assert_raises Parslet::ParseFailed do
|
35
36
|
@parser.parse bad_sql
|
@@ -45,6 +46,10 @@ class ParserTest < MiniTest::Unit::TestCase
|
|
45
46
|
'select a,b from c where 20 < a or b = 234',
|
46
47
|
'select a, b from c where a = "oo"',
|
47
48
|
'select a, b from c where a = "oo" and c > 235',
|
49
|
+
'select a, b from c where a = "120.0, ~!@#$%^&*+_\}{\" asdfasf"',
|
50
|
+
#'select a from b where a > 20.0',
|
51
|
+
#'select a from b where a > 0.0',
|
52
|
+
#'select a from b where a > -0',
|
48
53
|
].each do |where_sql|
|
49
54
|
s = @parser.parse where_sql
|
50
55
|
assert_equal(expected = "select", actual = s[:op])
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mohair
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 5
|
10
|
+
version: 0.0.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- UENISHI Kota
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2013-
|
18
|
+
date: 2013-06-01 00:00:00 Z
|
19
19
|
dependencies: []
|
20
20
|
|
21
21
|
description: a new type of Riak client
|