information_schema.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # mssql/information_schema.py
  2. # Copyright (C) 2005-2017 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. # TODO: should be using the sys. catalog with SQL Server, not information
  8. # schema
  9. from ... import Table, MetaData, Column
  10. from ...types import String, Unicode, UnicodeText, Integer, TypeDecorator
  11. from ... import cast
  12. from ... import util
  13. from ...sql import expression
  14. from ...ext.compiler import compiles
  15. ischema = MetaData()
  16. class CoerceUnicode(TypeDecorator):
  17. impl = Unicode
  18. def process_bind_param(self, value, dialect):
  19. if util.py2k and isinstance(value, util.binary_type):
  20. value = value.decode(dialect.encoding)
  21. return value
  22. def bind_expression(self, bindvalue):
  23. return _cast_on_2005(bindvalue)
  24. class _cast_on_2005(expression.ColumnElement):
  25. def __init__(self, bindvalue):
  26. self.bindvalue = bindvalue
  27. @compiles(_cast_on_2005)
  28. def _compile(element, compiler, **kw):
  29. from . import base
  30. if compiler.dialect.server_version_info < base.MS_2005_VERSION:
  31. return compiler.process(element.bindvalue, **kw)
  32. else:
  33. return compiler.process(cast(element.bindvalue, Unicode), **kw)
  34. schemata = Table("SCHEMATA", ischema,
  35. Column("CATALOG_NAME", CoerceUnicode, key="catalog_name"),
  36. Column("SCHEMA_NAME", CoerceUnicode, key="schema_name"),
  37. Column("SCHEMA_OWNER", CoerceUnicode, key="schema_owner"),
  38. schema="INFORMATION_SCHEMA")
  39. tables = Table("TABLES", ischema,
  40. Column("TABLE_CATALOG", CoerceUnicode, key="table_catalog"),
  41. Column("TABLE_SCHEMA", CoerceUnicode, key="table_schema"),
  42. Column("TABLE_NAME", CoerceUnicode, key="table_name"),
  43. Column(
  44. "TABLE_TYPE", String(convert_unicode=True),
  45. key="table_type"),
  46. schema="INFORMATION_SCHEMA")
  47. columns = Table("COLUMNS", ischema,
  48. Column("TABLE_SCHEMA", CoerceUnicode, key="table_schema"),
  49. Column("TABLE_NAME", CoerceUnicode, key="table_name"),
  50. Column("COLUMN_NAME", CoerceUnicode, key="column_name"),
  51. Column("IS_NULLABLE", Integer, key="is_nullable"),
  52. Column("DATA_TYPE", String, key="data_type"),
  53. Column("ORDINAL_POSITION", Integer, key="ordinal_position"),
  54. Column("CHARACTER_MAXIMUM_LENGTH", Integer,
  55. key="character_maximum_length"),
  56. Column("NUMERIC_PRECISION", Integer, key="numeric_precision"),
  57. Column("NUMERIC_SCALE", Integer, key="numeric_scale"),
  58. Column("COLUMN_DEFAULT", Integer, key="column_default"),
  59. Column("COLLATION_NAME", String, key="collation_name"),
  60. schema="INFORMATION_SCHEMA")
  61. constraints = Table("TABLE_CONSTRAINTS", ischema,
  62. Column("TABLE_SCHEMA", CoerceUnicode, key="table_schema"),
  63. Column("TABLE_NAME", CoerceUnicode, key="table_name"),
  64. Column("CONSTRAINT_NAME", CoerceUnicode,
  65. key="constraint_name"),
  66. Column("CONSTRAINT_TYPE", String(
  67. convert_unicode=True), key="constraint_type"),
  68. schema="INFORMATION_SCHEMA")
  69. column_constraints = Table("CONSTRAINT_COLUMN_USAGE", ischema,
  70. Column("TABLE_SCHEMA", CoerceUnicode,
  71. key="table_schema"),
  72. Column("TABLE_NAME", CoerceUnicode,
  73. key="table_name"),
  74. Column("COLUMN_NAME", CoerceUnicode,
  75. key="column_name"),
  76. Column("CONSTRAINT_NAME", CoerceUnicode,
  77. key="constraint_name"),
  78. schema="INFORMATION_SCHEMA")
  79. key_constraints = Table("KEY_COLUMN_USAGE", ischema,
  80. Column("TABLE_SCHEMA", CoerceUnicode,
  81. key="table_schema"),
  82. Column("TABLE_NAME", CoerceUnicode,
  83. key="table_name"),
  84. Column("COLUMN_NAME", CoerceUnicode,
  85. key="column_name"),
  86. Column("CONSTRAINT_NAME", CoerceUnicode,
  87. key="constraint_name"),
  88. Column("ORDINAL_POSITION", Integer,
  89. key="ordinal_position"),
  90. schema="INFORMATION_SCHEMA")
  91. ref_constraints = Table("REFERENTIAL_CONSTRAINTS", ischema,
  92. Column("CONSTRAINT_CATALOG", CoerceUnicode,
  93. key="constraint_catalog"),
  94. Column("CONSTRAINT_SCHEMA", CoerceUnicode,
  95. key="constraint_schema"),
  96. Column("CONSTRAINT_NAME", CoerceUnicode,
  97. key="constraint_name"),
  98. # TODO: is CATLOG misspelled ?
  99. Column("UNIQUE_CONSTRAINT_CATLOG", CoerceUnicode,
  100. key="unique_constraint_catalog"),
  101. Column("UNIQUE_CONSTRAINT_SCHEMA", CoerceUnicode,
  102. key="unique_constraint_schema"),
  103. Column("UNIQUE_CONSTRAINT_NAME", CoerceUnicode,
  104. key="unique_constraint_name"),
  105. Column("MATCH_OPTION", String, key="match_option"),
  106. Column("UPDATE_RULE", String, key="update_rule"),
  107. Column("DELETE_RULE", String, key="delete_rule"),
  108. schema="INFORMATION_SCHEMA")
  109. views = Table("VIEWS", ischema,
  110. Column("TABLE_CATALOG", CoerceUnicode, key="table_catalog"),
  111. Column("TABLE_SCHEMA", CoerceUnicode, key="table_schema"),
  112. Column("TABLE_NAME", CoerceUnicode, key="table_name"),
  113. Column("VIEW_DEFINITION", CoerceUnicode, key="view_definition"),
  114. Column("CHECK_OPTION", String, key="check_option"),
  115. Column("IS_UPDATABLE", String, key="is_updatable"),
  116. schema="INFORMATION_SCHEMA")