Class: Oraora::Completion

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

Constant Summary collapse

TEMPLATES =
{
    's'  => 'SELECT ',
    's*' => 'SELECT * FROM ',
    'c*' => 'SELECT COUNT(*) FROM ',
    'i'  => 'INSERT ',
    'u'  => 'UPDATE ',
    'd'  => 'DELETE ',
    'a'  => 'ALTER ',
    'c'  => 'CREATE ',
    'cr' => 'CREATE OR REPLACE '
}

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Completion

Returns a new instance of Completion.



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

def initialize(app)
  @app = app
end

Instance Method Details

#comp_procObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/oraora/completion.rb', line 19

def comp_proc
  Proc.new do |s|
    # Complete with template alone if matched
    if TEMPLATES[Readline.line_buffer.downcase]
      TEMPLATES[Readline.line_buffer.downcase]

    else
      # Complete for SQL keywords
      comp = App::SQL_KEYWORDS

      # Complete for current context
      if s !~ /[\.\/]/
        comp += @app.meta.find(@app.context).list rescue []
        context = @app.context.dup
        comp += @app.meta.find(context.up).list while context.level

      # Complete for input
      else
        context = @app.context.dup
        path = s.split(/(?<=[\.\/])/, -1)
        last = path.pop
        loop do
          comp_context = @app.context_for(context, path.join) rescue nil
          if comp_context
            comp += @app.meta.find(comp_context).list.collect { |n| path.join + n } rescue []
          end
          break if context.level == nil
          context.up
        end
      end

      comp.sort.uniq.grep(/^#{Regexp.escape(s)}/i)
    end
  end
end