forked from PiTiLeZarD/workbench_alchemy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlalchemy_grt.py
More file actions
297 lines (240 loc) · 10.7 KB
/
sqlalchemy_grt.py
File metadata and controls
297 lines (240 loc) · 10.7 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import re
version = '0.11.3'
types = {
'sqla': [],
'sqla_alt': [],
'mysql': [],
}
typesmap = {
'INT': 'INTEGER',
}
sqlalchemy_typesmap = {
'Varchar': 'String',
'Text': 'String',
'Tinyint': 'Integer',
'Timestamp': 'DateTime',
'Datetime': 'DateTime',
'Double': 'Float',
'Blob': 'String',
}
USE_MYSQL_TYPES = True
mysqltypes = [
'BIGINT', 'BINARY', 'BIT', 'BLOB', 'BOOLEAN', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'DECIMAL', 'DOUBLE', 'ENUM', 'FLOAT', 'INTEGER',
'LONGBLOB', 'LONGTEXT', 'MEDIUMBLOB', 'MEDIUMINT', 'MEDIUMTEXT', 'NCHAR', 'NUMERIC', 'NVARCHAR', 'REAL', 'SET', 'SMALLINT', 'TEXT',
'TIME', 'TIMESTAMP', 'TINYBLOB', 'TINYINT', 'TINYTEXT', 'VARBINARY', 'VARCHAR', 'YEAR']
def camelize(name):
return re.sub(r"(?:^|_)(.)", lambda x: x.group(0)[-1].upper(), name)
def endsWith(name, all):
name = name.lower()
for i in all:
if name.endswith(i):
return True
return False
def singular(name):
if endsWith(name, ('indices',)):
name = name[:-4] + 'ex'
elif endsWith(name, ('suffixes',)):
name = name[:-3] + 'x'
elif endsWith(name, ('aliases', 'dresses')):
name = name[:-2]
elif name.endswith('ies'):
name = name[:-3] + 'y'
elif endsWith(name, ('ces', 'mes')):
name = name[:-1]
elif name.endswith('es'):
name = name[:-2]
elif name.endswith('s'):
name = name[:-1]
return name
def getType(column):
column_type = column.formattedType
column_type = re.match(r'(?P<type>[^\(\)]+)(\((?P<size>[^\(\)]+)\))?', column_type).groupdict()
column_type, size = (column_type['type'], column_type['size'])
column_type = typesmap.get(column_type.upper(), column_type).upper()
if USE_MYSQL_TYPES and column_type in mysqltypes:
# case of mysql types
column_type = column_type.upper()
if column_type not in types['mysql']:
types['mysql'].append(column_type)
sqla = camelize(column_type.lower())
sqla = sqlalchemy_typesmap.get(sqla, sqla)
types['sqla_alt'].append("%s as %s" % (sqla, column_type))
if 'UNSIGNED' in column.flags and 'INT' in column_type:
column_type = '%s(unsigned=True)' % column_type
else:
# sqlalchemy types
column_type = camelize(column_type.lower())
column_type = sqlalchemy_typesmap.get(column_type, column_type)
if column_type not in types['sqla']:
types['sqla'].append(column_type)
if size and 'INT' not in column_type.upper():
column_type = '%s(%s)' % (column_type, size)
return column_type
def exportTable(table):
export = []
# yeah I know... but I can't prevent myself...
# this is to convert all column's comments from opt1=value1,opt2=value2
# to a dict like {column_name: {opt1:value1, opt2:value2} ...}
options = dict([(c.name, dict([t.split('=') for t in (c.comment or '').split(',') if '=' in t])) for c in table.columns])
classname = singular(camelize(table.name))
indices = {'PRIMARY': [], 'INDEX': [], 'UNIQUE': []}
for index in table.indices:
if index.indexType == 'PRIMARY':
indices['PRIMARY'] += [c.referencedColumn.name for c in index.columns]
if index.indexType == 'INDEX':
indices['INDEX'] += [c.referencedColumn.name for c in index.columns]
if index.indexType == 'UNIQUE':
indices['UNIQUE'] += [(index.name, [c.referencedColumn.name for c in index.columns])]
foreignKeys = {}
for fk in table.foreignKeys:
if len(fk.referencedColumns) > 1:
# I don't even think that sqlalchemy handles multi column foreign keys...
continue
for i in range(0, len(fk.referencedColumns)):
relation = '%s.%s' % (fk.referencedColumns[i].owner.name, fk.referencedColumns[i].name)
fktable = camelize(fk.referencedColumns[i].owner.name)
ondelete = onupdate = None
if fk.deleteRule and fk.deleteRule != "NO ACTION":
ondelete = fk.deleteRule
if fk.updateRule and fk.updateRule != "NO ACTION":
onupdate = fk.updateRule
foreignKeys[fk.columns[i].name] = (relation, fktable, ondelete, onupdate)
inherits = 'Base'
if 'abstract' in table.comment:
inherits = 'object'
export.append("")
export.append("class %s(%s):" % (classname, inherits))
if 'abstract' not in table.comment:
export.append(" __tablename__ = '%s'" % table.name)
table_args = {}
if table.tableEngine:
table_args['mysql_engine'] = table.tableEngine
charset = table.defaultCharacterSetName or table.owner.defaultCharacterSetName
if charset:
table_args['mysql_charset'] = charset
if sum([column.autoIncrement for column in table.columns]) > 0:
table_args['sqlite_autoincrement'] = True
export.append(" __table_args__ = %s" % table_args)
export.append("")
aliases = {}
for column in table.columns:
column_name = column.name
column_type = getType(column)
if 'alias' in options[column_name]:
aliases[column_name] = options[column_name]['alias']
column_options = []
column_options.append(column_type)
if column.name in foreignKeys:
fkcol, fktable, ondelete, onupdate = foreignKeys[column.name]
fkopts = []
if ondelete:
fkopts.append('ondelete="%s"' % ondelete)
if onupdate:
fkopts.append('onupdate="%s"' % onupdate)
fkopts = len(fkopts) and ', ' + ', '.join(fkopts) or ''
column_options.append('ForeignKey("%s"%s)' % (fkcol, fkopts))
if column.isNotNull == 1:
column_options.append('nullable=False')
if column.autoIncrement == 1:
column_options.append('autoincrement=True')
if column.name in indices['PRIMARY']:
column_options.append('primary_key=True')
if (len(indices['PRIMARY']) == 1) and (column_name != 'id') and (column_name not in aliases):
aliases[column_name] = 'id'
elif column.autoIncrement != 1:
column_options.append('autoincrement=False')
if column.name in indices['INDEX']:
column_options.append('index=True')
if column.name in [i[1][0] for i in indices['UNIQUE'] if len(i[1]) == 1]:
column_options.append('unique=True')
if column.defaultValue:
column_options.append('default=%s' % column.defaultValue)
if column_name in aliases:
column_options = ['"%s"' % column_name] + column_options
column_name = aliases[column_name]
export.append(" %s = Column(%s)" % (column_name, ', '.join(column_options)))
uniques_multi = [i for i in indices['UNIQUE'] if len(i[1]) > 1]
if len(uniques_multi):
export.append("")
for index_name, columns in uniques_multi:
export.append(" UniqueConstraint('%s', name='%s')" % ("', '".join(columns), index_name))
if len(foreignKeys.items()):
export.append("")
for column_name, v in foreignKeys.items():
fkcol, fktable, ondelete, onupdate = v
fkname = singular(fktable)
fkname = fkname[0].lower() + fkname[1:]
if options[column_name].get('fkname', None) is not None:
fkname = options[column_name].get('fkname', None)
if 'norelations' in table.comment:
export.append(' # relationship %s ignored globally on the table' % fkname)
continue
if options[column_name].get('relation', True) == 'False':
export.append(' # relationship %s ignored by column' % fkname)
continue
backref = ''
if options[column_name].get('backref', True) != 'False':
if options[column_name].get('backrefname', None) is not None:
backref = options[column_name].get('backrefname', None)
else:
backref = camelize(table.name)
backref = backref[0].lower() + backref[1:]
backref = ', backref="%s"' % backref
if options[column_name].get('remote_side', None):
backref += ', remote_side=[%s]' % options[column_name]['remote_side']
column_name = aliases.get(column_name, column_name)
export.append(' %s = relationship("%s", foreign_keys=[%s]%s)' % (fkname, singular(fktable), column_name, backref))
export.append("")
export.append(' def __repr__(self):')
export.append(' return self.__str__()')
export.append("")
# take all column you say or by default the primary ones (unless specified otherwise)
toprint = [aliases.get(c, c) for c in [p for p, o in options.items() if o.get('toprint', str(p in indices['PRIMARY'] and options[p].get('toprint', True) != 'False')) == 'True']]
export.append(' def __str__(self):')
export.append(" return '<" + classname + " " + ' '.join(['%(' + i + ')s' for i in toprint]) + ">' % self.__dict__")
export.append("")
return export
print "-" * 20
print "-- SQLAlchemy export v%s" % version
print "-" * 20
export = []
export.append('#!/usr/bin/env python')
export.append('#-*- coding: utf-8 -*-')
export.append('"""')
export.append('This file has been automatically generated with workbench_alchemy v%s' % version)
export.append('For more details please check here:')
export.append('https://github.com/PiTiLeZarD/workbench_alchemy')
export.append('"""')
export.append("")
export.append("USE_MYSQL_TYPES = %s" % USE_MYSQL_TYPES)
export.append("try:")
export.append(" from . import USE_MYSQL_TYPES")
export.append("except:")
export.append(" pass")
export.append("")
tables = []
for table in grt.root.wb.doc.physicalModels[0].catalog.schemata[0].tables:
print " -> Working on %s" % table.name
tables.extend(exportTable(table))
export.append("")
export.append("import datetime")
export.append("")
export.append("from sqlalchemy.orm import relationship")
export.append("from sqlalchemy import Column, ForeignKey")
export.append("from sqlalchemy.schema import UniqueConstraint")
export.append("from sqlalchemy.ext.declarative import declarative_base")
if len(types['sqla']):
export.append("from sqlalchemy import %s" % ', '.join(types['sqla']))
export.append("")
export.append("if USE_MYSQL_TYPES:")
if len(types['mysql']):
export.append(" from sqlalchemy.dialects.mysql import %s" % ', '.join(types['mysql']))
export.append("else:")
if len(types['sqla_alt']):
export.append(" from sqlalchemy import %s" % ', '.join(types['sqla_alt']))
export.append("")
export.append("Base = declarative_base()")
export.append("")
export.extend(tables)
grt.modules.Workbench.copyToClipboard('\n'.join(export))
print "Copied to clipboard"