MySQL Shell 9.0
You can register an SQL handler in either of the following ways:
register_Sql_Handler
has the following
syntax:
shell.register_sql_handler(name, description, prefixes, callback)
name
: the unique identifier of the SQL
handler.
description
: a brief description of the
SQL extensions provided by the handler.
prefixes
: a list of prefixes (string)
identifying the SQL statements processed by this handler.
You must define at least one prefix.
callback
: name of the function to execute
when a statement matching the prefix is identified.
The function must have the following signature:
function(session, sql): [Result]
You can also use the Python decorator
@sql_handler
to register the SQL handler. The
decorator uses shell.registerSqlHandler
to
register the handler. The same restrictions apply for
parameters.
For example:
from mysqlsh.plugin_manager import sql_handler @sql_handler(prefixes=['SHOW ']) "Prints a notice when a SHOW command is executed" def show_preprocessor(session, sql): print(f"SHOW COMMAND EXECUTED: {sql}")