Skip to content

Commit e12e1c7

Browse files
committed
Ctrl+F5, autosave before run, fix readme
1 parent 305f47a commit e12e1c7

File tree

5 files changed

+135
-104
lines changed

5 files changed

+135
-104
lines changed

client/README.md

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,37 @@
11
# pgsql extension
22

3-
This extension is fixed [clone](https://github.com/jptarqu/VSCodeExtension-PostgreSQL) of [postgresql by JPTarquino](https://marketplace.visualstudio.com/items?itemName=JPTarquino.postgresql)
3+
This extension is fixed clone of [postgresql](https://marketplace.visualstudio.com/items?itemName=JPTarquino.postgresql) by [JPTarquino](https://github.com/jptarqu/VSCodeExtension-PostgreSQL)
44

55
## Features:
6-
- Run current file into Postgres via psql
7-
- Colorization
8-
- Completion Lists for global postgres functions (copied from the Postgres official documentation)
9-
- Snippets
6+
- Execute sql file into Postgres (*Ctrl+F5*) via [psql](https://www.postgresql.org/docs/current/static/app-psql.html)
7+
- Colorization - _converted from [mulander](https://github.com/mulander/postgres.tmbundle)_
8+
- Completion lists for keywords - _copied from the Postgres [documentation](https://www.postgresql.org/docs/current/static/sql-keywords-appendix.html#KEYWORDS-TABLE)_
9+
- Few snippets ( *Ctrl+Space, type 'pg'* )
1010

11-
<img src="https://raw.githubusercontent.com/doublefint/vscode-pgsql/master/client/images/example.gif" alt="demo" style="width:480px;"/>
12-
13-
## Fixes
14-
- use connection string with port and password
15-
- add stderr output ( thanks for [khushboo shah](https://marketplace.visualstudio.com/items?itemName=JPTarquino.postgresql) )
16-
- shorter command
11+
The extension recognizes the \*.sql, \*.ddl, \*.dml, \*.pgsql extension as sql files intended to be run in Postgres
1712

18-
The extension recognizes the .sql,.ddl,.dml,.pgsql extension as sql files intended to be run in Postgres.
13+
<img src="https://raw.githubusercontent.com/doublefint/vscode-pgsql/master/client/images/example.gif" alt="demo" style="width:480px;"/>
1914

20-
## Using
21-
To run the current sql file through psql (Postgres native client) you must add the following settings to your workspace:
15+
## Usage
2216

17+
- Setup **psql** is in the OS executable path
18+
- Customize Postgress connection ( settings in workspace ):
2319
```javascript
2420
{ "pgsql.connection": "postgres://username:password@host:port/database" }
2521
```
26-
You must also ensure that psql is in the OS executable path (it will be executed as simply "psql" from vscode).
27-
The command to run the current file is "pgsql: run in postgres"
22+
23+
- Open file with pgsql type, press **Ctrl+F5** ( Cmd+F5 on Mac )
24+
- For snippets press **Ctrl+Space**, type '**pg**'
25+
26+
27+
## Release Notes
28+
29+
### 0.0.7
30+
- run pgsql files - press Ctrl+F5
31+
- autosave changed pgsql file before run
32+
- change [Create Function](http://rob.conery.io/2015/02/21/its-time-to-get-over-that-stored-procedure-aversion-you-have/) snippet
33+
34+
### 0.0.2
35+
- add connection config with port and password
36+
- add stderr output ( thanks for [khushboo shah](https://marketplace.visualstudio.com/items?itemName=JPTarquino.postgresql) )
37+
- rename to pgsql

client/package.json

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "pgsql",
33
"description": "postgres language support, snippets, runner. Fixed clone of [email protected] by JPTarquino",
44
"license": "MIT",
5-
"version": "0.0.6",
5+
"version": "0.0.7",
66
"publisher": "doublefint",
77
"engines": { "vscode": "^0.10.1" },
88
"icon": "images/logo.png",
@@ -12,17 +12,20 @@
1212
"main": "./out/extension",
1313
"contributes": {
1414

15-
"commands": [
16-
{ "category": "pgsql", "command": "pgsql.run", "title": "run sql in postgres" }
17-
],
15+
"commands": [{ "category": "pgsql", "command": "pgsql.run", "title": "run in postgres" }],
16+
17+
"keybindings": [{
18+
"command": "pgsql.run",
19+
"key": "ctrl+f5", "mac": "cmd+f5",
20+
"when": "editorTextFocus && editorLangId == pgsql"
21+
}],
1822

1923
"languages": [{
2024
"id": "pgsql",
2125
"extensions": [ ".sql", ".ddl", ".dml", ".pgsql" ],
22-
"aliases": [ "pgsql", "postgres", "postgreSQL", "plpgsql", "pg" ],
26+
"aliases": [ "pgsql", "postgres", "PostgreSQL" ],
2327
"configuration": "./pgsql.configuration.json"
24-
}
25-
],
28+
}],
2629

2730
"snippets": [ { "language": "pgsql", "path": "./snippets/pgsql_snippets.json" } ],
2831

client/pgsql.configuration.json

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,5 @@
99
["[", "]"],
1010
["(", ")"]
1111
]
12-
/*
13-
enhancedBrackets:[
14-
{ openTrigger: 'n', open: /begin$/i, closeComplete: 'end', matchCase: true },
15-
{ openTrigger: 'e', open: /case$/i, closeComplete: 'end', matchCase: true },
16-
{ openTrigger: 'n', open: /when$/i, closeComplete: 'then', matchCase: true }
17-
],
18-
noindentBrackets: '()',
19-
*/
12+
2013
}
Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,43 @@
11
{
2-
3-
"Select simple": {
4-
"prefix": "sel",
2+
"Select Statement": {
3+
"prefix": "pgsql",
54
"body": [
6-
"SELECT ${tableName}.",
7-
"FROM ${tableName}",
8-
"WHERE"
5+
"Select * ",
6+
"From ${tableName}",
7+
"Where 1=1 And id = ? ",
8+
";"
99
],
10-
"description": "Simple Select with Where"
10+
"description": "Insert 'Select * From ... Where ... ' statement"
1111
},
12-
13-
"PL/PGSQL Function": {
14-
"prefix": "funcplpg",
12+
13+
"Create Function": {
14+
"prefix": "pgsql",
1515
"body": [
16-
"CREATE OR REPLACE FUNCTION ${funcName}(i integer) RETURNS integer AS $$",
17-
"BEGIN",
18-
"\tRETURN i + 1;",
19-
"END;",
20-
"$$ LANGUAGE plpgsql;"
16+
"Create Or Replace Function ${funcName}( data json ) Returns json As $$",
17+
"\tDeclare",
18+
"\t\t-- declare variables",
19+
"\t\t-- for example: id int := Cast( data->>'id' as int ); ",
20+
"Begin",
21+
"\t\t --do something useful",
22+
"\tReturn '{}';",
23+
"End;",
24+
"$$ language plpgsql;"
2125
],
22-
"description": "Simple PL/PGSQL Function"
23-
},
24-
25-
"SQL Function": {
26-
"prefix": "funcplpg",
27-
"body": [
28-
"CREATE OR REPLACE FUNCTION ${funcName}(i integer) RETURNS integer AS $$",
29-
"\tSELECT $1, CAST($1 AS text) || ' is text' ",
30-
"$$ LANGUAGE SQL;"
31-
],
32-
"description": "Simple PL/PGSQL Function"
26+
"description": "Insert 'Create Or Replace Function ...' statement"
3327
},
3428

3529
"Create Table": {
36-
"prefix": "tablepgsql",
30+
"prefix": "pgsql",
3731
"body": [
38-
"CREATE TABLE ${tableName} (",
39-
"\tid serial,",
40-
"\tname text,",
41-
"\tquantity integer,",
42-
"\tCONSTRAINT pk_${tableName} PRIMARY KEY(id)",
43-
");"
44-
],
45-
"description": "Create Postgres Table"
32+
"Create Table ${tableName} (",
33+
"\tid serial primary key,",
34+
"\tcd text not null,",
35+
"\tnm text,",
36+
"\t--ref int references another.table,",
37+
"\tcreated timestamptz default now()",
38+
");",
39+
"Create Index on ${tableName} (cd);"
40+
],
41+
"description": "Insert 'Create Table ...' statement"
4642
}
4743
}
Lines changed: 67 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,108 @@
11

2-
import * as vscode from 'vscode';
3-
import * as ChPr from 'child_process';
2+
import { window, workspace, ViewColumn, OutputChannel, Disposable } from 'vscode';
3+
import * as ChildProcess from 'child_process';
44
import LineDecoder from './lineDecoder';
55

66

77
export default class pgsqlCommandProvider {
88

99
private executable :string = 'psql'
1010
private connection :string
11-
private outChannel : vscode.OutputChannel;
11+
private outChannel : OutputChannel;
1212

13-
public activate(subscriptions: vscode.Disposable[]) {
13+
public activate(subscriptions: Disposable[]) {
1414
let cp = this
15-
cp.outChannel = vscode.window.createOutputChannel("pgsql")
16-
vscode.workspace.onDidChangeConfiguration( cp.loadConfiguration, cp, subscriptions )
15+
cp.outChannel = window.createOutputChannel("pgsql")
16+
workspace.onDidChangeConfiguration( cp.loadConfiguration, cp, subscriptions )
1717
cp.loadConfiguration()
1818
}
1919

2020
public loadConfiguration():void {
21-
let section = vscode.workspace.getConfiguration('pgsql')
21+
let section = workspace.getConfiguration('pgsql')
2222
if (section) {
2323
this.connection = section.get<string>('connection', null)
2424
}
2525
}
2626

2727
public run():void {
2828

29-
let cpv = this // [c]ommand [p]ro[v]ider
29+
if ( !window.activeTextEditor ) return // No open text editor
30+
31+
let doc = window.activeTextEditor.document
32+
33+
// if doc never saved before
34+
// vscode change editor after save
35+
if ( doc.isUntitled ) {
36+
37+
window.showInformationMessage( 'pgsql: Please, save document and press Ctrl+F5 again' )
38+
return
39+
}
40+
41+
let pgsql = this
3042

31-
let editor = vscode.window.activeTextEditor
43+
// file already have real filename, just run it via psql
44+
if ( !doc.isDirty ) return pgsql.runFile( doc.fileName )
3245

33-
if ( !editor ) return // No open text editor
46+
// file was changed (dirty), save it andthen
47+
doc.save().then( ( saved: boolean ) => {
48+
if ( !saved ) return
49+
pgsql.runFile( doc.fileName )
50+
}, e => {
51+
console.log( 'pgsql: doc.save() rejected' )
52+
})
3453

35-
let args = [
36-
"-d", cpv.connection,
37-
"-f", editor.document.fileName
38-
]
54+
}
55+
56+
public runFile( fileName: string ): void {
57+
58+
let pgsql = this,
59+
args = [
60+
"-d", pgsql.connection,
61+
"-f", fileName
62+
]
3963

40-
let childProcess = ChPr.spawn( cpv.executable, args )
41-
args.unshift( cpv.executable )
42-
console.log( args.join(" ") )
64+
let cp = ChildProcess.spawn( pgsql.executable, args )
65+
66+
//args.unshift( pgsql.executable )
67+
//console.log( args.join(" ") )
4368

44-
childProcess.on('error', ( err: Error ) => {
69+
cp.on( 'error', ( err: Error ) => {
4570

4671
let ecode: string = (<any>err).code
47-
let message: string = err.message || `Failed to run: ${cpv.executable} ${args.join(' ')}. ${ecode}.`
72+
let defmsg = `Failed to run: ${pgsql.executable} ${args.join(' ')}. ${ecode}.`
73+
let message: string = err.message || defmsg
4874

4975
if ((<any>err).code === 'ENOENT') {
5076
message = `The 'psql' program was not found. Please ensure the 'psql' is in your Path`
5177
}
52-
vscode.window.showInformationMessage( message )
78+
window.showInformationMessage( message )
5379

5480
});
5581

56-
if (childProcess.pid) {
57-
58-
let decoder = new LineDecoder()
59-
cpv.outChannel.show( vscode.ViewColumn.Two )
60-
61-
childProcess.stdout.on('data', (data) => {
62-
decoder.write(data).forEach( function (line:string) {
63-
cpv.outChannel.appendLine( line )
64-
})
82+
pgsql.outChannel.show( ViewColumn.Two )
83+
84+
if ( !cp.pid ){
85+
return pgsql.outChannel.appendLine( 'pgsql: can\'t spawn child proceess' )
86+
}
87+
88+
let decoder = new LineDecoder()
89+
pgsql.outChannel.show( ViewColumn.Two )
90+
91+
cp.stdout.on('data', (data) => {
92+
decoder.write(data).forEach( function (line:string) {
93+
pgsql.outChannel.appendLine( line )
6594
})
66-
67-
childProcess.stderr.on('data', ( data ) => {
68-
decoder.write( data ).forEach( function (line:string) {
69-
cpv.outChannel.appendLine( line )
70-
})
95+
})
96+
97+
cp.stderr.on('data', ( data ) => {
98+
decoder.write( data ).forEach( function (line:string) {
99+
pgsql.outChannel.appendLine( line )
71100
})
101+
})
72102

73-
childProcess.stdout.on('end', () => {
74-
cpv.outChannel.appendLine('pgsql executed.')
75-
})
103+
cp.stdout.on('end', () => {
104+
pgsql.outChannel.appendLine('pgsql end.')
105+
})
76106

77-
}
78107
}
79108
}

0 commit comments

Comments
 (0)