Git Product home page Git Product logo

prettier-plugin-hana-sql's Introduction

Prettier plugin SQL-CST npm version build status

A Prettier plugin for SQL that uses sql-parser-cst and the actual Prettier formatting algorithm.

Try it live!

Like Prettier for JavaScript, this plugin formats SQL expressions differently depending on their length. A short SQL query will be formatted on a single line:

SELECT a, b, c FROM tbl WHERE x > 10;

A longer query, will get each clause printed on a separate line:

SELECT id, client.name, client.priority
FROM client
WHERE client.id IN (12, 18, 121);

An even longer one gets the contents of each clause indented:

SELECT
  client.id,
  client.name AS client_name,
  organization.name AS org_name,
  count(orders.id) AS nr_of_orders
FROM
  client
  LEFT JOIN organization ON client.organization_id = organization.id
  LEFT JOIN orders ON orders.client_id = client.id
WHERE
  client.status = 'active'
  AND client.id IN (28, 214, 457)
  AND orders.status IN ('active', 'pending', 'processing')
GROUP BY client.id
ORDER BY client.name
LIMIT 100;

Formatting philosophy

  • Adapt formatting based on expression length.
  • Stick to one style and avoid configuration options.
  • Format embedded languages (like JSON data and JavaScript programs).
  • When unsure, preserve existing syntax.

Currently this plugin preserves most of the syntax elements and concentrates mainly on the layout of whitespace.

See STYLE_GUIDE for overview of the SQL formatting style used.

Getting started

Install it as any other Prettier plugin:

npm install --save-dev prettier prettier-plugin-sql-cst

Then use it on SQL files through Prettier command line tool or Prettier extension for your editor of choice.

Choosing an SQL dialect

By default the plugin will determine SQL dialect based on file extension:

  • .sql or .sqlite - SQLite
  • .bigquery - BigQuery

You can override this behavior with a prettier configuration:

{
  "overrides": [
    {
      "files": ["*.sql"],
      "options": { "parser": "bigquery" }
    }
  ]
}

The plugin provides the following parsers:

  • sqlite
  • bigquery

Configuration

The standard Prettier options printWidth, tabWidth, useTabs apply. There are also some SQL-specific options:

API Option Default Description
sqlKeywordCase upper Converts SQL keywords to upper or lower case, or preserve existing.
sqlParamTypes [] Array of bound parameter types: ?, ?nr, :name, @name, $name.

Limitations and development status

Currently this plugin supports two SQL dialects:

  • SQLite - full support.
  • BigQuery - full support.

The specifics of the SQL formatting style are still very much subject to change. Though the general principles should be mostly in place by now.

prettier-plugin-hana-sql's People

Contributors

lalilaloe avatar nene avatar

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.