-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path__init__.py
275 lines (189 loc) · 6.72 KB
/
__init__.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# -*- encoding: utf-8 -*-
"""
Copyright (c) 2019 - present AppSeed.us
License: MIT
"""
from util.helpers import *
from peewee import SqliteDatabase, MySQLDatabase, PostgresqlDatabase
from playhouse.reflection import generate_models, print_model, print_table_sql
from playhouse.dataset import DataSet
def test():
print( 'working!' )
class DbWrapper:
# private
_db = None
_ds = None
_models = None
# public data
driver = None
db_name = None
db_user = None
db_pass = None
# reset
def reset(self):
self._db = None
self._ds = None
self._models = None
self.driver = None
self.db_name = None
self.db_user = None
self.db_pass = None
# helpers
def connect(self):
if not self.driver:
print( ' > Error DB driver not set' )
if self.driver == COMMON.DB_SQLITE:
self._db = SqliteDatabase( self.db_name )
self._ds = DataSet( self._db )
return True
elif self.driver == COMMON.DB_MYSQL:
self._db = MySQLDatabase( self.db_name, user=self.db_user, password=self.db_pass )
self._ds = DataSet( self._db )
return True
elif self.driver == COMMON.DB_PGSQL:
self._db = PostgresqlDatabase( self.db_name, user=self.db_user, password=self.db_pass )
self._ds = DataSet( self._db )
return True
else:
print( ' > Error unsupported driver [' + driver + ']' )
return False
def load_models(self):
if not self._db:
print( ' > Error: No DB connection' )
return None
if not self._models:
self._models = generate_models( self._db )
return True
def get_model(self, aModelName):
if not self._db:
print( ' > Error: No DB connection' )
return None
if not self._models:
print( ' > Error: No DB models' )
return None
if not aModelName in self._models:
print( ' > Error Model [' + aModelName + '] not found' )
return None
return self._models[ aModelName ]
def get_model_data(self, aModelName):
aModel = self.get_model( aModelName )
if not aModel:
return None
return self._ds[ aModelName ].all()
def print_all_models(self):
if not self._db:
print( ' > Error: No DB connection' )
return
if not self._models:
print( ' > Error: No DB models' )
return
print( list(self._models.items()) )
def print_db_model(self, aModelName, aSql=False):
if not self._models:
print( ' > Error: No DB models' )
return
if not aModelName in self._models:
print( ' > Error Model [' + aModelName + '] not found' )
return
aModel = self._models[ aModelName ]
if aSql:
print_table_sql( aModel )
else:
print_model(aModel)
return
def sql_select(self, aModelName):
if not self._models:
print( ' > Error: No DB models' )
return
if not aModelName in self._models:
print( ' > Error Model [' + aModelName + '] not found' )
return None
aModel = self._models[ aModelName ]
for item in aModel.select():
print ( item )
def dump_model_data(self, aModelName, aFileName=None):
items = self.get_model_data( aModelName )
if not items:
return None
if len( items ) == 0:
print( ' > Model [' + aModelName + '] is empty ' )
return None
header = items[0].keys()
sqlContent = ''
for col in header:
sqlContent += col + ','
sqlContent += '\n'
for row in items:
for i in row.values():
sqlContent += str( i ) + ','
sqlContent += '\n'
if not aFileName:
aFileName = get_date() + '_' + self.driver + '_' + aModelName
if not aFileName.endswith('.sql'):
aFileName += '.sql'
file_write('output/' + aFileName, sqlContent)
return True
def dump_tables(self, aFileName=None):
sqlContent = ''
if not self._models:
print( ' > Error: No DB models' )
return False
if not aFileName:
aFileName = get_date() + '_' + self.driver
if not aFileName.endswith('.sql'):
aFileName += '.sql'
for table_name in self._models.keys():
# SQLite Engine Metadata
if 'sqlite_sequence' == table_name:
continue
# Unused
aModel = self._models[ table_name ]
# Hack the print
with Capturing() as output:
self.print_db_model( table_name, True)
sqlContent += '\n'
sqlContent += '-- Table: ' + table_name + '\n'
sqlContent += h_list_to_s( output, '\n' )
sqlContent += ';\n'
file_write('output/' + aFileName, sqlContent)
return True
def dump_models(self, aFileName=None):
sqlContent = ''
if not self._models:
print( ' > Error: No DB models' )
return False
if not aFileName:
aFileName = get_date() + '_' + self.driver + '_models'
if not aFileName.endswith('.sql'):
aFileName += '.sql'
for table_name in self._models.keys():
# SQLite Engine Metadata
if 'sqlite_sequence' == table_name:
continue
# Unused
aModel = self._models[ table_name ]
# Hack the print
with Capturing() as output:
self.print_db_model( table_name )
sqlContent += '\n'
sqlContent += '-- Table: ' + table_name + '\n'
sqlContent += h_list_to_s( output, '\n' )
sqlContent += ';\n'
file_write('output/' + aFileName, sqlContent)
return True
def dump_tables_data(self, aFileName=None):
sqlContent = ''
if not self._models:
print( ' > Error: No DB models' )
return False
if not aFileName:
aFileName = get_date() + '_' + self.driver
if not aFileName.endswith('.sql'):
aFileName += '.sql'
for table_name in self._models.keys():
# SQLite Engine Metadata
if 'sqlite_sequence' == table_name:
continue
print( ' > Dump data for [' + table_name + ']' )
self.dump_model_data( table_name )
return True