MySQL 外壳 8.0  / 第 10 章扩展 MySQL Shell  / 10.2 向 MySQL Shell 添加扩展对象  /  10.2.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方式调用成员时,成员名使用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})