alex-hhh / emacs-sql-indent

Syntax based indentation for SQL files inside GNU Emacs
GNU General Public License v3.0
121 stars 18 forks source link

Create table inside transaction block #66

Closed GlennS closed 6 years ago

GlennS commented 6 years ago

Hi,

Firstly, thanks very much for creating sql-indent. It works very well, and the customization rules are simple and clear.

I think I've noticed a bug with your block detection, where a CREATE TABLE statement forces everything to outdent all the way to the left:

BEGIN TRANSACTION;
  CREATE TABLE my_table ( id text );

-- I would expect this select statement to also be indented?
SELECT id FROM my_table;
COMMIT;

It still happens if I have some other statements first:

BEGIN TRANSACTION;
  SELECT version();
  CREATE TABLE my_table ( id text );

-- I would expect this select statement to also be indented?
SELECT id FROM my_table;
COMMIT;

Here's what happens if I have multiple blocks:

BEGIN TRANSACTION;
  BEGIN TRANSACTION;
    CREATE TABLE my_table ( id text );

SELECT id FROM my_table;

COMMIT;

For now I shall work around this by disabling transaction block indentation.

alex-hhh commented 6 years ago

This is indeed a bug, however, the bug is that the statement begin transaction; is considered a block start by mistake. CREATE TABLE or any other statement should not be indented after begin transaction;, because the indentation is based on syntactic constructs not on the logical structure of the code and begin transaction; is a statement, just like create table or select....

I will fix this, but I won't be able to do it in the next 2 weeks, unfortunately. I also cannot think of a workaround for this.

GlennS commented 6 years ago

No worries, it's still far better than the default sql-mode indentation, even with this little bug.

alex-hhh commented 6 years ago

fixed in commit ce2002ca8fe039ed1fc58b6d26246b09ca90b89f

GlennS commented 6 years ago

Thanks :)