10.2.4 Примеры объектов расширения MySQL Shell
Пример 10.1 Создание и регистрация объектов расширения - Python
В этом примере создается функция hello_world(), которая доступна через глобальный объект MySQL Shell demo. Код создает новый объект расширения и добавляет функцию hello_world() в качестве члена, затем регистрирует объект расширения как глобальный объект MySQL Shell demo.
# Define a hello_world function that will be exposed by the global object 'demo'
def hello_world():
print("Hello world!")
# Create an extension object where the hello_world function will be registered
plugin_obj = shell.create_extension_object()
shell.add_extension_object_member(plugin_obj, "helloWorld", hello_world,
{"brief": "Prints 'Hello world!'", "parameters": []})
# Registering the 'demo' global object
shell.register_global("demo", plugin_obj,
{"brief": "A demo plugin that showcases MySQL Shell's plugin feature."})
Обратите внимание, что имя члена задано в верхнем регистре camel case в функции shell.add_extension_object_member(). При вызове члена в режиме Python используйте snake case для имени члена, и MySQL Shell автоматически выполнит преобразование. В режиме JavaScript функция вызывается следующим образом:
mysql-js> demo.helloWorld()В режиме Python функция вызывается следующим образом:
mysql-py> demo.hello_world()
Пример 10.2 Создание и регистрация объектов расширения - JavaScript
В этом примере создается объект расширения с функцией listTables() в качестве члена и регистрируется он непосредственно как глобальный объект MySQL Shell tools:
// Define a listTables function that will be exposed by the global object tools
function listTables(session, schemaName, options) {
...
}
// Create an extension object and add the listTables function to it as a member
var object = shell.createExtensionObject()
shell.addExtensionObjectMember(object, "listTables", listTables,
{
brief:"Retrieves the tables from a given schema.",
details: ["Retrieves the tables of the schema named schemaName.",
"If excludeCollections is true, the collection tables will not be returned"],
parameters:
[
{
name: "session",
type: "object",
class: "Session",
brief: "An X Protocol session object."
},
{
name: "schemaName",
type: "string",
brief: "The name of the schema from which the table list will be pulled."
},
{
name: "options",
type: "dictionary",
brief: "Additional options that affect the function behavior.",
options: [
{
name: "excludeViews",
type: "bool",
brief: "If set to true, the views will not be included on the list, default is false",
},
{
name: "excludeCollections",
type: "bool",
brief: "If set to true, the collections will not be included on the list, default is false",
}
]
},
]
});
// Register the extension object as the global object "tools"
shell.registerGlobal("tools", object, {brief:"Global object for ExampleCom administrator tools",
details:[
"Global object to access homegrown ExampleCom administrator tools.",
"Add new tools to this global object as members with shell.addExtensionObjectMember()."]})
В режиме JavaScript функция вызывается следующим образом:
mysql-js> tools.listTables(session, "world_x", {excludeViews: true})
В режиме Python функция вызывается следующим образом:
mysql-py> tools.list_tables(session, "world_x", {"excludeViews": True})
© 2025 Oracle
Licensed under the GPLv2 License.