Skip to content

Commit

Permalink
Sidebar click to connect
Browse files Browse the repository at this point in the history
  • Loading branch information
mtxr committed Jan 19, 2019
1 parent dafbd6d commit 49364cc
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .test-database/pgsql/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ services:
image: postgres
restart: unless-stopped
ports:
- '5432:5432'
- '5433:5432'
volumes:
- ./pg_hba.conf:/etc/postgresql/pg_hba.conf:Z
- ./postgresql.conf:/etc/postgresql/postgresql.conf:Z
Expand Down
1 change: 1 addition & 0 deletions packages/extension/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ namespace SQLTools {
let password = null;
if (c && c.askForPassword) password = await askForPassword(c);
lastUsedConn = c;
connectionExplorer.setActiveConnection(lastUsedConn);
updateStatusBar();
lastUsedConn = (await languageClient.sendRequest(
OpenConnectionRequest.method,
Expand Down
4 changes: 2 additions & 2 deletions packages/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -486,12 +486,12 @@
},
{
"command": "SQLTools.appendToCursor",
"when": "view == SQLTools.tableExplorer && viewItem != connection.structure",
"when": "view == SQLTools.tableExplorer && viewItem != connection.schema_group",
"group": "sqltools.dbExplorer@1"
},
{
"command": "SQLTools.appendToCursor",
"when": "view == SQLTools.tableExplorer && viewItem != connection.structure",
"when": "view == SQLTools.tableExplorer && viewItem != connection.schema_group",
"group": "inline@3"
},
{
Expand Down
26 changes: 18 additions & 8 deletions packages/extension/providers/connection-explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import {
SidebarColumn,
SidebarConnection,
SidebarDatabaseStructure,
SidebarDatabaseSchemaGroup,
SidebarTable,
SidebarView,
} from './sidebar-provider/sidebar-tree-items';
Expand All @@ -20,7 +20,7 @@ export type SidebarDatabaseItem = SidebarConnection | SidebarTable | SidebarColu
export class ConnectionExplorer implements TreeDataProvider<SidebarDatabaseItem> {
public onDidChange: EventEmitter<SidebarDatabaseItem | undefined> = new EventEmitter();
public readonly onDidChangeTreeData: Event<SidebarDatabaseItem | undefined> =
this.onDidChange.event;
this.onDidChange.event;
private tree: { [database: string]: SidebarConnection} = {};
public fireUpdate(): void {
this.onDidChange.fire();
Expand All @@ -41,7 +41,7 @@ export class ConnectionExplorer implements TreeDataProvider<SidebarDatabaseItem>
} else if (
element instanceof SidebarTable
|| element instanceof SidebarView
|| element instanceof SidebarDatabaseStructure
|| element instanceof SidebarDatabaseSchemaGroup
) {
return Promise.resolve(this.toArray(element.items));
}
Expand All @@ -54,8 +54,8 @@ export class ConnectionExplorer implements TreeDataProvider<SidebarDatabaseItem>
public setConnections(connections: ConnectionCredentials[]) {
const keys = [];
connections.forEach((conn) => {
this.tree[this.getDbKey(conn)] = this.tree[this.getDbKey(conn)] || new SidebarConnection(conn);
keys.push(this.getDbKey(conn));
this.tree[this.getDbId(conn)] = this.tree[this.getDbId(conn)] || new SidebarConnection(conn);
keys.push(this.getDbId(conn));
});

if (Object.keys(this.tree).length !== keys.length) {
Expand All @@ -69,7 +69,7 @@ export class ConnectionExplorer implements TreeDataProvider<SidebarDatabaseItem>

public setTreeData(conn: SerializedConnection, tables, columns) {
if (!conn) return;
const treeKey = this.getDbKey(conn);
const treeKey = this.getDbId(conn);

if (this.tree[treeKey]) this.tree[treeKey].reset();

Expand All @@ -84,8 +84,18 @@ export class ConnectionExplorer implements TreeDataProvider<SidebarDatabaseItem>
this.refresh();
}

private getDbKey(c: SerializedConnection | ConnectionCredentials) {
return `${c.name}:${c.database}`;
public setActiveConnection(c?: SerializedConnection) {
const idActive = c ? this.getDbId(c) : null;
Object.keys(this.tree).forEach(id => {
if (id === idActive) {
return this.tree[id].activate();
}
return this.tree[id].deactivate();
});
}

private getDbId(c: SerializedConnection | ConnectionCredentials) {
return `${c.name}#${c.database}#${c.dialect}`;
}

private toArray(obj: any) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,44 @@ import {
import ContextManager from '../../context';
import ConfigManager from '@sqltools/core/config-manager';
import { DatabaseInterface, ConnectionCredentials } from '@sqltools/core/interface';
import { DISPLAY_NAME } from '@sqltools/core/constants';

export class SidebarConnection extends TreeItem {
public contextValue = 'connection';
public value: string;
public tables: SidebarDatabaseSchemaGroup = new SidebarDatabaseSchemaGroup('Tables');
public views: SidebarDatabaseSchemaGroup = new SidebarDatabaseSchemaGroup('Views');
public get description() {
return this.isActive ? 'active' : '';
}

private isActive = false;
public get id() {
return this.getId();
}
public get value() {
return this.conn.database;
}

public get tooltip() {
if (this.isActive) return `Active Connection - Queries will run for this connection`;
return undefined;
}

public tables: SidebarDatabaseStructure = new SidebarDatabaseStructure('Tables');
public views: SidebarDatabaseStructure = new SidebarDatabaseStructure('Views');
constructor(public conn: ConnectionCredentials) {
super(conn.database, TreeItemCollapsibleState.None);
this.value = conn.database;
this.label = `${conn.database}@${conn.name}`;
super(`${conn.database}@${conn.name}`, TreeItemCollapsibleState.None);
this.iconPath = {
dark: ContextManager.context.asAbsolutePath('icons/database-dark.svg'),
light: ContextManager.context.asAbsolutePath('icons/database-light.svg'),
};
this.command = {
title: '',
command: `${DISPLAY_NAME}.selectConnection`,
arguments: [this],
};
}

public getId() {
return `${this.conn.name}#${this.conn.database}#${this.conn.dialect}`;
}

public addItem(item) {
Expand All @@ -34,11 +57,19 @@ export class SidebarConnection extends TreeItem {
if (this.tables) this.tables.reset();
this.collapsibleState = TreeItemCollapsibleState.None;
}

public activate() {
this.isActive = true;
}

public deactivate() {
this.isActive = false;
}
}

export class SidebarDatabaseStructure extends TreeItem {
export class SidebarDatabaseSchemaGroup extends TreeItem {
public iconPath = ThemeIcon.Folder;
public contextValue = 'connection.structure';
public contextValue = 'connection.schema_group';
public items: { [name: string]: SidebarTable | SidebarView} = {};
constructor(private name) {
super(name, TreeItemCollapsibleState.Expanded);
Expand Down
1 change: 1 addition & 0 deletions packages/extension/tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"object-literal-sort-keys": false,
"no-namespace": false,
"align": [ true, "parameters", "statements" ],
"arrow-parens": false,
"arrow-return-shorthand": [ true ],
"ban-types": [ false ],
"comment-format": [ true, "check-space", { "ignore-words": [ "TODO", "HACK" ] } ],
Expand Down

0 comments on commit 49364cc

Please sign in to comment.