forked from lefred/mysqlshell-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysqlsh_plugins_common.py
89 lines (75 loc) · 2.91 KB
/
mysqlsh_plugins_common.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
# mysqlsh_plugins_common.py
# -------------------------
# This file holds common code that is shared among the individual plugins
# located in sub-folders.
def is_consumer_enabled(event_name, session, shell):
stmt = """SELECT NAME, ENABLED FROM performance_schema.setup_consumers
WHERE NAME LIKE '{}' AND ENABLED='NO';""".format(event_name)
result = session.run_sql(stmt)
consumers = result.fetch_all()
ok = False
if len(consumers) > 0:
consumers_str = ""
for consumer in consumers:
consumers_str += "%s, " % consumer[0]
answer = shell.prompt("""Some consumers are not enabled: %s
Do you want to enabled them now ? (y/N) """
% consumers_str, {'defaultValue':'n'})
if answer.lower() == 'y':
stmt = """UPDATE performance_schema.setup_consumers
SET ENABLED = 'YES'
WHERE NAME LIKE '{}'
AND ENABLED='NO'""".format(event_name)
result = session.run_sql(stmt)
ok = True
else:
ok = True
return ok
def are_instruments_enabled(instrument_name, session, shell):
stmt = """SELECT NAME, ENABLED
FROM performance_schema.setup_instruments
WHERE NAME LIKE '{}'
AND ENABLED='NO'""".format(instrument_name)
result = session.run_sql(stmt)
instruments = result.fetch_all()
ok = False
if len(instruments) > 0:
instruments_str = ""
for instrument in instruments:
instruments_str += "%s, " % instrument[0]
answer = shell.prompt("""Some instruments are not enabled: %s
Do you want to enabled them now ? (y/N) """
% instruments_str, {'defaultValue':'n'})
if answer.lower() == 'y':
stmt = """UPDATE performance_schema.setup_instruments
SET ENABLED = 'YES', TIMED = 'YES'
WHERE NAME LIKE '{}'
AND ENABLED='NO'""".format(instrument_name)
result = session.run_sql(stmt)
ok = True
else:
ok = True
return ok
def get_version(session):
result = session.run_sql("SELECT @@version")
col = result.fetch_one()
return col[0].split('.')[0]
def get_major_version(session):
result = session.run_sql("SELECT @@version")
col = result.fetch_one()
return col[0].split('.')[0]+'.'+col[0].split('.')[1]
def run_and_show(stmt, format='table',session=None):
import mysqlsh
shell = mysqlsh.globals.shell
if session is None:
session = shell.get_session()
if session is None:
print("No session specified. Either pass a session object to this "
"function or connect the shell to a database")
return
try:
result = session.run_sql(stmt)
shell.dump_rows(result, format)
except:
print("This query cannot be executed.")
return