-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_database.py
More file actions
67 lines (55 loc) · 1.68 KB
/
Copy pathsql_database.py
File metadata and controls
67 lines (55 loc) · 1.68 KB
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
from connection_string import ConnectionString
from sql_handler import MsSqlHandler
class SQLDatabase(MsSqlHandler):
@classmethod
def prod(cls, read_only=True):
return cls(ConnectionString().prod, read_only)
@classmethod
def qa(cls, read_only=False):
return cls(ConnectionString().qa, read_only)
@classmethod
def dev(cls, read_only=False):
return cls(ConnectionString().dev, read_only)
@classmethod
def local(cls, read_only=False):
return cls(ConnectionString().local, read_only)
def get_tables(self):
return self.query(
"""
SELECT * FROM sys.tables
WHERE SCHEMA_NAME(schema_id) = 'dbo';
"""
)
def get_columns(self, table_name):
return self.query(
f"""
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = '{table_name}'
"""
)
def get_views(self):
return self.query(
"""
SELECT * FROM sys.objects
WHERE type_desc = 'VIEW';
"""
)
def get_view_definition(self, view_name):
return self.query(
f"""
EXEC sp_helptext {view_name};
"""
)
def get_procedures(self):
return self.query(
"""SELECT * FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_TYPE = 'PROCEDURE';
"""
)
def get_procedure_definition(self, procedure_name):
return self.query(
f"""
SELECT * FROM INFORMATION_SCHEMA.PARAMETERS
WHERE SPECIFIC_NAME='{procedure_name}';
""")