Is there a better way to write this? Checking scenarios

Refresh

December 2018

Views

133 time

2

So I have this code that checks 4 parameters (author, title, keyword and subject) to do some dynamic sql query generation for a project I'm working on and I was just wondering if anyone knew of a better way to write this out. I get the feeling this is a really inefficient way to do this but it's late and I'm tired. Please let me know if you know of any better way to do this, it's just a bunch of if/else case checking statements at this point.

Thanks!

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 answers

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;
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);