Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1from sqlalchemy.ext.compiler import compiles 

2from sqlalchemy.sql import sqltypes 

3 

4from .base import AddColumn 

5from .base import alter_table 

6from .base import ColumnComment 

7from .base import ColumnDefault 

8from .base import ColumnName 

9from .base import ColumnNullable 

10from .base import ColumnType 

11from .base import format_column_name 

12from .base import format_server_default 

13from .base import format_table_name 

14from .base import format_type 

15from .base import RenameTable 

16from .impl import DefaultImpl 

17 

18 

19class OracleImpl(DefaultImpl): 

20 __dialect__ = "oracle" 

21 transactional_ddl = False 

22 batch_separator = "/" 

23 command_terminator = "" 

24 type_synonyms = DefaultImpl.type_synonyms + ( 

25 {"VARCHAR", "VARCHAR2"}, 

26 {"BIGINT", "INTEGER", "SMALLINT", "DECIMAL", "NUMERIC", "NUMBER"}, 

27 ) 

28 

29 def __init__(self, *arg, **kw): 

30 super(OracleImpl, self).__init__(*arg, **kw) 

31 self.batch_separator = self.context_opts.get( 

32 "oracle_batch_separator", self.batch_separator 

33 ) 

34 

35 def _exec(self, construct, *args, **kw): 

36 result = super(OracleImpl, self)._exec(construct, *args, **kw) 

37 if self.as_sql and self.batch_separator: 

38 self.static_output(self.batch_separator) 

39 return result 

40 

41 def emit_begin(self): 

42 self._exec("SET TRANSACTION READ WRITE") 

43 

44 def emit_commit(self): 

45 self._exec("COMMIT") 

46 

47 

48@compiles(AddColumn, "oracle") 

49def visit_add_column(element, compiler, **kw): 

50 return "%s %s" % ( 

51 alter_table(compiler, element.table_name, element.schema), 

52 add_column(compiler, element.column, **kw), 

53 ) 

54 

55 

56@compiles(ColumnNullable, "oracle") 

57def visit_column_nullable(element, compiler, **kw): 

58 return "%s %s %s" % ( 

59 alter_table(compiler, element.table_name, element.schema), 

60 alter_column(compiler, element.column_name), 

61 "NULL" if element.nullable else "NOT NULL", 

62 ) 

63 

64 

65@compiles(ColumnType, "oracle") 

66def visit_column_type(element, compiler, **kw): 

67 return "%s %s %s" % ( 

68 alter_table(compiler, element.table_name, element.schema), 

69 alter_column(compiler, element.column_name), 

70 "%s" % format_type(compiler, element.type_), 

71 ) 

72 

73 

74@compiles(ColumnName, "oracle") 

75def visit_column_name(element, compiler, **kw): 

76 return "%s RENAME COLUMN %s TO %s" % ( 

77 alter_table(compiler, element.table_name, element.schema), 

78 format_column_name(compiler, element.column_name), 

79 format_column_name(compiler, element.newname), 

80 ) 

81 

82 

83@compiles(ColumnDefault, "oracle") 

84def visit_column_default(element, compiler, **kw): 

85 return "%s %s %s" % ( 

86 alter_table(compiler, element.table_name, element.schema), 

87 alter_column(compiler, element.column_name), 

88 "DEFAULT %s" % format_server_default(compiler, element.default) 

89 if element.default is not None 

90 else "DEFAULT NULL", 

91 ) 

92 

93 

94@compiles(ColumnComment, "oracle") 

95def visit_column_comment(element, compiler, **kw): 

96 ddl = "COMMENT ON COLUMN {table_name}.{column_name} IS {comment}" 

97 

98 comment = compiler.sql_compiler.render_literal_value( 

99 (element.comment if element.comment is not None else ""), 

100 sqltypes.String(), 

101 ) 

102 

103 return ddl.format( 

104 table_name=element.table_name, 

105 column_name=element.column_name, 

106 comment=comment, 

107 ) 

108 

109 

110@compiles(RenameTable, "oracle") 

111def visit_rename_table(element, compiler, **kw): 

112 return "%s RENAME TO %s" % ( 

113 alter_table(compiler, element.table_name, element.schema), 

114 format_table_name(compiler, element.new_table_name, None), 

115 ) 

116 

117 

118def alter_column(compiler, name): 

119 return "MODIFY %s" % format_column_name(compiler, name) 

120 

121 

122def add_column(compiler, column, **kw): 

123 return "ADD %s" % compiler.get_column_specification(column, **kw)