我正在构建一个grails应用程序,groovy控制器抛出错误 - “方法代码太大”
代码:
class ComController {
def ajaxGetMv = {
if(params.metric == 'Marketing visits')
{
//All-4 "All" condition
if (params.mv == 'All' && params.browser == 'All' && params.platform == 'All' && params.device == 'All')
{
def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.time_period])
render com as JSON
}
// 1 All conditions
else if(params.mv == 'All' && params.browser != 'All' && params.platform != 'All' && params.device != 'All')
{
def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.device = ? and p.browser = ? and p.platform = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.device, params.browser, params.platform, params.time_period])
render com as JSON
}
else if(params.mv != 'All' && params.browser == 'All' && params.platform != 'All' && params.device != 'All')
{
def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.device = ? and p.mv = ? and p.platform = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.device, params.mv, params.platform, params.time_period])
render com as JSON
}
else if(params.mv != 'All' && params.browser != 'All' && params.platform == 'All' && params.device != 'All')
{
def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.device = ? and p.mv = ? and p.browser = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.device, params.mv, params.browser, params.time_period])
render com as JSON
}
else if(params.mv != 'All' && params.browser != 'All' && params.platform != 'All' && params.device == 'All')
{
def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.platform = ? and p.mv = ? and p.browser = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.platform, params.mv, params.browser, params.time_period])
render com as JSON
}
// 2 All conditions
else if(params.mv == 'All' && params.browser == 'All' && params.platform != 'All' && params.device != 'All')
{
def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.platform = ? and p.device = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.platform, params.device, params.time_period])
render com as JSON
}
else if(params.mv == 'All' && params.browser != 'All' && params.platform == 'All' && params.device != 'All')
{
def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.device = ? and p.browser = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.device, params.browser, params.time_period])
render com as JSON
}
else if(params.mv == 'All' && params.browser != 'All' && params.platform != 'All' && params.device == 'All')
{
def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.platform = ? and p.browser = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.platform, params.browser, params.time_period])
render com as JSON
}
else if(params.mv != 'All' && params.browser == 'All' && params.platform == 'All' && params.device != 'All')
{
def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.mv = ? and p.device = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.mv, params.device, params.time_period])
render com as JSON
}
else if(params.mv != 'All' && params.browser != 'All' && params.platform == 'All' && params.device == 'All')
{
def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.mv = ? and p.browser = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.mv, params.browser, params.time_period])
render com as JSON
}
else if(params.mv != 'All' && params.browser == 'All' && params.platform != 'All' && params.device == 'All')
{
def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.mv = ? and p.platform = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.mv, params.platform, params.time_period])
render com as JSON
}
//3 All conditions
else if(params.mv == 'All' && params.browser == 'All' && params.platform == 'All' && params.device != 'All')
{
def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.device = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.device, params.time_period])
render com as JSON
}
else if(params.mv != 'All' && params.browser == 'All' && params.platform == 'All' && params.device == 'All')
{
def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.mv = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.mv, params.time_period])
render com as JSON
}
else if(params.mv == 'All' && params.browser != 'All' && params.platform == 'All' && params.device == 'All')
{
def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.browser = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.browser, params.time_period])
render com as JSON
}
else if(params.mv == 'All' && params.browser == 'All' && params.platform != 'All' && params.device == 'All')
{
def com = Com.executeQuery("select p.date_hour, (sum(p.marketing)*100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.platform = ? and p.time_period = ? group by p.date_hour order by col_0_0_ asc ",[params.department, params.platform, params.time_period])
render com as JSON
}
//No all - basic condition
else
{
def com = Com.executeQuery("select p.date_hour, (p.marketing * 100), case when marketing_ly IN (0,null) THEN 0 ELSE ((marketing/marketing_ly - 1)*100*100) END as yoy, case when marketing_lw IN (0,null) THEN 0 ELSE ((marketing/marketing_lw - 1)*100*100) END as wow, p.mv, p.browser, p.department, p.platform, p.device, p.time_period from Com p where p.department = ? and p.device = ? and p.browser = ? and p.platform = ? and p.mv = ? and p.time_period = ? order by col_0_0_ asc ",[params.department, params.device, params.browser, params.platform, params.mv, params.time_period])
render com as JSON
}
}
if中的整个代码将重复15次,因为有15个度量(params.metric),因此出错。 查找错误后唯一的选择是拆分代码。
所以我在grails中使用“redirect”动作拆分它,并为每个if-else语句使用不同的方法来防止方法代码太大
新代码:
def ajaxGetMv = {
if(params.metric == 'Marketing visits')
{
redirect ( action : "ajaxGetMv2" )
}
但是,这不起作用,甚至没有抛出错误。为什么这种重定向不起作用?
我可以分割代码的其他方法是什么?
欢迎所有方法/建议。
答案 0 :(得分:0)
查看criteria query
。它可以帮助您参数化查询并摆脱那些可怕的if-else
- 块
答案 1 :(得分:0)
通过简单地调用方法
中的方法解决了这个问题代码:
def ajaxGetMv = {
if(params.metric == 'Marketing visits')
{
ajaxGetMv2();
}
def ajaxGetMv2 = {
//All if-else conditions
}
以这种方式书写确保没有方法代码> 64kB这是这里的限制