-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyDb.py
124 lines (112 loc) · 4.35 KB
/
MyDb.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
'''
Created on 21.07.2022
@author: wk
'''
import mysql.connector
from SilentLog import SilentLog
from Configuration import Configuration
class MyDb (SilentLog):
def __init__(self):
'''Constructor.
'''
SilentLog.__init__(self, 100)
self._host = 'localhost'
self._dbName = 'appsunmonitor'
self._dbUser = 'sunx'
self._dbCode = 'sun4suny'
self._connection = None
self._cursor = None
self._autocommit = True
def _dbConfigOne(self, name: str, configuration: Configuration, defaultValue: str=None) -> str:
'''Handles one configuration variable.
@param name: the variable's name
@param configuration: the configuration manager
@param defaultValue: this value is used if the variable does not exist or an error has occurred
@return: the value of the variable
'''
rc = defaultValue
if not configuration.hasKey(name):
if defaultValue == None:
self.error(f'missing {name}')
else:
rc = configuration.asString(name, defaultValue)
return rc
def dbConfig(self, configuration: Configuration) -> bool:
'''Gets the variables associated to the database from the configuration data.
@param configuration: the configuration manager
@return True: all needed data found
False: missing some needed configuration
'''
self._dbName = self._dbConfigOne('db.name', configuration)
self._dbUser = self._dbConfigOne('db.user', configuration)
self._dbHost = self._dbConfigOne('db.host', configuration, 'localhost')
self._dbCode = self._dbConfigOne('db.code', configuration)
found = self._dbName != None and self._dbUser != None and self._dbCode != None
return found
def dbConnect(self):
'''Connects the database with the login data from the configuration.
'''
self._connection = mysql.connector.connect(host=self._host, user=self._dbUser, password=self._dbCode,
database=self._dbName, autocommit=self._autocommit)
if self._connection == None:
self.error('cannot connect to db')
else:
self.log(f'connected to {self._dbName}')
def dbClose(self):
'''Closes the connection.
'''
self.debug(f'closing {self._dbName}')
self.dbCloseCursor()
if self._connection != None:
self._connection.close()
self._connection = None
def dbCloseCursor(self):
'''Closes the cursor and delete that.
'''
self.debug('dbCloseCursor')
if self._cursor != None:
self._cursor.close()
# self._connection.consume_results()
self._cursor = None
def dbCommit(self):
'''Commits the last transaction.
'''
self.debug('dbCommit')
if not self._autocommit:
try:
self._connection.commit()
except Exception as exc:
self.debug(f'commit failed: {exc}')
self.dbCloseCursor()
def dbExecute(self, sql: str, values=None):
'''Executes a SQL statement without result: INSERT, UPDATE, DELETE...
@param sql: the SQL statement
@param values: None or the positional parameters
'''
self.debug('dbExecute ' + sql[0:20])
if self._cursor == None:
if not self._connection.is_connected():
self.dbReconnect()
self._cursor = self._connection.cursor()
self._cursor.execute(sql, values)
self.dbCommit()
def dbReconnect(self):
'''Closes a database connection and reopen that.
'''
self.debug('reconnecting...')
self._connection.reconnect()
def dbSelect(self, sql, values=None):
'''Executes a SQL statement without result: INSERT, UPDATE, DELETE...
@param sql: the SQL statement
@param values: None or the positional parameters
@return a list of records
'''
self.debug('dbSelect ' + sql[0:20])
if self._cursor == None:
if not self._connection.is_connected():
self.dbConnect()
self._cursor = self._connection.cursor()
self._cursor.execute(sql, values)
rc = self._cursor.fetchall()
self.dbCloseCursor()
return rc