Skip to content

Commit a8378da

Browse files
committed
Updated the logic for retrieving database metadata through the get_catalogs, get_schemas, get_tables, and get_columns API methods
1 parent 686782e commit a8378da

20 files changed

+2535
-875
lines changed

README.rst

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ redshift_connector
1010
.. |Python Version| image:: https://img.shields.io/badge/python->=3.6-brightgreen.svg
1111
:target: https://pypi.org/project/redshift_connector/
1212

13-
⚠️ Python Driver v2.1.4 has been recalled. Python Driver v2.1.3 is recommended for use instead.
14-
1513
``redshift_connector`` is the Amazon Redshift connector for
1614
Python. Easy integration with `pandas <https://github.com/pandas-dev/pandas>`_ and `numpy <https://github.com/numpy/numpy>`_, as well as support for numerous Amazon Redshift specific features help you get the most out of your data
1715

@@ -31,13 +29,13 @@ Getting Started
3129
Install from Binary
3230
~~~~~~~~~~~~~~~~~~~
3331

34-
+----------------------------------------------------------------+--------------------+--------------------------------------------------------------+
35-
| Package Manager | Downloads | Installation Command |
36-
+================================================================+====================+==============================================================+
37-
| `PyPi <https://pypi.org/project/redshift-connector/>`_ | |PyPi Downloads| | ``pip install 'redshift_connector==2.1.3'`` |
38-
+----------------------------------------------------------------+--------------------+--------------------------------------------------------------+
39-
| `Conda <https://anaconda.org/conda-forge/redshift_connector>`_ | |Conda Downloads| | ``conda install -c conda-forge 'redshift_connector==2.1.3'`` |
40-
+----------------------------------------------------------------+--------------------+--------------------------------------------------------------+
32+
+----------------------------------------------------------------+--------------------+-----------------------------------------------------+
33+
| Package Manager | Downloads | Installation Command |
34+
+================================================================+====================+=====================================================+
35+
| `PyPi <https://pypi.org/project/redshift-connector/>`_ | |PyPi Downloads| | ``pip install redshift_connector`` |
36+
+----------------------------------------------------------------+--------------------+-----------------------------------------------------+
37+
| `Conda <https://anaconda.org/conda-forge/redshift_connector>`_ | |Conda Downloads| | ``conda install -c conda-forge redshift_connector`` |
38+
+----------------------------------------------------------------+--------------------+-----------------------------------------------------+
4139

4240
.. |PyPi Downloads| image:: https://pepy.tech/badge/redshift_connector
4341
.. |Conda Downloads| image:: https://img.shields.io/conda/dn/conda-forge/redshift_connector.svg

redshift_connector/cursor.py

Lines changed: 155 additions & 145 deletions
Large diffs are not rendered by default.

redshift_connector/metadataAPIHelper.py

Lines changed: 137 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,29 @@
1616

1717

1818
class MetadataAPIHelper:
19-
def __init__(self: "MetadataAPIHelper") -> None:
20-
self._CatalogsColNum: int = 1
21-
self._get_catalogs_col: typing.Dict = {"TABLE_CAT": int(RedshiftOID.VARCHAR)}
22-
23-
self._SchemasColNum: int = 2
24-
self._get_schemas_col: typing.Dict = {"TABLE_SCHEM": int(RedshiftOID.VARCHAR),
25-
"TABLE_CATALOG": int(RedshiftOID.VARCHAR)}
26-
27-
self._TablesColNum: int = 10
28-
self._get_tables_col: typing.Dict = {"TABLE_CAT": int(RedshiftOID.VARCHAR),
29-
"TABLE_SCHEM": int(RedshiftOID.VARCHAR),
30-
"TABLE_NAME": int(RedshiftOID.VARCHAR),
31-
"TABLE_TYPE": int(RedshiftOID.VARCHAR),
32-
"REMARKS": int(RedshiftOID.VARCHAR),
33-
"TYPE_CAT": int(RedshiftOID.VARCHAR),
34-
"TYPE_SCHEM": int(RedshiftOID.VARCHAR),
35-
"TYPE_NAME": int(RedshiftOID.VARCHAR),
36-
"SELF_REFERENCING_COL_NAME": int(RedshiftOID.VARCHAR),
37-
"REF_GENERATION": int(RedshiftOID.VARCHAR)}
38-
39-
self._ColumnsColNum: int = 24
40-
self._get_columns_col: typing.Dict = {"TABLE_CAT": int(RedshiftOID.VARCHAR),
19+
_row_description_col_label_index: int = 0
20+
21+
_CatalogsColNum: int = 1
22+
_get_catalogs_col: typing.Dict = {"TABLE_CAT": int(RedshiftOID.VARCHAR)}
23+
24+
_SchemasColNum: int = 2
25+
_get_schemas_col: typing.Dict = {"TABLE_SCHEM": int(RedshiftOID.VARCHAR),
26+
"TABLE_CATALOG": int(RedshiftOID.VARCHAR)}
27+
28+
_TablesColNum: int = 10
29+
_get_tables_col: typing.Dict = {"TABLE_CAT": int(RedshiftOID.VARCHAR),
30+
"TABLE_SCHEM": int(RedshiftOID.VARCHAR),
31+
"TABLE_NAME": int(RedshiftOID.VARCHAR),
32+
"TABLE_TYPE": int(RedshiftOID.VARCHAR),
33+
"REMARKS": int(RedshiftOID.VARCHAR),
34+
"TYPE_CAT": int(RedshiftOID.VARCHAR),
35+
"TYPE_SCHEM": int(RedshiftOID.VARCHAR),
36+
"TYPE_NAME": int(RedshiftOID.VARCHAR),
37+
"SELF_REFERENCING_COL_NAME": int(RedshiftOID.VARCHAR),
38+
"REF_GENERATION": int(RedshiftOID.VARCHAR)}
39+
40+
_ColumnsColNum: int = 24
41+
_get_columns_col: typing.Dict = {"TABLE_CAT": int(RedshiftOID.VARCHAR),
4142
"TABLE_SCHEM": int(RedshiftOID.VARCHAR),
4243
"TABLE_NAME": int(RedshiftOID.VARCHAR),
4344
"COLUMN_NAME": int(RedshiftOID.VARCHAR),
@@ -62,109 +63,118 @@ def __init__(self: "MetadataAPIHelper") -> None:
6263
"IS_AUTOINCREMENT": int(RedshiftOID.VARCHAR),
6364
"IS_GENERATEDCOLUMN": int(RedshiftOID.VARCHAR)}
6465

65-
self._SHOW_DATABASES_database_name: str = 'database_name'
66-
67-
self._SHOW_SCHEMA_database_name: str = 'database_name'
68-
self._SHOW_SCHEMA_schema_name: str = 'schema_name'
69-
70-
self._SHOW_TABLES_database_name: str = 'database_name'
71-
self._SHOW_TABLES_schema_name: str = 'schema_name'
72-
self._SHOW_TABLES_table_name: str = 'table_name'
73-
self._SHOW_TABLES_table_type: str = 'table_type'
74-
self._SHOW_TABLES_remarks: str = 'remarks'
75-
76-
self._SHOW_COLUMNS_database_name: str = "database_name"
77-
self._SHOW_COLUMNS_schema_name: str = "schema_name"
78-
self._SHOW_COLUMNS_table_name: str = "table_name"
79-
self._SHOW_COLUMNS_column_name: str = "column_name"
80-
self._SHOW_COLUMNS_ordinal_position: str = "ordinal_position"
81-
self._SHOW_COLUMNS_column_default: str = "column_default"
82-
self._SHOW_COLUMNS_is_nullable: str = "is_nullable"
83-
self._SHOW_COLUMNS_data_type: str = "data_type"
84-
self._SHOW_COLUMNS_character_maximum_length: str = "character_maximum_length"
85-
self._SHOW_COLUMNS_numeric_precision: str = "numeric_precision"
86-
self._SHOW_COLUMNS_numeric_scale: str = "numeric_scale"
87-
self._SHOW_COLUMNS_remarks: str = "remarks"
88-
89-
90-
self._sql_show_databases: str = "SHOW DATABASES;"
91-
self._sql_show_databases_like: str = "SHOW DATABASES LIKE '{0}';"
92-
self._sql_show_schemas: str = "SHOW SCHEMAS FROM DATABASE {0};"
93-
self._sql_show_schemas_like: str = "SHOW SCHEMAS FROM DATABASE {0} LIKE '{1}';"
94-
self._sql_show_tables: str = "SHOW TABLES FROM SCHEMA {0}.{1};"
95-
self._sql_show_tables_like: str = "SHOW TABLES FROM SCHEMA {0}.{1} LIKE '{2}';"
96-
self._sql_show_columns: str = "SHOW COLUMNS FROM TABLE {0}.{1}.{2};"
97-
self._sql_show_columns_like: str = "SHOW COLUMNS FROM TABLE {0}.{1}.{2} LIKE '{3}';"
98-
99-
self.__rs_type_map = {
100-
"character varying": "varchar",
101-
"\"char\"": "char",
102-
"character": "char",
103-
"smallint": "int2",
104-
"integer": "int4",
105-
"bigint": "int8",
106-
"real": "float4",
107-
"double precision": "float8",
108-
"boolean": "bool",
109-
"time without time zone": "time",
110-
"time with time zone": "timetz",
111-
"timestamp without time zone": "timestamp",
112-
"timestamp with time zone": "timestamptz",
113-
"interval year to month": "intervaly2m",
114-
"interval year": "intervaly2m",
115-
"interval month": "intervaly2m",
116-
"interval day to second": "intervald2s",
117-
"interval day": "intervald2s",
118-
"interval second": "intervald2s",
119-
"binary varying": "varbyte"
120-
}
121-
122-
self.__sql_type_mapping = {
123-
"varchar": int(SQLType.SQL_VARCHAR),
124-
"char": int(SQLType.SQL_CHAR),
125-
"int2": int(SQLType.SQL_SMALLINT),
126-
"int4": int(SQLType.SQL_INTEGER),
127-
"int8": int(SQLType.SQL_BIGINT),
128-
"float4": int(SQLType.SQL_REAL),
129-
"float8": int(SQLType.SQL_DOUBLE),
130-
"numeric": int(SQLType.SQL_NUMERIC),
131-
"bool": int(SQLType.SQL_BIT),
132-
"date": int(SQLType.SQL_DATE),
133-
"time": int(SQLType.SQL_TIME),
134-
"timetz": int(SQLType.SQL_TIME_WITH_TIMEZONE),
135-
"timestamp": int(SQLType.SQL_TIMESTAMP),
136-
"timestamptz": int(SQLType.SQL_TIMESTAMP_WITH_TIMEZONE),
137-
"intervaly2m": int(SQLType.SQL_OTHER),
138-
"intervald2s": int(SQLType.SQL_OTHER),
139-
"super": int(SQLType.SQL_LONGVARCHAR),
140-
"geometry": int(SQLType.SQL_LONGVARBINARY),
141-
"geography": int(SQLType.SQL_LONGVARBINARY),
142-
"varbyte": int(SQLType.SQL_LONGVARBINARY)
143-
}
144-
145-
self.__data_type_length = {
146-
"bool": 1,
147-
"bit": 1,
148-
"boolean": 1,
149-
"int2": 5,
150-
"smallint": 5,
151-
"int4": 10,
152-
"integer": 10,
153-
"int": 10,
154-
"int8": 19,
155-
"bigint": 19,
156-
"float4": 8,
157-
"real": 8,
158-
"float8": 17,
159-
"double precision": 17,
160-
"date": 13,
161-
"time": 15,
162-
"timetz": 21,
163-
"timestamp": 29,
164-
"timestamptz": 35,
165-
"intervaly2m": 32,
166-
"intervald2s": 64
167-
}
66+
_SHOW_DATABASES_database_name: str = 'database_name'
67+
68+
_SHOW_SCHEMA_database_name: str = 'database_name'
69+
_SHOW_SCHEMA_schema_name: str = 'schema_name'
70+
71+
_SHOW_TABLES_database_name: str = 'database_name'
72+
_SHOW_TABLES_schema_name: str = 'schema_name'
73+
_SHOW_TABLES_table_name: str = 'table_name'
74+
_SHOW_TABLES_table_type: str = 'table_type'
75+
_SHOW_TABLES_remarks: str = 'remarks'
76+
77+
_SHOW_COLUMNS_database_name: str = "database_name"
78+
_SHOW_COLUMNS_schema_name: str = "schema_name"
79+
_SHOW_COLUMNS_table_name: str = "table_name"
80+
_SHOW_COLUMNS_column_name: str = "column_name"
81+
_SHOW_COLUMNS_ordinal_position: str = "ordinal_position"
82+
_SHOW_COLUMNS_column_default: str = "column_default"
83+
_SHOW_COLUMNS_is_nullable: str = "is_nullable"
84+
_SHOW_COLUMNS_data_type: str = "data_type"
85+
_SHOW_COLUMNS_character_maximum_length: str = "character_maximum_length"
86+
_SHOW_COLUMNS_numeric_precision: str = "numeric_precision"
87+
_SHOW_COLUMNS_numeric_scale: str = "numeric_scale"
88+
_SHOW_COLUMNS_remarks: str = "remarks"
89+
90+
_sql_show_databases: str = "SHOW DATABASES;"
91+
_sql_show_databases_like: str = "SHOW DATABASES LIKE {0};"
92+
_sql_show_schemas: str = "SHOW SCHEMAS FROM DATABASE {0};"
93+
_sql_show_schemas_like: str = "SHOW SCHEMAS FROM DATABASE {0} LIKE {1};"
94+
_sql_show_tables: str = "SHOW TABLES FROM SCHEMA {0}.{1};"
95+
_sql_show_tables_like: str = "SHOW TABLES FROM SCHEMA {0}.{1} LIKE {2};"
96+
_sql_show_columns: str = "SHOW COLUMNS FROM TABLE {0}.{1}.{2};"
97+
_sql_show_columns_like: str = "SHOW COLUMNS FROM TABLE {0}.{1}.{2} LIKE {3};"
98+
99+
# define constant for QUOTE_IDENT()
100+
_prepare_quote_ident: str = "select pg_catalog.QUOTE_IDENT(%s); "
101+
_quote_iden_result_row: int = 0
102+
_quote_iden_result_col: int = 0
103+
104+
# define constant for QUOTE_LITERAL
105+
_prepare_quote_literal: str = "select pg_catalog.QUOTE_LITERAL(%s); "
106+
_quote_literal_result_row: int = 0
107+
_quote_literal_result_col: int = 0
108+
109+
__rs_type_map = {
110+
"character varying": "varchar",
111+
"\"char\"": "char",
112+
"character": "char",
113+
"smallint": "int2",
114+
"integer": "int4",
115+
"bigint": "int8",
116+
"real": "float4",
117+
"double precision": "float8",
118+
"boolean": "bool",
119+
"time without time zone": "time",
120+
"time with time zone": "timetz",
121+
"timestamp without time zone": "timestamp",
122+
"timestamp with time zone": "timestamptz",
123+
"interval year to month": "intervaly2m",
124+
"interval year": "intervaly2m",
125+
"interval month": "intervaly2m",
126+
"interval day to second": "intervald2s",
127+
"interval day": "intervald2s",
128+
"interval second": "intervald2s",
129+
"binary varying": "varbyte"
130+
}
131+
132+
__sql_type_mapping = {
133+
"varchar": int(SQLType.SQL_VARCHAR),
134+
"char": int(SQLType.SQL_CHAR),
135+
"int2": int(SQLType.SQL_SMALLINT),
136+
"int4": int(SQLType.SQL_INTEGER),
137+
"int8": int(SQLType.SQL_BIGINT),
138+
"float4": int(SQLType.SQL_REAL),
139+
"float8": int(SQLType.SQL_DOUBLE),
140+
"numeric": int(SQLType.SQL_NUMERIC),
141+
"bool": int(SQLType.SQL_BIT),
142+
"date": int(SQLType.SQL_DATE),
143+
"time": int(SQLType.SQL_TIME),
144+
"timetz": int(SQLType.SQL_TIME_WITH_TIMEZONE),
145+
"timestamp": int(SQLType.SQL_TIMESTAMP),
146+
"timestamptz": int(SQLType.SQL_TIMESTAMP_WITH_TIMEZONE),
147+
"intervaly2m": int(SQLType.SQL_OTHER),
148+
"intervald2s": int(SQLType.SQL_OTHER),
149+
"super": int(SQLType.SQL_LONGVARCHAR),
150+
"geometry": int(SQLType.SQL_LONGVARBINARY),
151+
"geography": int(SQLType.SQL_LONGVARBINARY),
152+
"varbyte": int(SQLType.SQL_LONGVARBINARY)
153+
}
154+
155+
__data_type_length = {
156+
"bool": 1,
157+
"bit": 1,
158+
"boolean": 1,
159+
"int2": 5,
160+
"smallint": 5,
161+
"int4": 10,
162+
"integer": 10,
163+
"int": 10,
164+
"int8": 19,
165+
"bigint": 19,
166+
"float4": 8,
167+
"real": 8,
168+
"float8": 17,
169+
"double precision": 17,
170+
"date": 13,
171+
"time": 15,
172+
"timetz": 21,
173+
"timestamp": 29,
174+
"timestamptz": 35,
175+
"intervaly2m": 32,
176+
"intervald2s": 64
177+
}
168178

169179
def get_second_fraction(self, data_type: str = None) -> (str, bool):
170180
date_time_customize_precision: bool = False
@@ -234,11 +244,5 @@ def get_auto_increment(col_def: str) -> str:
234244
return "NO"
235245

236246
@staticmethod
237-
def check_name_is_not_pattern(name: str) -> bool:
238-
return name is None or not name or name == "%"
239-
240-
@staticmethod
241-
def check_name_is_exact_name(name: str) -> bool:
242-
if name is not None and len(name) != 0 and ("%" not in name):
243-
return True
244-
return False
247+
def is_none_or_empty(input_str: str) -> bool:
248+
return input_str is None or input_str == ""

0 commit comments

Comments
 (0)