Advertisement
Guest User

Untitled

a guest
May 20th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. DELIMITER $$
  2.  
  3. CREATE PROCEDURE `tableGoStruct`(
  4. IN dbName VARCHAR(50) ,
  5. IN tableName VARCHAR(50)
  6. )
  7. BEGIN
  8. DECLARE struct LONGTEXT;
  9. DECLARE structType VARCHAR(10);
  10. DECLARE colName VARCHAR(64);
  11. DECLARE dataType VARCHAR(64);
  12. DECLARE colType LONGTEXT;
  13. DECLARE done INT DEFAULT FALSE;
  14. DECLARE c CURSOR FOR SELECT column_name, data_type, column_type FROM information_schema.COLUMNS WHERE table_name = tableName AND table_schema = dbName;
  15. DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
  16. SET struct = concat("type ", tableName, " struct {\n");
  17. OPEN c;
  18. read_loop: LOOP
  19. FETCH c INTO colName, dataType, colType;
  20. IF done THEN
  21. LEAVE read_loop;
  22. END IF;
  23.  
  24. SET structType = CASE
  25. WHEN dataType IN('int', 'tinyint', 'smallint') AND locate('unsigned', colType) = 0 THEN 'int' /* int -> int */
  26. WHEN dataType IN('int', 'tinyint', 'smallint') AND locate('unsigned', colType) > 0 THEN 'uint' /* int unsigned -> uint */
  27. WHEN dataType = 'bigint' AND locate('unsigned', colType) = 0 THEN 'int64' /* bigint -> int64 */
  28. WHEN dataType = 'bigint' AND locate('unsigned', colType) > 0 THEN 'uint64' /* bigint unsigned -> uint64 */
  29. ELSE 'string'
  30. END;
  31.  
  32. SET struct = concat(struct, "\t", colName, " ", structType, "\n");
  33. END LOOP;
  34. CLOSE c;
  35. SET struct = concat(struct, "}");
  36. SELECT struct;
  37. END$$
  38.  
  39. DELIMITER ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement