mariadb state wise where case 子句在视图中

mariadb state wise where case clause inside view

我在协作中创建了以下视图 table

CREATE VIEW contents(
  id,
  title
)
AS
select 
   mytable.id as id,
   mytable.title as title
from mytable 
where mytable.owner = substring_index(user(), '@', 1);

是否有任何方法可以在 where 子句中检测视图内的当前状态,例如 select, insert, update, delete

我希望像下面这样,不知道如何在 mysql/mariadb

中生成等效项
/* during select statement user can see all available data*/
if state == 'select' then
    where 1 = 1 /* can see all data */
else
/* if state is update or delete user is allowed to modify or delete data which for which he/she is owner*/
    where mytable.owner = substring_index(user(), '@', 1);
endif

Here is my sample data

MariaDB [test]> select * from mytable;
+----+-------------------+-------+
| id | title             | owner |
+----+-------------------+-------+
|  1 | created by root   | root  |
|  4 | created by helen  | helen |
|  6 | created by helen1 | helen |
|  7 | 123               | lina |
+----+-------------------+-------+

用户 helen 和 lina 有 SELECT, INSERT, UPDATE, DELETE 内容查看授权

   new_database
         table1
         table2
         .....
         .....
         tableN

作为check constrants cannot use user. you can use TRIGGERS to enforce constraints like fiddle:

插入:

CREATE TRIGGER enforce_insert
BEFORE
INSERT ON mytable
FOR EACH ROW
  IF NEW.owner != substring_index(user(), '@', 1) THEN
    SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'conflict of owner on insert';
  END IF

更新:

CREATE TRIGGER enforce_update
BEFORE
UPDATE ON mytable
FOR EACH ROW
  IF OLD.owner != substring_index(user(), '@', 1)
     OR NEW.owner != substring_index(user(), '@', 1) THEN
    SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'conflict of owner on update';
  END IF

删除:

CREATE TRIGGER enforce_delete
BEFORE
DELETE ON mytable
FOR EACH ROW
  IF OLD.owner != substring_index(user(), '@', 1) THEN
    SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'conflict of owner on delete';
  END IF

但通常建议应用程序强制执行数据结构的原理图。

参考:trigger manual