L
L
LebedevStr2015-10-23 14:54:15
MySQL
LebedevStr, 2015-10-23 14:54:15

How to make a multi trigger in PMA?

Hello. Tell me please.
Initial data.
Database name: db_name
Table name: table_name
Column name: line_name
Line_name
Contains data with words, from 3 to 10.
Task.
Make a trigger on the line_table column with this function
1) Contains the first three words
2) Turn on the blacklist of words
Example.
It was: how much an adult elephant weighs the answer is four tons
It became: an adult elephant weighs
* "how much" - falls under the black list.
That is, you need to filter the number of words with the possibility of introducing a blacklist.
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aleksey Ratnikov, 2015-10-26
@mahoho

The decision by means of MySQL will be, to put it mildly. not very elegant due to a very poor functional set.
We need a helper function to remove extra spaces :

CREATE FUNCTION DELETE_DOUBLE_SPACES ( title VARCHAR(250) )
  RETURNS VARCHAR(250) DETERMINISTIC
  BEGIN
    DECLARE result VARCHAR(250);
    SET result = REPLACE( title, '  ', ' ' );
    WHILE (result <> title) DO
      SET title = result;
      SET result = REPLACE( title, '  ', ' ' );
    END WHILE;
    RETURN result;
  END;

Table for storing stop words:
create table stop_words(
  word VARCHAR(20) NOT NULL ,
  PRIMARY KEY (word)
) ENGINE = InnoDB CHAR SET utf8 COLLATE utf8_general_ci;

And the BEFORE INSERT trigger itself:
CREATE TRIGGER check_column BEFORE INSERT ON table_name FOR EACH ROW
  BEGIN
    DECLARE var_censored_string VARCHAR(255);
    DECLARE done BOOLEAN DEFAULT false;
    DECLARE var_current_stop_word VARCHAR(20);

    DECLARE stop_words_list CURSOR FOR select word from stop_words;
    DECLARE CONTINUE HANDLER FOR NOT FOUND set done = true;

    SET var_censored_string = NEW.line_name;

    -- убираем стоп-слова
    OPEN stop_words_list;
    `IterateStopWords`: LOOP
      FETCH `stop_words_list` INTO var_current_stop_word;
      IF done THEN
        LEAVE `IterateStopWords`;
      END IF;

      SET var_censored_string = REPLACE(var_censored_string, var_current_stop_word, '');

    END LOOP `IterateStopWords`;

    -- убираем лишние пробелы
    SET var_censored_string = trim(DELETE_DOUBLE_SPACES(var_censored_string));

    -- берем первые три слова
    SET var_censored_string = SUBSTRING_INDEX(var_censored_string, ' ', 3);

    SET NEW.line_name = var_censored_string;
  END;

Similar for BEFORE UPDATE if needed. In general, it's easier to do this in php, because there is explode (), which works like a human.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question