summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/sql
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2009-10-25 21:27:08 +0000
committerMike Bayer <mike_mp@zzzcomputing.com>2009-10-25 21:27:08 +0000
commiteb9763febe58655ca0f61fa758925c56b94ece9b (patch)
tree52b93cd7ef50ae799d16fd4bc9d1c5ff5fd34e41 /lib/sqlalchemy/sql
parenta5f827b12dbceb1c6e8f8b787548b9de326fe076 (diff)
downloadsqlalchemy-eb9763febe58655ca0f61fa758925c56b94ece9b.tar.gz
- generalized Enum to issue a CHECK constraint + VARCHAR on default platform
- added native_enum=False flag to do the same on MySQL, PG, if desired
Diffstat (limited to 'lib/sqlalchemy/sql')
-rw-r--r--lib/sqlalchemy/sql/compiler.py29
-rw-r--r--lib/sqlalchemy/sql/expression.py6
2 files changed, 26 insertions, 9 deletions
diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py
index c1b421843..088ca1969 100644
--- a/lib/sqlalchemy/sql/compiler.py
+++ b/lib/sqlalchemy/sql/compiler.py
@@ -964,7 +964,10 @@ class DDLCompiler(engine.Compiled):
for column in table.columns:
text += separator
separator = ", \n"
- text += "\t" + self.get_column_specification(column, first_pk=column.primary_key and not first_pk)
+ text += "\t" + self.get_column_specification(
+ column,
+ first_pk=column.primary_key and not first_pk
+ )
if column.primary_key:
first_pk = True
const = " ".join(self.process(constraint) for constraint in column.constraints)
@@ -976,15 +979,18 @@ class DDLCompiler(engine.Compiled):
if table.primary_key:
text += ", \n\t" + self.process(table.primary_key)
- const = ", \n\t".join(
- self.process(constraint) for constraint in table.constraints
+ 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 (not self.dialect.supports_alter or not getattr(constraint, 'use_alter', False))
+ and (
+ not self.dialect.supports_alter or
+ not getattr(constraint, 'use_alter', False)
+ )) if p is not None
)
if const:
text += ", \n\t" + const
-
+
text += "\n)%s\n\n" % self.post_create_table(table)
return text
@@ -1121,6 +1127,17 @@ 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:
@@ -1247,7 +1264,7 @@ class GenericTypeCompiler(engine.TypeCompiler):
return self.visit_TEXT(type_)
def visit_enum(self, type_):
- raise NotImplementedError("Enum not supported generically")
+ return self.visit_VARCHAR(type_)
def visit_null(self, type_):
raise NotImplementedError("Can't generate DDL for the null type")
diff --git a/lib/sqlalchemy/sql/expression.py b/lib/sqlalchemy/sql/expression.py
index b71c1892b..9324ed6a0 100644
--- a/lib/sqlalchemy/sql/expression.py
+++ b/lib/sqlalchemy/sql/expression.py
@@ -29,12 +29,12 @@ to stay the same in future releases.
import itertools, re
from operator import attrgetter
-from sqlalchemy import util, exc, types as sqltypes
+from sqlalchemy import util, exc #, types as sqltypes
from sqlalchemy.sql import operators
from sqlalchemy.sql.visitors import Visitable, cloned_traverse
import operator
-functions, schema, sql_util = None, None, None
+functions, schema, sql_util, sqltypes = None, None, None, None
DefaultDialect, ClauseAdapter, Annotated = None, None, None
__all__ = [
@@ -3071,7 +3071,7 @@ class TableClause(_Immutable, FromClause):
__visit_name__ = 'table'
named_with_column = True
-
+
def __init__(self, name, *columns):
super(TableClause, self).__init__()
self.name = self.fullname = name