How to get enumerated values from a Postgres Database using python plugin #1949
-
Hello,
while from __future__ import print_function
import textwrap
from schemacrawler.schemacrawler import \
IdentifiersBuilder # pylint: disable=import-error
# Print document title
if title:
print("# " + title)
else:
print("# Database Schema")
print("")
# Build identifier options (for quoting)
identifiers = IdentifiersBuilder.builder().toOptions()
# Iterate over each schema in the catalog
for schema in catalog.getSchemas():
tables = catalog.getTables(schema)
if not tables:
continue
# Schema heading
print("## " + schema.fullName)
print("")
for table in tables:
# Table heading
kind = "view" if table.tableType.isView() else "table"
print("### " + table.name + " (" + kind + ")")
if table.remarks:
print("")
print(table.remarks)
print("")
# === Columns ===
print("#### Columns")
print("| Name | Type | PK | FK | Nullable | Comments |")
print("| - | - | - | - | - | - |")
for column in table.columns:
name = column.name
dtype = column.columnDataType.toString()
is_pk = "X" if column.isPartOfPrimaryKey() else ""
is_fk = "X" if column.isPartOfForeignKey() else ""
...
# Spacer before next table
print("") so far, the tool works incredibly well and fits my needs, however, i would love that right after all tables have been printed, to print the
Some metadata regarding schemacrawler:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
@josedavidricodias Sorry for the late response. Take a look at the Jaba unit test to get an idea of you can use it in Python: code The idea is that |
Beta Was this translation helpful? Give feedback.
-
Thanks @sualeh for your response. With your help I was able to solve my use-case. For future reference in case someone stumbles on this, i decided to store all found enums in a from __future__ import print_function
import textwrap
from schemacrawler.schemacrawler import \
IdentifiersBuilder # pylint: disable=import-error
import copy
# Print document title
if title:
print("# " + title)
else:
print("# Database Schema")
print("")
# Build identifier options (for quoting)
identifiers = IdentifiersBuilder.builder().toOptions()
# Iterate over each schema in the catalog
for schema in catalog.getSchemas():
tables = catalog.getTables(schema)
if not tables:
continue
all_enums = dict() # Holder for all the appearing enums
# Schema heading
print("## " + schema.fullName)
print("")
for table in tables:
# Table heading
kind = "view" if table.tableType.isView() else "table"
print("### " + table.name + " (" + kind + ")")
if table.remarks:
print("")
print(table.remarks)
print("")
# === Columns ===
print("#### Columns")
print("| Name | Type | PK | FK | Nullable | Comments |")
print("| - | - | - | - | - | - |")
for column in table.columns:
# Save enums for later display
if column.getColumnDataType().isEnumerated():
if column.name not in all_enums.keys():
all_enums[copy.deepcopy(column.name)] = copy.deepcopy(column.getColumnDataType().getEnumValues())
name = column.name
dtype = column.columnDataType.toString()
is_pk = "X" if column.isPartOfPrimaryKey() else ""
is_fk = "X" if column.isPartOfForeignKey() else ""
...
# Spacer before next table
print("")
# Display all enums
for enum in all_enums.keys():
print("### " + enum + " (enum)")
print("| Value |")
print("| - |")
for value in all_enums[enum]:
print("| " + value + " |")
print("") |
Beta Was this translation helpful? Give feedback.
@josedavidricodias Sorry for the late response. Take a look at the Jaba unit test to get an idea of you can use it in Python: code
The idea is that
column.columnDataType
has.isEnumerated()
and.getEnumValues()
that will give you what you need. In Python, I expect that thecolumn.columnDataType.enumValues
syntax will work, but I have not tried it out.