-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMySublimeQL.py
79 lines (63 loc) · 2.19 KB
/
MySublimeQL.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
#!/usr/bin/python
import sublime, sublime_plugin, sys, os
# Crazy shit needed to get MySQLdb working
directory = os.path.dirname(os.path.realpath(__file__)) + "\\"
sys.path.append(directory+"\\MySQLdb")
sys.path.append(directory+"\\MySQLdb\\constants")
from MySQLdb import *
from MySQLdb.constants import *
class DBManager:
def __init__(self):
self.settings = sublime.load_settings('MySublimeQL.sublime-settings')
self.get_connections()
self.connect_to_database()
def get_connections(self):
self.connections = self.settings.get('connections')
return self.connections
def connect_to_database(self, database=None):
params = {}
if database is None:
database = self.settings.get('default_schema')
for connection in self.connections:
if connection.get('name') == database:
params = connection
self.db = connect(params.get('host'), params.get('user'), params.get('pass'), params.get('db'))
def query(self, query):
cursor = self.db.cursor()
cursor.execute(query)
return cursor.fetchall()
db = DBManager()
def autocomplete(show_tables=True, table_name=None):
query = ''
if show_tables:
query = 'SHOW TABLES'
else:
query = 'DESCRIBE ' + table_name
data = db.query(query)
completions = [(x[0],) * 2 for x in data]
return completions
completions = autocomplete()
class SwitchSchemaCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.connection_list = []
for connection in db.get_connections():
self.connection_list.append([connection.get('name'), 'Host: ' + connection.get('host')])
window = sublime.active_window()
window.show_quick_panel(self.connection_list, self.on_done)
def on_done(self, picked):
if picked >= 0:
db.connect_to_database(self.connection_list[picked][0])
class MySublimeQL(sublime_plugin.EventListener):
def on_modified(self, view):
global completions
view_sel = view.sel()
sel = view_sel[0]
pos = sel.end()
text = view.substr(sublime.Region(pos - 1, pos))
if text == '.' :
completions = autocomplete(False, view.substr(view.word(pos -1)))
elif text == ' ':
completions = autocomplete()
def on_query_completions(self, view, prefix, locations):
if view.match_selector(locations[0], "source.sql"):
return (completions)