PDF (US Ltr)
- 1.4Mb
PDF (A4)
- 1.4Mb
このページは機械翻訳したものです。
例 7.1 拡張オブジェクトの作成および登録 - Python
この例では、ユーザー定義の MySQL Shell グローバルオブジェクト demo
を介して使用可能にする関数 hello_world()
を作成します。 このコードは、新しい拡張オブジェクトを作成し、そのオブジェクトに hello_world()
関数をメンバーとして追加してから、その拡張オブジェクトを MySQL Shell グローバルオブジェクト demo
として登録します。
Press CTRL+C to copy# 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 モードでは、関数は次のようにコールされます:
Press CTRL+C to copymysql-js> demo.helloWorld()
Python モードでは、関数は次のようにコールされます:
Press CTRL+C to copymysql-py> demo.hello_world()
例 7.2 拡張オブジェクトの作成および登録 - JavaScript
この例では、関数 listTables()
をメンバーとして使用して拡張オブジェクトを作成し、MySQL Shell グローバルオブジェクト tools
として直接登録します:
Press CTRL+C to copy// 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 モードでは、関数は次のようにコールされます:
Press CTRL+C to copymysql-js> tools.listTables(session, "world_x", {excludeViews: true})
Python モードでは、関数は次のようにコールされます:
Press CTRL+C to copymysql-py> tools.list_tables(session, "world_x", {"excludeViews": True})