Advertisement
Guest User

Untitled

a guest
Apr 11th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.59 KB | None | 0 0
  1. CREATE DEFINER=`proc_user7`@`localhost` PROCEDURE `survey_report`(IN survey_id varchar(10))
  2. SQL SECURITY INVOKER
  3. BEGIN
  4.  
  5. declare tname varchar(24) default concat('survey_survey_',survey_id); -- COLLATE utf8mb4_unicode_ci;
  6.  
  7. declare counter int default 0;
  8.  
  9. set @counter = 0;
  10.  
  11. select
  12. @counter := @counter+1 AS newindex,
  13. survey_answers_lookup.id as id,
  14. survey_answers_lookup.survey_table_row_id,
  15. survey_answers_lookup.qid,
  16. question,
  17. -- Types can be: ! * 1 E F K L M N Q R S T X Y
  18. if (type IN ('S','K'),
  19. (select answer
  20. from survey_answers_lookup
  21. where qid not in (select qid from survey_answers)
  22. and id = @counter
  23. ),
  24. (select answer
  25. from survey_answers
  26. where survey_questions.qid = survey_answers.qid
  27. and survey_answers_lookup.qid = survey_questions.qid
  28. and survey_answers_lookup.answer = survey_answers.code
  29. )
  30. ) as answer
  31. from survey_questions
  32. join survey_answers_lookup on survey_answers_lookup.qid = survey_questions.qid
  33. where survey_questions.sid = survey_id
  34. order by survey_answers_lookup.id;
  35. END
  36.  
  37. '1', '1', '1', '740', 'How would you rate our product's quality?', 'Excellent'
  38. '2', '2', '1', '741', 'Do you think our product helps your business?', 'Yes'
  39. '3', '3', '1', '742', 'One a scale of 1-10, how would you rate the value of our product?', '5.0000000000'
  40. '4', '4', '1', '745', 'Will you recommend our product?', 'Yes'
  41. '5', '5', '1', '748', 'Please enter your email address or phone number if you would like someone to contact you.', '1@email.com'
  42. '6', '6', '2', '740', 'How would you rate our product's quality?', 'Very good'
  43. '7', '7', '2', '741', 'Do you think our product helps your business?', 'No'
  44. '8', '8', '2', '742', 'One a scale of 1-10, how would you rate the value of our product?', '8.0000000000'
  45. '9', '9', '2', '745', 'Will you recommend our product?', 'No'
  46. '10', '10', '2', '748', 'Please enter your email address or phone number if you would like someone to contact you.', '2@email.com'
  47. '11', '11', '3', '740', 'How would you rate our product's quality?', 'Poor'
  48. '12', '12', '3', '741', 'Do you think our product helps your business?', 'No'
  49. '13', '13', '3', '742', 'One a scale of 1-10, how would you rate the value of our product?', '10.0000000000'
  50. '14', '14', '3', '745', 'Will you recommend our product?', 'No'
  51. '15', '15', '3', '748', 'Please enter your email address or phone number if you would like someone to contact you.', '3@email.com'
  52.  
  53. select column_name, substring(column_name, 11+1, 3) as qid
  54. from information_schema.columns
  55. where table_name = 'survey_survey_144477'
  56. and column_name regexp 'X';
  57.  
  58. '144477X148X740', '740'
  59. '144477X148X741', '741'
  60. '144477X148X742SQ001', '742'
  61. '144477X148X745', '745'
  62. '144477X148X748', '748'
  63.  
  64. CREATE DEFINER=`proc_user7`@`localhost` PROCEDURE `proc_get_survey_answers`(IN survey_id varchar(10))
  65. SQL SECURITY INVOKER
  66. BEGIN
  67.  
  68. declare col_name varchar (25);
  69. declare qid int;
  70. declare done int default false;
  71. declare tname varchar(24) default concat('survey_survey_',survey_id);
  72. declare counter int default 0;
  73.  
  74. -- col_name = 144477X148X742SQ001
  75. -- qid = 742
  76.  
  77. declare cur1 cursor for
  78. select column_name, substring(column_name, 11+1, 3) as qid
  79. from information_schema.columns
  80. where table_name = 'survey_survey_144477'
  81. and column_name regexp 'X';
  82.  
  83. DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
  84. SET done = FALSE;
  85.  
  86. open cur1;
  87. read_loop: LOOP
  88. FETCH cur1 INTO col_name, qid;
  89.  
  90. -- SET @sql = CONCAT('SELECT ', col_name, ' FROM survey_survey_144477 ');
  91. -- PREPARE stmt FROM @sql;
  92. -- EXECUTE stmt;
  93. -- DEALLOCATE PREPARE stmt;
  94.  
  95. select
  96. @counter := @counter+1 AS newindex,
  97. question,
  98. qid,
  99. if (type IN ('S','K'),
  100. (select col_name
  101. from survey_survey_144477
  102. where qid not in (select qid from survey_answers) -- ERROR
  103. and id = @counter
  104. ),
  105. (select answer
  106. from survey_answers
  107. where survey_questions.qid = survey_answers.qid
  108. and qid = survey_questions.qid
  109. -- and @sql = survey_answers.code
  110. -- and survey_survey_144477.col_name = survey_answers.code -- this needs to be survey_survey_144477.144477X148X742SQ001 =
  111. -- and 'survey_survey_144477.144477X148X740' = survey_answers.code
  112. )
  113. ) as answer
  114. from survey_questions
  115. -- join survey_survey_144477 survey_survey_144477 on survey_survey_144477.qid = survey_questions.qid -- I dont think this will work
  116. where survey_questions.sid = survey_id;
  117. -- order by survey_survey_144477.id;
  118. IF done THEN
  119. LEAVE read_loop;
  120. END IF;
  121. SELECT col_name, qid;
  122. END LOOP;
  123. CLOSE cur1;
  124.  
  125. END
  126.  
  127. NULL, 'Please enter your email address or phone number if you would like someone to contact you.', '748', NULL
  128. NULL, 'How would you rate our product's quality?', '748', NULL
  129. NULL, 'Do you think our product helps your business?', '748', NULL
  130. NULL, 'One a scale of 1-10, how would you rate the value of our product?', '748', NULL
  131. NULL, 'Will you recommend our product?', '748', NULL
  132.  
  133. CREATE DEFINER=`proc_user7`@`localhost` PROCEDURE `createOrReplaceOutputTable`(IN survey_id varchar(10))
  134. SQL SECURITY INVOKER
  135. BEGIN
  136.  
  137. declare col_name varchar (25);
  138. declare qid int;
  139. declare done int default false;
  140. declare tname varchar(24) default concat('survey_survey_',survey_id);
  141. declare counter int default 0;
  142.  
  143. -- col_name = 144477X148X742SQ001
  144. -- qid = 742
  145.  
  146. declare cur1 cursor for
  147. select column_name, substring(column_name, 11+1, 3) as qid
  148. from information_schema.columns
  149. where table_name = 'survey_survey_144477'
  150. and column_name regexp 'X';
  151.  
  152. DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
  153. set done = FALSE;
  154.  
  155. open cur1;
  156. read_loop: LOOP
  157. FETCH cur1 INTO col_name, qid;
  158.  
  159. -- SELECT id, col_name FROM survey_survey_144477;
  160. -- UNION
  161. -- SELECT qid, col_name;
  162. -- select col_name, qid;
  163.  
  164.  
  165. IF done THEN
  166. LEAVE read_loop;
  167. END IF;
  168. SELECT col_name, qid;
  169. END LOOP;
  170. CLOSE cur1;
  171.  
  172. '144477X148X748', '748'
  173.  
  174.  
  175. describe survey_survey_144477;
  176. +---------------------+----------------+------+-----+---------+----------------+
  177. | Field | Type | Null | Key | Default | Extra |
  178. +---------------------+----------------+------+-----+---------+----------------+
  179. | id | int(11) | NO | PRI | NULL | auto_increment |
  180. | token | varchar(35) | YES | | NULL | |
  181. | submitdate | datetime | YES | | NULL | |
  182. | lastpage | int(11) | YES | | NULL | |
  183. | startlanguage | varchar(20) | NO | | NULL | |
  184. | ipaddr | text | YES | | NULL | |
  185. | refurl | text | YES | | NULL | |
  186. | 144477X148X740 | varchar(5) | YES | | NULL | |
  187. | 144477X148X741 | varchar(5) | YES | | NULL | |
  188. | 144477X148X742SQ001 | decimal(30,10) | YES | | NULL | |
  189. | 144477X148X745 | varchar(5) | YES | | NULL | |
  190. | 144477X148X748 | text | YES | | NULL | |
  191. +---------------------+----------------+------+-----+---------+----------------+
  192.  
  193. describe survey_questions ;
  194. +----------------+--------------+------+-----+---------+----------------+
  195. | Field | Type | Null | Key | Default | Extra |
  196. +----------------+--------------+------+-----+---------+----------------+
  197. | qid | int(11) | NO | PRI | NULL | auto_increment |
  198. | parent_qid | int(11) | NO | MUL | 0 | |
  199. | sid | int(11) | NO | MUL | 0 | |
  200. | gid | int(11) | NO | MUL | 0 | |
  201. | type | varchar(1) | NO | MUL | T | |
  202. | title | varchar(20) | NO | | | |
  203. | question | mediumtext | NO | | NULL | |
  204. | preg | mediumtext | YES | | NULL | |
  205. | help | mediumtext | YES | | NULL | |
  206. | other | varchar(1) | NO | | N | |
  207. | mandatory | varchar(1) | YES | | NULL | |
  208. | question_order | int(11) | NO | | NULL | |
  209. | language | varchar(20) | NO | PRI | en | |
  210. | scale_id | int(11) | NO | | 0 | |
  211. | same_default | int(11) | NO | | 0 | |
  212. | relevance | mediumtext | YES | | NULL | |
  213. | modulename | varchar(255) | YES | | NULL | |
  214. +----------------+--------------+------+-----+---------+----------------+
  215.  
  216. describe survey_survey_144477;
  217. +---------------------+----------------+------+-----+---------+----------------+
  218. | Field | Type | Null | Key | Default | Extra |
  219. +---------------------+----------------+------+-----+---------+----------------+
  220. | id | int(11) | NO | PRI | NULL | auto_increment |
  221. | token | varchar(35) | YES | | NULL | |
  222. | submitdate | datetime | YES | | NULL | |
  223. | lastpage | int(11) | YES | | NULL | |
  224. | startlanguage | varchar(20) | NO | | NULL | |
  225. | ipaddr | text | YES | | NULL | |
  226. | refurl | text | YES | | NULL | |
  227. | 144477X148X740 | varchar(5) | YES | | NULL | |
  228. | 144477X148X741 | varchar(5) | YES | | NULL | |
  229. | 144477X148X742SQ001 | decimal(30,10) | YES | | NULL | |
  230. | 144477X148X745 | varchar(5) | YES | | NULL | |
  231. | 144477X148X748 | text | YES | | NULL | |
  232. +---------------------+----------------+------+-----+---------+----------------+
  233.  
  234. describe survey_questions ;
  235. +----------------+--------------+------+-----+---------+----------------+
  236. | Field | Type | Null | Key | Default | Extra |
  237. +----------------+--------------+------+-----+---------+----------------+
  238. | qid | int(11) | NO | PRI | NULL | auto_increment |
  239. | parent_qid | int(11) | NO | MUL | 0 | |
  240. | sid | int(11) | NO | MUL | 0 | |
  241. | gid | int(11) | NO | MUL | 0 | |
  242. | type | varchar(1) | NO | MUL | T | |
  243. | title | varchar(20) | NO | | | |
  244. | question | mediumtext | NO | | NULL | |
  245. | preg | mediumtext | YES | | NULL | |
  246. | help | mediumtext | YES | | NULL | |
  247. | other | varchar(1) | NO | | N | |
  248. | mandatory | varchar(1) | YES | | NULL | |
  249. | question_order | int(11) | NO | | NULL | |
  250. | language | varchar(20) | NO | PRI | en | |
  251. | scale_id | int(11) | NO | | 0 | |
  252. | same_default | int(11) | NO | | 0 | |
  253. | relevance | mediumtext | YES | | NULL | |
  254. | modulename | varchar(255) | YES | | NULL | |
  255. +----------------+--------------+------+-----+---------+----------------+
  256.  
  257. describe survey_answers_lookup;
  258. +---------------------+------------+------+-----+---------+----------------+
  259. | Field | Type | Null | Key | Default | Extra |
  260. +---------------------+------------+------+-----+---------+----------------+
  261. | id | int(11) | NO | PRI | NULL | auto_increment |
  262. | sid | int(11) | NO | | 0 | |
  263. | qid | int(11) | NO | | 0 | |
  264. | survey_table_row_id | int(11) | NO | | 0 | |
  265. | answer | mediumtext | NO | | NULL | |
  266. +---------------------+------------+------+-----+---------+----------------+
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement