live_sql 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 54b7fcbd0ff4dcad2f1cceaa5364da65597caa06
4
+ data.tar.gz: f96754e32f6f59e747322dc02507aa01b29c0145
5
+ SHA512:
6
+ metadata.gz: df1290c2e5878ca6202b1d23d388c08d193360b857c80795fa98caf0bca7bc2d7b84c372a5de5c92a5e88b7dfb092680e896b351b04e70313e030b44541ca547
7
+ data.tar.gz: 06e8008ed6dbc71ccfd2743a457968d8222108bcdfe853a18631c6b18e42e216ff9de5a96f36dc1ee44d052fd1d337cea6f01905a0cf3d4b24ae26e48b84ee66
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.2
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'sqlite3'
6
+ gem 'table_print'
7
+ gem 'colorize'
8
+ gem 'pry'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Fred Sladkey
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # LiveSql
2
+
3
+ Hello friends! live_sql is a little program I wrote to watch how your sqlite queries change your results as you type. This is a baby gem. It's has a few known bugs, probably way more unknown bugs, not to mention it could use some sexy new features. If you want to contribute either by pointing out bugs, fixing problems. or adding new features, your help would be greatly appreciated!
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'live_sql'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install live_sql
20
+ ## Usage
21
+
22
+ To use the test database, just download this repo and run bin/console. You can check out how it works with a simple actor, casting, and movie table.
23
+
24
+ To use on any sqlite3 db, navigate to the folder containing the db, run pry (or irb) and
25
+
26
+ require 'live_sql'
27
+ LiveSQL.run_with('your-db-name-here.db')
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/fsladkey/live_sql
32
+
33
+ ## Running list of bugs and potential features:
34
+
35
+ - Oh god, I already forgot the most recent bug. It'll come back to me....
36
+
37
+ - The most obvious extension is to get this thing hooked up to postgres as well as sqlite3.
38
+
39
+
40
+ ## License
41
+
42
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "live_sql"
5
+
6
+ db = ARGV[0]
7
+ db ||= './lib/live_sql/default_db/movie.db'
8
+
9
+ live_sql = LiveSQL.new(db)
10
+ live_sql.run
11
+
12
+
13
+ # You can add fixtures and/or initialization code here to make experimenting
14
+ # with your gem easier. You can also use a different console, if you like.
15
+
16
+ require "pry"
17
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
Binary file
Binary file
Binary file
@@ -0,0 +1,68 @@
1
+ require 'io/console'
2
+
3
+ # Reads keypresses from the user including 2 and 3 escape character sequences.
4
+
5
+ class Interface
6
+ attr_accessor :string
7
+
8
+ def read_char
9
+ STDIN.echo = false
10
+ STDIN.raw!
11
+
12
+ input = STDIN.getc.chr
13
+ if input == "\e" then
14
+ input << STDIN.read_nonblock(3) rescue nil
15
+ input << STDIN.read_nonblock(2) rescue nil
16
+ end
17
+ ensure
18
+ STDIN.echo = true
19
+ STDIN.cooked!
20
+
21
+ return input
22
+ end
23
+
24
+ # oringal case statement from:
25
+ # http://www.alecjacobson.com/weblog/?p=75
26
+ def get_keyboard_input
27
+ c = read_char
28
+
29
+ case c
30
+ # when " "
31
+ # "SPACE"
32
+ # when "\t"
33
+ # :query
34
+ when "\r"
35
+ :error
36
+ # when "\n"
37
+ # "LINE FEED"
38
+ when "\e"
39
+ :abort
40
+ # when "\e[A"
41
+ # "UP ARROW"
42
+ # :error
43
+ # when "\e[B"
44
+ # "DOWN ARROW"
45
+ # :error
46
+ when "\e[C"
47
+ "RIGHT ARROW"
48
+ :right
49
+ when "\e[D"
50
+ "LEFT ARROW"
51
+ :left
52
+ when "\177"
53
+ :backspace
54
+ # when "\004"
55
+ # "DELETE"
56
+ # when "\e[3~"
57
+ # "ALTERNATE DELETE"
58
+ # when "\u0003"
59
+ # "CONTROL-C"
60
+ # exit 0
61
+ when /^.$/
62
+ c
63
+ else
64
+ :error
65
+ end
66
+ end
67
+
68
+ end
@@ -0,0 +1,3 @@
1
+ module LiveSql
2
+ VERSION = "0.1.0"
3
+ end
data/lib/live_sql.rb ADDED
@@ -0,0 +1,115 @@
1
+ require "live_sql/version"
2
+ require "live_sql/interface"
3
+ require 'singleton'
4
+ require 'sqlite3'
5
+ require 'table_print'
6
+ require 'colorize'
7
+
8
+
9
+ class QuestionsDatabase < SQLite3::Database
10
+
11
+ def initialize(db)
12
+ super(db)
13
+
14
+ self.results_as_hash = true
15
+ self.type_translation = true
16
+ end
17
+ end
18
+
19
+ class LiveSQL
20
+ attr_accessor :result
21
+
22
+ def self.run_default
23
+ live_sql = LiveSQL.new('./live_sql/default_db/movie.db')
24
+ live_sql.run
25
+ end
26
+
27
+ def self.run_with(db_name)
28
+ live_sql = LiveSQL.new(db_name)
29
+ live_sql.run
30
+ end
31
+
32
+ def initialize(db)
33
+ @interface = Interface.new
34
+ @db = QuestionsDatabase.new(db)
35
+
36
+ @string = " "
37
+ @result = []
38
+
39
+ @move = :invalid
40
+ @cursor_pos = 0
41
+ @errors = []
42
+ end
43
+
44
+ def run
45
+ while true
46
+ print_display
47
+ input = @interface.get_keyboard_input
48
+ handle_input(input)
49
+ end
50
+ end
51
+
52
+ def handle_input(input)
53
+ if input == :abort
54
+ system("clear")
55
+ abort
56
+ elsif input == :error
57
+ elsif input == :right
58
+ @cursor_pos += 1 unless @cursor_pos == @string.length - 1
59
+ elsif input == :left
60
+ @cursor_pos -= 1 unless @cursor_pos == 0
61
+ elsif input == :backspace
62
+ if @string.length > 1
63
+ @string.slice!(@cursor_pos - 1)
64
+ @cursor_pos -= 1 unless @cursor_pos == 0
65
+ end
66
+ attempt_to_query_db
67
+ else
68
+ @string.insert(@cursor_pos, input)
69
+ @cursor_pos += 1
70
+ attempt_to_query_db
71
+ end
72
+ end
73
+
74
+ def attempt_to_query_db
75
+ old_table = @result
76
+ @result = query_db(@string)
77
+ @mode = :valid
78
+ rescue StandardError => e
79
+ @errors << e.message
80
+ @result = old_table
81
+ @mode = :invalid
82
+ end
83
+
84
+ def print_display
85
+ system("clear")
86
+ puts "Enter a query!".underline.bold
87
+ puts "Green is for a valid query, red for syntax error."
88
+ puts "The table always shows the last valid query."
89
+ puts "Press esc to quit."
90
+ if @mode == :invalid
91
+ print @string[0...@cursor_pos].colorize(:red)
92
+ print @string[@cursor_pos].colorize(background: :cyan, color: :red)
93
+ print @string[@cursor_pos + 1..-1].colorize(:red)
94
+ puts
95
+ else
96
+ print @string[0...@cursor_pos].colorize(:green)
97
+ print @string[@cursor_pos].colorize(background: :cyan)
98
+ print @string[@cursor_pos + 1..-1].colorize(:green)
99
+ puts
100
+ end
101
+ print_table
102
+ end
103
+
104
+ def print_table
105
+ tp @result
106
+ end
107
+
108
+ def query_db(string)
109
+ @db.execute(<<-SQL)
110
+ #{string}
111
+ LIMIT 30
112
+ SQL
113
+ end
114
+
115
+ end
data/live_sql.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'live_sql/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "live_sql"
8
+ spec.version = LiveSql::VERSION
9
+ spec.authors = ["Fred Sladkey"]
10
+ spec.email = ["fsladkey@gmail.com"]
11
+
12
+ spec.summary = %q{Interact with sqlite3 db and watch your query as you type.}
13
+ spec.homepage = "https://github.com/fsladkey/Live-SQL"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
17
+ # delete this section to allow pushing this gem to any host.
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.10"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+
27
+ spec.add_dependency 'sqlite3'
28
+ spec.add_dependency 'colorize'
29
+ spec.add_dependency 'table_print'
30
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: live_sql
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Fred Sladkey
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: colorize
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: table_print
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - fsladkey@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".travis.yml"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - bin/console
97
+ - bin/setup
98
+ - lib/live_sql.rb
99
+ - lib/live_sql/default_db/.DS_Store
100
+ - lib/live_sql/default_db/cats.db
101
+ - lib/live_sql/default_db/movie.db
102
+ - lib/live_sql/interface.rb
103
+ - lib/live_sql/version.rb
104
+ - live_sql.gemspec
105
+ homepage: https://github.com/fsladkey/Live-SQL
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.2.2
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: Interact with sqlite3 db and watch your query as you type.
129
+ test_files: []