jrubysql 0.1.1 → 0.1.3
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/.gitignore +1 -0
- data/CHANGELOG.md +12 -0
- data/Guardfile +7 -0
- data/README.md +12 -8
- data/jrubysql.gemspec +3 -0
- data/lib/jrubysql/config.rb +1 -2
- data/lib/jrubysql/constants.rb +4 -3
- data/lib/jrubysql/controller.rb +9 -3
- data/lib/jrubysql/input/console.rb +2 -2
- data/lib/jrubysql/input/{file.rb → script.rb} +2 -3
- data/lib/jrubysql/messages.yml +2 -0
- data/lib/jrubysql/option_parser.rb +11 -3
- data/lib/jrubysql/output/csv.rb +0 -3
- data/lib/jrubysql/rdbms.rb +12 -1
- data/lib/jrubysql/version.rb +1 -1
- data/lib/jrubysql.rb +1 -1
- data/test/helper.rb +34 -0
- data/test/test_jrubysql.rb +211 -6
- data/test/test_option_parser.rb +122 -0
- metadata +188 -190
- data/test/test_cterm.rb +0 -0
- data/test/test_term_output.rb +0 -0
data/.gitignore
CHANGED
data/CHANGELOG.md
ADDED
data/Guardfile
ADDED
data/README.md
CHANGED
@@ -14,8 +14,8 @@ Usage
|
|
14
14
|
|
15
15
|
```
|
16
16
|
usage: jrubysql [options]
|
17
|
-
jrubysql -t DBMS_TYPE -h HOSTNAME [-u USERNAME -p [PASSWORD] [-d DATABASE]
|
18
|
-
jrubysql -c CLASSNAME -j JDBC_URL [-u USERNAME -p [PASSWORD] [-d DATABASE]
|
17
|
+
jrubysql -t DBMS_TYPE -h HOSTNAME [-u USERNAME] [-p [PASSWORD]] [-d DATABASE]
|
18
|
+
jrubysql -c CLASSNAME -j JDBC_URL [-u USERNAME] [-p [PASSWORD]] [-d DATABASE]
|
19
19
|
|
20
20
|
-t, --type DBMS_TYPE Database type: mysql/oracle/postgres/sqlserver
|
21
21
|
-h, --host HOST DBMS host address
|
@@ -28,6 +28,7 @@ usage: jrubysql [options]
|
|
28
28
|
-d, --database DATABASE Name of the database (optional)
|
29
29
|
|
30
30
|
-f, --filename FILENAME SQL script file
|
31
|
+
-e, --execute SQLSCRIPT SQL script
|
31
32
|
-o, --output OUTPUT_TYPE Output type: cterm|term|csv (default: cterm)
|
32
33
|
|
33
34
|
--help Show this message
|
@@ -47,20 +48,27 @@ export CLASSPATH=$CLASSPATH:~/lib/mysql-connector-java-5.1.17-bin.jar:~/lib/ojdb
|
|
47
48
|
### With type (-t) and hostname (-h)
|
48
49
|
|
49
50
|
```
|
50
|
-
# Supports MySQL/Oracle/PostgreSQL/MSSQL
|
51
|
+
# Supports MySQL/Oracle/PostgreSQL/MSSQL/SQLite
|
51
52
|
|
52
53
|
jrubysql -t mysql -h localhost -d test -u user -p
|
53
54
|
jrubysql -t oracle -h localhost:1521/orcl -u user -p password
|
54
55
|
jrubysql -t postgres -h localhost -u root
|
55
|
-
jrubysql -t sqlserver -h
|
56
|
+
jrubysql -t sqlserver -h localhost -u user -p password
|
57
|
+
jrubysql -t sqlite -h my.db
|
58
|
+
# In case of SQLite, Host = DB file
|
56
59
|
```
|
57
60
|
|
58
61
|
### Connect with class name of JDBC driver (-c) and JDBC URL (-j)
|
59
62
|
|
60
63
|
```
|
61
64
|
# You can connect to any database with its JDBC driver
|
65
|
+
# SQLite
|
66
|
+
bin/jrubysql -c org.sqlite.JDBC -j jdbc:sqlite:my.db
|
62
67
|
|
68
|
+
# PostgreSQL
|
63
69
|
bin/jrubysql -corg.postgresql.Driver -jjdbc:postgresql://localhost/test
|
70
|
+
|
71
|
+
# MySQL
|
64
72
|
bin/jrubysql -ccom.mysql.jdbc.Driver -jjdbc:mysql://localhost/test -uuser -p
|
65
73
|
```
|
66
74
|
|
@@ -68,10 +76,6 @@ Screenshot
|
|
68
76
|
----------
|
69
77
|

|
70
78
|
|
71
|
-
TODO
|
72
|
-
----
|
73
|
-
TESTS!!!
|
74
|
-
|
75
79
|
Copyright
|
76
80
|
---------
|
77
81
|
Copyright (c) 2012 Junegunn Choi. See LICENSE.txt for
|
data/jrubysql.gemspec
CHANGED
@@ -20,6 +20,9 @@ Gem::Specification.new do |s|
|
|
20
20
|
|
21
21
|
# specify any dependencies here; for example:
|
22
22
|
s.add_development_dependency "test-unit"
|
23
|
+
s.add_development_dependency "mocha"
|
24
|
+
s.add_development_dependency "guard"
|
25
|
+
s.add_development_dependency "guard-test"
|
23
26
|
|
24
27
|
s.add_runtime_dependency "jdbc-helper", '~> 0.7.2'
|
25
28
|
s.add_runtime_dependency "insensitive_hash", '~> 0.2.3'
|
data/lib/jrubysql/config.rb
CHANGED
@@ -4,9 +4,8 @@ require 'fileutils'
|
|
4
4
|
module JRubySQL
|
5
5
|
# A simple key-value config in YAML
|
6
6
|
class Config
|
7
|
-
DEFAULT_PATH = File.join(ENV['HOME'], '.jrubysqlrc')
|
8
7
|
|
9
|
-
def initialize path =
|
8
|
+
def initialize path = JRubySQL::Constants::DEFAULT_RC_PATH
|
10
9
|
@path = path
|
11
10
|
if @path && File.exists?(@path)
|
12
11
|
@yaml = YAML.load(File.read(@path))
|
data/lib/jrubysql/constants.rb
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
module JRubySQL
|
2
2
|
module Constants
|
3
|
-
SUPPORTED_DBMS_TYPES = [ :mysql, :oracle, :postgres, :sqlserver ]
|
3
|
+
SUPPORTED_DBMS_TYPES = [ :mysql, :oracle, :postgres, :sqlserver, :sqlite ]
|
4
4
|
|
5
5
|
# .jrubysqlrc
|
6
|
-
|
6
|
+
DEFAULT_RC_PATH = File.join(ENV['HOME'], '.jrubysqlrc')
|
7
7
|
MAX_CONNECTION_HISTORY = 10
|
8
|
+
# MAX_COMMAND_HISTORY = 100
|
8
9
|
|
9
10
|
# Terminal (TBD)
|
10
|
-
MAX_COLUMN_WIDTH = 80
|
11
|
+
# MAX_COLUMN_WIDTH = 80
|
11
12
|
MIN_SCREEN_ROWS = 10
|
12
13
|
MAX_SCREEN_ROWS = 50
|
13
14
|
|
data/lib/jrubysql/controller.rb
CHANGED
@@ -20,7 +20,7 @@ class Controller
|
|
20
20
|
puts "[#{idx + 1}] #{history.first}"
|
21
21
|
end
|
22
22
|
print '> '
|
23
|
-
select =
|
23
|
+
select = JRubySQL::Controller.get_console_input
|
24
24
|
select = select && select.chomp
|
25
25
|
if (1..(histories.length)).include?(select.to_i)
|
26
26
|
history = histories[select.to_i - 1]
|
@@ -40,7 +40,9 @@ class Controller
|
|
40
40
|
|
41
41
|
# Setting up input: file or console (and more?)
|
42
42
|
if @options[:filename]
|
43
|
-
@input = JRubySQL::Input::
|
43
|
+
@input = JRubySQL::Input::Script.new(self, ::File.read(@options[:filename]))
|
44
|
+
elsif @options[:script]
|
45
|
+
@input = JRubySQL::Input::Script.new(self, @options[:script])
|
44
46
|
else
|
45
47
|
@input = JRubySQL::Input::Console.new(self)
|
46
48
|
end
|
@@ -101,6 +103,10 @@ class Controller
|
|
101
103
|
end
|
102
104
|
|
103
105
|
private
|
106
|
+
def self.get_console_input
|
107
|
+
$stdin.gets
|
108
|
+
end
|
109
|
+
|
104
110
|
def output result
|
105
111
|
@output.print_result result
|
106
112
|
end
|
@@ -120,7 +126,7 @@ private
|
|
120
126
|
when :autocommit
|
121
127
|
if params.nil?
|
122
128
|
@output.info m(:current_autocommit, @rdbms.autocommit ? 'on' : 'off')
|
123
|
-
elsif %[on off].include?(params.downcase)
|
129
|
+
elsif %w[on off].include?(params.downcase)
|
124
130
|
@rdbms.autocommit = params.downcase == 'on'
|
125
131
|
@output.info m(:turn_autocommit, params.downcase)
|
126
132
|
else
|
@@ -26,7 +26,7 @@ class Console
|
|
26
26
|
{ :commands => [cmd].compact }
|
27
27
|
# Line with delimiters
|
28
28
|
elsif line.include?(@esql.delimiter)
|
29
|
-
@esql << line +
|
29
|
+
@esql << line + $/
|
30
30
|
result = @esql.shift
|
31
31
|
result[:sqls].each do |sql|
|
32
32
|
Readline::HISTORY << sql + @esql.delimiter
|
@@ -34,7 +34,7 @@ class Console
|
|
34
34
|
{ :sqls => result[:sqls] }
|
35
35
|
# SQL without delimiter
|
36
36
|
else
|
37
|
-
@esql << line +
|
37
|
+
@esql << line + $/
|
38
38
|
empty_response
|
39
39
|
end
|
40
40
|
)
|
@@ -2,10 +2,9 @@ require 'each_sql'
|
|
2
2
|
|
3
3
|
module JRubySQL
|
4
4
|
module Input
|
5
|
-
class
|
6
|
-
def initialize controller,
|
5
|
+
class Script
|
6
|
+
def initialize controller, script
|
7
7
|
@controller = controller
|
8
|
-
script = ::File.read(file_path)
|
9
8
|
sqls = EachSQL(script, JRubySQL::Input.get_each_sql_type(@controller.db_type))
|
10
9
|
@ret = { :sqls => sqls }
|
11
10
|
end
|
data/lib/jrubysql/messages.yml
CHANGED
@@ -13,12 +13,12 @@ module OptionParser
|
|
13
13
|
opts.banner =
|
14
14
|
[
|
15
15
|
"usage: jrubysql [options]",
|
16
|
-
" jrubysql -t DBMS_TYPE -h HOSTNAME [-u USERNAME -p [PASSWORD] [-d DATABASE]
|
17
|
-
" jrubysql -c CLASSNAME -j JDBC_URL [-u USERNAME -p [PASSWORD] [-d DATABASE]
|
16
|
+
" jrubysql -t DBMS_TYPE -h HOSTNAME [-u USERNAME] [-p [PASSWORD]] [-d DATABASE]",
|
17
|
+
" jrubysql -c CLASSNAME -j JDBC_URL [-u USERNAME] [-p [PASSWORD]] [-d DATABASE]"
|
18
18
|
].join($/)
|
19
19
|
opts.separator ''
|
20
20
|
|
21
|
-
opts.on('-t', '--type DBMS_TYPE', 'Database type: mysql/oracle/postgres/sqlserver') do |v|
|
21
|
+
opts.on('-t', '--type DBMS_TYPE', 'Database type: mysql/oracle/postgres/sqlserver/sqlite') do |v|
|
22
22
|
options[:type] = v.downcase.to_sym
|
23
23
|
end
|
24
24
|
|
@@ -56,6 +56,10 @@ module OptionParser
|
|
56
56
|
options[:filename] = v
|
57
57
|
end
|
58
58
|
|
59
|
+
opts.on('-e', '--execute SQLSCRIPT', 'SQL script') do |v|
|
60
|
+
options[:script] = v
|
61
|
+
end
|
62
|
+
|
59
63
|
opts.on('-o', '--output OUTPUT_TYPE', 'Output type: cterm|term|csv (default: cterm)') do |v|
|
60
64
|
options[:output] = v
|
61
65
|
end
|
@@ -96,6 +100,10 @@ private
|
|
96
100
|
raise ArgumentError.new m(:invalid_output)
|
97
101
|
end
|
98
102
|
|
103
|
+
if opts[:script] && opts[:filename]
|
104
|
+
raise ArgumentError.new m(:both_script_and_filename)
|
105
|
+
end
|
106
|
+
|
99
107
|
if (!opts[:type] && !opts[:driver]) || (opts[:type] && opts[:driver])
|
100
108
|
raise ArgumentError.new m(:invalid_connection)
|
101
109
|
end
|
data/lib/jrubysql/output/csv.rb
CHANGED
data/lib/jrubysql/rdbms.rb
CHANGED
@@ -14,6 +14,8 @@ class RDBMS
|
|
14
14
|
:postgres
|
15
15
|
when /sqlserver/
|
16
16
|
:sqlserver
|
17
|
+
when /sqlite/
|
18
|
+
:sqlite
|
17
19
|
else
|
18
20
|
:unknown
|
19
21
|
end
|
@@ -40,6 +42,15 @@ class RDBMS
|
|
40
42
|
when :sqlserver
|
41
43
|
JDBCHelper::SqlServerConnector.connect(
|
42
44
|
options[:host], options[:user], options[:password], options[:database])
|
45
|
+
when :sqlite
|
46
|
+
JDBCHelper::Connection.new(
|
47
|
+
{
|
48
|
+
:driver => 'org.sqlite.JDBC',
|
49
|
+
:url => "jdbc:sqlite:#{options[:host]}",
|
50
|
+
:user => options[:user],
|
51
|
+
:password => options[:password]
|
52
|
+
}.reject { |k, v| v.nil? }
|
53
|
+
)
|
43
54
|
end
|
44
55
|
elsif options[:driver]
|
45
56
|
JDBCHelper::Connection.new(
|
@@ -48,7 +59,7 @@ class RDBMS
|
|
48
59
|
:url => options[:url],
|
49
60
|
:user => options[:user],
|
50
61
|
:password => options[:password]
|
51
|
-
|
62
|
+
}.reject { |k, v| v.nil? }
|
52
63
|
)
|
53
64
|
else
|
54
65
|
raise ArgumentError.new m(:invalid_connection)
|
data/lib/jrubysql/version.rb
CHANGED
data/lib/jrubysql.rb
CHANGED
@@ -12,7 +12,7 @@ require 'jrubysql/rdbms'
|
|
12
12
|
require 'jrubysql/option_parser'
|
13
13
|
require 'jrubysql/input/input'
|
14
14
|
require 'jrubysql/input/console'
|
15
|
-
require 'jrubysql/input/
|
15
|
+
require 'jrubysql/input/script'
|
16
16
|
require 'jrubysql/output/csv'
|
17
17
|
require 'jrubysql/output/term'
|
18
18
|
require 'jrubysql/output/cterm'
|
data/test/helper.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
ENV['BUNDLE_GEMFILE'] = File.join(File.dirname(__FILE__), '..', 'Gemfile')
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
require 'test/unit'
|
6
|
+
require 'mocha'
|
7
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
8
|
+
require 'jrubysql'
|
9
|
+
|
10
|
+
module JRubySQLTestHelper
|
11
|
+
JRubySQL::Constants.const_set :DEFAULT_RC_PATH, '/tmp/.jrubysqlrc'
|
12
|
+
|
13
|
+
def capture &block
|
14
|
+
begin
|
15
|
+
$stdout = StringIO.new
|
16
|
+
$stderr = StringIO.new
|
17
|
+
|
18
|
+
begin
|
19
|
+
ret = block.call
|
20
|
+
rescue SystemExit => x
|
21
|
+
ret = x.status
|
22
|
+
end
|
23
|
+
|
24
|
+
return {
|
25
|
+
:stdout => $stdout.string,
|
26
|
+
:stderr => $stderr.string,
|
27
|
+
:return => ret
|
28
|
+
}
|
29
|
+
ensure
|
30
|
+
$stdout = STDOUT
|
31
|
+
$stderr = STDERR
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/test/test_jrubysql.rb
CHANGED
@@ -1,9 +1,214 @@
|
|
1
|
-
require '
|
2
|
-
require 'bundler'
|
3
|
-
ENV['BUNDLE_GEMFILE'] = File.join(File.dirname(__FILE__), '..', 'Gemfile')
|
4
|
-
Bundler.setup(:default, :development)
|
5
|
-
require 'test/unit'
|
6
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
1
|
+
require 'helper'
|
7
2
|
|
3
|
+
# Acceptance test
|
8
4
|
class TestJRubySQL < Test::Unit::TestCase
|
5
|
+
include JRubySQLTestHelper
|
6
|
+
|
7
|
+
def test_help
|
8
|
+
queue "
|
9
|
+
help
|
10
|
+
exit
|
11
|
+
"
|
12
|
+
ret = capture { launch }
|
13
|
+
assert_equal 0, ret[:return]
|
14
|
+
assert_match /Display this message/, ret[:stdout]
|
15
|
+
assert_match /Goodbye!/, ret[:stdout]
|
16
|
+
|
17
|
+
assert_prev_conn
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_delimiter
|
21
|
+
queue "
|
22
|
+
select 1 from dual where 1 = 0;
|
23
|
+
delimiter //
|
24
|
+
select 1 -- //
|
25
|
+
from /* // */ dual
|
26
|
+
where 1 = 0 //
|
27
|
+
//
|
28
|
+
//
|
29
|
+
//
|
30
|
+
delimiter ;
|
31
|
+
select 1 from dual where 1 = 0;
|
32
|
+
exit
|
33
|
+
"
|
34
|
+
ret = capture { launch }
|
35
|
+
|
36
|
+
assert_equal 3, ret[:stdout].scan(/0 row/).length
|
37
|
+
assert_match /Goodbye!/, ret[:stdout]
|
38
|
+
|
39
|
+
assert_prev_conn
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_autocommit
|
43
|
+
queue "
|
44
|
+
drop table if exists jrubysql;
|
45
|
+
create table if not exists jrubysql (a int) engine=innodb;
|
46
|
+
autocommit
|
47
|
+
|
48
|
+
insert
|
49
|
+
into /* ;;; */
|
50
|
+
jrubysql
|
51
|
+
values (1);
|
52
|
+
|
53
|
+
autocommit off
|
54
|
+
|
55
|
+
insert into -- ;;;
|
56
|
+
jrubysql values (2);
|
57
|
+
|
58
|
+
insert into jrubysql values (3);
|
59
|
+
select count(*) from jrubysql;
|
60
|
+
rollback;
|
61
|
+
select count(*) from jrubysql;
|
62
|
+
autocommit
|
63
|
+
autocommit on
|
64
|
+
autocommit
|
65
|
+
drop table jrubysql;
|
66
|
+
exit
|
67
|
+
"
|
68
|
+
# ANSI codes make it difficult to test
|
69
|
+
ret = capture { launch [ '-o', 'term' ] }
|
70
|
+
|
71
|
+
assert_equal %w[0 0 1 1 1 0 0], ret[:stdout].scan(/([0-9]+) rows? affected/).map(&:first)
|
72
|
+
assert_equal %w[on off on], ret[:stdout].scan(/Current autocommit: (on|off)/).map(&:first)
|
73
|
+
assert_equal %w[off on], ret[:stdout].scan(/Turning autocommit (on|off)/).map(&:first)
|
74
|
+
assert_equal %w[3 1], ret[:stdout].scan(/^\| ([0-9]+)/).map(&:first)
|
75
|
+
|
76
|
+
assert_prev_conn /-o term/
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_now
|
80
|
+
queue "
|
81
|
+
now
|
82
|
+
now
|
83
|
+
now
|
84
|
+
exit
|
85
|
+
"
|
86
|
+
ret = capture { launch }
|
87
|
+
ymd = %r|#{Time.now.strftime('%Y/%m/%d %H')}:[0-9]{2}:[0-9]{2}.[0-9]{3}|
|
88
|
+
assert_equal 3, ret[:stdout].scan(ymd).count
|
89
|
+
|
90
|
+
assert_prev_conn
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_csv
|
94
|
+
queue "
|
95
|
+
drop table if exists jrubysql;
|
96
|
+
create table if not exists jrubysql (a int, b varchar(100), c varchar(100), d varchar(100));
|
97
|
+
insert into jrubysql values (100, 'abc', 'def', 'ghi');
|
98
|
+
insert into jrubysql values (200, 'x,y,z', null, '');
|
99
|
+
select * from jrubysql order by a;
|
100
|
+
drop table jrubysql;
|
101
|
+
drop table jrubysql;
|
102
|
+
exit
|
103
|
+
"
|
104
|
+
ret = capture { launch [ '-ocsv' ] }
|
105
|
+
|
106
|
+
assert_equal [
|
107
|
+
'a,b,c,d',
|
108
|
+
'100,abc,def,ghi',
|
109
|
+
'200,"x,y,z",,""' ], ret[:stdout].lines.map(&:chomp)
|
110
|
+
# CSV prints error messages to STDERR
|
111
|
+
assert_match /Unknown table/, ret[:stderr]
|
112
|
+
|
113
|
+
assert_prev_conn /-ocsv/
|
114
|
+
end
|
115
|
+
|
116
|
+
def test_plural
|
117
|
+
queue "
|
118
|
+
drop table if exists jrubysql;
|
119
|
+
create table if not exists jrubysql (a int);
|
120
|
+
update jrubysql set a = 0;
|
121
|
+
insert into jrubysql values (1);
|
122
|
+
update jrubysql set a = 2;
|
123
|
+
insert into jrubysql values (3);
|
124
|
+
update jrubysql set a = 4;
|
125
|
+
drop table jrubysql;
|
126
|
+
exit;
|
127
|
+
"
|
128
|
+
ret = capture { launch }
|
129
|
+
|
130
|
+
assert_equal [
|
131
|
+
'0 row',
|
132
|
+
'0 row',
|
133
|
+
'0 row',
|
134
|
+
'1 row',
|
135
|
+
'1 row',
|
136
|
+
'1 row',
|
137
|
+
'2 rows',
|
138
|
+
'0 row'
|
139
|
+
], ret[:stdout].scan(/([0-9]+ rows?) affected/).map(&:first)
|
140
|
+
|
141
|
+
assert_prev_conn
|
142
|
+
end
|
143
|
+
|
144
|
+
def test_file
|
145
|
+
require 'tempfile'
|
146
|
+
tf = Tempfile.new('jrubysql')
|
147
|
+
tf << "select 1 from dual where 1 = 0"
|
148
|
+
tf.flush
|
149
|
+
|
150
|
+
ret = capture { launch ['-f', tf.path] }
|
151
|
+
assert_equal 1, ret[:stdout].scan(/0 row/).count
|
152
|
+
|
153
|
+
assert_prev_conn /-f/
|
154
|
+
end
|
155
|
+
|
156
|
+
def test_script
|
157
|
+
ret = capture { launch ['-e', "select 1 from dual where 1 = 0; select 1 from dual where 1 = 0"] }
|
158
|
+
assert_equal 2, ret[:stdout].scan(/0 row/).count
|
159
|
+
|
160
|
+
assert_prev_conn /-e/
|
161
|
+
end
|
162
|
+
|
163
|
+
def test_using_connection_history
|
164
|
+
queue "
|
165
|
+
drop table if exists jrubysql;
|
166
|
+
create table if not exists jrubysql (a int);
|
167
|
+
insert into jrubysql values (999);
|
168
|
+
exit
|
169
|
+
"
|
170
|
+
capture { launch ['-ocsv'] }
|
171
|
+
|
172
|
+
# FIXME: private static method
|
173
|
+
JRubySQL::Controller.expects(:get_console_input).returns('1')
|
174
|
+
queue "
|
175
|
+
select * from jrubysql;
|
176
|
+
drop table jrubysql;
|
177
|
+
exit
|
178
|
+
"
|
179
|
+
ret = capture { JRubySQL.launch [] }
|
180
|
+
assert_equal '999', ret[:stdout].lines.map(&:chomp)[-1]
|
181
|
+
|
182
|
+
assert_prev_conn /csv/
|
183
|
+
end
|
184
|
+
|
185
|
+
def test_interrupt
|
186
|
+
pend do
|
187
|
+
assert false, 'Need to test interrupt signal handling'
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
private
|
192
|
+
def assert_prev_conn command = nil
|
193
|
+
prev_conn = JRubySQL::Config.new['connections'].first
|
194
|
+
# assert_equal :sqlite, prev_conn.last[:type]
|
195
|
+
# assert_match 'test.db', prev_conn.last[:host]
|
196
|
+
assert_equal :mysql, prev_conn.last[:type]
|
197
|
+
assert_match 'localhost', prev_conn.last[:host]
|
198
|
+
|
199
|
+
if command
|
200
|
+
assert_match command, prev_conn.first
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
def queue str
|
205
|
+
lines = str.strip.lines.map { |s| s.strip }
|
206
|
+
Readline.expects(:readline).times(lines.length).returns(*lines)
|
207
|
+
end
|
208
|
+
|
209
|
+
def launch argv = []
|
210
|
+
# db = File.join( File.dirname(__FILE__), 'test.db' )
|
211
|
+
# JRubySQL.launch ['-tsqlite', '-h', db ] + argv
|
212
|
+
JRubySQL.launch %w[-tmysql -hlocalhost -dtest] + argv
|
213
|
+
end
|
9
214
|
end
|
data/test/test_option_parser.rb
CHANGED
@@ -0,0 +1,122 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestOptionParser < Test::Unit::TestCase
|
4
|
+
include JRubySQLTestHelper
|
5
|
+
|
6
|
+
def test_dbms_type
|
7
|
+
# No host
|
8
|
+
assert_error /Invalid connection/, parse(%w[-t mysql])
|
9
|
+
assert_error /Invalid connection/, parse(%w[--type mysql])
|
10
|
+
|
11
|
+
# No type
|
12
|
+
assert_error /Invalid connection/, parse(%w[-h localhost])
|
13
|
+
assert_error /Invalid connection/, parse(%w[--host localhost])
|
14
|
+
|
15
|
+
# Invalid type
|
16
|
+
assert_error /not supported/, parse(%w[-t yoursql -h localhost])
|
17
|
+
|
18
|
+
# Optional options
|
19
|
+
opts = parse(%w[-t MySQL -h localhost])
|
20
|
+
assert_equal :mysql, opts[:type]
|
21
|
+
assert_equal 'localhost', opts[:host]
|
22
|
+
|
23
|
+
opts = parse(%w[-t MySQL -h localhost -uroot])
|
24
|
+
assert_equal :mysql, opts[:type]
|
25
|
+
assert_equal 'localhost', opts[:host]
|
26
|
+
assert_equal 'root', opts[:user]
|
27
|
+
|
28
|
+
opts = parse(%w[-t MySQL -h localhost -uroot -dtest])
|
29
|
+
assert_equal :mysql, opts[:type]
|
30
|
+
assert_equal 'localhost', opts[:host]
|
31
|
+
assert_equal 'root', opts[:user]
|
32
|
+
assert_equal 'test', opts[:database]
|
33
|
+
|
34
|
+
[
|
35
|
+
%w[-t mysql -h localhost -u username -p password -d database -o csv],
|
36
|
+
%w[--type mysql --host localhost --user username --password password --database database --output csv]
|
37
|
+
].each do |argv|
|
38
|
+
opts = parse(argv)
|
39
|
+
assert_equal :mysql, opts[:type]
|
40
|
+
assert_equal 'localhost', opts[:host]
|
41
|
+
assert_equal 'username', opts[:user]
|
42
|
+
assert_equal 'password', opts[:password]
|
43
|
+
assert_equal 'database', opts[:database]
|
44
|
+
assert_equal 'csv', opts[:output]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_class_name
|
49
|
+
# No JDBC URL
|
50
|
+
assert_error /Invalid connection/, parse(%w[-c com.mysql.jdbc.Driver])
|
51
|
+
assert_error /Invalid connection/, parse(%w[--class-name com.mysql.jdbc.Driver])
|
52
|
+
|
53
|
+
# No class name
|
54
|
+
assert_error /Invalid connection/, parse(%w[-j jdbc:mysql://localhost/test])
|
55
|
+
assert_error /Invalid connection/, parse(%w[--jdbc-url jdbc:mysql://localhost/test])
|
56
|
+
|
57
|
+
[
|
58
|
+
%w[-c com.mysql.jdbc.Driver -j jdbc:mysql://localhost -u username -p password -d database -o cterm],
|
59
|
+
%w[--class-name com.mysql.jdbc.Driver --jdbc-url jdbc:mysql://localhost
|
60
|
+
--user username --password password --database database --output cterm]
|
61
|
+
].each do |argv|
|
62
|
+
opts = parse(argv)
|
63
|
+
assert_equal 'com.mysql.jdbc.Driver', opts[:driver]
|
64
|
+
assert_equal 'jdbc:mysql://localhost', opts[:url]
|
65
|
+
assert_equal 'username', opts[:user]
|
66
|
+
assert_equal 'password', opts[:password]
|
67
|
+
assert_equal 'database', opts[:database]
|
68
|
+
assert_equal 'cterm', opts[:output]
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_invalid_output
|
73
|
+
assert_error /Invalid output/, parse(%w[-t mysql -h localhost -o xml])
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_invalid_combination
|
77
|
+
assert_error /Invalid connection/, parse(%w[-t mysql -j jdbc:mysql://localhost])
|
78
|
+
assert_error /Invalid connection/, parse(%w[-c com.mysql.jdbc.Driver -h localhost])
|
79
|
+
|
80
|
+
assert_error /both filename and script/, parse(%w[-f aaa -e bbb])
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_filename
|
84
|
+
assert_error /File not found/, parse(%w[-t mysql -h localhost -f no-such-file.sql])
|
85
|
+
|
86
|
+
opts = parse(%w[-t mysql -h localhost -f] + [__FILE__])
|
87
|
+
assert_equal __FILE__, opts[:filename]
|
88
|
+
|
89
|
+
opts = parse(%w[-t mysql -h localhost --filename] + [__FILE__])
|
90
|
+
assert_equal __FILE__, opts[:filename]
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_script
|
94
|
+
opts = parse(%w[-t mysql -h localhost -e commit])
|
95
|
+
assert_equal 'commit', opts[:script]
|
96
|
+
|
97
|
+
opts = parse(%w[-t mysql -h localhost --execute commit])
|
98
|
+
assert_equal 'commit', opts[:script]
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_password_input
|
102
|
+
# FIXME: ask_password is a private method. any better way?
|
103
|
+
JRubySQL::OptionParser.expects(:ask_password).returns('password')
|
104
|
+
opts = parse(%w[-t mysql -h localhost -p])
|
105
|
+
assert_equal 'password', opts[:password]
|
106
|
+
end
|
107
|
+
|
108
|
+
def parse argv
|
109
|
+
parse_with_output(argv)[:return]
|
110
|
+
end
|
111
|
+
|
112
|
+
def parse_with_output argv
|
113
|
+
capture { JRubySQL::OptionParser.parse(argv) }
|
114
|
+
end
|
115
|
+
|
116
|
+
def assert_error msg, argv
|
117
|
+
ret = parse_with_output argv
|
118
|
+
assert_equal 1, ret[:return]
|
119
|
+
assert msg, ret[:stdout] =~ msg
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
metadata
CHANGED
@@ -1,206 +1,204 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: jrubysql
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
5
|
-
version: 0.1.
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.3
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
8
|
-
- Junegunn Choi
|
9
|
-
autorequire:
|
7
|
+
authors:
|
8
|
+
- Junegunn Choi
|
9
|
+
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
- !ruby/object:Gem::
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
- - ~>
|
137
|
-
- !ruby/object:Gem::Version
|
138
|
-
version: 2.7.0
|
139
|
-
none: false
|
140
|
-
prerelease: false
|
141
|
-
type: :runtime
|
12
|
+
|
13
|
+
date: 2012-03-28 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: test-unit
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
type: :development
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: mocha
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: "0"
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id002
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: guard
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id003
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: guard-test
|
50
|
+
prerelease: false
|
51
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
type: :development
|
58
|
+
version_requirements: *id004
|
59
|
+
- !ruby/object:Gem::Dependency
|
60
|
+
name: jdbc-helper
|
61
|
+
prerelease: false
|
62
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ~>
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: 0.7.2
|
68
|
+
type: :runtime
|
69
|
+
version_requirements: *id005
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: insensitive_hash
|
72
|
+
prerelease: false
|
73
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ~>
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: 0.2.3
|
79
|
+
type: :runtime
|
80
|
+
version_requirements: *id006
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
name: tabularize
|
83
|
+
prerelease: false
|
84
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ~>
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.1.1
|
90
|
+
type: :runtime
|
91
|
+
version_requirements: *id007
|
92
|
+
- !ruby/object:Gem::Dependency
|
93
|
+
name: each_sql
|
94
|
+
prerelease: false
|
95
|
+
requirement: &id008 !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ~>
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: 0.3.1
|
101
|
+
type: :runtime
|
102
|
+
version_requirements: *id008
|
103
|
+
- !ruby/object:Gem::Dependency
|
104
|
+
name: highline
|
105
|
+
prerelease: false
|
106
|
+
requirement: &id009 !ruby/object:Gem::Requirement
|
107
|
+
none: false
|
108
|
+
requirements:
|
109
|
+
- - ~>
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: 1.6.11
|
112
|
+
type: :runtime
|
113
|
+
version_requirements: *id009
|
114
|
+
- !ruby/object:Gem::Dependency
|
115
|
+
name: ansi
|
116
|
+
prerelease: false
|
117
|
+
requirement: &id010 !ruby/object:Gem::Requirement
|
118
|
+
none: false
|
119
|
+
requirements:
|
120
|
+
- - ~>
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: 1.4.2
|
123
|
+
type: :runtime
|
124
|
+
version_requirements: *id010
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: erubis
|
127
|
+
prerelease: false
|
128
|
+
requirement: &id011 !ruby/object:Gem::Requirement
|
129
|
+
none: false
|
130
|
+
requirements:
|
131
|
+
- - ~>
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: 2.7.0
|
134
|
+
type: :runtime
|
135
|
+
version_requirements: *id011
|
142
136
|
description: An SQL client for any JDBC-compliant database. Written in JRuby.
|
143
|
-
email:
|
144
|
-
- junegunn.c@gmail.com
|
145
|
-
executables:
|
146
|
-
- jrubysql
|
137
|
+
email:
|
138
|
+
- junegunn.c@gmail.com
|
139
|
+
executables:
|
140
|
+
- jrubysql
|
147
141
|
extensions: []
|
142
|
+
|
148
143
|
extra_rdoc_files: []
|
149
|
-
|
150
|
-
|
151
|
-
-
|
152
|
-
-
|
153
|
-
-
|
154
|
-
-
|
155
|
-
-
|
156
|
-
-
|
157
|
-
-
|
158
|
-
-
|
159
|
-
-
|
160
|
-
- lib/jrubysql
|
161
|
-
- lib/jrubysql/
|
162
|
-
- lib/jrubysql/
|
163
|
-
- lib/jrubysql/
|
164
|
-
- lib/jrubysql/
|
165
|
-
- lib/jrubysql/
|
166
|
-
- lib/jrubysql/
|
167
|
-
- lib/jrubysql/
|
168
|
-
- lib/jrubysql/
|
169
|
-
- lib/jrubysql/
|
170
|
-
- lib/jrubysql/
|
171
|
-
- lib/jrubysql/
|
172
|
-
- lib/jrubysql/
|
173
|
-
-
|
174
|
-
-
|
175
|
-
-
|
176
|
-
- test/
|
144
|
+
|
145
|
+
files:
|
146
|
+
- .gitignore
|
147
|
+
- CHANGELOG.md
|
148
|
+
- Gemfile
|
149
|
+
- Guardfile
|
150
|
+
- LICENSE.txt
|
151
|
+
- README.md
|
152
|
+
- Rakefile
|
153
|
+
- bin/jrubysql
|
154
|
+
- jrubysql.gemspec
|
155
|
+
- lib/jrubysql.rb
|
156
|
+
- lib/jrubysql/config.rb
|
157
|
+
- lib/jrubysql/constants.rb
|
158
|
+
- lib/jrubysql/controller.rb
|
159
|
+
- lib/jrubysql/doc/help.txt.erb
|
160
|
+
- lib/jrubysql/input/console.rb
|
161
|
+
- lib/jrubysql/input/input.rb
|
162
|
+
- lib/jrubysql/input/script.rb
|
163
|
+
- lib/jrubysql/messages.rb
|
164
|
+
- lib/jrubysql/messages.yml
|
165
|
+
- lib/jrubysql/option_parser.rb
|
166
|
+
- lib/jrubysql/output/csv.rb
|
167
|
+
- lib/jrubysql/output/cterm.rb
|
168
|
+
- lib/jrubysql/output/term.rb
|
169
|
+
- lib/jrubysql/rdbms.rb
|
170
|
+
- lib/jrubysql/version.rb
|
171
|
+
- test/helper.rb
|
172
|
+
- test/test_jrubysql.rb
|
173
|
+
- test/test_option_parser.rb
|
177
174
|
homepage: https://github.com/junegunn/jrubysql
|
178
175
|
licenses: []
|
179
|
-
|
176
|
+
|
177
|
+
post_install_message:
|
180
178
|
rdoc_options: []
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
- - ! '>='
|
186
|
-
- !ruby/object:Gem::Version
|
187
|
-
version: '0'
|
179
|
+
|
180
|
+
require_paths:
|
181
|
+
- lib
|
182
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
188
183
|
none: false
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
184
|
+
requirements:
|
185
|
+
- - ">="
|
186
|
+
- !ruby/object:Gem::Version
|
187
|
+
version: "0"
|
188
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
194
189
|
none: false
|
190
|
+
requirements:
|
191
|
+
- - ">="
|
192
|
+
- !ruby/object:Gem::Version
|
193
|
+
version: "0"
|
195
194
|
requirements: []
|
195
|
+
|
196
196
|
rubyforge_project: jrubysql
|
197
|
-
rubygems_version: 1.8.
|
198
|
-
signing_key:
|
197
|
+
rubygems_version: 1.8.21
|
198
|
+
signing_key:
|
199
199
|
specification_version: 3
|
200
200
|
summary: An SQL client for any JDBC-compliant database.
|
201
|
-
test_files:
|
202
|
-
- test/
|
203
|
-
- test/test_jrubysql.rb
|
204
|
-
- test/test_option_parser.rb
|
205
|
-
- test/test_term_output.rb
|
206
|
-
...
|
201
|
+
test_files:
|
202
|
+
- test/helper.rb
|
203
|
+
- test/test_jrubysql.rb
|
204
|
+
- test/test_option_parser.rb
|
data/test/test_cterm.rb
DELETED
File without changes
|
data/test/test_term_output.rb
DELETED
File without changes
|