Class: LiveSQL

Inherits:
Object
  • Object
show all
Includes:
Cursorable, Util
Defined in:
lib/live_sql.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#aggregate_func_names

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_posObject

Returns the value of attribute cursor_pos.



14
15
16
# File 'lib/live_sql.rb', line 14

def cursor_pos
  @cursor_pos
end

#dbObject (readonly)

Returns the value of attribute db.



15
16
17
# File 'lib/live_sql.rb', line 15

def db
  @db
end

#errorsObject

Returns the value of attribute errors.



14
15
16
# File 'lib/live_sql.rb', line 14

def errors
  @errors
end

#interfaceObject (readonly)

Returns the value of attribute interface.



15
16
17
# File 'lib/live_sql.rb', line 15

def interface
  @interface
end

#limitObject (readonly)

Returns the value of attribute limit.



15
16
17
# File 'lib/live_sql.rb', line 15

def limit
  @limit
end

#resultObject

Returns the value of attribute result.



14
15
16
# File 'lib/live_sql.rb', line 14

def result
  @result
end

#stateObject

Returns the value of attribute state.



14
15
16
# File 'lib/live_sql.rb', line 14

def state
  @state
end

#stringObject

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_dbObject



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.message
    self.result = old_table
    self.state = :invalid
end

#filter_aggregate_functionsObject



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


86
87
88
89
90
# File 'lib/live_sql.rb', line 86

def print_display
  system("clear")
  prompt
  render
end


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


119
120
121
# File 'lib/live_sql.rb', line 119

def print_table
  tp result
end


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

#promptObject



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

#renderObject



100
101
102
103
# File 'lib/live_sql.rb', line 100

def render
  state == :invalid ? print_invalid_query : print_valid_query
  print_table
end

#runObject



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

Returns:

  • (Boolean)


73
74
75
# File 'lib/live_sql.rb', line 73

def terminated?
  !!(string =~ /\;\s*$/)
end