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

1# mssql/information_schema.py 

2# Copyright (C) 2005-2020 the SQLAlchemy authors and contributors 

3# <see AUTHORS file> 

4# 

5# This module is part of SQLAlchemy and is released under 

6# the MIT License: http://www.opensource.org/licenses/mit-license.php 

7 

8# TODO: should be using the sys. catalog with SQL Server, not information 

9# schema 

10 

11from ... import cast 

12from ... import Column 

13from ... import MetaData 

14from ... import Table 

15from ... import util 

16from ...ext.compiler import compiles 

17from ...sql import expression 

18from ...types import Boolean 

19from ...types import Integer 

20from ...types import String 

21from ...types import TypeDecorator 

22from ...types import Unicode 

23 

24 

25ischema = MetaData() 

26 

27 

28class CoerceUnicode(TypeDecorator): 

29 impl = Unicode 

30 

31 def process_bind_param(self, value, dialect): 

32 if util.py2k and isinstance(value, util.binary_type): 

33 value = value.decode(dialect.encoding) 

34 return value 

35 

36 def bind_expression(self, bindvalue): 

37 return _cast_on_2005(bindvalue) 

38 

39 

40class _cast_on_2005(expression.ColumnElement): 

41 def __init__(self, bindvalue): 

42 self.bindvalue = bindvalue 

43 

44 

45@compiles(_cast_on_2005) 

46def _compile(element, compiler, **kw): 

47 from . import base 

48 

49 if ( 

50 compiler.dialect.server_version_info is None 

51 or compiler.dialect.server_version_info < base.MS_2005_VERSION 

52 ): 

53 return compiler.process(element.bindvalue, **kw) 

54 else: 

55 return compiler.process(cast(element.bindvalue, Unicode), **kw) 

56 

57 

58schemata = Table( 

59 "SCHEMATA", 

60 ischema, 

61 Column("CATALOG_NAME", CoerceUnicode, key="catalog_name"), 

62 Column("SCHEMA_NAME", CoerceUnicode, key="schema_name"), 

63 Column("SCHEMA_OWNER", CoerceUnicode, key="schema_owner"), 

64 schema="INFORMATION_SCHEMA", 

65) 

66 

67tables = Table( 

68 "TABLES", 

69 ischema, 

70 Column("TABLE_CATALOG", CoerceUnicode, key="table_catalog"), 

71 Column("TABLE_SCHEMA", CoerceUnicode, key="table_schema"), 

72 Column("TABLE_NAME", CoerceUnicode, key="table_name"), 

73 Column("TABLE_TYPE", CoerceUnicode, key="table_type"), 

74 schema="INFORMATION_SCHEMA", 

75) 

76 

77columns = Table( 

78 "COLUMNS", 

79 ischema, 

80 Column("TABLE_SCHEMA", CoerceUnicode, key="table_schema"), 

81 Column("TABLE_NAME", CoerceUnicode, key="table_name"), 

82 Column("COLUMN_NAME", CoerceUnicode, key="column_name"), 

83 Column("IS_NULLABLE", Integer, key="is_nullable"), 

84 Column("DATA_TYPE", String, key="data_type"), 

85 Column("ORDINAL_POSITION", Integer, key="ordinal_position"), 

86 Column( 

87 "CHARACTER_MAXIMUM_LENGTH", Integer, key="character_maximum_length" 

88 ), 

89 Column("NUMERIC_PRECISION", Integer, key="numeric_precision"), 

90 Column("NUMERIC_SCALE", Integer, key="numeric_scale"), 

91 Column("COLUMN_DEFAULT", Integer, key="column_default"), 

92 Column("COLLATION_NAME", String, key="collation_name"), 

93 schema="INFORMATION_SCHEMA", 

94) 

95 

96constraints = Table( 

97 "TABLE_CONSTRAINTS", 

98 ischema, 

99 Column("TABLE_SCHEMA", CoerceUnicode, key="table_schema"), 

100 Column("TABLE_NAME", CoerceUnicode, key="table_name"), 

101 Column("CONSTRAINT_NAME", CoerceUnicode, key="constraint_name"), 

102 Column("CONSTRAINT_TYPE", CoerceUnicode, key="constraint_type"), 

103 schema="INFORMATION_SCHEMA", 

104) 

105 

106column_constraints = Table( 

107 "CONSTRAINT_COLUMN_USAGE", 

108 ischema, 

109 Column("TABLE_SCHEMA", CoerceUnicode, key="table_schema"), 

110 Column("TABLE_NAME", CoerceUnicode, key="table_name"), 

111 Column("COLUMN_NAME", CoerceUnicode, key="column_name"), 

112 Column("CONSTRAINT_NAME", CoerceUnicode, key="constraint_name"), 

113 schema="INFORMATION_SCHEMA", 

114) 

115 

116key_constraints = Table( 

117 "KEY_COLUMN_USAGE", 

118 ischema, 

119 Column("TABLE_SCHEMA", CoerceUnicode, key="table_schema"), 

120 Column("TABLE_NAME", CoerceUnicode, key="table_name"), 

121 Column("COLUMN_NAME", CoerceUnicode, key="column_name"), 

122 Column("CONSTRAINT_NAME", CoerceUnicode, key="constraint_name"), 

123 Column("CONSTRAINT_SCHEMA", CoerceUnicode, key="constraint_schema"), 

124 Column("ORDINAL_POSITION", Integer, key="ordinal_position"), 

125 schema="INFORMATION_SCHEMA", 

126) 

127 

128ref_constraints = Table( 

129 "REFERENTIAL_CONSTRAINTS", 

130 ischema, 

131 Column("CONSTRAINT_CATALOG", CoerceUnicode, key="constraint_catalog"), 

132 Column("CONSTRAINT_SCHEMA", CoerceUnicode, key="constraint_schema"), 

133 Column("CONSTRAINT_NAME", CoerceUnicode, key="constraint_name"), 

134 # TODO: is CATLOG misspelled ? 

135 Column( 

136 "UNIQUE_CONSTRAINT_CATLOG", 

137 CoerceUnicode, 

138 key="unique_constraint_catalog", 

139 ), 

140 Column( 

141 "UNIQUE_CONSTRAINT_SCHEMA", 

142 CoerceUnicode, 

143 key="unique_constraint_schema", 

144 ), 

145 Column( 

146 "UNIQUE_CONSTRAINT_NAME", CoerceUnicode, key="unique_constraint_name" 

147 ), 

148 Column("MATCH_OPTION", String, key="match_option"), 

149 Column("UPDATE_RULE", String, key="update_rule"), 

150 Column("DELETE_RULE", String, key="delete_rule"), 

151 schema="INFORMATION_SCHEMA", 

152) 

153 

154views = Table( 

155 "VIEWS", 

156 ischema, 

157 Column("TABLE_CATALOG", CoerceUnicode, key="table_catalog"), 

158 Column("TABLE_SCHEMA", CoerceUnicode, key="table_schema"), 

159 Column("TABLE_NAME", CoerceUnicode, key="table_name"), 

160 Column("VIEW_DEFINITION", CoerceUnicode, key="view_definition"), 

161 Column("CHECK_OPTION", String, key="check_option"), 

162 Column("IS_UPDATABLE", String, key="is_updatable"), 

163 schema="INFORMATION_SCHEMA", 

164) 

165 

166computed_columns = Table( 

167 "computed_columns", 

168 ischema, 

169 Column("object_id", Integer), 

170 Column("name", CoerceUnicode), 

171 Column("is_computed", Boolean), 

172 Column("is_persisted", Boolean), 

173 Column("definition", CoerceUnicode), 

174 schema="sys", 

175)