Giter Site home page Giter Site logo

barklan / inline_sql_syntax Goto Github PK

View Code? Open in Web Editor NEW
48.0 2.0 19.0 735 KB

Highlight and lint inline SQL strings.

Home Page: https://marketplace.visualstudio.com/items?itemName=qufiwefefwoyn.inline-sql-syntax

License: MIT License

TypeScript 100.00%
sql postgres vscode vscode-extension mysql linter

inline_sql_syntax's Introduction

Inline SQL shield

Also available in Open VSX Registry

python

Highlight and lint inline SQL strings. Supported languages are Python, Go, JavaScript, TypeScript, Ruby, Java, C#, Rust, PHP, Lua.

Syntax highlighting works for strings starting with --sql or any of the SELECT, INSERT, INTO, DELETE, UPDATE, CREATE TABLE.

Also works with ES6 Template Strings:

const query = sql`
    select * from book;
`;

Linting and diagnostics powered entirely by awesome joereynolds/sql-lint and works for multiline strings that start with either `--sql (backtick followed by --sql), "--sql or """--sql.

Contributors

jwhitaker-swiftnav
jwhitaker-swiftnav
Wild-W
Connor Bren
tamasfe
Ferenc Tamás
nossrannug
Gunnar Sv Sigurbjörnsson
JonathanWolfe
Jon Wolfe
titouancreach
Titouan CREACH

Safety

The proper way to sanitize data for insertion into your database is to use placeholders for all variables to be inserted into your SQL strings. In other words, NEVER do this (Python example):

query = f"INSERT INTO foo (bar, baz) VALUES ( {variable1}, {variable2} )";

Instead, use $ placeholders (or ? in some databases):

query = "INSERT INTO foo (bar, baz) VALUES ( $1, $2  )";

And then pass the variables to be replaced when you execute the query. For example with pgx (Go example):

err = conn.QueryRow(
    context.Background(),
    "select name, weight from widgets where id=$1",
    42,
).Scan(&name, &weight)

Integration with real database

Integration with real database is available and controlled through VSCode options:

{
    "inlineSQL.enableDBIntegration": true,
    "inlineSQL.dbDriver": "postgres",
    "inlineSQL.dbHost": "localhost",
    "inlineSQL.dbPort": 5432,
    "inlineSQL.dbUser": "postgres",
    "inlineSQL.dbPassword": "postgres"
}

Examples

Python

python

JavaScript/TypeScript

js

Go

go

go

Python JavaScript/TypeScript
Ruby Java

Limitations

Semantic highlighting

Highlighting does not work with semantic token highlighting enabled (feature provided by some LSP servers).

Currently gopls semantic token highlighting (option gopls.ui.semanticTokens - off by default) overrides extension's syntax.

gopls

{
    "gopls.ui.semanticTokens": false
}

rust-analyzer

{
    "rust-analyzer.highlighting.strings": false
}

C#

{
    "csharp.semanticHighlighting.enabled": false
}

Motivation

This small extension is meant to help those who don't use ORM and don't like SQL builders like squirrel, but still want inline sql in their code to be something more than magic strings, helping to avoid small bugs and typos almost instantly.

Related

inline_sql_syntax's People

Contributors

barklan avatar github-actions[bot] avatar jonathanwolfe avatar nossrannug avatar tamasfe avatar titouancreach avatar wild-w avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

inline_sql_syntax's Issues

Can't connect to DB

I have put my database credentials in the settings.json, but it does not seem to connect to my database. Is there any Log?

Code folding feature support for inline SQLs

Hi, Team,

Currently inline SQL helps me a lot on writing codes with SQLs.

Recently I find that in VSCode, a pure SQL script supports code folding for a long SQL statement.

So maybe this extension can be enhanced to have SQL code folding feature, too.

It would be a super-helpful feature.

Best Regards.

Feature request: Ignore leading whitespace for SQL detection

In our code, our queries often look like this:

const query = {
	text: `
		SELECT id
		FROM my_neat_table
...

The leading whitespace is helpful for keeping the queries aligned and tidy. Unfortunately, it also prevents the extension from detecting SQL. I recognize that I can use --sql at the beginning of the query but not everyone uses this extension. My request is to use trimStart() before checking the beginning of the string constant for a matching keyword.

Connecting to a SQLite database

Hi,

thank you for creating this project!

I wanted to ask if it is possible to connect it to a sqlite file, and if yes what I would have to enter in my settings.json.

Thanks!

SQL errors are not cleared out upon save

Using Visual Studio Code 1.73.1

Using

  • enableDBIntegration = true
  • dbDriver = mysql
  • lintSQLFiles = true

Create an .sql file in VS Code, enter a invalid SQL select query and hit Ctrl+S: the plugin warns about an SQL error.
So far so good.

Now, correct the SQL error and hit Ctrl+S again: the red underline does not go away and the error stays.
Close the .sql file and open it back: the error has disappeared.

The extension always seems to display the latest error even when the SQL syntax indeed is valid.

Support for strings starting with "-- sql"

Assuming Ruby (on Rails, with ActiveRecord) and MySQL/MariaDB - these databases require a space after "--" in a comment.

Example

# valid MySQL/MariaDB query
queryA = Model.where("value=1").select("
-- sql
valueA AS A,
valueB AS B
")

# not valid MySQL/MariaDB query :(
queryB = Model.where("value=1").select("
--sql
valueA AS A,
valueB AS B
")

Current behaviour

In queryA: SQL is not highlighted.

In queryB: SQL is highlighted but it's not valid MySQL/MariaDB query.

Expected behaviour

Both queries should have highlighted SQL.

Support for YAML

Like yaml-string-sql, purpose possibility to parse SQL in YAML.

Example:

Single request

   data: |
          --sql
          SELECT * FROM test;

Multiple requests

   data: |
          --sql-start
          SELECT * FROM test;
          SELECT COUNT(*) from test_2;
          --sql-end

Enhanced auto-syntax highlighting

Currently, the extension automatically detects strings starting with certain words as sql syntax but this wouldn't work if the string first starts with tabs, spaces, and line breaks and then has those words. It'd be great if it could skip whitespace characters and check the first valid word for syntax highlighting.

Feature request: Mark a SQL syntax with `/* sql */` and `sql` comment tag in JS

So instead of:

const query = `--sql SELECT * FROM table`;

We can write this (to be more consistent with the rest of inline extensions):

const query = /* sql */`SELECT * FROM table`;
const html  = /* html */`<div id="root"></div>`;

or:

const query   = sql`SELECT * FROM table`;
const html    = html`<div id="root"></div>`
const graphql = gql`query { testConnection }`;

MySQL dbDriver: is it possible to automatically select a database in settings.json?

With the MySQL driver, one must explicitly specify the database name in queries, eg.

SELECT name FROM mydatabase.customer

where 'mydatabase' is the name of the database.
This is really annoying - would it be possible to have the extension automatically use a specified database? So that it would just be sufficient to type

SELECT name FROM customer

Thanks.

SQL capture group highlight correctness

(--sql)|SELECT |INSERT INTO |DELETE |UPDATE |CREATE TABLE)

These should be in diffferent rules - --sql should be comment.sql, the rest should be keyword (probably).

Inline queries formatting

Thank you for the extension!
I'm wondering, is it possible to add support to format inline queries?

Support for PHP

In php files multi line sql is often written as

  $query = <<<SQL
      SELECT * FROM FOO WHERE FOO.bar = '1'
  SQL;

Semicolumn in string in SQL can end the query and raise "Unmatched parentheses" error

Something like :

SELECT series_id, GROUP_CONCAT(auth_name SEPARATOR '; ') AS auths
FROM Authors
NATURAL JOIN `Series-Authors`
GROUP BY series_id

will be considered an "Unmatched parentheses" error with red squiggles everywhere because the semicolumn ; used as a separator here is considered the end of the query, which causes the closing parenthesis to be in 'another' query.

I'm working in python 3.10, inline sql syntax 2.16.0.

.

.

bash shell script heredoc inline SQL support

Hi, Team,

Thanks for your great extension, it helps me a lot😊.

I hope to know whether it is possible to support heredoc SQLs in bash shell script.

Heredoc is a way to pass multiple line SQLs to RDBMS client tools (PostgreSQL psql, MySQL mysql, Oracle sqlplus, etc.) inside shell scripts.

If SQL syntax highlighting can also be supported in shell script heredoc, more people can get benefit from it.

A postgres psql example likes below.

#!/bin/bash
psql -v ON_ERROR_STOP=1 -AqtX <<EOF
--sql
BEGIN;
SELECT * FROM table_1;
INSERT INTO table_2 VALUES (1,'hello');
COMMIT;
EOF

Best Regards.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.