Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DELIMITER $$
- CREATE PROCEDURE `tableGoStruct`(
- IN dbName VARCHAR(50) ,
- IN tableName VARCHAR(50)
- )
- BEGIN
- DECLARE struct LONGTEXT;
- DECLARE structType VARCHAR(10);
- DECLARE colName VARCHAR(64);
- DECLARE dataType VARCHAR(64);
- DECLARE colType LONGTEXT;
- DECLARE done INT DEFAULT FALSE;
- DECLARE c CURSOR FOR SELECT column_name, data_type, column_type FROM information_schema.COLUMNS WHERE table_name = tableName AND table_schema = dbName;
- DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
- SET struct = concat("type ", tableName, " struct {\n");
- OPEN c;
- read_loop: LOOP
- FETCH c INTO colName, dataType, colType;
- IF done THEN
- LEAVE read_loop;
- END IF;
- SET structType = CASE
- WHEN dataType IN('int', 'tinyint', 'smallint') AND locate('unsigned', colType) = 0 THEN 'int' /* int -> int */
- WHEN dataType IN('int', 'tinyint', 'smallint') AND locate('unsigned', colType) > 0 THEN 'uint' /* int unsigned -> uint */
- WHEN dataType = 'bigint' AND locate('unsigned', colType) = 0 THEN 'int64' /* bigint -> int64 */
- WHEN dataType = 'bigint' AND locate('unsigned', colType) > 0 THEN 'uint64' /* bigint unsigned -> uint64 */
- ELSE 'string'
- END;
- SET struct = concat(struct, "\t", colName, " ", structType, "\n");
- END LOOP;
- CLOSE c;
- SET struct = concat(struct, "}");
- SELECT struct;
- END$$
- DELIMITER ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement