Codelens

Codelens

Since v0.17.15 you can run queries using VS Code’s Codelens feature.

To use a Codelens, first you need to define query blocks using the @block comment. See the example below:

-- @block create company table
-- @conn PGSQL
CREATE TABLE IF NOT EXISTS COMPANY(
    ID INT PRIMARY KEY NOT NULL,
    NAME TEXT NOT NULL,
    AGE INT NOT NULL,
    ADDRESS CHAR(50),
    SALARY REAL
);

INSERT INTO COMPANY VALUES (1, 'Name', 12, 'address', 0.2);

-- @block create department table
CREATE TABLE IF NOT EXISTS DEPARTMENT(
    ID INT PRIMARY KEY NOT NULL,
    DEPT CHAR(50) NOT NULL,
    EMP_ID INT NOT NULL
);

This will be transformed into:

static/codelens/codelens-example.png

Everything inside a block will run together.

If you add -- @conn ConnectionName to the start of your query block, your queries will be executed on the specified connection.

You can even have multiple blocks using different connections in the same file.

Each statement in a block can optionally be preceded by a comment line of the form -- @label Some Text to make the result tab’s label become Some Text instead of the first 16 characters of the statement.

Additionally, if the first statement of a multi-statement block is preceded by two -- @label ... comments then the first will set the label of the block’s tab and the second the label of the statement sub-tab. For example:

-- @block create company table
-- @conn PGSQL
-- @label Set up COMPANY table
-- @label Create
CREATE TABLE IF NOT EXISTS COMPANY(
    ID INT PRIMARY KEY NOT NULL,
    NAME TEXT NOT NULL,
    AGE INT NOT NULL,
    ADDRESS CHAR(50),
    SALARY REAL
);

-- @label Insert row #1
INSERT INTO COMPANY VALUES (1, 'Name', 12, 'address', 0.2);