有没有更好的方法来写这个?检查方案

时间:2012-03-17 07:23:24

标签: java optimization if-statement

所以我有这个代码检查4个参数(作者,标题,关键字和主题)为我正在研究的项目做一些动态sql查询生成我只是想知道是否有人知道更好的写作方式这个。我觉得这是一种非常低效的方法,但是已经很晚了,我很累。如果您知道更好的方法,请告诉我,此时只是一堆if / else案例检查语句。

谢谢!

if(_author!=null)
                {
                    query += authorQ;
                    if(_title != null)
                    {
                        if(conjunct[0] == 0)
                        {
                            query += " AND ";
                            query += titleQ;
                        }
                        else
                        {
                            query += " OR ";
                            query += titleQ;
                        }
                        if(_keyword != null)
                        {
                            if(conjunct[1] == 0)
                            {
                                query += " AND ";
                                query += keywordQ;
                            }
                            else
                            {
                                query += " OR ";
                                query += keywordQ;
                            }
                            if(_subject != null)
                            {
                                if(conjunct[2] == 0)
                                {
                                    query += " AND ";
                                    query += subjectQ;
                                }
                                else
                                {
                                    query += " OR ";
                                    query += subjectQ;
                                }
                            }                               
                        }
                        else
                        {
                            if(_subject != null)
                            {
                                if(conjunct[2] == 0)
                                {
                                    query += " AND ";
                                    query += subjectQ;
                                }
                                else
                                {
                                    query += " OR ";
                                    query += subjectQ;
                                }
                            }                       
                        }
                    }//title = null
                    else
                    {
                        if(_keyword != null)
                        {
                            if(conjunct[1] == 0)
                            {
                                query += " AND ";
                                query += keywordQ;
                            }
                            else
                            {
                                query += " OR ";
                                query += keywordQ;
                            }
                            if(_subject != null)
                            {
                                if(conjunct[2] == 0)
                                {
                                    query += " AND ";
                                    query += subjectQ;
                                }
                                else
                                {
                                    query += " OR ";
                                    query += subjectQ;
                                }
                            }                               
                        }
                        else //keyword null
                        {
                            if(_subject != null)
                            {
                                if(conjunct[2] == 0)
                                {
                                    query += " AND ";
                                    query += subjectQ;
                                }
                                else
                                {
                                    query += " OR ";
                                    query += subjectQ;
                                }
                            }
                            //if subject's null at this point we don't care
                        }
                    }
                }
                else //author null
                {
                    if(_title != null)
                    {
                        if(conjunct[0] == 0)
                        {
                            query += " AND ";
                            query += titleQ;
                        }
                        else
                        {
                            query += " OR ";
                            query += titleQ;
                        }
                        if(_keyword != null)
                        {
                            if(conjunct[1] == 0)
                            {
                                query += " AND ";
                                query += keywordQ;
                            }
                            else
                            {
                                query += " OR ";
                                query += keywordQ;
                            }
                            if(_subject != null)
                            {
                                if(conjunct[2] == 0)
                                {
                                    query += " AND ";
                                    query += subjectQ;
                                }
                                else
                                {
                                    query += " OR ";
                                    query += subjectQ;
                                }
                            }                               
                        }
                        else
                        {
                            if(_subject != null)
                            {
                                if(conjunct[2] == 0)
                                {
                                    query += " AND ";
                                    query += subjectQ;
                                }
                                else
                                {
                                    query += " OR ";
                                    query += subjectQ;
                                }
                            }                       
                        }
                    }//title = null
                    else
                    {
                        if(_keyword != null)
                        {
                            if(conjunct[1] == 0)
                            {
                                query += " AND ";
                                query += keywordQ;
                            }
                            else
                            {
                                query += " OR ";
                                query += keywordQ;
                            }
                            if(_subject != null)
                            {
                                if(conjunct[2] == 0)
                                {
                                    query += " AND ";
                                    query += subjectQ;
                                }
                                else
                                {
                                    query += " OR ";
                                    query += subjectQ;
                                }
                            }                               
                        }
                        else //keyword null
                        {
                            if(_subject != null)
                            {
                                if(conjunct[2] == 0)
                                {
                                    query += " AND ";
                                    query += subjectQ;
                                }
                                else
                                {
                                    query += " OR ";
                                    query += subjectQ;
                                }
                            }
                            //if subject's null at this point we don't care
                        }
                    }
                }

if(_author!=null) { query += authorQ; if(_title != null) { if(conjunct[0] == 0) { query += " AND "; query += titleQ; } else { query += " OR "; query += titleQ; } if(_keyword != null) { if(conjunct[1] == 0) { query += " AND "; query += keywordQ; } else { query += " OR "; query += keywordQ; } if(_subject != null) { if(conjunct[2] == 0) { query += " AND "; query += subjectQ; } else { query += " OR "; query += subjectQ; } } } else { if(_subject != null) { if(conjunct[2] == 0) { query += " AND "; query += subjectQ; } else { query += " OR "; query += subjectQ; } } } }//title = null else { if(_keyword != null) { if(conjunct[1] == 0) { query += " AND "; query += keywordQ; } else { query += " OR "; query += keywordQ; } if(_subject != null) { if(conjunct[2] == 0) { query += " AND "; query += subjectQ; } else { query += " OR "; query += subjectQ; } } } else //keyword null { if(_subject != null) { if(conjunct[2] == 0) { query += " AND "; query += subjectQ; } else { query += " OR "; query += subjectQ; } } //if subject's null at this point we don't care } } } else //author null { if(_title != null) { if(conjunct[0] == 0) { query += " AND "; query += titleQ; } else { query += " OR "; query += titleQ; } if(_keyword != null) { if(conjunct[1] == 0) { query += " AND "; query += keywordQ; } else { query += " OR "; query += keywordQ; } if(_subject != null) { if(conjunct[2] == 0) { query += " AND "; query += subjectQ; } else { query += " OR "; query += subjectQ; } } } else { if(_subject != null) { if(conjunct[2] == 0) { query += " AND "; query += subjectQ; } else { query += " OR "; query += subjectQ; } } } }//title = null else { if(_keyword != null) { if(conjunct[1] == 0) { query += " AND "; query += keywordQ; } else { query += " OR "; query += keywordQ; } if(_subject != null) { if(conjunct[2] == 0) { query += " AND "; query += subjectQ; } else { query += " OR "; query += subjectQ; } } } else //keyword null { if(_subject != null) { if(conjunct[2] == 0) { query += " AND "; query += subjectQ; } else { query += " OR "; query += subjectQ; } } //if subject's null at this point we don't care } } }

2 个答案:

答案 0 :(得分:2)

如何采用更加结构化和可扩展的方法,请参见下面的示例。顺便说一句,你不应该直接将用户输入放入SQL查询 - 而是使用预准备语句来防止注入攻击。

// test values
String _author = "authorfoo";
String _title = "titlebar";
String _keyword = null;
String _subject = "subjectfoo";
String authorQ = "author=" + _author;
String subjectQ = "subject=" + _subject;
String titleQ = "title="+ _title;
String keywordQ = "keyword=" + _keyword;
int conjunct[] = new int[]{ 0, 1, 1, 0};

// query building code
String []inputs = {_author, _title, _keyword, _subject};
String []queries = {authorQ, titleQ, keywordQ, subjectQ};
StringBuilder builder = new StringBuilder();
for (int i = 0 ; i < inputs.length; i++) {
    if (inputs[i] != null) {
        if (builder.length() > 0) { // don't start query with connective
            if (conjunct[i] == 0) {
                builder.append(" AND ");
            } else {
                builder.append(" OR ");
            }
        }
        builder.append(queries[i]);
    }
}
String query = builder.toString();
System.out.println(query);

答案 1 :(得分:0)

a = _author ? authorQ  : 1;
t = _title ? titleQ  : 1;
k = _keyword ? keywordQ  : 1;
s = _subject ? subjectQ  : 1;

c1 = conjunct[0] ? " OR " : " AND ";
c2 = conjunct[1] ? " OR " : " AND ";
c3 = conjunct[2] ? " OR " : " AND ";

query += a + c1 + t + c2 + k + c3 + s;