Laravel raw sql migration error

I want to use Levenshtein to search in Laravel. I found the SQL function (saved as app/database/migrations/levenshtain.sql ):

  DELIMITER //
     CREATE FUNCTION `LEVENSHTEIN` (s1 VARCHAR (255) CHARACTER SET utf8, s2 VARCHAR (255) CHARACTER SET utf8) RETURNS int (11)
         DETERMINISTIC
     BEGIN
         DECLARE s1_len, s2_len, i, j, c, c_temp, cost INT;
         DECLARE s1_char CHAR CHARACTER SET utf8;
         - max strlen = 255 for this function
         DECLARE cv0, cv1 VARBINARY (256);

         SET s1_len = CHAR_LENGTH (s1),
             s2_len = CHAR_LENGTH (s2),
             cv1 = 0x00,
             j = 1,
             i = 1,
             c is 0;

         IF (s1 = s2) THEN
           RETURN (0);
         ELSEIF (s1_len = 0) THEN
           RETURN (s2_len);
         ELSEIF (s2_len = 0) THEN
           RETURN (s1_len);
         END IF;

         WHILE (j c_temp) THEN
               SET c = c_temp;
             END IF;

             SET c_temp = ORD (SUBSTRING (cv1, j + 1, 1)) + 1;
             IF (c> c_temp) THEN
               SET c = c_temp;
             END IF;

             SET cv0 = CONCAT (cv0, CHAR (c)),
                 j = j + 1;
           END WHILE;

           SET cv1 = cv0,
               i = i + 1;
         END WHILE;

         RETURN (c);
       END //
     DELIMITER;

And during the migration process:

     public function up () {
         DB :: statement (file_get_contents (__ DIR __. '/ Levenshtein.sql'));
     }
     public function down () {
         DB :: statement ('DROP FUNCTION `LEVENSHTEIN`;');
     }

I tried DB::statement , DB::unprepared and DB::raw , but Laravel migration causes an error:

     [Illuminate \ Database \ QueryException]
       SQLSTATE [42000]: Syntax error or access violation: 1064 You have an error in yo
       ur SQL syntax;  check the manual that corresponds to your MySQL server version f
       or the right syntax to use near 'DELIMITER //
       CREATE FUNCTION `LEVENSHTEIN` (s1 VARCHAR (255) CHARACTER SET utf8, 'at line 1

This works when I execute it using HeidiSQL, what is the problem?

0
source share
1 answer

I found a solution in Creating a MYSQL procedure in Laravel 4 Migrations , which @peterm answered.

The problem is DELIMITER // , which is not a valid sql operation. This is just a MySql client team.

0
source

Source: https://habr.com/ru/post/1243559/


All Articles