diff options
Diffstat (limited to 'lib/sqlalchemy/dialects/mysql/base.py')
| -rw-r--r-- | lib/sqlalchemy/dialects/mysql/base.py | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/lib/sqlalchemy/dialects/mysql/base.py b/lib/sqlalchemy/dialects/mysql/base.py index 80067f9dc..1a30e15fd 100644 --- a/lib/sqlalchemy/dialects/mysql/base.py +++ b/lib/sqlalchemy/dialects/mysql/base.py @@ -197,6 +197,32 @@ within an application, overrides the compilation of the fully rendering CAST; else the internal element of the construct is rendered directly. + +.. _mysql_indexes: + +MySQL Specific Index Options +---------------------------- + +MySQL-specific extensions to the :class:`.Index` construct are available. + +Index Length +~~~~~~~~~~~~~ + +MySQL provides an option to create index entries with a certain length, where +"length" refers to the number of characters or bytes in each value which will +become part of the index. SQLAlchemy provides this feature via the +``mysql_length`` parameter:: + + Index('my_index', my_table.c.data, mysql_length=10) + +Prefix lengths are given in characters for nonbinary string types and in bytes +for binary string types. The value passed to the keyword argument will be +simply passed through to the underlying CREATE INDEX command, so it *must* be +an integer. MySQL only allows a length for an index if it is for a CHAR, +VARCHAR, TEXT, BINARY, VARBINARY and BLOB. + +More information can be found at: +http://dev.mysql.com/doc/refman/5.0/en/create-index.html """ import datetime, inspect, re, sys @@ -1395,6 +1421,30 @@ class MySQLDDLCompiler(compiler.DDLCompiler): table_opts.append(joiner.join((opt, arg))) return ' '.join(table_opts) + def visit_create_index(self, create): + index = create.element + preparer = self.preparer + text = "CREATE " + if index.unique: + text += "UNIQUE " + text += "INDEX %s ON %s " \ + % (preparer.quote(self._index_identifier(index.name), + index.quote),preparer.format_table(index.table)) + if 'mysql_length' in index.kwargs: + length = index.kwargs['mysql_length'] + else: + length = None + if length is not None: + text+= "(%s(%d))" \ + % (', '.join(preparer.quote(c.name, c.quote) + for c in index.columns), length) + else: + text+= "(%s)" \ + % (', '.join(preparer.quote(c.name, c.quote) + for c in index.columns)) + return text + + def visit_drop_index(self, drop): index = drop.element |
