如何使这个查询更简单?

时间:2011-12-25 01:09:32

标签: c++ mysql

我有几个文本框,用户可以在其中添加日期条件,然后他获取textbox1<year<textbox2 and textbox2<month<textbox3 and textbox4<day<textbox5 and textbox6<hour<textbox7 and textbox8<minute<textbox9

的表格

如何使查询更简单(没有很多IF-ELSE)?

QString request::date_search(QString type, int y1, int y2, int mon1, int mon2,
                             int d1, int d2, int h1, int h2, int min1, int min2)
{
  QString req = "DATE("+type+") ";
  QString temp;
  if (((y1==0) && (mon1==0) && (d1==0) && (h1==0) && (min1==0)) &&
      ((y2==0) && (mon2==0) && (d2==0) && (h2==0) && (min2==0)))
  {
    return "";
  }

  if ((y1==0) && (mon1==0) && (d1==0) && (h1==0) && (min1==0))
  {
    return req += "= '" + temp.setNum(y2)   + "-" + temp.setNum(mon2) + "-" +
                          temp.setNum(d2)   + "-" + temp.setNum(h2)   + "-" +
                          temp.setNum(min2) + "-" + "0'";
  }
  if ((y2==0) && (mon2==0) && (d2==0) && (h2==0) && (min2==0)) 
  {
    return req += "= '" + temp.setNum(y1)   + "-" + temp.setNum(mon1) + "-" +
                          temp.setNum(d1)   + "-" + temp.setNum(h1)   + "-" +
                          temp.setNum(min1) + "-" + "0'";
  }

  req += "BETWEEN '" + temp.setNum(y2)   + "-" + temp.setNum(mon2) + "-" +
                       temp.setNum(d2)   + "-" + temp.setNum(h2)   + "-" +
                       temp.setNum(min2) + "-" +"0' "
       + "AND" + "'" + temp.setNum(y1)   + "-" + temp.setNum(mon1) + "-" +
                       temp.setNum(d1)   + "-" + temp.setNum(h1)   + "-" +
                       temp.setNum(min1) + "-" + "0'";

  return req;
}

这是一个函数,其中y1等是文本框中的参数。

1 个答案:

答案 0 :(得分:0)

显然,您需要创建一个函数来格式化组件的日期:

// Are you sure you don't need colons between hour and minute and second?
// What is the punctuation between the date and the time?
QString request::date_string(int yy, int mo, int dd, int hh, int mi)
{
    QString temp;  // Why?
    return "'" + temp.setNum(yy) + "-" + temp.setNum(mo) + "-" +
                 temp.setNum(dd) + "-" + temp.setNum(hh) + "-" +
                 temp.setNum(mi) + "-" + "0'";
}

QString request::date_search(QString type, int y1, int y2, int mon1, int mon2,
                             int d1, int d2, int h1, int h2, int min1, int min2)
{
  if ((y1 == 0 && mon1 == 0 && d1 == 0 && h1 == 0 && min1 == 0) &&
      (y2 == 0 && mon2 == 0 && d2 == 0 && h2 == 0 && min2 == 0))
      return "";

  QString req = "DATE(" + type + ") ";

  if (y1 == 0 && mon1 == 0 && d1 == 0 && h1 == 0 && min1 == 0)
      return req += "= " + date_string(y2, mon2, d2, h2, min2);
  if (y2 == 0 && mon2 == 0 && d2 == 0 && h2 == 0 && min2 == 0) 
      return req += "= " + date_string(y1, mon1, d1, h1, min1);

  return req += "BETWEEN " + date_string(y2, mon2, d2, h2, min2) +
                " AND "    + date_string(y1, mon1, d1, h1, min1);
}

在我看来,这是一个明智的选择。我不清楚你为什么需要变量temp;但是,我认为这是必要的。我担心不在DATE值的时间分量之间使用冒号。我不确定在白天和小时之间应该使用哪个分隔符(ISO 8601将使用“T”; ISO 9075(SQL)将使用空白“”。在测试中你也真的不需要这么多括号。

然而,这是简单的C ++编码风格;幕后的查询不能简单得多。请注意,在显示的代码中,“date1”的值(从y1创建,... min1)位于“date2”之前,然后查询将返回没有结果。您还没有显示任何验证日期的各个组成部分是有效的。