Class: LiveSQL

Inherits:
Object
  • Object
show all
Defined in:
lib/live_sql.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db) ⇒ LiveSQL



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/live_sql.rb', line 32

def initialize(db)
  @interface = Interface.new
  @db = QuestionsDatabase.new(db)

  @string = " "
  @result = []

  @move = :invalid
  @cursor_pos = 0
  @errors = []
end

Instance Attribute Details

#resultObject

Returns the value of attribute result.



20
21
22
# File 'lib/live_sql.rb', line 20

def result
  @result
end

Class Method Details

.run_defaultObject



22
23
24
25
# File 'lib/live_sql.rb', line 22

def self.run_default
  live_sql = LiveSQL.new('./live_sql/default_db/movie.db')
  live_sql.run
end

.run_with(db_name) ⇒ Object



27
28
29
30
# File 'lib/live_sql.rb', line 27

def self.run_with(db_name)
  live_sql = LiveSQL.new(db_name)
  live_sql.run
end

Instance Method Details

#attempt_to_query_dbObject



74
75
76
77
78
79
80
81
82
# File 'lib/live_sql.rb', line 74

def attempt_to_query_db
  old_table = @result
  @result = query_db(@string)
  @mode = :valid
  rescue StandardError => e
    @errors << e.message
    @result = old_table
    @mode = :invalid
end

#handle_input(input) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/live_sql.rb', line 52

def handle_input(input)
  if input == :abort
    system("clear")
    abort
  elsif input == :error
  elsif input == :right
    @cursor_pos += 1 unless @cursor_pos == @string.length - 1
  elsif input == :left
    @cursor_pos -= 1 unless @cursor_pos == 0
  elsif input == :backspace
    if @string.length > 1
      @string.slice!(@cursor_pos - 1)
      @cursor_pos -= 1 unless @cursor_pos == 0
    end
    attempt_to_query_db
  else
    @string.insert(@cursor_pos, input)
    @cursor_pos += 1
    attempt_to_query_db
  end
end


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/live_sql.rb', line 84

def print_display
  system("clear")
  puts "Enter a query!".underline.bold
  puts "Green is for a valid query, red for syntax error."
  puts "The table always shows the last valid query."
  puts "Press esc to quit."
  if @mode == :invalid
    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
  else
    print @string[0...@cursor_pos].colorize(:green)
    print @string[@cursor_pos].colorize(background: :cyan)
    print @string[@cursor_pos + 1..-1].colorize(:green)
    puts
  end
  print_table
end


104
105
106
# File 'lib/live_sql.rb', line 104

def print_table
  tp @result
end

#query_db(string) ⇒ Object



108
109
110
111
112
113
# File 'lib/live_sql.rb', line 108

def query_db(string)
  @db.execute("    \#{string}\n    LIMIT 30\n  SQL\nend\n")

#runObject



44
45
46
47
48
49
50
# File 'lib/live_sql.rb', line 44

def run
  while true
    print_display
    input = @interface.get_keyboard_input
    handle_input(input)
  end
end