简体   繁体   English

这个mySQL触发器语法是否正确?

[英]Is this mySQL trigger syntax correct?

Please help me out. 请帮帮我。 I'm inserting a String into a column in a mysql db table. 我正在将一个String插入mysql db表中的一列。 but since I'm using a predefined API to insert into the table I need a Trigger to help parse the string into the rest of the columns. 但由于我使用预定义的API插入表中,我需要一个触发器来帮助将字符串解析为其余的列。

the String coming in looks like this: ((admin)) [[clientId]] >>>Update client Profile 进入的字符串如下所示: ((admin))[[clientId]] >>>更新客户端配置文件

that String will be inserted into the column 'Message'. 该String将插入“消息”列中。 but there are 3 other columns in that table I want to insert to: user_id, client_id & action. 但是我要插入的表中有3个其他列:user_id,client_id和action。 so I need to parse the message into 3 different strings in order to put them in the correct column. 所以我需要将消息解析为3个不同的字符串,以便将它们放入正确的列中。

This is what I have, Is this correct??? 这就是我所拥有的,这是正确的吗?

CREATE TRIGGER 'parse_log4j_message' BEFORE INSERT ON 'util_audit' 
FOR EACH ROW BEGIN   
select NEW.MESSAGE into @message; //adding String into this variable
    select LEFT(message, 1) from table into @firstChar; //get first Char in Message
IF strcmp(@firstChar,'(') = 0 THEN  //if first char is '(', then message is correct
    SELECT SUBSTRING_INDEX(@message, ')', 2) into @userId; //-> admin
    SELECT SUBSTRING_INDEX(@message, ']', 2) into @tempClientId;  
    SELECT SUBSTRING_INDEX(@message, '[', -1) into @clientId; //-> clientId
    SELECT SUBSTRING_INDEX(@message, '>', -1) into @action; //-> the rest of the message
    SET NEW.USER_ID = @userId;  // set into the columns of the table
    SET NEW.CLIENT_ID = @clientId;
    SET NEW.ACTION = @action; 
END IF; 
END;

Try this parsing script firstly - 首先尝试这个解析脚本 -

SET @message = '((admin)) [[clientId]] >>>Update client Profile';

SET @userId = SUBSTRING_INDEX(SUBSTRING_INDEX(@message, '))', 1), '((', -1);
SET @clientId = SUBSTRING_INDEX(SUBSTRING_INDEX(@message, ']]', 1), '[[', -1);
SET @action = SUBSTRING_INDEX(@message, '>>>', -1);

SELECT @userId, @clientId, @action;

+---------+-----------+-----------------------+
| @userId | @clientId | @action               |
+---------+-----------+-----------------------+
| admin   | clientId  | Update client Profile |
+---------+-----------+-----------------------+

If this is correct, then add this solution to your trigger. 如果这是正确的,那么将此解决方案添加到您的触发器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM