diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2015-03-10 14:12:58 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2015-03-10 14:12:58 -0400 |
commit | ddf35e2f04c592256762d5f79ee9e13e4d0b5818 (patch) | |
tree | f8cd1d2d9270c652dfd85f7dfd694cfeeafdda69 | |
parent | 26d598348936dd70d87591bf85187ff0ee285aaa (diff) | |
parent | 30ce7e93f4068b00ced0db785fdd578dc8a45975 (diff) | |
download | sqlalchemy-ddf35e2f04c592256762d5f79ee9e13e4d0b5818.tar.gz |
Merge branch 'sqlite-partial-indexes' of https://bitbucket.org/groner/sqlalchemy into pr42
-rw-r--r-- | lib/sqlalchemy/dialects/sqlite/base.py | 18 | ||||
-rw-r--r-- | test/dialect/test_sqlite.py | 23 |
2 files changed, 38 insertions, 3 deletions
diff --git a/lib/sqlalchemy/dialects/sqlite/base.py b/lib/sqlalchemy/dialects/sqlite/base.py index 1ed89bacb..437a7794a 100644 --- a/lib/sqlalchemy/dialects/sqlite/base.py +++ b/lib/sqlalchemy/dialects/sqlite/base.py @@ -714,9 +714,20 @@ class SQLiteDDLCompiler(compiler.DDLCompiler): return preparer.format_table(table, use_schema=False) def visit_create_index(self, create): - return super(SQLiteDDLCompiler, self).visit_create_index( + index = create.element + + text = super(SQLiteDDLCompiler, self).visit_create_index( create, include_table_schema=False) + whereclause = index.dialect_options["sqlite"]["where"] + if whereclause is not None: + where_compiled = self.sql_compiler.process( + whereclause, include_table=False, + literal_binds=True) + text += " WHERE " + where_compiled + + return text + class SQLiteTypeCompiler(compiler.GenericTypeCompiler): def visit_large_binary(self, type_, **kw): @@ -823,7 +834,10 @@ class SQLiteDialect(default.DefaultDialect): construct_arguments = [ (sa_schema.Table, { "autoincrement": False - }) + }), + (sa_schema.Index, { + "where": None, + }), ] _broken_fk_pragma_quotes = False diff --git a/test/dialect/test_sqlite.py b/test/dialect/test_sqlite.py index 44e4eda42..17920c127 100644 --- a/test/dialect/test_sqlite.py +++ b/test/dialect/test_sqlite.py @@ -8,7 +8,7 @@ from sqlalchemy.testing import eq_, assert_raises, \ assert_raises_message, is_ from sqlalchemy import Table, select, bindparam, Column,\ MetaData, func, extract, ForeignKey, text, DefaultClause, and_, \ - create_engine, UniqueConstraint + create_engine, UniqueConstraint, Index from sqlalchemy.types import Integer, String, Boolean, DateTime, Date, Time from sqlalchemy import types as sqltypes from sqlalchemy import event, inspect @@ -732,6 +732,27 @@ class SQLTest(fixtures.TestBase, AssertsCompiledSQL): ")" ) + def test_create_partial_index(self): + m = MetaData() + tbl = Table('testtbl', m, Column('data', Integer)) + idx = Index('test_idx1', tbl.c.data, + sqlite_where=and_(tbl.c.data > 5, tbl.c.data < 10)) + + # test quoting and all that + + idx2 = Index('test_idx2', tbl.c.data, + sqlite_where=and_(tbl.c.data > 'a', tbl.c.data + < "b's")) + self.assert_compile(schema.CreateIndex(idx), + 'CREATE INDEX test_idx1 ON testtbl (data) ' + 'WHERE data > 5 AND data < 10', + dialect=sqlite.dialect()) + self.assert_compile(schema.CreateIndex(idx2), + "CREATE INDEX test_idx2 ON testtbl (data) " + "WHERE data > 'a' AND data < 'b''s'", + dialect=sqlite.dialect()) + + class InsertTest(fixtures.TestBase, AssertsExecutionResults): |