-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPySql.py
136 lines (109 loc) · 4.57 KB
/
PySql.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import mysql.connector
from API.GoogleSheet import *
class PySql:
def __init__(self, username, userPassword):
try:
self.db = mysql.connector.connect(
host="localhost",
user=username,
password=userPassword
)
self.cursor = self.db.cursor()
except:
print("Invalid Credentials")
exit(-1)
self.currentDatabase = "";
self.gSheetAPI = None;
########## Database Stuffs ##########
def useDatabase(self, databaseName):
self.__safeExecution(f"USE {databaseName};", "Using Database")
self.currentDatabase = databaseName
def createDatabase(self, databaseName):
self.__safeExecution(
f"CREATE DATABASE {databaseName};", "Database Creation")
def deleteDatabase(self, databaseName):
self.__safeExecution(
f"DROP DATABASE {databaseName};", "Database Deletion")
def getDatabaseList(self):
self.__safeExecution(f"SHOW DATABASES;", "Getting Database List")
return self.cursor.fetchall()
########## Table Stuff ##########
# Command parameter can accept a set of list in a form of
# - {"columnName Type CONSTRAINTS","CONSTRAINTS"}
def createTable(self, tableName, command):
tempStr = ""
if(isinstance(command, set) or isinstance(command, list)):
for data in command:
tempStr += data + ','
tempStr = tempStr[:-1]
else:
tempStr = command
if self.currentDatabase == "":
print("No Database Currently in-use")
exit(-1)
else:
self.__safeExecution(
f"CREATE TABLE {tableName} ({tempStr});", "Creating Table")
def getTables(self):
self.__safeExecution("SHOW TABLES;", "Getting Tables")
return self.cursor.fetchall()
def deleteTable(self, tableName):
self.__safeExecution(f"DROP TABLE {tableName}", "Deleting Table")
def addTableColumn(self, tableName, command):
tempStr = ""
if(isinstance(command, set) or isinstance(command, list)):
for data in command:
tempStr += data + ','
tempStr = tempStr[:-1]
else:
tempStr = command
if self.currentDatabase == "":
print("No Database Currently in-use")
exit(-1)
else:
self.__safeExecution(
f"ALTER TABLE {tableName} ADD {command};", "Adding Table Column")
def deleteTableColumn(self, tableName, columnName):
self.__safeExecution(
f"ALTER TABLE {tableName} DROP COLUMN {columnName};", "Deleting a Table")
########## Selecting ##########
# Returns a list of tuples
def getAllTableData(self, tableName):
self.__safeExecution(f"SELECT * FROM {tableName}", "Getting Table Data")
return self.cursor.fetchall()
def getTableData(self, tableName, columnName, value, operator="="):
self.__safeExecution(
f"SELECT * FROM {tableName} WHERE {columnName} {operator} {value}", "Getting Table Data")
return self.cursor.fetchall()
########## Other Methods ##########
def getCurrentUser(self):
"""Return a List of Tuples"""
self.__safeExecution(f"SELECT CURRENT_USER();", "Getting Current User")
return self.cursor.fetchall()
def getCurrentDatabaseName(self):
return self.currentDatabase
def exportToCSV(self, tableName, fileName):
self.__safeExecution(f"SELECT * FROM {tableName} INTO OUTFILE '{fileName}.csv' FIELDS TERMINATED BY ','", "Exporting to CSV")
########## API ##########
def initGoogleSheet(self, SCOPE, TOKEN_PATH):
self.gSheetAPI = GoogleSheetAPI(SCOPE, TOKEN_PATH);
def exportToGoogleSheet(self, tableName, RANGE_VALUE, SHEET_ID):
tableValue = self.__tupleToListOfList(self.getAllTableData(tableName))
# API STUFF
self.gSheetAPI.updateSpreadsheetData(RANGE_VALUE,SHEET_ID, tableValue);
pass
########## Private Methods ##########
def __tupleToListOfList(self, value):
tempList = [];
for row in value:
tempListValue = list(row);
dateTimeObject = tempListValue[4];
tempListValue[4] = dateTimeObject.strftime("%d-%b-%Y");
tempList.append(tempListValue);
return tempList;
def __safeExecution(self, command, typeOfCommand="Unknown"):
try:
self.cursor.execute(command)
except:
print(f"Problem: {typeOfCommand}")
exit(-1)