Question

as you know the small version of MariaDB (5.5-) is supposedly MySQL compatible (I've found flaws but they say its 90%). I use both in our environment, MySQL on AWS RDS and Maria on our local Dev servers and Dev boxes. I am trying to currently hack together a function that will autopopulate a row with random data

The table structure would be pretty simple, something like this

DROP DATABASE IF EXISTS `foodb`;
CREATE DATABASE `foodb`;
USE `foodb`;

DROP TABLE IF EXISTS `footable`;
CREATE TABLE `footable` (
  `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `garbage`    varchar(128) DEFAULT garbageString(),
   PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;

Where the function garbageString() would be something like this

DROP FUNCTION IF EXISTS garbageString;
CREATE FUNCTION garbageString($length int)
  RETURNS varchar(128)
  BEGIN

    SET @chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    SET @charLen = length(@chars);

    SET @randomString = '';

    WHILE length(@randomString) < $length DO
      SET @randomString = concat(@randomString, substring(@chars,CEILING(RAND() * @charLen),1));
    END WHILE;

    RETURN @randomString ;
  END;

I'm getting a lot of headaches though in both MySQL and Maria trying to get this function running, here's an error dump from MY

MySQL [foodatabase]> CREATE FUNCTION garbageString($length int)
    ->   RETURNS varchar(128)
    ->   BEGIN
    -> 
    ->     SET @chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 5
MySQL [foodatabase]>     SET @charLen = length(@chars);
Query OK, 0 rows affected (0.03 sec)

MySQL [foodatabase]> 
MySQL [foodatabase]>     SET @randomString = '';
Query OK, 0 rows affected (0.03 sec)

MySQL [foodatabase]> 
MySQL [foodatabase]>     WHILE length(@randomString) < $length DO
    ->       SET @randomString = concat(@randomString, substring(@chars,CEILING(RAND() * @charLen),1));
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHILE length(@randomString) < $length DO
      SET @randomString = concat(@rando' at line 1
MySQL [foodatabase]>     END WHILE;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END WHILE' at line 1
MySQL [foodatabase]> 
MySQL [foodatabase]>     RETURN @randomString ;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'RETURN @randomString' at line 1
MySQL [foodatabase]>   END;

And here's one from Maria

MariaDB [foodatabase]> CREATE FUNCTION garbageString($length int)
    ->   RETURNS varchar(128)
    ->   BEGIN
    -> 
    ->     SET @chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 5
MariaDB [foodatabase]>     SET @charLen = length(@chars);
Query OK, 0 rows affected (0.00 sec)

MariaDB [foodatabase]> 
MariaDB [foodatabase]>     SET @randomString = '';
Query OK, 0 rows affected (0.00 sec)

MariaDB [foodatabase]> 
MariaDB [foodatabase]>     WHILE length(@randomString) < $length DO
    ->       SET @randomString = concat(@randomString, substring(@chars,CEILING(RAND() * @charLen),1));
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHILE length(@randomString) < $length DO
      SET @randomString = concat(@rando' at line 1
MariaDB [foodatabase]>     END WHILE;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'END WHILE' at line 1
MariaDB [foodatabase]> 
MariaDB [foodatabase]>     RETURN @randomString ;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'RETURN @randomString' at line 1
MariaDB [foodatabase]>   END;

Any way to get this function working, and get it working with both DB types?

It looks like they're both choking in the same areas, I just don't want any unexpecteds

Was it helpful?

Solution

When defining a function or procedure using the cmdline client you need to change the DELIMITER. The command line client checks input for the current query delimiter, breaks multiple queries apart at this point, and sends queries to the server one by one.

The usual delimiter is ;, but this is also used as command delimiter in the ANSI procedure language, so your procedure code gets broken up into little peaces that by themselves are not valid SQL statements. So you need to change the client side delimiter to something else, e.g. //:

DELIMITER //

After this you can enter your procedure text just fine without the errors you're currently getting, and at the end of it you add // to tell the client to send the full procedure text to the server as a single statement.

You can then change the delimiter back to ; if you want to:

DELIMITER ;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top