-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatter.py
More file actions
123 lines (102 loc) · 4.23 KB
/
formatter.py
File metadata and controls
123 lines (102 loc) · 4.23 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
import sqlalchemy
import config, constants, util
def textclause_repr(self):
return 'text(%r)' % self.text
def table_repr(self):
data = {
'name': self.name,
'columns': constants.NLTAB.join([repr(cl) for cl in self.columns]),
'constraints': constants.NLTAB.join(
[repr(cn) for cn in self.constraints
if not isinstance(cn, sqlalchemy.PrimaryKeyConstraint)]),
'index': '',
'schema': self.schema != None and ",\n%sschema='%s'" % (constants.TAB, self.schema) or '',
}
if data['constraints']:
data['constraints'] = '\n%s%s,' % (constants.TAB, data['constraints'])
if data['columns']:
data['columns'] = '\n%s%s' % (constants.TAB, data['columns'])
return util.as_out_str(constants.TABLE % data)
def _repr_coltype_as(coltype, as_type):
"""repr a Type instance as a super type."""
specimen = object.__new__(as_type)
specimen.__dict__ = coltype.__dict__
return repr(specimen)
def column_repr(self):
kwarg = []
if self.key != self.name:
kwarg.append( 'key')
if hasattr(self, 'primary_key'):
kwarg.append( 'primary_key')
if not self.nullable:
kwarg.append( 'nullable')
if self.onupdate:
kwarg.append( 'onupdate')
#issue:
#http://www.sqlalchemy.org/trac/wiki/06Migration#AnImportantExpressionLanguageGotcha
if self.default is not None:
kwarg.append( 'default')
elif self.server_default:
self.default = self.server_default.arg
kwarg.append( 'default')
ks = ', '.join('%s=%r' % (k, getattr(self, k)) for k in kwarg )
name = self.name
if not hasattr(config, 'options') and config.options.generictypes:
coltype = repr(self.type)
elif type(self.type).__module__ == 'sqlalchemy.types':
coltype = repr(self.type)
else:
# Try to 'cast' this column type to a cross-platform type
# from sqlalchemy.types, dropping any database-specific type
# arguments.
for base in type(self.type).__mro__:
if (base.__module__ == 'sqlalchemy.types' and
base.__name__ in sqlalchemy.__all__):
coltype = _repr_coltype_as(self.type, base)
break
# FIXME: if a dialect has a non-standard type that does not
# derive from an ANSI type, there's no choice but to ignore
# generic-types and output the exact type. However, import
# headers have already been output and lack the required
# dialect import.
else:
coltype = repr(self.type)
data = {'name': self.name,
'type': coltype,
'constraints': ', '.join([repr(cn) for cn in self.constraints]),
'args': ks and ks or '',
}
if data['constraints']:
data['constraints'] = ', ' + data['constraints']
if data['args']:
data['args'] = ', ' + data['args']
return util.as_out_str(constants.COLUMN % data)
def foreignkeyconstraint_repr(self):
data = {'name': repr(self.name),
'names': repr([x.parent.name for x in self.elements]),
'specs': repr([x._get_colspec() for x in self.elements])
}
return util.as_out_str(constants.FOREIGN_KEY % data)
def index_repr(index):
cols = []
for column in index.columns:
# FIXME: still punting on the issue of unicode table names
if util.is_python_identifier(column.name):
cols.append('%s.c.%s' % (column.table.name, column.name))
else:
cols.append('%s.c[%r]' % (column.table.name, column.name))
data = {'name': repr(index.name),
'columns': ', '.join(cols),
'unique': repr(index.unique),
}
return util.as_out_str(constants.INDEX % data)
def check_constraint_repr(cc):
data = {'sqltext': cc.sqltext}
return util.as_out_str(constants.CHECK_CONSTRAINT % data)
def monkey_patch_sa():
sqlalchemy.sql.expression._TextClause.__repr__ = textclause_repr
sqlalchemy.schema.Table.__repr__ = table_repr
sqlalchemy.schema.Column.__repr__ = column_repr
sqlalchemy.schema.ForeignKeyConstraint.__repr__ = foreignkeyconstraint_repr
sqlalchemy.schema.Index.__repr__ = index_repr
sqlalchemy.schema.CheckConstraint.__repr__ = check_constraint_repr