sql-formatter-org / sql-formatter

A whitespace formatter for different query languages
https://sql-formatter-org.github.io/sql-formatter/
MIT License
2.37k stars 405 forks source link

[Question] How to apply formatting inside of table definition? #721

Closed christopher-caldwell closed 8 months ago

christopher-caldwell commented 8 months ago

Apologies if this is answered somewhere, but I have not been able to find it.

Is there a way to apply formatting inside of the table definition?

Result

DROP TABLE IF EXISTS contact CASCADE;

CREATE TABLE contact (
  contact_id SMALLINT,
  date_created TIMESTAMPTZ DEFAULT DEFAULT CURRENT_TIMESTAMP,
  date_modified TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
  first_name VARCHAR(100) NOT NULL,
  last_name VARCHAR(100),
  company VARCHAR(100),
  notes TEXT,
  PRIMARY KEY (id)
);

Want

DROP TABLE IF EXISTS contact CASCADE;

CREATE TABLE contact (
  contact_id    SMALLINT      NOT NULL GENERATED  ALWAYS AS IDENTITY,
  date_created  TIMESTAMPTZ   NOT NULL DEFAULT    CURRENT_TIMESTAMP,
  date_modified TIMESTAMPTZ   NOT NULL DEFAULT    CURRENT_TIMESTAMP,
  first_name    VARCHAR(100)  NOT NULL,
  last_name     VARCHAR(100),
  company       VARCHAR(100),
  notes         TEXT,
  PRIMARY KEY   (id)
);

I'm not sure if this supported, but figured I'd ask.

indentStyle: tabularLeft does a version of this, but it doesn't extend to inside the parenthesis of the definition

DROP      TABLE IF EXISTS contact CASCADE;

CREATE    TABLE contact (
contact_id SMALLINT NOT NULL GENERATED ALWAYS AS IDENTITY,
date_created TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
date_modified TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100),
company VARCHAR(100),
notes TEXT,
PRIMARY KEY (id)
);
nene commented 8 months ago

No. This style is not supported and there's currently no plan of supporting it.

Also the indentStyle:tabularLeft/Right is deprecated and will be removed in a future release.

christopher-caldwell commented 8 months ago

@nene Oh, I see that in GIANT CAPITAL LETTERS lol. My bad.

Thanks for the response. What's the reason for this not being a configuration option?

nene commented 8 months ago

If you mean, "why is the identStyle option going to be removed"...

...you can already see it from your own example that the output of it looks pretty bad. There was no clear vision of what sort of formatting style this feature is meant to support. Plus it was implemented as one big hack, so it has lots of problems.

If you mean, "why don't we add a config option for the style you're suggesting"...

...all such tabular alignment styles are problematic. They might look nice in a simple case, but if you try to generalize it, you'll bump into all sorts of strange cases. For example, if on has several column constraints... I really don't know how one should format this in this tabular style:

CREATE TABLE project (
  id                           SMALLINT NOT NULL,
  initial_temporary_creator_id BIGINT   NOT NULL CHECK (initial_temporary_creator_id > 0) NO INHERIT REFERENCES company_user (user_id)
);

...it also means one needs to reindent the whole statement when the name of the longest column name changes. Which is a bad characteristic of any code formatting style.

...and it's pretty hard to implement with the current architecture of sql-formatter which doesn't really reliably distinguish between column names, data types and doesn't even a understand the concept of column constraint.

christopher-caldwell commented 8 months ago

@nene Understood! Thank you. My question of why was aimed at your second response.

Thanks for taking the time to explain, I appreciate it!