MySQL Shell 9.0  /  ...  /  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})