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