Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for trigger FK reference fix on table/temp variables #3440

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions test/JDBC/expected/fk_trigger_temp-vu-cleanup.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

-- triggers
DROP TRIGGER IF EXISTS tr_SpecialChar_Insert
DROP TRIGGER IF EXISTS tr_Child_CaseInsensitive
DROP TRIGGER IF EXISTS tr_Level3_Insert
DROP TRIGGER IF EXISTS tr_Child_NullHandling
DROP TRIGGER IF EXISTS tr_Parent_Insert
DROP TRIGGER IF EXISTS tr_Child_Update
DROP TRIGGER IF EXISTS tr_Child_Insert
DROP TRIGGER IF EXISTS [trg_i_COD]
GO

-- tables
DROP TABLE IF EXISTS Level3
DROP TABLE IF EXISTS Level2
DROP TABLE IF EXISTS Level1
DROP TABLE IF EXISTS [Child#Table]
DROP TABLE IF EXISTS [Parent@Table]
DROP TABLE IF EXISTS Child
DROP TABLE IF EXISTS Parent
DROP TABLE IF EXISTS GrandParent
DROP TABLE IF EXISTS [dbo].[OrderItem]
DROP TABLE IF EXISTS [order]
GO
151 changes: 151 additions & 0 deletions test/JDBC/expected/fk_trigger_temp-vu-prepare.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
USE MASTER;
roshan0708 marked this conversation as resolved.
Show resolved Hide resolved
GO

CREATE TABLE [order] (
pkorder INT IDENTITY(25001000,1) PRIMARY KEY,
screatedby sys.varchar(255)
);
GO

CREATE TABLE orderitem (
pkorderitem INT IDENTITY(1,1) PRIMARY KEY,
fkorder integer,
screatedby sys.varchar(255)
);
GO

ALTER TABLE [dbo].[OrderItem] ADD CONSTRAINT [FK_OrderItem_Order] FOREIGN KEY([fkOrder])REFERENCES [dbo].[Order] ([pkOrder]);
GO







CREATE TRIGGER [trg_i_COD] ON [dbo].[OrderItem]
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON ;
DECLARE @HasMoreRow BIT ,
@pkOrderItem INT ,
@IsExpedit BIT ,
@IsShippingItem BIT ,
@IsPackage BIT ,
@IsAddOn BIT ,
@IsCrossSell BIT ,
@ExpeditePackageBitValue INT ,
@PackageRelationshipType INT,
@ShippingRelationshipType INT ,
@AddOnRelationshipType INT ,
@CrossSellRelationshipType INT
DECLARE @OrderItemList TABLE
(
pkOrderItem INT NOT NULL ,
IsUsed BIT DEFAULT ( 0 )/*REWRITTEN*/ , /**/ PRIMARY KEY ( pkOrderItem )
)
INSERT INTO @OrderItemList
( pkOrderItem )
SELECT pkOrderItem
FROM inserted
SELECT TOP 1
@pkOrderItem = pkOrderItem ,
@hasMoreRow = 1
FROM @OrderItemList
WHERE IsUsed = 0
END
GO

INSERT INTO [dbo].[order] (sCreatedBy) VALUES('test');
GO
~~ROW COUNT: 1~~


INSERT INTO [dbo].[orderItem] (fkOrder, sCreatedBy) VALUES(25001000, 'test');
GO
~~ROW COUNT: 1~~


-- Create base tables
CREATE TABLE GrandParent (ID INT PRIMARY KEY);
GO

CREATE TABLE Parent (
ID INT PRIMARY KEY,
GrandParentID INT
);
GO

ALTER TABLE [dbo].[Parent] ADD CONSTRAINT [FK_Parent_GrandParent] FOREIGN KEY ([GrandParentID]) REFERENCES [dbo].[GrandParent] ([ID]);
GO

CREATE TABLE Child (
ID INT PRIMARY KEY,
ParentID INT
);
GO

ALTER TABLE [dbo].[Child] ADD CONSTRAINT [FK_Child_Parent] FOREIGN KEY ([ParentID]) REFERENCES [dbo].[Parent] ([ID]);
GO

-- Create special character tables
CREATE TABLE [Parent@Table] (ID INT PRIMARY KEY);
GO

CREATE TABLE [Child#Table] (
ID INT PRIMARY KEY,
ParentID INT
);
GO

ALTER TABLE [dbo].[Child#Table] ADD CONSTRAINT [FK_ChildTable_ParentTable] FOREIGN KEY ([ParentID]) REFERENCES [dbo].[Parent@Table] ([ID]);
GO

-- Insert some initial data
INSERT INTO GrandParent VALUES (1), (2);
GO
~~ROW COUNT: 2~~


INSERT INTO Parent VALUES (1, 1), (2, 2);
GO
~~ROW COUNT: 2~~


INSERT INTO [Parent@Table] VALUES (1), (2);
GO
~~ROW COUNT: 2~~


-- Create multi-level tables for nesting tests
CREATE TABLE Level1 (ID INT PRIMARY KEY);
GO

CREATE TABLE Level2 (
ID INT PRIMARY KEY,
Level1ID INT
);
GO

ALTER TABLE [dbo].[Level2] ADD CONSTRAINT [FK_Level2_Level1] FOREIGN KEY ([Level1ID]) REFERENCES [dbo].[Level1] ([ID]);
GO

CREATE TABLE Level3 (
ID INT PRIMARY KEY,
Level2ID INT
);
GO

ALTER TABLE [dbo].[Level3] ADD CONSTRAINT [FK_Level3_Level2] FOREIGN KEY ([Level2ID]) REFERENCES [dbo].[Level2] ([ID]);
GO

INSERT INTO Level1 VALUES (1), (2);
GO
~~ROW COUNT: 2~~


INSERT INTO Level2 VALUES (1, 1), (2, 2);
GO
~~ROW COUNT: 2~~

205 changes: 205 additions & 0 deletions test/JDBC/expected/fk_trigger_temp-vu-verify.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
-- tsql user=jdbc_user password=1
roshan0708 marked this conversation as resolved.
Show resolved Hide resolved
-- Case 1: Original repro issue
INSERT INTO [dbo].[orderitem] (fkorder, screatedby) VALUES(25001000, 'test');
GO
~~ROW COUNT: 1~~


INSERT INTO [dbo].[orderitem] (fkorder, screatedby) VALUES(25001000, 'test');
GO
~~ROW COUNT: 1~~



-- Case 2: Trigger Functionality with Table Variables
CREATE TRIGGER tr_Child_Insert ON Child AFTER INSERT AS
BEGIN
SET NOCOUNT ON;
DECLARE @TempParents TABLE (ID INT);
INSERT INTO @TempParents (ID)
SELECT DISTINCT ParentID FROM inserted;
IF EXISTS (
SELECT 1 FROM @TempParents t
LEFT JOIN Parent p ON t.ID = p.ID
WHERE p.ID IS NULL
)
BEGIN
RAISERROR ('One or more parents do not exist', 16, 1);
ROLLBACK TRANSACTION;
END
END;
GO

INSERT INTO Child VALUES (1, 1); -- Should succeed
GO
~~ROW COUNT: 1~~


INSERT INTO Child VALUES (2, 999); -- Should fail
GO
~~ERROR (Code: 547)~~

~~ERROR (Message: insert or update on table "child" violates foreign key constraint "fk_child_parent")~~




-- Case 3: Trigger Functionality with Temporary Tables
CREATE TRIGGER tr_Child_Update ON Child AFTER UPDATE AS
BEGIN
SET NOCOUNT ON;
IF UPDATE(ParentID)
BEGIN
CREATE TABLE #UpdatedParents (ID INT);
INSERT INTO #UpdatedParents (ID)
SELECT DISTINCT ParentID FROM inserted;
IF EXISTS (
SELECT 1 FROM #UpdatedParents u
LEFT JOIN Parent p ON u.ID = p.ID
WHERE p.ID IS NULL
)
BEGIN
RAISERROR ('Invalid ParentID', 16, 1);
ROLLBACK TRANSACTION;
END
DROP TABLE #UpdatedParents;
END
END;
GO

UPDATE Child SET ParentID = 2 WHERE ID = 1; -- Should succeed
GO
~~ROW COUNT: 1~~


UPDATE Child SET ParentID = 999 WHERE ID = 1; -- Should fail
GO
~~ERROR (Code: 547)~~

~~ERROR (Message: insert or update on table "child" violates foreign key constraint "fk_child_parent")~~



-- Case 4: Complex Trigger Scenarios
CREATE TRIGGER tr_Parent_Insert ON Parent AFTER INSERT AS
BEGIN
SET NOCOUNT ON;
DECLARE @TempGrandParents TABLE (ID INT);
INSERT INTO @TempGrandParents (ID)
SELECT DISTINCT GrandParentID FROM inserted;
IF EXISTS (
SELECT 1 FROM @TempGrandParents t
LEFT JOIN GrandParent gp ON t.ID = gp.ID
WHERE gp.ID IS NULL
)
BEGIN
RAISERROR ('GrandParent does not exist', 16, 1);
ROLLBACK TRANSACTION;
END
END;
GO

INSERT INTO Parent VALUES (3, 1); -- Should succeed
GO
~~ROW COUNT: 1~~


INSERT INTO Parent VALUES (4, 999); -- Should fail
GO
~~ERROR (Code: 547)~~

~~ERROR (Message: insert or update on table "parent" violates foreign key constraint "fk_parent_grandparent")~~



-- Case 5: NULL test
CREATE TRIGGER tr_Child_NullHandling ON Child AFTER INSERT AS
BEGIN
SET NOCOUNT ON;
DECLARE @NullTemp TABLE (ID INT);
INSERT INTO @NullTemp (ID)
SELECT ParentID FROM inserted WHERE ParentID IS NOT NULL
UNION ALL
SELECT NULL;
IF EXISTS (
SELECT 1 FROM @NullTemp t
LEFT JOIN Parent p ON t.ID = p.ID
WHERE (p.ID IS NULL AND t.ID IS NOT NULL) OR t.ID IS NULL
)
BEGIN
RAISERROR ('Invalid or NULL ParentID', 16, 1);
ROLLBACK TRANSACTION;
END
END;
GO

INSERT INTO Child VALUES (1000, NULL); -- Should fail
GO
~~ERROR (Code: 50000)~~

~~ERROR (Message: One or more parents do not exist)~~



-- Case 6: Nesting test
CREATE TRIGGER tr_Level3_Insert ON Level3 AFTER INSERT AS
BEGIN
SET NOCOUNT ON;
DECLARE @Temp3 TABLE (ID INT);
INSERT INTO @Temp3 (ID)
SELECT DISTINCT Level2ID FROM inserted;
IF EXISTS (
SELECT 1 FROM @Temp3 t
LEFT JOIN Level2 l2 ON t.ID = l2.ID
WHERE l2.ID IS NULL
)
BEGIN
RAISERROR ('Invalid Level2ID', 16, 1);
ROLLBACK TRANSACTION;
END
END;
GO

INSERT INTO Level3 VALUES (1, 1); -- Should succeed
GO
~~ROW COUNT: 1~~


INSERT INTO Level3 VALUES (2, 999); -- Should fail
GO
~~ERROR (Code: 547)~~

~~ERROR (Message: insert or update on table "level3" violates foreign key constraint "fk_level3_level2")~~



-- Case 7: Special Characters support test
CREATE TRIGGER tr_SpecialChar_Insert ON [Child#Table] AFTER INSERT AS
BEGIN
SET NOCOUNT ON;
DECLARE @SpecialTemp TABLE (ID INT);
INSERT INTO @SpecialTemp (ID)
SELECT DISTINCT ParentID FROM inserted;
IF EXISTS (
SELECT 1 FROM @SpecialTemp t
LEFT JOIN [Parent@Table] p ON t.ID = p.ID
WHERE p.ID IS NULL
)
BEGIN
RAISERROR ('Invalid ParentID in special character tables', 16, 1);
ROLLBACK TRANSACTION;
END
END;
GO

INSERT INTO [Child#Table] VALUES (1, 1); -- Should succeed
GO
~~ROW COUNT: 1~~


INSERT INTO [Child#Table] VALUES (2, 999); -- Should fail
GO
~~ERROR (Code: 547)~~

~~ERROR (Message: insert or update on table "child#table" violates foreign key constraint "fk_childtable_parenttable")~~

Loading
Loading