dbsh 0.0.1

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.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .rbenv-version
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dbsh.gemspec
4
+ gemspec
5
+ gem 'rake'
6
+ gem 'sqlite3'
7
+ gem 'ruby-debug'
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Dan Hensgen
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,40 @@
1
+ # Dbsh
2
+
3
+ A database console like psql. This is a proof of concept; use at your own risk.
4
+
5
+ ## Installation
6
+
7
+ $ gem install dbsh
8
+
9
+ ## Usage
10
+
11
+ Define a connection in `~/.dbsh`:
12
+
13
+ mydb:
14
+ adapter: mysql
15
+ host: localhost
16
+ username: root
17
+ password: password
18
+ database: mydb
19
+
20
+ Then run `dbsh` with your connection:
21
+
22
+ $ dbsh mydb
23
+
24
+ And ask dbsh for help:
25
+
26
+ > \h
27
+
28
+ Dbsh uses [Sequel](http://sequel.rubyforge.org/) for database access, so you
29
+ should be able to connect to anything Sequel can. You'll probably need to
30
+ install [the right database
31
+ gem](http://sequel.rubyforge.org/rdoc/files/doc/opening_databases_rdoc.html) to
32
+ get connected.
33
+
34
+ ## Contributing
35
+
36
+ 1. Fork it
37
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
38
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
39
+ 4. Push to the branch (`git push origin my-new-feature`)
40
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,6 @@
1
+ class Dbsh < Thor
2
+ desc 'db_reset', 'Delete and recreate the database'
3
+ def db_reset
4
+ system 'rm test.sqlite3 && sqlite3 test.sqlite3 < schema.sql'
5
+ end
6
+ end
@@ -0,0 +1,210 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require 'readline'
4
+ require 'sequel'
5
+ require 'terminal-table'
6
+ require 'yaml'
7
+
8
+ #DB = Sequel.oracle 'cink', :user => 'dhensgen', :password => '...'
9
+ #puts DB.fetch('select count(1) from prod_sched').first
10
+
11
+ config = YAML.load(File.read("#{ENV['HOME']}/.dbsh"))[ARGV[0]]
12
+ DB = Sequel.connect(config)
13
+
14
+ File.open("#{ENV['HOME']}/.dbsh_history", 'r') do |f|
15
+ f.each_line do |line|
16
+ Readline::HISTORY << line.strip
17
+ end
18
+ end
19
+
20
+ #Example ~/.dbsh:
21
+ #dbsh_test:
22
+ # adapter: sqlite
23
+ # database: test.sqlite3
24
+ #
25
+ ## :adapter=>'postgres', :host=>'localhost', :database=>'blog', :user=>'user', :password=>'password'
26
+
27
+ HELP = <<END
28
+ \\h Show this help
29
+ \\d Show tables
30
+ \\d table_name Show table columns
31
+ \\. file_name Execute a sql file
32
+ \\e file_name Edit file and then execute it
33
+ \\q Quit
34
+ END
35
+
36
+ def statement_type(statement)
37
+ case statement
38
+ when /\s*select/i then :select
39
+ when /\s*insert/i then :insert
40
+ when /\s*update/i then :update
41
+ when /\s*delete/i then :delete
42
+ when /\s*\\/i then :command
43
+ else nil
44
+ end
45
+ end
46
+
47
+ def process_statement(statement)
48
+ case statement_type(statement)
49
+ when :select
50
+ dataset = DB[statement]
51
+ table = Terminal::Table.new(:headings => dataset.columns) do |t|
52
+ dataset.each do |row|
53
+ t.add_row(dataset.columns.map {|col| row[col]})
54
+ end
55
+ end
56
+ puts table
57
+ when :insert
58
+ DB[statement].insert
59
+ puts 'Inserted row'
60
+ when :update
61
+ puts "Updated #{DB[statement].update} rows"
62
+ when :delete
63
+ puts "Deleted #{DB[statement].delete} rows"
64
+ else
65
+ puts 'Unknown statement type'
66
+ end
67
+ end
68
+
69
+ def process_command(command)
70
+ case command
71
+ when /\\q/
72
+ exit(0)
73
+ when /\\\./
74
+ process_statement(File.read(command.split(/\s+/)[1]))
75
+ when /\\d(\s+\w+)?/
76
+ if $1
77
+ table = Terminal::Table.new(:headings => ['Name', 'Type']) do |t|
78
+ DB.schema($1.strip).each do |col|
79
+ t.add_row [col.first.to_s, col.last[:db_type]]
80
+ end
81
+ end
82
+ puts table
83
+ else
84
+ puts DB.tables.join("\n")
85
+ end
86
+ when /\\e\s+(.+)?/
87
+ if $1
88
+ system(ENV['EDITOR'], $1)
89
+ process_statement(File.read $1)
90
+ else
91
+ puts DB.tables.join("\n")
92
+ end
93
+ when /\\h/
94
+ puts HELP
95
+ else
96
+ puts 'Unknown command'
97
+ end
98
+ end
99
+
100
+ File.open("#{ENV['HOME']}/.dbsh_history", 'a') do |history|
101
+ while line = Readline.readline('> ', true)
102
+ history.write(line + "\n"); history.flush
103
+ begin
104
+ statement_type = statement_type(line)
105
+ if statement_type == :command
106
+ process_command(line)
107
+ else
108
+ process_statement(line)
109
+ end
110
+ rescue Exception => e
111
+ exit(0) if SystemExit === e
112
+ puts 'Error: ' + e.message
113
+ end
114
+ end
115
+ end
116
+
117
+ __END__
118
+
119
+ OVERVIEW
120
+
121
+ A clone of mysql cli with these enhancements:
122
+
123
+ MVP
124
+
125
+ X Connect to databases by name:
126
+ X
127
+ X $ dbsh rockwell_dev
128
+ X ~/.dbsh:
129
+ X rockwell_dev:
130
+ X adapter: mysql
131
+ X username: root
132
+ X password:
133
+ X database: rockwell_dev
134
+
135
+ X Connect to different types of databases:
136
+ X
137
+ X * mysql
138
+ X * oracle
139
+ X * sqlite
140
+ X * postgres
141
+
142
+ X Execute sql:
143
+ X
144
+ X > select * from illustrations;
145
+ X id name updated_at
146
+ X -- ------- ----------
147
+ X 10 Filmore 2012-06-12
148
+ X
149
+ X > delete from illustrations;
150
+ X Deleted 1 row.
151
+
152
+ X Quit:
153
+ X
154
+ X > \q
155
+ X $
156
+
157
+ X Report errors:
158
+ X
159
+ X > foo;
160
+ X ERROR: What is foo?
161
+
162
+ X Install:
163
+ X
164
+ X $ gem install dbsh
165
+
166
+ X Readline support:
167
+ X
168
+ X * History
169
+ X * Edit line
170
+
171
+ X Execute a sql script:
172
+ X
173
+ X > \. illustrations.sql
174
+ X ...
175
+
176
+ X Explore the database:
177
+ X
178
+ X > show tables;
179
+ X name
180
+ X -------------
181
+ X illustrations
182
+ X builds
183
+ X
184
+ X > desc illustrations;
185
+ X name type
186
+ X ---------- ------------
187
+ X id integer
188
+ X name varchar(256)
189
+ X updated_at datetime
190
+
191
+ X Edit and run a sql script:
192
+ X
193
+ X > \e illustrations.sql
194
+ X (open script in $EDITOR, execute script if changes were made)
195
+
196
+ Semicolons
197
+
198
+ > select 1; select 2
199
+
200
+ Execute a sql script from the command line:
201
+
202
+ $ dbsh rockwell_dev < illustrations.sql
203
+ ...
204
+
205
+ Beyond MVP:
206
+
207
+ * Export csv
208
+ * Color output
209
+ * Parameterized sql scripts
210
+ * Column filtering
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/dbsh/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Dan Hensgen"]
6
+ gem.email = ["dan@methodhead.com"]
7
+ gem.description = %q{A database cli in Ruby, like the psql client for Postgres.}
8
+ gem.summary = %q{A database cli in Ruby}
9
+ gem.homepage = "http://github.com/dmeiz/dbsh"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "dbsh"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Dbsh::VERSION
17
+
18
+ gem.add_dependency('sequel', '>= 3.36.0')
19
+ gem.add_dependency('terminal-table', '>= 1.4.0')
20
+ end
@@ -0,0 +1,5 @@
1
+ require "dbsh/version"
2
+
3
+ module Dbsh
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,3 @@
1
+ module Dbsh
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ create table dogs (
2
+ id integer,
3
+ name varchar(255),
4
+ breed varchar(255),
5
+ weight double,
6
+ created_at datetime,
7
+ updated_at datetime
8
+ );
9
+
10
+ insert into dogs values (1, 'Rover', 'Lab', 25.0, '2012-06-06 00:00:00', '2012-06-06 00:00:00');
11
+ insert into dogs values (2, 'Rex', 'Retreiver', 35.0, '2012-06-07 00:00:00', '2012-06-07 00:00:00');
12
+
13
+ create table cats (
14
+ id integer,
15
+ name varchar(255),
16
+ breed varchar(255),
17
+ weight double,
18
+ created_at datetime,
19
+ updated_at datetime
20
+ );
21
+
22
+ insert into cats values (1, 'Garfield', 'Tabby', 25.0, '2012-06-06 00:00:00', '2012-06-06 00:00:00');
23
+ insert into cats values (2, 'Tiger', 'Persian', 35.0, '2012-06-07 00:00:00', '2012-06-07 00:00:00');
@@ -0,0 +1,4 @@
1
+ select
2
+ *
3
+ from
4
+ dogs
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'terminal-table'
3
+
4
+ table = Terminal::Table.new(:headings => ['foo', 'bar']) do |t|
5
+ t.add_row ['wam', 'bam']
6
+ end
7
+
8
+ puts table
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dbsh
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Dan Hensgen
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-06-28 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: sequel
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 151
29
+ segments:
30
+ - 3
31
+ - 36
32
+ - 0
33
+ version: 3.36.0
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: terminal-table
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 7
45
+ segments:
46
+ - 1
47
+ - 4
48
+ - 0
49
+ version: 1.4.0
50
+ type: :runtime
51
+ version_requirements: *id002
52
+ description: A database cli in Ruby, like the psql client for Postgres.
53
+ email:
54
+ - dan@methodhead.com
55
+ executables:
56
+ - dbsh
57
+ extensions: []
58
+
59
+ extra_rdoc_files: []
60
+
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE
65
+ - README.md
66
+ - Rakefile
67
+ - Thorfile
68
+ - bin/dbsh
69
+ - dbsh.gemspec
70
+ - lib/dbsh.rb
71
+ - lib/dbsh/version.rb
72
+ - schema.sql
73
+ - show_dogs.sql
74
+ - terminal_table.rb
75
+ homepage: http://github.com/dmeiz/dbsh
76
+ licenses: []
77
+
78
+ post_install_message:
79
+ rdoc_options: []
80
+
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ hash: 3
89
+ segments:
90
+ - 0
91
+ version: "0"
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ hash: 3
98
+ segments:
99
+ - 0
100
+ version: "0"
101
+ requirements: []
102
+
103
+ rubyforge_project:
104
+ rubygems_version: 1.8.15
105
+ signing_key:
106
+ specification_version: 3
107
+ summary: A database cli in Ruby
108
+ test_files: []
109
+
110
+ has_rdoc: