-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_handler.py
177 lines (129 loc) · 5.18 KB
/
db_handler.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# import pymongo
import socketio
'''
class MongoDB:
def __init__(self, host, port=None):
print('MongoDB client is opening.')
self._client = pymongo.MongoClient(host, port)
self.db = None
def __enter__(self):
return self
def __exit__(self, type, value, trace):
self.close()
def connect(self, db_name):
print(f'MongoDB is connect to {db_name} database.')
self.db = self._client[db_name]
def close(self):
print('MongoDB client is closing.')
self._client.close()
def _db_connected(self):
if not self.db:
raise BrokenPipeError('MongoDB database have been connecting.')
def insert(self, col_name, data):
self._db_connected()
col = self.db[col_name]
result_id = None
if type(data) is dict:
result_id = col.insert_one(data).inserted_id
elif type(data) is list:
result_id = col.insert_many(data).inserted_id
return result_id
def find(self, col_name):
self._db_connected()
return self.db[col_name].find_one()
def select(self, col_name, query={}, condition={}):
self._db_connected()
# Change SQL grammar to MongoDB grammar
if type(col_name) is str and \
'SELECT' in col_name.upper() and \
not query and not condition:
strings = col_name.split(' ')
uppers = [map(lambda e: e.upper(), strings)]
if strings[1] == '*':
condition = {}
else:
condition = {}
start = uppers.index('SELECT') + 1
end = uppers.index('FROM')
for i, q in enumerate(strings[start: end]):
q = q.replace(',', '').strip()
strings[start + i] = q
condition[q] = 1
col_name = strings[uppers.index('FROM') + 1]
if 'WHERE' in uppers:
query = {}
start = col_name.upper().index('WHERE')
end = start + 4
temp = ''.join(col_name[end + 1:].split()).split('and')
for q in temp:
key, value = q.split('=', 1) # 第二個參數為預防值有包含=的存在,而被切割
query[key] = value
col = self.db[col_name]
if type(query) is dict and type(condition) is dict:
return col.find(query, condition)
return None
def delete_one(self, col_name, condition):
self._db_connected()
if type(condition) is dict:
return self.db[col_name].delete_one(condition)
return None
def delete(self, col_name, condition):
self._db_connected()
# Change SQL grammar to MongoDB grammar
if type(col_name) is str and \
'DELETE' in col_name.upper() and not condition:
strings = col_name.split(' ')
uppers = [map(lambda e: e.upper(), strings)]
col_name = strings[uppers.index('FROM') + 1]
condition = {}
if 'WHERE' in uppers:
start = col_name.upper().index('WHERE')
end = start + 4
temp = ''.join(col_name[end + 1:].split()).split('and')
for q in temp:
key, value = q.split('=', 1) # 第二個參數為預防值有包含=的存在,而被切割
condition[key] = value
col = self.db[col_name]
if type(condition) is dict:
return col.delete_many(condition).deleted_count
return 0
def drop(self, col_name):
self._db_connected()
if type(col_name) is str and 'DROP' in col_name.upper():
strings = col_name.split(' ')
uppers = [map(lambda e: e.upper(), strings)]
col_name = strings[uppers.index('TABLE') + 1]
return self.db[col_name].drop()
def collection(self, col_name):
self._db_connected()
return self.db[col_name]
'''
class MongoDB:
def __init__(self, host, port):
self._socket = socketio.Client()
self._socket.connect(f'http://{host}:{port}')
print('MongoDB client is connecting.')
def __enter__(self):
return self
def __exit__(self, type, value, trace):
self.close()
def close(self):
print('MongoDB client is closing.')
self._socket.disconnect()
def trigger(self, _type, event, query, callback=None):
query['event'] = event
self._event_route(_type, query, callback, event)
def select(self, query, callback=None):
self._event_route('get', query, callback)
def insert(self, query, callback=None):
self._event_route('insert', query, callback)
def delete(self, query, callback=None):
self._event_route('delete', query, callback)
def update(self, query, callback=None):
self._event_route('update', query, callback)
def _event_route(self, req_event, query, callback, res_event=None):
self._socket.emit(req_event, query)
if callback and callable(callback):
@self._socket.on(res_event or f'{req_event}Result')
def _handler(data):
callback(data)