Class: LiveSQL
Instance Attribute Summary collapse
-
#cursor_pos ⇒ Object
Returns the value of attribute cursor_pos.
-
#db ⇒ Object
readonly
Returns the value of attribute db.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#interface ⇒ Object
readonly
Returns the value of attribute interface.
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#result ⇒ Object
Returns the value of attribute result.
-
#state ⇒ Object
Returns the value of attribute state.
-
#string ⇒ Object
Returns the value of attribute string.
Instance Method Summary collapse
- #attempt_to_query_db ⇒ Object
- #filter_aggregate_functions ⇒ Object
-
#handle_input(input) ⇒ Object
private.
-
#initialize(db, opts = {}) ⇒ LiveSQL
constructor
A new instance of LiveSQL.
- #print_display ⇒ Object
- #print_invalid_query ⇒ Object
- #print_table ⇒ Object
- #print_valid_query ⇒ Object
- #prompt ⇒ Object
- #query_db(string) ⇒ Object
- #render ⇒ Object
- #run ⇒ Object
- #terminated? ⇒ Boolean
Methods included from Util
Methods included from Cursorable
#backspace, #delete, #error, #left, #quit, #right
Constructor Details
#initialize(db, opts = {}) ⇒ LiveSQL
Returns a new instance of LiveSQL.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/live_sql.rb', line 17 def initialize(db, opts = {}) if opts[:db] == :sqlite3 @db = Sqlite3DatabaseConnection.new(db) else @db = PostgresDatabaseConnection.new(db) end @interface = Interface.new @limit = 30 @string = " " @result = [] @state = :invalid @cursor_pos = 0 @errors = [] end |
Instance Attribute Details
#cursor_pos ⇒ Object
Returns the value of attribute cursor_pos.
14 15 16 |
# File 'lib/live_sql.rb', line 14 def cursor_pos @cursor_pos end |
#db ⇒ Object (readonly)
Returns the value of attribute db.
15 16 17 |
# File 'lib/live_sql.rb', line 15 def db @db end |
#errors ⇒ Object
Returns the value of attribute errors.
14 15 16 |
# File 'lib/live_sql.rb', line 14 def errors @errors end |
#interface ⇒ Object (readonly)
Returns the value of attribute interface.
15 16 17 |
# File 'lib/live_sql.rb', line 15 def interface @interface end |
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
15 16 17 |
# File 'lib/live_sql.rb', line 15 def limit @limit end |
#result ⇒ Object
Returns the value of attribute result.
14 15 16 |
# File 'lib/live_sql.rb', line 14 def result @result end |
#state ⇒ Object
Returns the value of attribute state.
14 15 16 |
# File 'lib/live_sql.rb', line 14 def state @state end |
#string ⇒ Object
Returns the value of attribute string.
14 15 16 |
# File 'lib/live_sql.rb', line 14 def string @string end |
Instance Method Details
#attempt_to_query_db ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/live_sql.rb', line 55 def attempt_to_query_db old_table = result filter_aggregate_functions self.result = query_db(string) self.state = :valid rescue StandardError => e errors << e. self.result = old_table self.state = :invalid end |
#filter_aggregate_functions ⇒ Object
67 68 69 70 71 |
# File 'lib/live_sql.rb', line 67 def filter_aggregate_functions if aggregate_func_names.any? { |func| string.upcase.include?(func) } && !terminated? raise "Aggregate queries must be terminated with a semi-colon" end end |
#handle_input(input) ⇒ Object
private
45 46 47 48 49 50 51 52 53 |
# File 'lib/live_sql.rb', line 45 def handle_input(input) if input.is_a?(Symbol) send(input) else string.insert(cursor_pos, input) self.cursor_pos += 1 attempt_to_query_db end end |
#print_display ⇒ Object
86 87 88 89 90 |
# File 'lib/live_sql.rb', line 86 def print_display system("clear") prompt render end |
#print_invalid_query ⇒ Object
105 106 107 108 109 110 |
# File 'lib/live_sql.rb', line 105 def print_invalid_query print @string[0...@cursor_pos].colorize(:red) print @string[@cursor_pos].colorize(background: :cyan, color: :red) print @string[@cursor_pos + 1..-1].colorize(:red) puts end |
#print_table ⇒ Object
119 120 121 |
# File 'lib/live_sql.rb', line 119 def print_table tp result end |
#print_valid_query ⇒ Object
112 113 114 115 116 117 |
# File 'lib/live_sql.rb', line 112 def print_valid_query print @string[0...@cursor_pos].colorize(:green) print @string[@cursor_pos].colorize(background: :cyan) print @string[@cursor_pos + 1..-1].colorize(:green) puts end |
#prompt ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/live_sql.rb', line 92 def prompt puts "Enter a query!".underline.bold puts "Green is for a valid query, red for syntax error." puts "Queries using AVG() or COUNT() must be terminated with a semi-colon." puts "The table always shows the last valid query." puts "Press esc to quit." end |
#query_db(string) ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/live_sql.rb', line 77 def query_db(string) return if string =~ /drop/i db.execute(<<-SQL) #{string} LIMIT #{limit} SQL end |
#render ⇒ Object
100 101 102 103 |
# File 'lib/live_sql.rb', line 100 def render state == :invalid ? print_invalid_query : print_valid_query print_table end |
#run ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/live_sql.rb', line 35 def run loop do print_display input = interface.get_keyboard_input handle_input(input) end end |
#terminated? ⇒ Boolean
73 74 75 |
# File 'lib/live_sql.rb', line 73 def terminated? !!(string =~ /\;\s*$/) end |