共用方式為


Azure Data Studio 擴充性 API

重要

Azure Data Studio 將於 2026 年 2 月 28 日停止使用。 建議您使用 Visual Studio Code。 如需移轉至 Visual Studio Code 的詳細資訊,請流覽 Azure Data Studio 發生什麼事?

Azure Data Studio 提供 API,讓延伸模組可用來與 Azure Data Studio 的其他部分 (例如物件總管) 互動。 這些 API 可從 src/sql/azdata.d.ts 檔案取得,如下所述。

連線管理

azdata.connection

連接管理的頂層函式

  • getCurrentConnection(): Thenable<azdata.connection.Connection>:根據使用中的編輯器或物件總管選取項目,取得目前的連線。

  • getActiveConnections(): Thenable<azdata.connection.Connection[]> 取得所有使用中用戶連線的清單。 如果沒有這類連線,會傳回空白清單。

  • getCredentials(connectionId: string): Thenable<{ [name: string]: string }>:取得字典,其中包含與連線相關聯的認證。 否則,這些會當做選項字典的一部分傳回 data.connection.Connection 物件,但從該物件中移除。

連接

  • options: { [name: string]: string }:連線選項的字典
  • providerName: string 連線提供者的名稱(例如 “MSSQL”)
  • connectionId: string:連線的唯一識別碼

範例程式碼

> let connection = azdata.connection.getCurrentConnection();
connection: {
    providerName: 'MSSQL',
    connectionId: 'd97bb63a-466e-4ef0-ab6f-00cd44721dcc',
    options: {
        server: 'mairvine-sql-server',
        user: 'sa',
        authenticationType: 'sqlLogin',
        ...
 },
    ...
}
> let credentials = azdata.connection.getCredentials(connection.connectionId);
credentials: {
    password: 'abc123'
}

物件總管

azdata.objectexplorer

物件總管的最上層函式

  • getNode(connectionId: string, nodePath?: string): Thenable<azdata.objectexplorer.ObjectExplorerNode>:取得對應至指定連線和路徑的物件總管節點。 如果沒有指定路徑,會傳回指定連線的最上層節點。 如果指定路徑上沒有節點,則會傳回 undefined。 注意:物件的 nodePath 是由 SQL 工具服務後端所產生,很難以手動方式建構。 未來的 API 改進可讓您根據您提供有關節點的元數據來取得節點,例如名稱、類型和架構。

  • getActiveConnectionNodes(): Thenable<azdata.objectexplorer.ObjectExplorerNode>:取得所有使用中的物件總管連線節點。

  • findNodes(connectionId: string, type: string, schema: string, name: string, database: string, parentObjectNames: string[]): Thenable<azdata.objectexplorer.ObjectExplorerNode[]>:尋找符合指定中繼資料的所有物件總管節點。 當 schemadatabaseparentObjectNames 參數不適用時,應設為 undefinedparentObjectNames 是一個非資料庫父物件的清單,從最高層級到最低層級的 Object Explorer 中,所需的物件位於其下。 例如,搜尋屬於資料表 "schema1.table1" 和資料庫 "database1" 且具有連線識別碼 connectionId 的資料行 "column1" 時,請呼叫 findNodes(connectionId, 'Column', undefined, 'column1', 'database1', ['schema1.table1'])。 另請參閱此 API 呼叫的預設 Azure Data Studio 支援類型清單

ObjectExplorerNode

  • connectionId: string 節點所在連線的標識碼

  • nodePath: string:節點的路徑,用於呼叫 getNode 函數。

  • nodeType: string:表示節點類型的字串

  • nodeSubType: string:表示節點子類型的字串

  • nodeStatus: string:表示節點狀態的字串

  • label: string:顯示在物件總管中的節點標籤

  • isLeaf: boolean:節點是否為分葉節點,因此沒有子系

  • metadata: azdata.ObjectMetadata:描述此節點所表示物件的中繼資料

  • errorMessage: string:節點處於錯誤狀態時顯示的訊息

  • isExpanded(): Thenable<boolean>:節點目前是否在物件總管中已展開

  • setExpandedState(expandedState: vscode.TreeItemCollapsibleState): Thenable<void>:設定要展開或摺疊節點。 如果狀態設定為 [無],則不會變更節點。

  • setSelected(selected: boolean, clearOtherSelections?: boolean): Thenable<void>:設定是否選取節點。 如果 clearOtherSelections 為 true,會在進行新的選取時清除其他選取項目。 如果是假,請保留任何現有的選擇。 當 clearOtherSelections 為 true 時,selected 預設為 true;當 selected 為 false 時,預設為 false。

  • getChildren(): Thenable<azdata.objectexplorer.ObjectExplorerNode[]>:取得此節點的所有子節點。 如果沒有子系,會傳回空白清單。

  • getParent(): Thenable<azdata.objectexplorer.ObjectExplorerNode>:取得此節點的父節點。 如果沒有父代,則傳回未定義。

範例程式碼

private async interactWithOENode(selectedNode: azdata.objectexplorer.ObjectExplorerNode): Promise<void> {
    let choices = ['Expand', 'Collapse', 'Select', 'Select (multi)', 'Deselect', 'Deselect (multi)'];
    if (selectedNode.isLeaf) {
 choices[0] += ' (is leaf)';
 choices[1] += ' (is leaf)';
 } else {
        let expanded = await selectedNode.isExpanded();
        if (expanded) {
 choices[0] += ' (is expanded)';
 } else {
 choices[1] += ' (is collapsed)';
 }
 }
    let parent = await selectedNode.getParent();
    if (parent) {
 choices.push('Get Parent');
 }
    let children = await selectedNode.getChildren();
 children.forEach(child => choices.push(child.label));
    let choice = await vscode.window.showQuickPick(choices);
 let nextNode: azdata.objectexplorer.ObjectExplorerNode = undefined;
    if (choice === choices[0]) {
 selectedNode.setExpandedState(vscode.TreeItemCollapsibleState.Expanded);
 } else if (choice === choices[1]) {
 selectedNode.setExpandedState(vscode.TreeItemCollapsibleState.Collapsed);
 } else if (choice === choices[2]) {
 selectedNode.setSelected(true);
 } else if (choice === choices[3]) {
 selectedNode.setSelected(true, false);
 } else if (choice === choices[4]) {
 selectedNode.setSelected(false);
 } else if (choice === choices[5]) {
 selectedNode.setSelected(false, true);
 } else if (choice === 'Get Parent') {
 nextNode = parent;
 } else {
        let childNode = children.find(child => child.label === choice);
 nextNode = childNode;
 }
    if (nextNode) {
        let updatedNode = await azdata.objectexplorer.getNode(nextNode.connectionId, nextNode.nodePath);
        this.interactWithOENode(updatedNode);
 }
}

vscode.commands.registerCommand('mssql.objectexplorer.interact', () => {
 azdata.objectexplorer.getActiveConnectionNodes().then(activeConnections => {
 vscode.window.showQuickPick(activeConnections.map(connection => connection.label + ' ' + connection.connectionId)).then(selection => {
            let selectedNode = activeConnections.find(connection => connection.label + ' ' + connection.connectionId === selection);
            this.interactWithOENode(selectedNode);
 });
 });
});

建議的 API

我們新增了建議的 API,允許延伸模組在對話方塊、精靈和文件索引標籤中顯示自訂 UI,以及其他功能。 如需更多檔,請參閱 建議的 API 類型檔案,但請注意,這些 API 隨時可能會變更。 在 「子服務」範例延伸模組中,可以找到如何使用其中一些 API 的範例