TSQL Formatter Service (#229)

- TSqlFormatterService with support for formatting document and text range inside document
- Settings support for all formatting options.
- Extensibility support so that the service can be initialized using MEF extensibility, and can find all necessary TSqlFormatters using the same process

Fix Initialize request error on startup
- Messages were being read from the input channel before all request handlers were registered
- In particular, the Initialize request which is key for any server to talk to the client was getting lost because the message reader thread begins consuming, and we take an extra few hundred milliseconds due to MEF startup before we register the handler
- The solution is to initialize the message handler so request handlers can register, but not actually start processing incoming messages until all handers are ready. This is a safer way to go and should improve reliability overall

Improvements from internal prototype:
- Normalizing baselines to handle the line ending differences on Mac & Linux vs. Windows
- Significantly shortened most lines by implementing base class methods to wrap common objects from Visitor.Context and removing unnecessary "this." syntax
- Refactored the SqlCommonTableExpressionFormatter and related classes to reduce code count significantly. This provides a pattern to follow when refactoring other classes for similar clarity. It's likely a lot of common logic could be found and reused across these.
- Reduced overall code size by adding utility methods
This commit is contained in:
Kevin Cunnane
2017-02-14 23:40:17 -08:00
committed by GitHub
parent eb4f2f2b91
commit 7477642854
212 changed files with 7793 additions and 184 deletions

View File

@@ -0,0 +1,5 @@
select *
from mytable
intersect
select *
from mytable

View File

@@ -0,0 +1,15 @@
select *
from mytable
union
select *
from mytable
union all
select *
from mytable
except
select *
from mytable
intersect
select *
from mytable

View File

@@ -0,0 +1,15 @@
seLect *
from mytable
unIon
selECT *
fROM mytable
union ALL
select *
from mytable
excepT
select *
from mytable
Intersect
select *
from mytable

View File

@@ -0,0 +1,15 @@
SELECT *
FROM mytable
UNION
SELECT *
FROM mytable
UNION ALL
SELECT *
FROM mytable
EXCEPT
SELECT *
FROM mytable
INTERSECT
SELECT *
FROM mytable

View File

@@ -0,0 +1,14 @@
select *
from mytable
union
select *
from mytable
except
select *
from mytable
union all
select *
from mytable
intersect
select *
from mytable

View File

@@ -0,0 +1,19 @@
USE AdventureWorks2008R2;
GO
-- Define the CTE expression name and column list.
WITH
Sales_CTE (SalesPersonID, SalesOrderID, SalesYear)
AS
-- Define the CTE query.
(
SELECT SalesPersonID, SalesOrderID, YEAR(OrderDate) AS SalesYear
FROM Sales.SalesOrderHeader
WHERE SalesPersonID IS NOT NULL
)
-- Define the outer query referencing the CTE name.
SELECT SalesPersonID, COUNT(SalesOrderID) AS TotalSales, SalesYear
FROM Sales_CTE
GROUP BY SalesYear, SalesPersonID
ORDER BY SalesPersonID, SalesYear;
GO

View File

@@ -0,0 +1,32 @@
USE AdventureWorks2008R2;
GO
-- Define the CTE expression name and column list.
WITH
Sales_CTE
(
SalesPersonID,
SalesOrderID,
SalesYear
)
AS
-- Define the CTE query.
(
SELECT
SalesPersonID,
SalesOrderID,
YEAR(OrderDate) AS SalesYear
FROM
Sales.SalesOrderHeader
WHERE SalesPersonID IS NOT NULL
)
-- Define the outer query referencing the CTE name.
SELECT
SalesPersonID,
COUNT(SalesOrderID) AS TotalSales,
SalesYear
FROM
Sales_CTE
GROUP BY SalesYear, SalesPersonID
ORDER BY SalesPersonID, SalesYear;
GO

View File

@@ -0,0 +1,19 @@
USE AdventureWorks2008R2;
GO
-- Define the CTE expression name and column list.
WITH
Sales_CTE (SalesPersonID ,SalesOrderID ,SalesYear)
AS
-- Define the CTE query.
(
SELECT SalesPersonID ,SalesOrderID ,YEAR(OrderDate) AS SalesYear
FROM Sales.SalesOrderHeader
WHERE SalesPersonID IS NOT NULL
)
-- Define the outer query referencing the CTE name.
SELECT SalesPersonID ,COUNT(SalesOrderID) AS TotalSales ,SalesYear
FROM Sales_CTE
GROUP BY SalesYear, SalesPersonID
ORDER BY SalesPersonID, SalesYear;
GO

View File

@@ -0,0 +1,32 @@
USE AdventureWorks2008R2;
GO
-- Define the CTE expression name and column list.
WITH
Sales_CTE
(
SalesPersonID,
SalesOrderID,
SalesYear
)
AS
-- Define the CTE query.
(
SELECT
SalesPersonID,
SalesOrderID,
YEAR(OrderDate) AS SalesYear
FROM
Sales.SalesOrderHeader
WHERE SalesPersonID IS NOT NULL
)
-- Define the outer query referencing the CTE name.
SELECT
SalesPersonID,
COUNT(SalesOrderID) AS TotalSales,
SalesYear
FROM
Sales_CTE
GROUP BY SalesYear, SalesPersonID
ORDER BY SalesPersonID, SalesYear;
GO

View File

@@ -0,0 +1,32 @@
USE AdventureWorks2008R2;
GO
-- Define the CTE expression name and column list.
WITH
Sales_CTE
(
SalesPersonID
,SalesOrderID
,SalesYear
)
AS
-- Define the CTE query.
(
SELECT
SalesPersonID
,SalesOrderID
,YEAR(OrderDate) AS SalesYear
FROM
Sales.SalesOrderHeader
WHERE SalesPersonID IS NOT NULL
)
-- Define the outer query referencing the CTE name.
SELECT
SalesPersonID
,COUNT(SalesOrderID) AS TotalSales
,SalesYear
FROM
Sales_CTE
GROUP BY SalesYear, SalesPersonID
ORDER BY SalesPersonID, SalesYear;
GO

View File

@@ -0,0 +1,32 @@
use AdventureWorks2008R2;
go
-- Define the CTE expression name and column list.
with
Sales_CTE
(
SalesPersonID,
SalesOrderID,
SalesYear
)
as
-- Define the CTE query.
(
select
SalesPersonID,
SalesOrderID,
YEAR(OrderDate) as SalesYear
from
Sales.SalesOrderHeader
where SalesPersonID is not null
)
-- Define the outer query referencing the CTE name.
select
SalesPersonID,
COUNT(SalesOrderID) as TotalSales,
SalesYear
from
Sales_CTE
group by SalesYear, SalesPersonID
order by SalesPersonID, SalesYear;
go

View File

@@ -0,0 +1,17 @@
with
my_initial_table( column1, column2 )
AS
(
select *
from mytable
),
my_other_table( column1X, column2X )
AS
(
select *
from mytable2
)
select distinct top (10) PERCENT with ties
alias1 = SIZE(mytable.mycol1), another = COUNT(new_elements), count(money) AS encore, id
INTO my_new_table
FROM my_initial_table

View File

@@ -0,0 +1,17 @@
USE AdventureWorks2008R2;
GO
-- Define the CTE expression name and column list.
WITH
Sales_CTE (SalesOrderID)
AS
-- Define the CTE query.
(
SELECT SalesOrderID
FROM Sales.SalesOrderHeader
WHERE SalesPersonID IS NOT NULL
)
-- Define the outer query referencing the CTE name.
SELECT COUNT(SalesOrderID) AS TotalSales
FROM Sales_CTE;
GO

View File

@@ -0,0 +1,32 @@
USE AdventureWorks2008R2;
GO
-- Define the CTE expression name and column list.
WITH
Sales_CTE
(
SalesPersonID,
SalesOrderID,
SalesYear
)
AS
-- Define the CTE query.
(
SELECT
SalesPersonID,
SalesOrderID,
YEAR(OrderDate) AS SalesYear
FROM
Sales.SalesOrderHeader
WHERE SalesPersonID IS NOT NULL
)
-- Define the outer query referencing the CTE name.
SELECT
SalesPersonID,
COUNT(SalesOrderID) AS TotalSales,
SalesYear
FROM
Sales_CTE
GROUP BY SalesYear, SalesPersonID
ORDER BY SalesPersonID, SalesYear;
GO

View File

@@ -0,0 +1,32 @@
USE AdventureWorks2008R2;
GO
-- Define the CTE expression name and column list.
WITH
Sales_CTE
(
SalesPersonID,
SalesOrderID,
SalesYear
)
AS
-- Define the CTE query.
(
SELECT
SalesPersonID,
SalesOrderID,
YEAR(OrderDate) AS SalesYear
FROM
Sales.SalesOrderHeader
WHERE SalesPersonID IS NOT NULL
)
-- Define the outer query referencing the CTE name.
SELECT
SalesPersonID,
COUNT(SalesOrderID) AS TotalSales,
SalesYear
FROM
Sales_CTE
GROUP BY SalesYear, SalesPersonID
ORDER BY SalesPersonID, SalesYear;
GO

View File

@@ -0,0 +1,4 @@
-- my comment
Create Procedure P1
AS

View File

@@ -0,0 +1,3 @@
Create Procedure dbo.P1
@param1 int
AS

View File

@@ -0,0 +1,18 @@
-- this is a comment before create table
CREATE TABLE [SimpleTable]
(
-- this is a comment before document
[DocumentID] INT IDENTITY (1, 1) NOT NULL,
-- this is a comment before Title
[Title] NVARCHAR (50) NOT NULL,
-- this is a comment before FileName
[FileName] NVARCHAR (400) NOT NULL,
-- this is a comment before FileExtension
[FileExtension] nvarchar(8)
);

View File

@@ -0,0 +1,22 @@
CREATE TABLE [Person].[Address]
(
[AddressID] INT IDENTITY (1, 1) NOT FOR REPLICATION NOT NULL,
CONSTRAINT [PK_Address_AddressID] PRIMARY KEY CLUSTERED ([AddressID] ASC),
[AddressLine1] NVARCHAR (60) NOT NULL,
[AddressLine2] NVARCHAR (60) NULL,
Address NVarChar (60) Null,
[City] NVARCHAR (30) NOT NULL,
[StateProvinceID] INT NOT NULL,
[PostalCode] NVARCHAR (15) NOT NULL,
[rowguid] UNIQUEIDENTIFIER CONSTRAINT [DF_Address_rowguid] DEFAULT (newid()) ROWGUIDCOL NOT NULL,
[ModifiedDate] DATETIME CONSTRAINT [DF_Address_ModifiedDate] DEFAULT (getdate()) NOT NULL,
CONSTRAINT [FK_Address_StateProvince_StateProvinceID] FOREIGN KEY ([StateProvinceID]) REFERENCES [Person].[StateProvince] ([StateProvinceID]) ON DELETE NO ACTION ON UPDATE NO ACTION
);

View File

@@ -0,0 +1,22 @@
CREATE TABLE [Person].[Address]
(
[AddressID] INT IDENTITY (1, 1) NOT FOR REPLICATION NOT NULL,
CONSTRAINT [PK_Address_AddressID] PRIMARY KEY CLUSTERED ([AddressID] ASC),
[AddressLine1] NVARCHAR (60) NOT NULL,
[AddressLine2] NVARCHAR (60) NULL,
Address NVarChar (60) Null,
[City] NVARCHAR (30) NOT NULL,
[StateProvinceID] INT NOT NULL,
[PostalCode] NVARCHAR (15) NOT NULL,
[rowguid] UNIQUEIDENTIFIER CONSTRAINT [DF_Address_rowguid] DEFAULT (newid()) ROWGUIDCOL NOT NULL,
[ModifiedDate] DATETIME CONSTRAINT [DF_Address_ModifiedDate] DEFAULT (getdate()) NOT NULL,
CONSTRAINT [FK_Address_StateProvince_StateProvinceID] FOREIGN KEY ([StateProvinceID]) REFERENCES [Person].[StateProvince] ([StateProvinceID]) ON DELETE NO ACTION ON UPDATE NO ACTION
);

View File

@@ -0,0 +1,9 @@
CREATE TABLE db_name.schema_name.table_name
(
col_name1 INT,
col_name2 VARCHAR (20)
-- here is my comment
)
ON partition_schema_name (partition_column_name);

View File

@@ -0,0 +1,18 @@
-- this is a comment before create table
CREATE TABLE [SimpleTable]
(
-- this is a comment before document
[DocumentID] INT IDENTITY (1, 1) NOT NULL,
-- this is a comment before Title
[Title] NVARCHAR (50) NOT NULL,
-- this is a comment before FileName
[FileName] NVARCHAR (400) NOT NULL,
-- this is a comment before FileExtension
[FileExtension] nvarchar(8)
);

View File

@@ -0,0 +1,18 @@
-- this is a comment before create table
CREATE TABLE [SimpleTable]
(
-- this is a comment before document
[DocumentID] INT IDENTITY (1, 1) NOT NULL,
-- this is a comment before Title
[Title] NVARCHAR (50) NOT NULL,
-- this is a comment before FileName
[FileName] NVARCHAR (400) NOT NULL,
-- this is a comment before FileExtension
[FileExtension] nvarchar(8)
);

View File

@@ -0,0 +1,18 @@
-- this is a comment before create table
CREATE TABLE [SimpleTable]
(
-- this is a comment before document
[DocumentID] INT IDENTITY (1, 1) NOT NULL,
-- this is a comment before Title
[Title] NVARCHAR (50) NOT NULL,
-- this is a comment before FileName
[FileName] NVARCHAR (400) NOT NULL,
-- this is a comment before FileExtension
[FileExtension] nvarchar(8)
);

View File

@@ -0,0 +1,18 @@
-- this is a comment before create table
CREATE TABLE [SimpleTable]
(
-- this is a comment before document
[DocumentID] INT IDENTITY (1, 1) NOT NULL
-- this is a comment before Title
,[Title] NVARCHAR (50) NOT NULL
-- this is a comment before FileName
,[FileName] NVARCHAR (400) NOT NULL
-- this is a comment before FileExtension
,[FileExtension] nvarchar(8)
);

View File

@@ -0,0 +1,13 @@
create table t1
(
id INT not null /* awesome */
,x INT not null
/* cool */
,y INT not null
-- tremendeous
)
with
(
data_compression = row
);

View File

@@ -0,0 +1,7 @@
create table t1
(
col1 int,
col2 int
)

View File

@@ -0,0 +1,18 @@
-- this is a comment before create table
CREATE TABLE [SimpleTable]
(
-- this is a comment before document
[DocumentID] int IDENTITY (1, 1) NOT NULL,
-- this is a comment before Title
[Title] nvarchar (50) NOT NULL,
-- this is a comment before FileName
[FileName] nvarchar (400) NOT NULL,
-- this is a comment before FileExtension
[FileExtension] nvarchar(8)
);

View File

@@ -0,0 +1,18 @@
-- this is a comment before create table
create table [SimpleTable]
(
-- this is a comment before document
[DocumentID] INT identity (1, 1) not null,
-- this is a comment before Title
[Title] NVARCHAR (50) not null,
-- this is a comment before FileName
[FileName] NVARCHAR (400) not null,
-- this is a comment before FileExtension
[FileExtension] nvarchar(8)
);

View File

@@ -0,0 +1,18 @@
-- this is a comment before create table
CREATE TABLE [SimpleTable]
(
-- this is a comment before document
[DocumentID] INT IDENTITY (1, 1) NOT NULL,
-- this is a comment before Title
[Title] NVARCHAR (50) NOT NULL,
-- this is a comment before FileName
[FileName] NVARCHAR (400) NOT NULL,
-- this is a comment before FileExtension
[FileExtension] NVARCHAR(8)
);

View File

@@ -0,0 +1,18 @@
-- this is a comment before create table
CREATE TABLE [SimpleTable]
(
-- this is a comment before document
[DocumentID] INT IDENTITY (1, 1) NOT NULL,
-- this is a comment before Title
[Title] NVARCHAR (50) NOT NULL,
-- this is a comment before FileName
[FileName] NVARCHAR (400) NOT NULL,
-- this is a comment before FileExtension
[FileExtension] nvarchar(8)
);

View File

@@ -0,0 +1,18 @@
-- this is a comment before create table
CREATE TABLE [SimpleTable]
(
-- this is a comment before document
[DocumentID] INT IDENTITY (1, 1) NOT NULL,
-- this is a comment before Title
[Title] NVARCHAR (50) NOT NULL,
-- this is a comment before FileName
[FileName] NVARCHAR (400) NOT NULL,
-- this is a comment before FileExtension
[FileExtension] nvarchar(8)
);

View File

@@ -0,0 +1,13 @@
CREATE VIEW my_schema.my_view_name
(
column1,
column2,
column3
)
WITH
SCHEMABINDING,
ENCRYPTION,
VIEW_METADATA
AS
SELECT *
FROM mytable

View File

@@ -0,0 +1,25 @@
CREATE VIEW my_schema.my_view_name
--and
(
/* we */
column1,
column2,
/* can */
column3
)
-- put
WITH
/* comments */
/*even multiple ones */
-- and of various types
SCHEMABINDING,
-- everywhere
ENCRYPTION,
-- we
VIEW_METADATA
/* want*/
AS
/* because */
SELECT *
FROM mytable
-- it's SQL

View File

@@ -0,0 +1,8 @@
CREATE VIEW my_view
(
mycol,
my_other_col
)
AS
select *
from mytable

View File

@@ -0,0 +1,8 @@
CREATE VIEW my_view_name
WITH
SCHEMABINDING,
ENCRYPTION,
VIEW_METADATA
AS
SELECT *
FROM mytable

View File

@@ -0,0 +1,8 @@
CREATE VIEW my_view
(
mycol
)
AS
select *
from mytable

View File

@@ -0,0 +1,10 @@
CREATE VIEW my_view
(
mycol
)
WITH
ENCRYPTION
AS
select *
from mytable

View File

@@ -0,0 +1,8 @@
CREATE VIEW my_view
WITH
ENCRYPTION
AS
select *
from mytable

View File

@@ -0,0 +1,5 @@
CREATE VIEW my_view
AS
select *
from mytable

View File

@@ -0,0 +1,3 @@
INSERT INTO NUMBERS
DEFAULT VALUES;

View File

@@ -0,0 +1,20 @@
with
my_initial_table( column1, column2 )
AS
(
select *
from mytable
),
my_other_table( column1, column2 )
AS
(
select *
from mytable
)
Insert top (10) PERCENT
into myserver.mydatabase.myschema.mytable_or_view WITH (TABLOCK)
( col1, col2, col3, col4, col5 )
VALUES
( DEFault, NULL, 1, N'My Value', 'Today'),
( 45, 5, 1, N'My Last Value', 'Yesterday'),
( 8, 6, 1, N'My Next Value', 'Tomorrow')

View File

@@ -0,0 +1,5 @@
INSERT OPENQUERY (OracleSvr, 'SELECT name FROM joe.titles')
-- comment
VALUES
('NewTitle');

View File

@@ -0,0 +1,8 @@
INSERT Production.ScrapReason
OUTPUT
INSERTED.ScrapReasonID, INSERTED.Name, INSERTED.ModifiedDate
INTO @MyTableVar
VALUES
(N'Operator error', GETDATE());

View File

@@ -0,0 +1,8 @@
INSERT Production.ScrapReason
/*comments like this one*/
OUTPUT
INSERTED.ScrapReasonID, INSERTED.Name, INSERTED.ModifiedDate
INTO @MyTableVar
VALUES
(N'Operator error',/*comments like this one*/ GETDATE());

View File

@@ -0,0 +1,9 @@
INSERT INTO myTable
(FileName, FileType, Document)
SELECT
'Text1.txt' AS FileName,
'.txt' AS FileType,
*
FROM
OPENROWSET(BULK N'C:\Text1.txt', SINGLE_BLOB) AS Document;

View File

@@ -0,0 +1,10 @@
INSERT INTO myTable
(FileName, FileType, Document)
--comment
SELECT
'Text1.txt' AS FileName,
'.txt' AS FileType,
*
FROM
OPENROWSET(BULK N'C:\Text1.txt', SINGLE_BLOB) AS Document;

View File

@@ -0,0 +1,6 @@
insert top (10)
into mytable
values
(10, 11);

View File

@@ -0,0 +1,6 @@
insert top (10)
mytable
/*comments like this one*//*comments like this one*/
values
/*comments like this one*/
(10, 11);

View File

@@ -0,0 +1,7 @@
select c1, c2, t3.c3
From t1, t2, t3

View File

@@ -0,0 +1,13 @@
select
c1,
c2,
t3.c3
From
t1,
t2,
t3

View File

@@ -0,0 +1,9 @@
select c1, c2, t3.c3
From t1, t2, t3
for browse

View File

@@ -0,0 +1,9 @@
select c1, c2, t3.c3
From t1, t2, t3
FOR XML AUTO, TYPE, XMLSCHEMA, ELEMENTS XSINIL;

View File

@@ -0,0 +1,13 @@
select
c1,
c2,
t3.c3
from
t1,
t2,
t3

View File

@@ -0,0 +1,13 @@
SELECT
c1,
c2,
t3.c3
FROM
t1,
t2,
t3

View File

@@ -0,0 +1,13 @@
select
c1,
c2,
t3.c3
From
t1,
t2,
t3