188 |
188 |
189 def aschema2sql(dbhelper, eschema, rschema, aschema, creating=True, indent=''): |
189 def aschema2sql(dbhelper, eschema, rschema, aschema, creating=True, indent=''): |
190 """write an attribute schema as SQL statements to stdout""" |
190 """write an attribute schema as SQL statements to stdout""" |
191 attr = rschema.type |
191 attr = rschema.type |
192 rdef = rschema.rdef(eschema.type, aschema.type) |
192 rdef = rschema.rdef(eschema.type, aschema.type) |
193 sqltype = type_from_constraints(dbhelper, aschema.type, rdef.constraints, |
193 sqltype = type_from_rdef(dbhelper, rdef, creating) |
194 creating) |
|
195 if SET_DEFAULT: |
194 if SET_DEFAULT: |
196 default = eschema.default(attr) |
195 default = eschema.default(attr) |
197 if default is not None: |
196 if default is not None: |
198 if aschema.type == 'Boolean': |
197 if aschema.type == 'Boolean': |
199 sqltype += ' DEFAULT %s' % dbhelper.boolean_value(default) |
198 sqltype += ' DEFAULT %s' % dbhelper.boolean_value(default) |
213 # else we're getting sql type to alter a column, we don't want key / indexes |
212 # else we're getting sql type to alter a column, we don't want key / indexes |
214 # / null modifiers |
213 # / null modifiers |
215 return sqltype |
214 return sqltype |
216 |
215 |
217 |
216 |
218 def type_from_constraints(dbhelper, etype, constraints, creating=True): |
217 def type_from_rdef(dbhelper, rdef, creating=True): |
219 """return a sql type string corresponding to the constraints""" |
218 """return a sql type string corresponding to the relation definition""" |
220 constraints = list(constraints) |
219 constraints = list(rdef.constraints) |
221 unique, sqltype = False, None |
220 unique, sqltype = False, None |
222 if etype == 'String': |
221 if rdef.object.type == 'String': |
223 for constraint in constraints: |
222 for constraint in constraints: |
224 if isinstance(constraint, SizeConstraint): |
223 if isinstance(constraint, SizeConstraint): |
225 if constraint.max is not None: |
224 if constraint.max is not None: |
226 size_constrained_string = dbhelper.TYPE_MAPPING.get( |
225 size_constrained_string = dbhelper.TYPE_MAPPING.get( |
227 'SizeConstrainedString', 'varchar(%s)') |
226 'SizeConstrainedString', 'varchar(%s)') |
228 sqltype = size_constrained_string % constraint.max |
227 sqltype = size_constrained_string % constraint.max |
229 elif isinstance(constraint, UniqueConstraint): |
228 elif isinstance(constraint, UniqueConstraint): |
230 unique = True |
229 unique = True |
231 if sqltype is None: |
230 if sqltype is None: |
232 sqltype = dbhelper.TYPE_MAPPING[etype] |
231 sqltype = sql_type(dbhelper, rdef) |
233 if creating and unique: |
232 if creating and unique: |
234 sqltype += ' UNIQUE' |
233 sqltype += ' UNIQUE' |
|
234 return sqltype |
|
235 |
|
236 |
|
237 def sql_type(dbhelper, rdef): |
|
238 sqltype = dbhelper.TYPE_MAPPING[rdef.object] |
|
239 if callable(sqltype): |
|
240 sqltype = sqltype(rdef) |
235 return sqltype |
241 return sqltype |
236 |
242 |
237 |
243 |
238 _SQL_SCHEMA = """ |
244 _SQL_SCHEMA = """ |
239 CREATE TABLE %(table)s ( |
245 CREATE TABLE %(table)s ( |