Skip to content

Commit

Permalink
Added BIML generation code for anchor/attribute loading.
Browse files Browse the repository at this point in the history
  • Loading branch information
Roenbaeck committed Apr 28, 2017
1 parent 209750f commit 17d1bd6
Show file tree
Hide file tree
Showing 10 changed files with 884 additions and 85 deletions.
375 changes: 375 additions & 0 deletions Example/biml/target.biml

Large diffs are not rendered by default.

114 changes: 37 additions & 77 deletions Example/sources/source.sql
Expand Up @@ -19,6 +19,7 @@ GO
IF Object_Id('etl.ToLocalTime', 'FS') IS NOT NULL
DROP FUNCTION [etl].[ToLocalTime];
GO
-- BEGIN! LEGACY --
IF EXISTS (
SELECT
*
Expand All @@ -28,100 +29,59 @@ IF EXISTS (
name = 'etlUtilities'
)
DROP ASSEMBLY etlUtilities;
IF NOT EXISTS (
SELECT
*
FROM
sys.assemblies
WHERE
name = 'etlUtilities'
)
BEGIN TRY -- using Microsoft.SQLServer.Types version 13 (2016)
CREATE ASSEMBLY etlUtilities
AUTHORIZATION dbo
FROM 'C:\sisula\code\Utilities2016.dll'
WITH PERMISSION_SET = SAFE;
PRINT 'The .NET CLR for SQL Server 2016 was installed.'
END TRY
BEGIN CATCH
PRINT 'The .NET CLR for SQL Server 2016 was NOT installed.'
END CATCH
IF NOT EXISTS (
SELECT
*
FROM
sys.assemblies
WHERE
name = 'etlUtilities'
)
BEGIN TRY -- using Microsoft.SQLServer.Types version 12 (2014)
CREATE ASSEMBLY etlUtilities
AUTHORIZATION dbo
FROM 'C:\sisula\code\Utilities2014.dll'
WITH PERMISSION_SET = SAFE;
PRINT 'The .NET CLR for SQL Server 2014 was installed.'
END TRY
BEGIN CATCH
PRINT 'The .NET CLR for SQL Server 2014 was NOT installed.'
END CATCH
IF NOT EXISTS (
-- END! LEGACY --
declare @version char(4) =
case
when @@VERSION like '% 2016 %' then '2016'
when @@VERSION like '% 2014 %' then '2014'
when @@VERSION like '% 2012 %' then '2012'
when @@VERSION like '% 2008 %' then '2008'
else '????'
end
IF EXISTS (
SELECT
*
FROM
sys.assemblies
WHERE
name = 'etlUtilities'
name = 'Utilities'
)
BEGIN TRY -- using Microsoft.SQLServer.Types version 11 (2012)
CREATE ASSEMBLY etlUtilities
AUTHORIZATION dbo
FROM 'C:\sisula\code\Utilities2012.dll'
BEGIN TRY
ALTER ASSEMBLY Utilities
FROM 'C:\sisula\code\Utilities' + @version + '.dll'
WITH PERMISSION_SET = SAFE;
PRINT 'The .NET CLR for SQL Server 2012 was installed.'
END TRY
BEGIN CATCH
PRINT 'The .NET CLR for SQL Server 2012 was NOT installed.'
END CATCH
IF NOT EXISTS (
SELECT
*
FROM
sys.assemblies
WHERE
name = 'etlUtilities'
)
BEGIN TRY -- using Microsoft.SQLServer.Types version 10 (2008)
CREATE ASSEMBLY etlUtilities
PRINT 'The .NET CLR for SQL Server ' + @version + ' was updated.'
END TRY BEGIN CATCH END CATCH
ELSE -- assembly does not exist
BEGIN TRY
CREATE ASSEMBLY Utilities
AUTHORIZATION dbo
FROM 'C:\sisula\code\Utilities2008.dll'
FROM 'C:\sisula\code\Utilities' + @version + '.dll'
WITH PERMISSION_SET = SAFE;
PRINT 'The .NET CLR for SQL Server 2008 was installed.'
END TRY
BEGIN CATCH
PRINT 'The .NET CLR for SQL Server 2008 was NOT installed.'
END CATCH
PRINT 'The .NET CLR for SQL Server ' + @version + ' was installed.'
END TRY BEGIN CATCH END CATCH
GO
CREATE FUNCTION [etl].Splitter(@row AS nvarchar(max), @pattern AS nvarchar(4000))
RETURNS TABLE (
[match] nvarchar(max),
[index] int
) AS EXTERNAL NAME etlUtilities.Splitter.InitMethod;
) AS EXTERNAL NAME Utilities.Splitter.InitMethod;
GO
CREATE FUNCTION [etl].IsType(@dataValue AS nvarchar(max), @dataType AS nvarchar(4000))
RETURNS bit
AS EXTERNAL NAME etlUtilities.IsType.InitMethod;
AS EXTERNAL NAME Utilities.IsType.InitMethod;
GO
CREATE FUNCTION [etl].ToLocalTime(@sqlDatetime AS datetime)
RETURNS datetime
AS EXTERNAL NAME etlUtilities.ToLocalTime.InitMethod;
AS EXTERNAL NAME Utilities.ToLocalTime.InitMethod;
GO
CREATE PROCEDURE [etl].ColumnSplitter(
@table AS nvarchar(4000),
@column AS nvarchar(4000),
@pattern AS nvarchar(4000),
@includeColumns AS nvarchar(4000) = null
)
AS EXTERNAL NAME etlUtilities.ColumnSplitter.InitMethod;
AS EXTERNAL NAME Utilities.ColumnSplitter.InitMethod;
GO
IF NOT EXISTS (
SELECT value
Expand Down Expand Up @@ -157,7 +117,7 @@ GO
-- _timestamp
-- The time the row was created.
--
-- Generated: Mon May 2 11:10:30 UTC+0200 2016 by e-lronnback
-- Generated: Fri Apr 28 14:44:02 UTC+0200 2017 by e-lronnback
-- From: TSE-9B50TY1 in the CORPNET domain
--------------------------------------------------------------------------
CREATE PROCEDURE [etl].[NYPD_Vehicle_CreateRawTable] (
Expand Down Expand Up @@ -226,7 +186,7 @@ GO
-- the target of the BULK INSERT operation, since it cannot insert
-- into a table with multiple columns without a format file.
--
-- Generated: Mon May 2 11:10:30 UTC+0200 2016 by e-lronnback
-- Generated: Fri Apr 28 14:44:02 UTC+0200 2017 by e-lronnback
-- From: TSE-9B50TY1 in the CORPNET domain
--------------------------------------------------------------------------
CREATE PROCEDURE [etl].[NYPD_Vehicle_CreateInsertView] (
Expand Down Expand Up @@ -296,7 +256,7 @@ GO
-- This job may called multiple times in a workflow when more than
-- one file matching a given filename pattern is found.
--
-- Generated: Mon May 2 11:10:30 UTC+0200 2016 by e-lronnback
-- Generated: Fri Apr 28 14:44:02 UTC+0200 2017 by e-lronnback
-- From: TSE-9B50TY1 in the CORPNET domain
--------------------------------------------------------------------------
CREATE PROCEDURE [etl].[NYPD_Vehicle_BulkInsert] (
Expand Down Expand Up @@ -425,7 +385,7 @@ GO
-- Create: NYPD_Vehicle_Collision_Split
-- Create: NYPD_Vehicle_CollisionMetadata_Split
--
-- Generated: Mon May 2 11:10:30 UTC+0200 2016 by e-lronnback
-- Generated: Fri Apr 28 14:44:02 UTC+0200 2017 by e-lronnback
-- From: TSE-9B50TY1 in the CORPNET domain
--------------------------------------------------------------------------
CREATE PROCEDURE [etl].[NYPD_Vehicle_CreateSplitViews] (
Expand Down Expand Up @@ -545,7 +505,7 @@ BEGIN TRY
FROM (
SELECT
[match],
ROW_NUMBER() OVER (ORDER BY [index] ASC) AS idx
ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS idx
FROM
[etl].Splitter(ISNULL(forcedMaterializationTrick.[row], ''''), N''(.*?);[0-9]{4}([0-9]{9})[^;]*;(.*?);(.*?);(.*?);(.*?);(.*?);(.*?);(.*?);(.*?);'')
) s
Expand Down Expand Up @@ -646,7 +606,7 @@ BEGIN TRY
FROM (
SELECT
[match],
ROW_NUMBER() OVER (ORDER BY [index] ASC) AS idx
ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS idx
FROM
[etl].Splitter(ISNULL(forcedMaterializationTrick.[row], ''''), N''(?=.*?(\w+)\s+[0-9]{4})?(?=.*?\w+\s+([0-9]{4}))?(?=.*?NOTES[^:]*:(.*))?'')
) s
Expand Down Expand Up @@ -707,7 +667,7 @@ GO
-- Create: NYPD_Vehicle_Collision_Error
-- Create: NYPD_Vehicle_CollisionMetadata_Error
--
-- Generated: Mon May 2 11:10:30 UTC+0200 2016 by e-lronnback
-- Generated: Fri Apr 28 14:44:02 UTC+0200 2017 by e-lronnback
-- From: TSE-9B50TY1 in the CORPNET domain
--------------------------------------------------------------------------
CREATE PROCEDURE [etl].[NYPD_Vehicle_CreateErrorViews] (
Expand Down Expand Up @@ -817,7 +777,7 @@ GO
-- Create: NYPD_Vehicle_Collision_Typed
-- Create: NYPD_Vehicle_CollisionMetadata_Typed
--
-- Generated: Mon May 2 11:10:30 UTC+0200 2016 by e-lronnback
-- Generated: Fri Apr 28 14:44:02 UTC+0200 2017 by e-lronnback
-- From: TSE-9B50TY1 in the CORPNET domain
--------------------------------------------------------------------------
CREATE PROCEDURE [etl].[NYPD_Vehicle_CreateTypedTables] (
Expand Down Expand Up @@ -926,7 +886,7 @@ GO
-- Load: NYPD_Vehicle_Collision_Split into NYPD_Vehicle_Collision_Typed
-- Load: NYPD_Vehicle_CollisionMetadata_Split into NYPD_Vehicle_CollisionMetadata_Typed
--
-- Generated: Mon May 2 11:10:30 UTC+0200 2016 by e-lronnback
-- Generated: Fri Apr 28 14:44:02 UTC+0200 2017 by e-lronnback
-- From: TSE-9B50TY1 in the CORPNET domain
--------------------------------------------------------------------------
CREATE PROCEDURE [etl].[NYPD_Vehicle_SplitRawIntoTyped] (
Expand Down Expand Up @@ -1106,7 +1066,7 @@ GO
-- Key: CrossStreet (as primary key)
-- Key: CollisionOrder (as primary key)
--
-- Generated: Mon May 2 11:10:30 UTC+0200 2016 by e-lronnback
-- Generated: Fri Apr 28 14:44:02 UTC+0200 2017 by e-lronnback
-- From: TSE-9B50TY1 in the CORPNET domain
--------------------------------------------------------------------------
CREATE PROCEDURE [etl].[NYPD_Vehicle_AddKeysToTyped] (
Expand Down
8 changes: 4 additions & 4 deletions Example/targets/target.sql
Expand Up @@ -11,7 +11,7 @@ GO
-- Map: StreetName to ST_NAM_Street_Name (as natural key)
-- Map: metadata_CO_ID to Metadata_ST (as metadata)
--
-- Generated: Mon May 2 11:10:30 UTC+0200 2016 by e-lronnback
-- Generated: Fri Apr 28 14:44:02 UTC+0200 2017 by e-lronnback
-- From: TSE-9B50TY1 in the CORPNET domain
--------------------------------------------------------------------------
CREATE PROCEDURE [etl].[lST_Street__NYPD_Vehicle_Collision_Typed] (
Expand Down Expand Up @@ -134,7 +134,7 @@ GO
-- Map: IS_ID_of to IS_ID (as surrogate key)
-- Map: metadata_CO_ID to Metadata_IS (as metadata)
--
-- Generated: Mon May 2 11:10:30 UTC+0200 2016 by e-lronnback
-- Generated: Fri Apr 28 14:44:02 UTC+0200 2017 by e-lronnback
-- From: TSE-9B50TY1 in the CORPNET domain
--------------------------------------------------------------------------
CREATE PROCEDURE [etl].[lIS_Intersection__NYPD_Vehicle_Collision_Typed__1] (
Expand Down Expand Up @@ -262,7 +262,7 @@ GO
-- Map: IS_ID_of to IS_ID_of
-- Map: metadata_CO_ID to Metadata_ST_intersecting_IS_of_ST_crossing (as metadata)
--
-- Generated: Mon May 2 11:10:30 UTC+0200 2016 by e-lronnback
-- Generated: Fri Apr 28 14:44:02 UTC+0200 2017 by e-lronnback
-- From: TSE-9B50TY1 in the CORPNET domain
--------------------------------------------------------------------------
CREATE PROCEDURE [etl].[lST_intersecting_IS_of_ST_crossing__NYPD_Vehicle_Collision_Typed] (
Expand Down Expand Up @@ -432,7 +432,7 @@ GO
-- Map: CollisionKilledCount to IS_KIL_Intersection_KilledCount
-- Map: ChangedAt to IS_KIL_ChangedAt
--
-- Generated: Mon May 2 11:10:30 UTC+0200 2016 by e-lronnback
-- Generated: Fri Apr 28 14:44:02 UTC+0200 2017 by e-lronnback
-- From: TSE-9B50TY1 in the CORPNET domain
--------------------------------------------------------------------------
CREATE PROCEDURE [etl].[lIS_Intersection__NYPD_Vehicle_Collision_Typed__2] (
Expand Down
8 changes: 4 additions & 4 deletions Example/workflows/workflow.sql
Expand Up @@ -25,7 +25,7 @@ OPEN schedules;
IF EXISTS (select job_id from [dbo].[sysjobs] where name = 'NYPD_Vehicle_Staging')
BEGIN
FETCH FIRST FROM schedules INTO @scheduleId, @scheduleName;
WHILE(@@FETCH_STATUS = 0)
WHILE(@@FETCH_STATUS = 0)
BEGIN
--PRINT 'Detaching schedule "' + @scheduleName + '" from NYPD_Vehicle_Staging';
EXEC msdb.dbo.sp_detach_schedule @job_name = 'NYPD_Vehicle_Staging', @schedule_id = @scheduleId;
Expand Down Expand Up @@ -233,7 +233,7 @@ EXEC sp_update_jobstep
@on_success_action = 4, -- go to step with id
@on_success_step_id = 11;
FETCH FIRST FROM schedules INTO @scheduleId, @scheduleName;
WHILE(@@FETCH_STATUS = 0)
WHILE(@@FETCH_STATUS = 0)
BEGIN
--PRINT 'Attaching schedule "' + @scheduleName + '" to NYPD_Vehicle_Staging';
EXEC msdb.dbo.sp_attach_schedule @job_name = 'NYPD_Vehicle_Staging', @schedule_id = @scheduleId;
Expand Down Expand Up @@ -264,7 +264,7 @@ OPEN schedules;
IF EXISTS (select job_id from [dbo].[sysjobs] where name = 'NYPD_Vehicle_Loading')
BEGIN
FETCH FIRST FROM schedules INTO @scheduleId, @scheduleName;
WHILE(@@FETCH_STATUS = 0)
WHILE(@@FETCH_STATUS = 0)
BEGIN
--PRINT 'Detaching schedule "' + @scheduleName + '" from NYPD_Vehicle_Loading';
EXEC msdb.dbo.sp_detach_schedule @job_name = 'NYPD_Vehicle_Loading', @schedule_id = @scheduleId;
Expand Down Expand Up @@ -372,7 +372,7 @@ EXEC sp_update_jobstep
@on_success_action = 4, -- go to step with id
@on_success_step_id = 6;
FETCH FIRST FROM schedules INTO @scheduleId, @scheduleName;
WHILE(@@FETCH_STATUS = 0)
WHILE(@@FETCH_STATUS = 0)
BEGIN
--PRINT 'Attaching schedule "' + @scheduleName + '" to NYPD_Vehicle_Loading';
EXEC msdb.dbo.sp_attach_schedule @job_name = 'NYPD_Vehicle_Loading', @schedule_id = @scheduleId;
Expand Down
12 changes: 12 additions & 0 deletions Sisulate.bat
Expand Up @@ -85,6 +85,18 @@ for %%f in (%FolderPath%\targets\*.xml) do (
set SQLFiles[!i!]=!OutputFile!
)

REM -------------------------------------------------------------------
REM Create BIML files
REM -------------------------------------------------------------------
for %%f in (%FolderPath%\targets\*.xml) do (
set OutputFile=%FolderPath%\biml\%%~nf.biml
echo * Transforming target to BIML file:
echo %%~f ...
echo !OutputFile!
Sisulator.js -x "%%~f" -m Target -d biml.directive -o "!OutputFile!"
IF ERRORLEVEL 1 GOTO ERROR
)

REM -------------------------------------------------------------------
REM Create SQL Server Agent job code
REM -------------------------------------------------------------------
Expand Down
8 changes: 8 additions & 0 deletions biml.directive
@@ -0,0 +1,8 @@
# These are the sisulets that create the loading logic.
sisulets/Polyfills.js
sisulets/Variables.js
sisulets/Metadata.js
sisulets/biml/Variables.js
sisulets/biml/Helpers.js
sisulets/biml/Naming.js
sisulets/biml/CreatePackages.js

0 comments on commit 17d1bd6

Please sign in to comment.