Skip to content

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
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions schema/defaults.py
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 |
Copy link
Collaborator

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.

# +--------------------------------+-----------------+----------------------------------------------------+---------------------------+
# 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)
88 changes: 60 additions & 28 deletions schema/init.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# schema/init.py
# --------------
# Initializes the schema plugins.
# Initializes the schema plugins.

# Contents
# --------
Expand All @@ -9,31 +9,63 @@

from ext.mysqlsh_plugins_common import register_plugin
from ext.schema import src as schema_src
from ext.schema import defaults as schema_defaults

register_plugin("showProcedures", schema_src.show_procedures,
{
"brief": "Lists all stored procedures.",
"parameters": [
{
"name": "schema",
"brief": "The schema which to list the stored procedures from.",
"type": "string",
"required": False
},
{
"name": "session",
"brief": "The session to be used on the operation.",
"type": "object",
"classes": ["Session", "ClassicSession"],
"required": False
}
]
},
"schema",
{
"brief": "Schema management and utilities.",
"details": [
"A collection of schema management tools and related "
"utilities that work on schemas"
]
})
register_plugin("showProcedures", schema_src.show_procedures,
{
"brief": "Lists all stored procedures.",
"parameters": [
{
"name": "schema",
"brief": "The schema which to list the stored procedures from.",
"type": "string",
"required": False
},
{
"name": "session",
"brief": "The session to be used on the operation.",
"type": "object",
"classes": ["Session", "ClassicSession"],
"required": False
}
]
},
"schema",
{
"brief": "Schema management and utilities.",
"details": [
"A collection of schema management tools and related "
"utilities that work on schemas"
]
})


register_plugin("showDefaults", schema_defaults.show_defaults,
{
"brief": "Lists the default values of each column in a table",
"parameters": [{
"name": "table",
"brief": "Table name to use.",
"type": "string",
"required": True},
{
"name": "schema",
"brief": "Schema to use.",
"type": "string",
"required": False},
{
"name": "session",
"brief": "The session to be used on the operation.",
"type": "object",
"classes": ["Session", "ClassicSession"],
"required":False}
]
},
"schema",
{
"brief": "Schema management and utilities.",
"details": [
"A collection of schema management tools and related "
"utilities that work on schemas"
]
})