meta.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # Licensed to the Apache Software Foundation (ASF) under one or more
  2. # contributor license agreements. See the NOTICE file distributed with
  3. # this work for additional information regarding copyright ownership.
  4. # The ASF licenses this file to You under the Apache License, Version 2.0
  5. # (the "License"); you may not use this file except in compliance with
  6. # the License. You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import sys
  16. import logging
  17. from phoenixdb.errors import ProgrammingError
  18. from phoenixdb.cursor import DictCursor
  19. __all__ = ['Meta']
  20. logger = logging.getLogger(__name__)
  21. class Meta(object):
  22. """Database meta for querying MetaData
  23. """
  24. def __init__(self, connection):
  25. self._connection = connection
  26. def get_catalogs(self):
  27. if self._connection._closed:
  28. raise ProgrammingError('The connection is already closed.')
  29. result = self._connection._client.get_catalogs(self._connection._id)
  30. with DictCursor(self._connection) as cursor:
  31. cursor._process_result(result)
  32. return cursor.fetchall()
  33. def get_schemas(self, catalog=None, schemaPattern=None):
  34. if self._connection._closed:
  35. raise ProgrammingError('The connection is already closed.')
  36. result = self._connection._client.get_schemas(self._connection._id, catalog, schemaPattern)
  37. with DictCursor(self._connection) as cursor:
  38. cursor._process_result(result)
  39. return self._fix_default(cursor.fetchall(), schemaPattern=schemaPattern)
  40. def get_tables(self, catalog=None, schemaPattern=None, tableNamePattern=None, typeList=None):
  41. if self._connection._closed:
  42. raise ProgrammingError('The connection is already closed.')
  43. result = self._connection._client.get_tables(
  44. self._connection._id, catalog, schemaPattern, tableNamePattern, typeList=typeList)
  45. with DictCursor(self._connection) as cursor:
  46. cursor._process_result(result)
  47. return self._fix_default(cursor.fetchall(), catalog, schemaPattern)
  48. def get_columns(self, catalog=None, schemaPattern=None, tableNamePattern=None,
  49. columnNamePattern=None):
  50. if self._connection._closed:
  51. raise ProgrammingError('The connection is already closed.')
  52. result = self._connection._client.get_columns(
  53. self._connection._id, catalog, schemaPattern, tableNamePattern, columnNamePattern)
  54. with DictCursor(self._connection) as cursor:
  55. cursor._process_result(result)
  56. return self._fix_default(cursor.fetchall(), catalog, schemaPattern)
  57. def get_table_types(self):
  58. if self._connection._closed:
  59. raise ProgrammingError('The connection is already closed.')
  60. result = self._connection._client.get_table_types(self._connection._id)
  61. with DictCursor(self._connection) as cursor:
  62. cursor._process_result(result)
  63. return cursor.fetchall()
  64. def get_type_info(self):
  65. if self._connection._closed:
  66. raise ProgrammingError('The connection is already closed.')
  67. result = self._connection._client.get_type_info(self._connection._id)
  68. with DictCursor(self._connection) as cursor:
  69. cursor._process_result(result)
  70. return cursor.fetchall()
  71. def _fix_default(self, rows, catalog=None, schemaPattern=None):
  72. '''Workaround for PHOENIX-6003'''
  73. if schemaPattern == '':
  74. rows = [row for row in rows if row['TABLE_SCHEM'] is None]
  75. if catalog == '':
  76. rows = [row for row in rows if row['TABLE_CATALOG'] is None]
  77. # Couldn't find a sane way to do it that works on 2 and 3
  78. if sys.version_info.major == 3:
  79. return [{k: v or '' for k, v in row.items()} for row in rows]
  80. else:
  81. return [{k: v or '' for k, v in row.iteritems()} for row in rows]