MySQL Shell 8.4  /  ...  /  MySQL Shell 扩展对象示例

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."})

请注意,成员名称在 shell.add_extension_object_member() 函数中以驼峰式大小写指定。在 Python 模式下调用成员时,请对成员名称使用蛇形大小写,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})