From f9cb6f5834fb1acf4460fd9bb6b72f8c76f8c36c Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sun, 6 Dec 2009 19:51:10 +0000 Subject: - reworked the DDL generation of ENUM and similar to be more platform agnostic. Uses a straight CheckConstraint with a generic expression. Preparing for boolean constraint in [ticket:1589] - CheckConstraint now accepts SQL expressions, though support for quoting of values will be very limited. we don't want to get into formatting dates and such. --- lib/sqlalchemy/sql/compiler.py | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) (limited to 'lib/sqlalchemy/sql/compiler.py') diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index 6802bfbef..a41a149d1 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -922,6 +922,11 @@ class SQLCompiler(engine.Compiled): class DDLCompiler(engine.Compiled): + + @util.memoized_property + def sql_compiler(self): + return self.dialect.statement_compiler(self.dialect, self.statement) + @property def preparer(self): return self.dialect.identifier_preparer @@ -982,7 +987,9 @@ class DDLCompiler(engine.Compiled): const = ", \n\t".join(p for p in (self.process(constraint) for constraint in table.constraints if constraint is not table.primary_key - and constraint.inline_ddl + and ( + constraint._create_rule is None or + constraint._create_rule(self)) and ( not self.dialect.supports_alter or not getattr(constraint, 'use_alter', False) @@ -1058,13 +1065,6 @@ class DDLCompiler(engine.Compiled): def post_create_table(self, table): return '' - def _compile(self, tocompile, parameters): - """compile the given string/parameters using this SchemaGenerator's dialect.""" - - compiler = self.dialect.statement_compiler(self.dialect, tocompile, parameters) - compiler.compile() - return compiler - def _validate_identifier(self, ident, truncate): if truncate: if len(ident) > self.dialect.max_identifier_length: @@ -1082,7 +1082,7 @@ class DDLCompiler(engine.Compiled): if isinstance(column.server_default.arg, basestring): return "'%s'" % column.server_default.arg else: - return unicode(self._compile(column.server_default.arg, None)) + return self.sql_compiler.process(column.server_default.arg) else: return None @@ -1091,7 +1091,8 @@ class DDLCompiler(engine.Compiled): if constraint.name is not None: text += "CONSTRAINT %s " % \ self.preparer.format_constraint(constraint) - text += " CHECK (%s)" % constraint.sqltext + sqltext = sql_util.expression_as_ddl(constraint.sqltext) + text += "CHECK (%s)" % self.sql_compiler.process(sqltext) text += self.define_constraint_deferrability(constraint) return text @@ -1138,17 +1139,6 @@ class DDLCompiler(engine.Compiled): text += self.define_constraint_deferrability(constraint) return text - def visit_enum_constraint(self, constraint): - text = "" - if constraint.name is not None: - text += "CONSTRAINT %s " % \ - self.preparer.format_constraint(constraint) - text += " CHECK (%s IN (%s))" % ( - self.preparer.format_column(constraint.column), - ",".join("'%s'" % x for x in constraint.type.enums) - ) - return text - def define_constraint_cascades(self, constraint): text = "" if constraint.ondelete is not None: -- cgit v1.2.1