Skip to content

Schema feature #241

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@
},
"search.exclude": {
"out": true // set this to false to include "out" folder in search results
}
},
"spellright.language": [
"en",
],
"editor.tabSize": 2,
"editor.insertSpaces": true
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,4 @@ This extension contributes the following settings:
* `vscode-postgres.virtualFolders`: set to array of virtual folders to be displayed under schema. Supported values for virtual folders are `functions`. Set to `null` or empty array `[]` to disable virtual folders.
* `vscode-postgres.defaultConnection`: set the default connection (by name) that should be used for any file.
* `vscode-postgres.defaultDatabase`: set the default database on the default connection that should be used for any file (must exist on server).
* `vscode-postgres.defaultSchema`: set the default schema on the default connection that should be used for any file (must exist in the database). Skip to use public and all other schemas.
2,044 changes: 6 additions & 2,038 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
"default": "",
"description": "The default database on the default connection to use"
},
"vscode-postgres.defaultSchema": {
"type": "string",
"default": "",
"description": "The default schema on the default connection to use (empty to use all)"
},
"vscode-postgres.showExplorer": {
"type": "boolean",
"default": true,
Expand Down
2 changes: 2 additions & 0 deletions src/commands/selectConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ export class selectConnectionCommand extends BaseCommand {
}
EditorState.connection = connection;
await vscode.commands.executeCommand('vscode-postgres.selectDatabase');
await vscode.commands.executeCommand('vscode-postgres.selectSchema');
return;
}

let result = await vscode.commands.executeCommand('vscode-postgres.addConnection');
if (!result) return;
await vscode.commands.executeCommand('vscode-postgres.selectDatabase');
await vscode.commands.executeCommand('vscode-postgres.selectSchema');
}
}
31 changes: 31 additions & 0 deletions src/commands/selectSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import BaseCommand from "../common/baseCommand";
import * as vscode from 'vscode';
import { IConnection } from "../common/IConnection";
import { EditorState } from "../common/editorState";
import { Database } from "../common/database";

'use strict';

export class selectSchemaCommand extends BaseCommand {
async run() {
// vscode.window.showInformationMessage('Select Database!');
let connectionDetails: IConnection = EditorState.connection;
if (!connectionDetails) return;

const connection = await Database.createConnection(connectionDetails, connectionDetails.database);

let schemas: string[] = [];
try {
const res = await connection.query('SELECT schema_name as sch FROM information_schema.schemata;');
schemas = res.rows.map<string>(schema => schema.sch);
} finally {
await connection.end();
}

//vscode.window.showInputBox
const schema = await vscode.window.showQuickPick(schemas, {placeHolder: 'Select a schema'});
if (!schema) return;
connectionDetails.schema = schema;
EditorState.connection = Database.getConnectionWithDB(connectionDetails, connectionDetails.database);
}
}
1 change: 1 addition & 0 deletions src/common/IConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface IConnection {
hasPassword?: boolean;
readonly port: number;
readonly database?: string;
schema?: string;
multipleStatements?: boolean;
readonly certPath?: string;
ssl?: any;
Expand Down
2 changes: 2 additions & 0 deletions src/common/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export class Database {
password: connection.password,
port: connection.port,
database: dbname,
schema: connection.schema,
multipleStatements: connection.multipleStatements,
certPath: connection.certPath
};
Expand All @@ -72,6 +73,7 @@ export class Database {
public static async createConnection(connection: IConnection, dbname?: string): Promise<PgClient> {
const connectionOptions: any = Object.assign({}, connection);
connectionOptions.database = dbname ? dbname : connection.database;
connectionOptions.schema = connection.schema;
if (connectionOptions.certPath && fs.existsSync(connectionOptions.certPath)) {
connectionOptions.ssl = {
ca: fs.readFileSync(connectionOptions.certPath).toString(),
Expand Down
6 changes: 6 additions & 0 deletions src/common/editorState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ export class EditorState {
}
}

// optional schema
let defaultSchema = Global.Configuration.get<string>("defaultSchema");
if (typeof defaultSchema === 'string' && defaultSchema.length) {
connection.schema = defaultSchema;
}

let defaultDatabase = Global.Configuration.get<string>("defaultDatabase");
if (defaultDatabase) {
const conn = await Database.createConnection(connection, 'postgres');
Expand Down
4 changes: 4 additions & 0 deletions src/language/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ async function setupDBConnection(connectionOptions: IDBConnection, uri: string):
password: connectionOptions.password,
port: connectionOptions.port,
database: 'postgres',
schema: '',
multipleStatements: connectionOptions.multipleStatements,
certPath: connectionOptions.certPath,
ssl: connectionOptions.ssl
Expand Down Expand Up @@ -199,6 +200,7 @@ async function loadCompletionCache(connectionOptions: IDBConnection) {

try {
if (connectionOptions.database) {
let schema = (typeof connectionOptions.schema === 'string' && connectionOptions.schema.length) ? connectionOptions.schema : false;
/*
SELECT tablename as name, true as is_table FROM pg_tables WHERE schemaname not in ('information_schema', 'pg_catalog')
union all
Expand All @@ -223,6 +225,7 @@ async function loadCompletionCache(connectionOptions: IDBConnection) {
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE
c.relkind = 'r'
${schema? "AND n.nspname = '"+schema+"'" : ''}
AND n.nspname not in ('information_schema', 'pg_catalog', 'pg_toast')
AND n.nspname not like 'pg_temp_%'
AND n.nspname not like 'pg_toast_temp_%'
Expand All @@ -240,6 +243,7 @@ async function loadCompletionCache(connectionOptions: IDBConnection) {
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE
c.relkind in ('v', 'm')
${schema? "AND n.nspname = '"+schema+"'" : ''}
AND n.nspname not in ('information_schema', 'pg_catalog', 'pg_toast')
AND n.nspname not like 'pg_temp_%'
AND n.nspname not like 'pg_toast_temp_%'
Expand Down