oracle.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. from sqlalchemy.ext.compiler import compiles
  2. from .impl import DefaultImpl
  3. from .base import alter_table, AddColumn, ColumnName, \
  4. format_column_name, ColumnNullable, \
  5. format_server_default, ColumnDefault, format_type, ColumnType
  6. class OracleImpl(DefaultImpl):
  7. __dialect__ = 'oracle'
  8. transactional_ddl = False
  9. batch_separator = "/"
  10. command_terminator = ""
  11. def __init__(self, *arg, **kw):
  12. super(OracleImpl, self).__init__(*arg, **kw)
  13. self.batch_separator = self.context_opts.get(
  14. "oracle_batch_separator",
  15. self.batch_separator)
  16. def _exec(self, construct, *args, **kw):
  17. result = super(OracleImpl, self)._exec(construct, *args, **kw)
  18. if self.as_sql and self.batch_separator:
  19. self.static_output(self.batch_separator)
  20. return result
  21. def emit_begin(self):
  22. self._exec("SET TRANSACTION READ WRITE")
  23. def emit_commit(self):
  24. self._exec("COMMIT")
  25. @compiles(AddColumn, 'oracle')
  26. def visit_add_column(element, compiler, **kw):
  27. return "%s %s" % (
  28. alter_table(compiler, element.table_name, element.schema),
  29. add_column(compiler, element.column, **kw),
  30. )
  31. @compiles(ColumnNullable, 'oracle')
  32. def visit_column_nullable(element, compiler, **kw):
  33. return "%s %s %s" % (
  34. alter_table(compiler, element.table_name, element.schema),
  35. alter_column(compiler, element.column_name),
  36. "NULL" if element.nullable else "NOT NULL"
  37. )
  38. @compiles(ColumnType, 'oracle')
  39. def visit_column_type(element, compiler, **kw):
  40. return "%s %s %s" % (
  41. alter_table(compiler, element.table_name, element.schema),
  42. alter_column(compiler, element.column_name),
  43. "%s" % format_type(compiler, element.type_)
  44. )
  45. @compiles(ColumnName, 'oracle')
  46. def visit_column_name(element, compiler, **kw):
  47. return "%s RENAME COLUMN %s TO %s" % (
  48. alter_table(compiler, element.table_name, element.schema),
  49. format_column_name(compiler, element.column_name),
  50. format_column_name(compiler, element.newname)
  51. )
  52. @compiles(ColumnDefault, 'oracle')
  53. def visit_column_default(element, compiler, **kw):
  54. return "%s %s %s" % (
  55. alter_table(compiler, element.table_name, element.schema),
  56. alter_column(compiler, element.column_name),
  57. "DEFAULT %s" %
  58. format_server_default(compiler, element.default)
  59. if element.default is not None
  60. else "DEFAULT NULL"
  61. )
  62. def alter_column(compiler, name):
  63. return 'MODIFY %s' % format_column_name(compiler, name)
  64. def add_column(compiler, column, **kw):
  65. return "ADD %s" % compiler.get_column_specification(column, **kw)