itds 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,13 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 1.9.3-p547
5
+ - 2.0.0
6
+
7
+ before_install:
8
+ - sudo apt-get update -qq
9
+ - sudo apt-get install -qq freetds-dev
10
+ - bundle install
11
+
12
+ script:
13
+ - bundle exec rake spec
data/README.md CHANGED
@@ -1,6 +1,24 @@
1
- # Itds
1
+ # Itds [![CI] (https://travis-ci.org/andl/itds.svg?branch=master)] (https://travis-ci.org/andl/itds)
2
2
 
3
- A very simple SQL server CLI tools just to ease the developer working on Mac or Linux.
3
+ A deadly simple SQL server CLI tools just to ease the developer working on Mac or Linux.
4
+
5
+ # Dependency
6
+
7
+ Install freetds using the package manager of your system.
8
+ ## Mac
9
+ ```
10
+ $homebrew install freetds
11
+ ```
12
+
13
+ ## Ubuntu
14
+ ```
15
+ $sudo apt-get install freetds-dev
16
+ ```
17
+
18
+ # Install
19
+ ```
20
+ $gem install itds
21
+ ```
4
22
 
5
23
  # Usage
6
24
 
@@ -11,7 +29,7 @@ $itds --help
11
29
 
12
30
  Command parameters:
13
31
  ```
14
- $itds -h <hostname> -P <password> -u <username> -d <contained_database> [SQL]
32
+ $itds -h <hostname> -p <password> -u <username> -d <contained_database> [SQL]
15
33
  ```
16
34
 
17
35
  Execute a command
@@ -24,15 +42,17 @@ $itds -h <hostname> -d <database> select 1
24
42
  +---+
25
43
  ```
26
44
 
27
- Interactive mode
45
+ Cancel a request in interactive mode.
28
46
  ```
29
47
  $itds -h <hostname> -d <mydb>
30
- mydb> select(1)
31
- +---+
32
-
33
- +---+
34
- | 1 |
35
- +---+
36
- mydb> exit
48
+ mydb> waitfor delay '00:00:04'
49
+ ^C
50
+ mydb> select * from test
51
+ +----+
52
+ | id |
53
+ +----+
54
+ | 10 |
55
+ +----+
56
+ mydb> ^C
37
57
  $
38
58
  ```
data/Rakefile CHANGED
@@ -1 +1,5 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task :default => :spec
@@ -22,4 +22,5 @@ Gem::Specification.new do |spec|
22
22
 
23
23
  spec.add_development_dependency "bundler", "~> 1.5"
24
24
  spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
25
26
  end
@@ -8,15 +8,21 @@ module Itds
8
8
  def_delegators :@backend, :close
9
9
 
10
10
  DEFAULT_OPTS = {
11
- port: 1433,
11
+ port: 1433,
12
12
  username: 'sa',
13
+ timeout: 5,
13
14
  }
14
15
 
15
16
  def initialize(opts={})
16
17
  opts = DEFAULT_OPTS.merge(opts)
17
18
  opts[:azure] = true if(opts[:database])
19
+ opts[:login_timeout] = opts[:timeout]
18
20
 
19
- @backend = TinyTds::Client.new(opts)
21
+ @backend = new_backend(opts)
22
+ end
23
+
24
+ def new_backend(opts)
25
+ TinyTds::Client.new(opts)
20
26
  end
21
27
 
22
28
  def execute(sql)
@@ -8,15 +8,24 @@ module Itds
8
8
  def config(options)
9
9
  @options = options
10
10
  @client = Client.new(options)
11
- self
12
11
  end
13
12
 
14
13
  def run
14
+ print_help
15
+
15
16
  while cmd = Readline.readline(prompt, true)
16
17
  execute_cmd(cmd)
17
18
  end
18
19
  end
19
20
 
21
+ def print_help
22
+ msg = <<-EOT
23
+ itds SQL server interactive console.
24
+ Press Ctrl-C to cancel current query, press again to quit.
25
+ EOT
26
+ puts msg
27
+ end
28
+
20
29
  def prompt
21
30
  @prompt ||= "#{@options[:database]}> "
22
31
  end
@@ -24,10 +33,19 @@ module Itds
24
33
  def execute_cmd(cmd)
25
34
  begin
26
35
  stop if cmd == "exit"
27
- res = @client.execute(cmd)
28
- print_result(res)
36
+ @res = @client.execute(cmd)
37
+ print_result(@res)
29
38
  rescue => e
30
39
  print_err(e)
40
+ ensure
41
+ cancel_request
42
+ end
43
+ end
44
+
45
+ def cancel_request
46
+ if @res
47
+ @res.cancel
48
+ @res = nil
31
49
  end
32
50
  end
33
51
 
@@ -38,15 +56,42 @@ module Itds
38
56
  rows << rowset.values
39
57
  end
40
58
 
41
- t = Terminal::Table.new :headings => fields, :rows => rows
42
- puts t
59
+ output = {}
60
+ output[:headings] = fields unless fields.empty?
61
+ output[:rows] = rows unless rows.empty?
62
+
63
+ unless output.empty?
64
+ puts Terminal::Table.new output
65
+ end
66
+
67
+ print_affected_row(res)
68
+ end
69
+
70
+ def print_affected_row(res)
71
+ # tiny_tds seems not only return affected_rows for update/delete
72
+ # but also select.
73
+ affected = res.affected_rows
74
+
75
+ if affected > 0
76
+ puts ""
77
+ puts "Affected Rows: #{affected}"
78
+ end
43
79
  end
44
80
 
45
81
  def print_err(err)
46
82
  puts "Error: #{err.message}"
47
83
  end
48
84
 
85
+ def singal
86
+ if @res
87
+ cancel_request
88
+ else
89
+ stop
90
+ end
91
+ end
92
+
49
93
  def stop
94
+ @client.close
50
95
  exit
51
96
  end
52
97
  end
@@ -65,7 +110,7 @@ module Itds
65
110
  options[:host] = h
66
111
  end
67
112
 
68
- opts.on("-p", "--port [PORT]", Integer, "port number, default is 1433") do |p|
113
+ opts.on("-P", "--port [PORT]", Integer, "port number, default is 1433") do |p|
69
114
  options[:port] = p
70
115
  end
71
116
 
@@ -77,10 +122,14 @@ module Itds
77
122
  options[:database] = d
78
123
  end
79
124
 
80
- opts.on("-P", "--password [PASSWORD]", "optional password") do |p|
125
+ opts.on("-p", "--password [PASSWORD]", "optional password") do |p|
81
126
  options[:password] = p
82
127
  end
83
128
 
129
+ opts.on("--timeout [TIMEOUT]", "connect and query timeout, 5 secs by default") do |t|
130
+ options[:timeout] = t
131
+ end
132
+
84
133
  opts.on_tail("--help", "show this help message") do
85
134
  puts opts
86
135
  exit
@@ -105,8 +154,8 @@ module Itds
105
154
  end
106
155
 
107
156
  Repl.config(options)
108
- if args.empty?
109
- trap("INT") { exit }
157
+ if args.empty? #interactive
158
+ trap("INT") { Repl.singal }
110
159
  Repl.run
111
160
  else
112
161
  Repl.execute_cmd(args.join(" "))
@@ -1,3 +1,3 @@
1
1
  module Itds
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ module Itds
4
+ describe Client do
5
+
6
+ it "able to set login timeout" do
7
+ timeout = 2
8
+ t = Thread.new do
9
+ Client.new(
10
+ {
11
+ host: localhost,
12
+ timeout: timeout,
13
+ }
14
+ )
15
+ end
16
+
17
+ sleep(timeout + 1)
18
+ t.alive?.should be_false
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,2 @@
1
+ require 'rspec'
2
+ require_relative '../lib/itds'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: itds
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-05-30 00:00:00.000000000 Z
12
+ date: 2014-05-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: tiny_tds
@@ -75,6 +75,22 @@ dependencies:
75
75
  - - ! '>='
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rspec
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
78
94
  description:
79
95
  email:
80
96
  - liusandrew@gmail.com
@@ -84,6 +100,7 @@ extensions: []
84
100
  extra_rdoc_files: []
85
101
  files:
86
102
  - .gitignore
103
+ - .travis.yml
87
104
  - Gemfile
88
105
  - LICENSE.txt
89
106
  - README.md
@@ -94,6 +111,8 @@ files:
94
111
  - lib/itds/client.rb
95
112
  - lib/itds/repl.rb
96
113
  - lib/itds/version.rb
114
+ - spec/itds/client_spec.rb
115
+ - spec/spec_helper.rb
97
116
  homepage: http://github.com/andl/itds
98
117
  licenses:
99
118
  - MIT
@@ -119,5 +138,7 @@ rubygems_version: 1.8.23
119
138
  signing_key:
120
139
  specification_version: 3
121
140
  summary: A very simple SQL server client.
122
- test_files: []
141
+ test_files:
142
+ - spec/itds/client_spec.rb
143
+ - spec/spec_helper.rb
123
144
  has_rdoc: