-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbo.spCompile.sql
More file actions
77 lines (74 loc) · 3.92 KB
/
dbo.spCompile.sql
File metadata and controls
77 lines (74 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
USE [Assembler]
GO
/****** Object: StoredProcedure [dbo].[spCompile] Script Date: 08/12/2018 23:15:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spCompile]
AS
SET NOCOUNT, CONCAT_NULL_YIELDS_NULL ON
BEGIN TRY
DECLARE @CRLF char(2) = char(13) + char(10)
DECLARE @TAB char(1) = char(09)
EXEC spLabelMapCreate
BEGIN TRAN
DELETE dbo.vwContext
INSERT dbo.RunningCode
(
LineNumber,
SQL,
Assembler
)
SELECT ROW_NUMBER() OVER(ORDER BY @@SPID),
REPLACE
(
REPLACE(O.Generation, N'%T', N'dbo.fnTop()'), -- %T -> Stack Top
N'%0',
CASE
WHEN A.TypeLookup = N'INT' THEN CONVERT(nvarchar(8), CONVERT(int, I.Arg1))
WHEN A.TypeLookup = N'LBL' THEN LA.LineNumber
WHEN A.TypeLookup = N'MEM' THEN I.Arg1 --Memory access is just via numbers, at the moment...
ELSE N''
END
),
FormattedInstructions.Str
FROM dbo.Instructions I
INNER JOIN dbo.OpCode O ON O.Code LIKE I.OpCode
LEFT JOIN dbo.vwLabelMap LL ON LL.SourceLabel LIKE I.Label
LEFT JOIN dbo.vwLabelMap LA ON LA.SourceLabel = I.Arg1
LEFT JOIN dbo.OpCodeArgument A ON O.Code LIKE A.OpCode
CROSS APPLY
(
-- No dependency on the specific columns in Instructions,
-- but no space between the labels/op codes etc. :(
SELECT
(
SELECT *
FROM Instructions II
WHERE I.Idx = II.Idx
FOR XML PATH(''),
type
).value('.', 'nvarchar(max)')
) AS FormattedInstructionsAll(Str)
CROSS APPLY
(
SELECT
(
SELECT ISNULL(II.Label + N':', N'') + N' ',
ISNULL(II.OpCode, N'') + N' ',
ISNULL(II.Arg1, N'') + N' ',
ISNULL(II.Arg2, N'') + N' '
FROM Instructions II
WHERE I.Idx = II.Idx
FOR XML PATH(''),
type
).value('.', 'nvarchar(max)')
) AS FormattedInstructions (Str)
COMMIT TRAN
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 1 ROLLBACK TRAN
EXEC spErrorHandle
RETURN 651
END CATCH