-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for Instead of Trigger On Views in Babelfish
Currently Postgres does not support Instead of Trigger On Views for DML queries. On running such a query on VIEW, query rewriting substitutes view with underlying base table. Added a hook for skipping the substitution for DML query on the VIEW. This change will add support for Instaed of Triggers for DML queries when using Babelfish. Task: BABEL-2170 Signed-off-by: Deepakshi Mittal <[email protected]>
- Loading branch information
1 parent
818bf34
commit 34c3d17
Showing
7 changed files
with
340 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
DROP TRIGGER IF EXISTS babel_2170_vu_employees_view_iot_insert; | ||
GO | ||
|
||
DROP TRIGGER IF EXISTS babel_2170_vu_employees_view_iot_update; | ||
GO | ||
|
||
DROP TRIGGER IF EXISTS babel_2170_vu_employees_view_iot_delete; | ||
GO | ||
|
||
DROP VIEW IF EXISTS babel_2170_vu_employees_view; | ||
GO | ||
|
||
DROP TABLE IF EXISTS babel_2170_vu_employees; | ||
GO | ||
|
||
SELECT name FROM sys.sysobjects WHERE name LIKE 'babel_2170%' ORDER BY name; | ||
GO | ||
~~START~~ | ||
varchar | ||
~~END~~ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
CREATE TABLE babel_2170_vu_employees | ||
( | ||
EmployeeID int NOT NULL, | ||
EmployeeName VARCHAR(50), | ||
EmployeeAddress VARCHAR(50), | ||
MonthSalary NUMERIC(10, 2) | ||
) | ||
GO | ||
|
||
INSERT INTO babel_2170_vu_employees VALUES(1, 'amber', '1st Street', '1000'); | ||
INSERT INTO babel_2170_vu_employees VALUES(2, 'angel', '1st Street', '2000'); | ||
INSERT INTO babel_2170_vu_employees VALUES(3, 'ana', '1st Street', '3000'); | ||
INSERT INTO babel_2170_vu_employees VALUES(4, 'adam', '1st Street', '4000'); | ||
GO | ||
~~ROW COUNT: 1~~ | ||
|
||
~~ROW COUNT: 1~~ | ||
|
||
~~ROW COUNT: 1~~ | ||
|
||
~~ROW COUNT: 1~~ | ||
|
||
|
||
CREATE VIEW babel_2170_vu_employees_view AS | ||
SELECT EmployeeID, | ||
EmployeeName, | ||
EmployeeAddress, | ||
MonthSalary | ||
FROM babel_2170_vu_employees | ||
WHERE EmployeeName LIKE 'a%'; | ||
GO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
-- Instead of Insert Trigger on View | ||
CREATE TRIGGER babel_2170_vu_employees_view_iot_insert ON babel_2170_vu_employees_view | ||
INSTEAD OF INSERT | ||
AS | ||
BEGIN | ||
UPDATE babel_2170_vu_employees SET EmployeeAddress = 'New Street' WHERE EmployeeID = 3; | ||
END | ||
GO | ||
|
||
INSERT INTO babel_2170_vu_employees_view VALUES(5, 'alex', '1st Street', '5000'); | ||
GO | ||
~~ROW COUNT: 1~~ | ||
|
||
~~ROW COUNT: 1~~ | ||
|
||
|
||
SELECT COUNT(EmployeeID) FROM babel_2170_vu_employees; | ||
GO | ||
~~START~~ | ||
int | ||
4 | ||
~~END~~ | ||
|
||
|
||
SELECT COUNT(EmployeeID) FROM babel_2170_vu_employees_view; | ||
GO | ||
~~START~~ | ||
int | ||
4 | ||
~~END~~ | ||
|
||
|
||
UPDATE babel_2170_vu_employees SET EmployeeAddress = '1st Street' WHERE EmployeeID = 3; | ||
GO | ||
~~ROW COUNT: 1~~ | ||
|
||
|
||
-- Instead of Update Trigger on View | ||
CREATE TRIGGER babel_2170_vu_employees_view_iot_update ON babel_2170_vu_employees_view | ||
INSTEAD OF UPDATE | ||
AS | ||
BEGIN | ||
UPDATE babel_2170_vu_employees SET MonthSalary = MonthSalary + 100 WHERE EmployeeID = 3; | ||
END | ||
GO | ||
|
||
UPDATE babel_2170_vu_employees_view SET MonthSalary = MonthSalary +1 WHERE EmployeeID = 3; | ||
GO | ||
~~ROW COUNT: 1~~ | ||
|
||
~~ROW COUNT: 1~~ | ||
|
||
|
||
SELECT EmployeeID,EmployeeName, EmployeeAddress, MonthSalary FROM babel_2170_vu_employees WHERE EmployeeID = 3 ORDER BY EmployeeID; | ||
GO | ||
~~START~~ | ||
int#!#varchar#!#varchar#!#numeric | ||
3#!#ana#!#1st Street#!#3100.00 | ||
~~END~~ | ||
|
||
|
||
UPDATE babel_2170_vu_employees SET MonthSalary = 3000 WHERE EmployeeID = 3; | ||
GO | ||
~~ROW COUNT: 1~~ | ||
|
||
|
||
BEGIN TRANSACTION | ||
UPDATE babel_2170_vu_employees_view SET MonthSalary = MonthSalary +1 WHERE EmployeeID IN (3, 4); | ||
COMMIT TRANSACTION; | ||
GO | ||
~~ROW COUNT: 1~~ | ||
|
||
~~ROW COUNT: 2~~ | ||
|
||
|
||
SELECT EmployeeID, EmployeeName, EmployeeAddress, MonthSalary FROM babel_2170_vu_employees WHERE EmployeeID IN (3, 4) ORDER BY EmployeeID; | ||
GO | ||
~~START~~ | ||
int#!#varchar#!#varchar#!#numeric | ||
3#!#ana#!#1st Street#!#3100.00 | ||
4#!#adam#!#1st Street#!#4000.00 | ||
~~END~~ | ||
|
||
|
||
UPDATE babel_2170_vu_employees SET MonthSalary = 3000 WHERE EmployeeID = 3; | ||
GO | ||
~~ROW COUNT: 1~~ | ||
|
||
|
||
-- Instead of Delete Trigger on View | ||
CREATE TRIGGER babel_2170_vu_employees_view_iot_delete ON babel_2170_vu_employees_view | ||
INSTEAD OF DELETE | ||
AS | ||
BEGIN | ||
UPDATE babel_2170_vu_employees SET MonthSalary = MonthSalary + 100 WHERE EmployeeID = 3; | ||
END | ||
GO | ||
|
||
BEGIN TRANSACTION | ||
DELETE FROM babel_2170_vu_employees_view WHERE EmployeeID IN (1, 2); | ||
COMMIT TRANSACTION; | ||
GO | ||
~~ROW COUNT: 1~~ | ||
|
||
~~ROW COUNT: 2~~ | ||
|
||
|
||
SELECT COUNT(EmployeeID) FROM babel_2170_vu_employees_view; | ||
GO | ||
~~START~~ | ||
int | ||
4 | ||
~~END~~ | ||
|
||
|
||
SELECT COUNT(EmployeeID) FROM babel_2170_vu_employees; | ||
GO | ||
~~START~~ | ||
int | ||
4 | ||
~~END~~ | ||
|
||
|
||
SELECT EmployeeID, EmployeeName, EmployeeAddress, MonthSalary FROM babel_2170_vu_employees_view ORDER BY EmployeeID; | ||
GO | ||
~~START~~ | ||
int#!#varchar#!#varchar#!#numeric | ||
1#!#amber#!#1st Street#!#1000.00 | ||
2#!#angel#!#1st Street#!#2000.00 | ||
3#!#ana#!#1st Street#!#3100.00 | ||
4#!#adam#!#1st Street#!#4000.00 | ||
~~END~~ | ||
|
||
|
||
UPDATE babel_2170_vu_employees SET MonthSalary = 3000 WHERE EmployeeID = 3; | ||
GO | ||
~~ROW COUNT: 1~~ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
DROP TRIGGER IF EXISTS babel_2170_vu_employees_view_iot_insert; | ||
GO | ||
|
||
DROP TRIGGER IF EXISTS babel_2170_vu_employees_view_iot_update; | ||
GO | ||
|
||
DROP TRIGGER IF EXISTS babel_2170_vu_employees_view_iot_delete; | ||
GO | ||
|
||
DROP VIEW IF EXISTS babel_2170_vu_employees_view; | ||
GO | ||
|
||
DROP TABLE IF EXISTS babel_2170_vu_employees; | ||
GO | ||
|
||
SELECT name FROM sys.sysobjects WHERE name LIKE 'babel_2170%' ORDER BY name; | ||
GO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
CREATE TABLE babel_2170_vu_employees | ||
( | ||
EmployeeID int NOT NULL, | ||
EmployeeName VARCHAR(50), | ||
EmployeeAddress VARCHAR(50), | ||
MonthSalary NUMERIC(10, 2) | ||
) | ||
GO | ||
|
||
INSERT INTO babel_2170_vu_employees VALUES(1, 'amber', '1st Street', '1000'); | ||
INSERT INTO babel_2170_vu_employees VALUES(2, 'angel', '1st Street', '2000'); | ||
INSERT INTO babel_2170_vu_employees VALUES(3, 'ana', '1st Street', '3000'); | ||
INSERT INTO babel_2170_vu_employees VALUES(4, 'adam', '1st Street', '4000'); | ||
GO | ||
|
||
CREATE VIEW babel_2170_vu_employees_view AS | ||
SELECT EmployeeID, | ||
EmployeeName, | ||
EmployeeAddress, | ||
MonthSalary | ||
FROM babel_2170_vu_employees | ||
WHERE EmployeeName LIKE 'a%'; | ||
GO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
-- Instead of Insert Trigger on View | ||
CREATE TRIGGER babel_2170_vu_employees_view_iot_insert ON babel_2170_vu_employees_view | ||
INSTEAD OF INSERT | ||
AS | ||
BEGIN | ||
UPDATE babel_2170_vu_employees SET EmployeeAddress = 'New Street' WHERE EmployeeID = 3; | ||
END | ||
GO | ||
|
||
INSERT INTO babel_2170_vu_employees_view VALUES(5, 'alex', '1st Street', '5000'); | ||
GO | ||
|
||
SELECT COUNT(EmployeeID) FROM babel_2170_vu_employees; | ||
GO | ||
|
||
SELECT COUNT(EmployeeID) FROM babel_2170_vu_employees_view; | ||
GO | ||
|
||
UPDATE babel_2170_vu_employees SET EmployeeAddress = '1st Street' WHERE EmployeeID = 3; | ||
GO | ||
|
||
-- Instead of Update Trigger on View | ||
CREATE TRIGGER babel_2170_vu_employees_view_iot_update ON babel_2170_vu_employees_view | ||
INSTEAD OF UPDATE | ||
AS | ||
BEGIN | ||
UPDATE babel_2170_vu_employees SET MonthSalary = MonthSalary + 100 WHERE EmployeeID = 3; | ||
END | ||
GO | ||
|
||
UPDATE babel_2170_vu_employees_view SET MonthSalary = MonthSalary +1 WHERE EmployeeID = 3; | ||
GO | ||
|
||
SELECT EmployeeID,EmployeeName, EmployeeAddress, MonthSalary FROM babel_2170_vu_employees WHERE EmployeeID = 3 ORDER BY EmployeeID; | ||
GO | ||
|
||
UPDATE babel_2170_vu_employees SET MonthSalary = 3000 WHERE EmployeeID = 3; | ||
GO | ||
|
||
BEGIN TRANSACTION | ||
UPDATE babel_2170_vu_employees_view SET MonthSalary = MonthSalary +1 WHERE EmployeeID IN (3, 4); | ||
COMMIT TRANSACTION; | ||
GO | ||
|
||
SELECT EmployeeID, EmployeeName, EmployeeAddress, MonthSalary FROM babel_2170_vu_employees WHERE EmployeeID IN (3, 4) ORDER BY EmployeeID; | ||
GO | ||
|
||
UPDATE babel_2170_vu_employees SET MonthSalary = 3000 WHERE EmployeeID = 3; | ||
GO | ||
|
||
-- Instead of Delete Trigger on View | ||
CREATE TRIGGER babel_2170_vu_employees_view_iot_delete ON babel_2170_vu_employees_view | ||
INSTEAD OF DELETE | ||
AS | ||
BEGIN | ||
UPDATE babel_2170_vu_employees SET MonthSalary = MonthSalary + 100 WHERE EmployeeID = 3; | ||
END | ||
GO | ||
|
||
BEGIN TRANSACTION | ||
DELETE FROM babel_2170_vu_employees_view WHERE EmployeeID IN (1, 2); | ||
COMMIT TRANSACTION; | ||
GO | ||
|
||
SELECT COUNT(EmployeeID) FROM babel_2170_vu_employees_view; | ||
GO | ||
|
||
SELECT COUNT(EmployeeID) FROM babel_2170_vu_employees; | ||
GO | ||
|
||
SELECT EmployeeID, EmployeeName, EmployeeAddress, MonthSalary FROM babel_2170_vu_employees_view ORDER BY EmployeeID; | ||
GO | ||
|
||
UPDATE babel_2170_vu_employees SET MonthSalary = 3000 WHERE EmployeeID = 3; | ||
GO |