-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpgcli_sublime_repl.py
95 lines (72 loc) · 2.68 KB
/
pgcli_sublime_repl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import logging
import sublime
from .pgcli_sublime import format_results
from time import sleep
try:
from SublimeREPL.repls import Repl
except ImportError:
Repl = object
logger = logging.getLogger('pgcli_sublime.repl')
class SublimePgcliRepl(Repl):
TYPE = "pgcli"
def __init__(self, encoding, pgcli_url=None):
super(SublimePgcliRepl, self).__init__(encoding,
additional_scopes=['sql'])
global psycopg2, has_meta_cmd, has_change_db_cmd
from .pgcli_sublime import (PGCli, psycopg2, has_meta_cmd,
has_change_db_cmd)
settings = sublime.load_settings('PgcliSublime.sublime_settings')
pgclirc = settings.get('pgclirc')
logger.debug('Pgcli url: %r', pgcli_url)
self.url = pgcli_url
self.pgcli = PGCli(pgclirc_file=pgclirc)
self.pgcli.connect_uri(pgcli_url)
self.pgcli.refresh_completions()
self._query = None
self._brand_new = True
def name(self):
return 'pgcli'
def write(self, sql):
logger.debug('Write: %r', sql)
self._query = sql
def prompt(self):
return '{}> '.format(self.pgcli.pgexecute.dbname)
def check_refresh(self):
if has_change_db_cmd(self._query):
self.pgcli.refresh_completions(reset=True)
elif has_meta_cmd(self._query):
self.pgcli.refresh_completions(reset=False)
def read(self):
# Show the initial prompt
if self._brand_new:
logger.debug('Brand new prompt')
self._brand_new = False
return self.prompt()
# Block until a command is entered
while not self._query:
sleep(.1)
logger.debug('Query: %r', self._query)
try:
results = self.pgcli.pgexecute.run(self._query,
pgspecial=self.pgcli.pgspecial)
results = format_results(results, self.pgcli.table_format)
except psycopg2.Error as e:
results = e.pgerror
finally:
self.check_refresh()
self._query = None
if results:
return '\n' + results + '\n\n' + self.prompt()
else:
return self.prompt()
def autocomplete_completions(self, whole_line, pos_in_line, *args, **kwargs):
comps = self.pgcli.get_completions(whole_line, pos_in_line)
return [(comp.text, comp.display) for comp in comps]
def is_alive(self):
return self.pgcli is not None
def kill(self):
self.pgcli = None
def allow_restarts(self):
return True
def autocomplete_available(self):
return True