SQLly 1.43: CREATE statements stop getting mangled
I went through procedures, functions, triggers and tables and fixed what SQLly's formatter, Enter key and suggestions did to them. Plus what's still wrong.
SQLly was pretty good at formatting a SELECT and pretty bad at everything that starts with CREATE. Version 1.43 is me going back through procedures, functions, triggers, tables and indexes and fixing what the formatter, the Enter key and the suggestion list were doing to them.
How this started
A little while ago I shipped a bug where typing CREATE TABLE t ( and pressing Enter
left the editor stuck. Format as You Go kept fighting you for the line after the open
parenthesis. I fixed that one, and then had the uncomfortable thought every developer has after a
bug like that: if that got through, what else is broken in the same neighborhood?
So I went looking. I wrote out DDL scripts for every engine SQLly formats (T-SQL, PostgreSQL, MySQL, Oracle, SQLite) and ran each one through the formatter, then through a test that types the script one key at a time and checks the result at every step. The answer to "what else is broken" was "a fair amount".
The formatter treated a procedure like one long query
That's the short version. A procedure body is a list of statements inside blocks, and the
formatter was laying it out as if it were a single query with some odd words in it. Now it
understands routines: the header gets a line per part, BEGIN / END,
IF / ELSE, WHILE, TRY / CATCH and
CASE bodies indent by depth, and each statement sits on its own line.
Type this on one line:
create trigger dbo.trg on dbo.Orders after insert, update as begin set nocount on; select 1; end
and you get this:
CREATE TRIGGER dbo.trg
ON dbo.Orders
AFTER INSERT, UPDATE
AS
BEGIN
SET NOCOUNT ON;
SELECT
1;
END
MySQL DELIMITER scripts are covered too. And because every test script is checked at
every point of being typed, Format as You Go shouldn't scramble a routine you're halfway through
writing.
Half the keywords were cased, half weren't
This one was embarrassing once I saw it. You'd have keyword casing set to upper, and get
ALTER TABLE in capitals followed by a lowercase add. Same for
AFTER, INSTEAD OF, FOR EACH ROW, TRUNCATE,
CASCADE, sequence options and SET NOCOUNT ON. They were left however you
typed them while everything around them got cased.
They follow your casing setting now, and so do data types in ALTER TABLE. The fiddly
part is that a column that happens to be named cache or cycle has to keep
your spelling, so these words are only treated as keywords where the grammar actually puts them.
Auto Detect guessed wrong on T-SQL
If you leave the formatter's dialect on Auto Detect, it has to work out the engine from the text. It was missing a lot of obvious T-SQL. This:
create proc p as begin select 1 end
got read as PostgreSQL, and came back with the END glued onto the last line of the
body. Auto Detect now picks up dbo., NVARCHAR, DATETIME2,
UNIQUEIDENTIFIER, SET NOCOUNT ON, CREATE PROC and
AS BEGIN blocks as T-SQL.
The better fix is not guessing at all. If the tab is bound to a connection, the editor now formats in that connection's dialect. SQLly already knew you were connected to SQL Server. It just wasn't telling the formatter.
Suggestions that belong in a CREATE statement
The suggestion list used to offer query functions almost everywhere in DDL. You'd be naming a new column and get a list of string functions, which is no help to anyone. It now offers what fits the spot you're in:
- Object kinds after
CREATE,ALTERandDROP. - Data types after a column or parameter name, then constraint words after the type.
- The table's own columns in keys, indexes,
CHECKand computed columns. - The right tables and columns after
REFERENCES. - After
DEFAULT, the usual choices first: current time, new ID,NULL.
And where you're naming something new, it stays quiet. There is nothing useful to suggest for a name that doesn't exist yet, so it doesn't try.
Regular queries got some of this too. After FROM and JOIN you only get
functions that return rows, not every scalar function in the database. Typing @ in a
procedure body lists its parameters and variables. END, ELSE,
WHEN and THEN show up inside an open CASE or block.
Enter, again
With Format as You Go off, typing a routine by hand had its own problems. Press Enter after
SELECT 1; inside a BEGIN block and the next line fell out of the block.
A trigger's AFTER INSERT, UPDATE line pushed the body one level too deep. And
END sat at the body's depth instead of lining up with its BEGIN.
New lines now stay at the block's depth, END moves back under its
BEGIN when you press Enter, and the line after a finished statement starts where that
statement did.
How I know it didn't break anything else
My one rule going in was that no existing IntelliSense test was allowed to break without me looking at it personally. Every change to what gets suggested risks trading one person's annoyance for another's. None broke. All 1,507 tests in the IntelliSense suite pass unchanged, plus 19 new ones for DDL and a set of tests that type whole DDL scripts key by key.
Two formatter tests did change, on purpose: they expected lowercase after and
before in triggers, which was the bug.
What's still wrong
I found more than I fixed. Things I know about and haven't done yet:
- If a column name you're typing starts like a keyword (
Id in, say), the suggestion popup closes for a keystroke. - After
RETURNyou only get statement keywords, not your variables. - Right after a procedure's
AS, the list still offersFROM,JOINandGROUP BY, which make no sense there. SELECT 1;breaks onto two lines inside a block but not at the top level. You can see it in the trigger above. It's consistent with how lists get formatted, and it still looks silly.- Option names inside an index's
WITH (...)don't follow your keyword casing.
I haven't written a stored procedure in every dialect in every style, so there will be shapes of DDL I never thought to test. That's where you come in.
Try it on your worst procedure
All of this is in SQLly 1.43, which you can download here. The details are in the changelog. Open the ugliest procedure you maintain, format it, and if it comes back worse than it went in, send it to me. It'll become a test.