Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. DELIMITER ;;
  2.  
  3. CREATE FUNCTION getValueFromJSON(object TEXT, path TINYTEXT) RETURNS text
  4. NO SQL
  5. DETERMINISTIC
  6. begin
  7.  
  8. declare element tinytext;
  9. declare value_ text;
  10.  
  11. if locate('.', path) then
  12. set element = substring(path, 1, locate('.', path) - 1);
  13. else
  14. set element = path;
  15. end if;
  16.  
  17. set value_ = getValueFromJSONWithoutPath(object, element);
  18. if element != path then
  19. return getValueFromJSONWithoutPath(value_, substring(path, length(element) + 2));
  20. else
  21. return value_;
  22. end if;
  23.  
  24. end ;;
  25.  
  26. CREATE FUNCTION getValueFromJSONWithoutPath(object TEXT, element TINYTEXT) RETURNS text
  27. NO SQL
  28. DETERMINISTIC
  29. begin
  30.  
  31. declare startPosition int;
  32. declare endPosition int;
  33. declare quote_ int;
  34. declare comma int;
  35. declare bracket int;
  36. declare delimiter_ char;
  37.  
  38. set startPosition = locate(concat('"', element, '":'), object);
  39. if startPosition = 0 then
  40. return null;
  41. else
  42. set startPosition = startPosition + length(element) + 3;
  43. end if;
  44. set quote_ = locate('"', object, startPosition);
  45. if quote_ = startPosition then
  46. set startPosition = startPosition + 1;
  47. set endPosition = locate('"', object, quote_ + 1);
  48. else
  49. set delimiter_ = substring(object, startPosition, 1);
  50. if delimiter_ = '[' then
  51. set endPosition = locate(']', object, startPosition) + 1;
  52. else
  53. if delimiter_ = '{' then
  54. set endPosition = locate('}', object, startPosition) + 1;
  55. else
  56. set comma = locate(',', object, startPosition);
  57. set bracket = locate('}', object, startPosition);
  58. if comma != 0 and comma < bracket then
  59. set endPosition = comma;
  60. else
  61. set endPosition = bracket;
  62. end if;
  63. end if;
  64. end if;
  65. end if;
  66.  
  67. RETURN substring(object, startPosition, endPosition - startPosition);
  68.  
  69. end ;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement