-
Notifications
You must be signed in to change notification settings - Fork 1
Add defaults method to retrieve default values of tables and example. #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lefred
wants to merge
2
commits into
master
Choose a base branch
from
schema_defaults
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# schema/default.py | ||
# ----------------- | ||
# Definition of member functions for the schema extension object to display default values | ||
# | ||
# Usage example: | ||
# -------------- | ||
# | ||
# MySQL > 2019-06-26 18:07:50 > | ||
# JS> ext.schema.showDefaults('testing') | ||
# No session specified. Either pass a session object to this function or connect the shell to a database | ||
# MySQL > 2019-06-26 18:07:52 > | ||
# JS> \c root@localhost | ||
# Creating a session to 'root@localhost' | ||
# Fetching schema names for autocompletion... Press ^C to stop. | ||
# Your MySQL connection id is 15 (X protocol) | ||
# Server version: 8.0.16 MySQL Community Server - GPL | ||
# No default schema selected; type \use <schema> to set one. | ||
# MySQL 8.0.16 > > localhost:33060+ > > 2019-06-26 18:08:01 > | ||
# JS> ext.schema.showDefaults('testing') | ||
# No schema specified. Either pass a schema name or use one | ||
# MySQL 8.0.16 > > localhost:33060+ > > 2019-06-26 18:08:04 > | ||
# JS> ext.schema.showDefaults('testing', 'big') | ||
# +--------------------------------+-----------------+----------------------------------------------------+---------------------------+ | ||
# | ColumnName | Type | Default | Example | | ||
# +--------------------------------+-----------------+----------------------------------------------------+---------------------------+ | ||
# | id | int | None | NULL | | ||
# | varchar_val | varchar | None | NULL | | ||
# +--------------------------------+-----------------+----------------------------------------------------+---------------------------+ | ||
# Total: 2 | ||
# | ||
# MySQL 8.0.16 > > localhost:33060+ > > > test > 2019-06-26 18:11:01 > | ||
# JS> ext.schema.showDefaults('default_test') | ||
# +--------------------------------+-----------------+----------------------------------------------------+---------------------------+ | ||
# | ColumnName | Type | Default | Example | | ||
# +--------------------------------+-----------------+----------------------------------------------------+---------------------------+ | ||
# | bi_col_exp | bigint | (8 * 8) | 64 | | ||
# | d_col | date | curdate() | 2019-06-26 | | ||
# | d_col_exp | date | (curdate() + 8) | 20190634 | | ||
# | dt_col | datetime | CURRENT_TIMESTAMP | 2019-06-26 18:11:16 | | ||
# | vc_col_exp | varchar | concat(_utf8mb4\'test\',_utf8mb4\'test\') | testtest | | ||
# +--------------------------------+-----------------+----------------------------------------------------+---------------------------+ | ||
# Total: 5 | ||
# | ||
|
||
def __returnDefaults(session, schema, table): | ||
# Define the query to get the routines | ||
i_s = session.get_schema("information_schema") | ||
filters = ["TABLE_SCHEMA = :schema"] | ||
filters.append("TABLE_NAME = :table") | ||
stmt = i_s.COLUMNS.select( | ||
"COLUMN_NAME AS ColName", | ||
"COLUMN_TYPE AS DataType", | ||
"COLUMN_DEFAULT") | ||
stmt = stmt.where(" AND ".join(filters)) | ||
|
||
# Execute the query and check for warnings | ||
stmt = stmt.bind("schema", schema).bind("table", table) | ||
result = stmt.execute() | ||
#routines = result.fetch_all() | ||
defaults = result.fetch_all() | ||
if (result.get_warnings_count() > 0): | ||
# Bail out and print the warnings | ||
print("Warnings occurred - bailing out:") | ||
print(result.get_warnings()) | ||
return False | ||
|
||
return defaults | ||
|
||
|
||
def show_defaults(table, schema=None, session=None): | ||
# Get hold of the global shell object | ||
import mysqlsh | ||
shell = mysqlsh.globals.shell | ||
|
||
if session is None: | ||
session = shell.get_session() | ||
if session is None: | ||
print("No session specified. Either pass a session object to this " | ||
"function or connect the shell to a database") | ||
return | ||
if schema is None: | ||
if session.current_schema is None: | ||
print("No schema specified. Either pass a schema name or use one") | ||
return | ||
schema = session.current_schema.name | ||
|
||
defaults = __returnDefaults(session, schema, table) | ||
|
||
fmt = "| {0:30s} | {1:15s} | {2:50s} | {3:25s} |" | ||
header = fmt.format("ColumnName", "Type", "Default", "Example") | ||
bar = "+" + "-" * 32 + "+" + "-" * 17 + "+" + "-" * 52 + "+" + "-" * 27 + "+" | ||
print bar | ||
print header | ||
print bar | ||
for row in defaults: | ||
col_expr = row[2] | ||
if col_expr is None: | ||
col_expr = 'NULL' | ||
else: | ||
col_expr = col_expr.replace('\\', '') | ||
query = session.sql("select {0}".format(col_expr)) | ||
if col_expr == 'NULL': | ||
ex_str = col_expr | ||
else: | ||
try: | ||
example = query.execute() | ||
for col in example.fetch_all(): | ||
ex_str = str(col[0]) | ||
except: | ||
ex_str = row[2] | ||
print fmt.format(row[0], row[1], row[2], ex_str) | ||
print bar | ||
|
||
return "Total: %d" % len(defaults) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
varchar how much? Would be nice to show that.