sqlite.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. from .. import util
  2. from .impl import DefaultImpl
  3. import re
  4. class SQLiteImpl(DefaultImpl):
  5. __dialect__ = 'sqlite'
  6. transactional_ddl = False
  7. """SQLite supports transactional DDL, but pysqlite does not:
  8. see: http://bugs.python.org/issue10740
  9. """
  10. def requires_recreate_in_batch(self, batch_op):
  11. """Return True if the given :class:`.BatchOperationsImpl`
  12. would need the table to be recreated and copied in order to
  13. proceed.
  14. Normally, only returns True on SQLite when operations other
  15. than add_column are present.
  16. """
  17. for op in batch_op.batch:
  18. if op[0] not in ('add_column', 'create_index', 'drop_index'):
  19. return True
  20. else:
  21. return False
  22. def add_constraint(self, const):
  23. # attempt to distinguish between an
  24. # auto-gen constraint and an explicit one
  25. if const._create_rule is None:
  26. raise NotImplementedError(
  27. "No support for ALTER of constraints in SQLite dialect")
  28. elif const._create_rule(self):
  29. util.warn("Skipping unsupported ALTER for "
  30. "creation of implicit constraint")
  31. def drop_constraint(self, const):
  32. if const._create_rule is None:
  33. raise NotImplementedError(
  34. "No support for ALTER of constraints in SQLite dialect")
  35. def compare_server_default(self, inspector_column,
  36. metadata_column,
  37. rendered_metadata_default,
  38. rendered_inspector_default):
  39. if rendered_metadata_default is not None:
  40. rendered_metadata_default = re.sub(
  41. r"^\"'|\"'$", "", rendered_metadata_default)
  42. if rendered_inspector_default is not None:
  43. rendered_inspector_default = re.sub(
  44. r"^\"'|\"'$", "", rendered_inspector_default)
  45. return rendered_inspector_default != rendered_metadata_default
  46. def correct_for_autogen_constraints(
  47. self, conn_unique_constraints, conn_indexes,
  48. metadata_unique_constraints,
  49. metadata_indexes):
  50. if util.sqla_100:
  51. return
  52. # adjustments to accommodate for SQLite unnamed unique constraints
  53. # not being reported from the backend; this was updated in
  54. # SQLA 1.0.
  55. def uq_sig(uq):
  56. return tuple(sorted(uq.columns.keys()))
  57. conn_unique_sigs = set(
  58. uq_sig(uq)
  59. for uq in conn_unique_constraints
  60. )
  61. for idx in list(metadata_unique_constraints):
  62. # SQLite backend can't report on unnamed UNIQUE constraints,
  63. # so remove these, unless we see an exact signature match
  64. if idx.name is None and uq_sig(idx) not in conn_unique_sigs:
  65. metadata_unique_constraints.remove(idx)
  66. # @compiles(AddColumn, 'sqlite')
  67. # def visit_add_column(element, compiler, **kw):
  68. # return "%s %s" % (
  69. # alter_table(compiler, element.table_name, element.schema),
  70. # add_column(compiler, element.column, **kw)
  71. # )
  72. # def add_column(compiler, column, **kw):
  73. # text = "ADD COLUMN %s" % compiler.get_column_specification(column, **kw)
  74. # need to modify SQLAlchemy so that the CHECK associated with a Boolean
  75. # or Enum gets placed as part of the column constraints, not the Table
  76. # see ticket 98
  77. # for const in column.constraints:
  78. # text += compiler.process(AddConstraint(const))
  79. # return text