0

I've got this following query builder. Some of the cases has the same peice of code (those who have parameter).

I know its not good to duplicate code that much if at all.

How would you suggest me to improve this loop? Thanks !

for ($i = 0; $i < $len; ++$i) {
    switch ($conditions[$i]['condition']) {
        case 'version':
            $value_counter++;
            $sql .= ' AND `musers`.`app_version` = :value'.$value_counter;
            $values[$value_counter] = $conditions[$i]['value'];
            break;
        case 'except_version':
            $value_counter++;
            $sql .= ' AND `musers`.`app_version` != :value'.$value_counter;
            $values[$value_counter] = $conditions[$i]['value'];
            break;
        case 'from_credits':
            $value_counter++;
            $sql .= ' AND `musers`.`credits` >= :value'.$value_counter;
            $values[$value_counter] = $conditions[$i]['value'];
            break;
        case 'to_credits':
            $value_counter++;
            $sql .= ' AND `musers`.`credits` <= :value'.$value_counter;
            $values[$value_counter] = $conditions[$i]['value'];
            break;
        case 'no_credits':
            $sql .= ' AND `musers`.`credits` = 0';
            break;
        case 'register_atleast':
            $value_counter++;
            $sql .= ' AND DATEDIFF(NOW(), `musers`.`creation_date`) > :value'.$value_counter;
            $values[$value_counter] = $conditions[$i]['value'];
            break;
        case 'talked_atleast':
            $value_counter++;
            $sql .= " AND `musers`.`total_talktime` >= :value".$value_counter;
            $values[$value_counter] = $conditions[$i]['value'];
            break;
        case 'never_talked':
            $sql .= " AND `musers`.`total_talktime` = 0";
            break;
        case 'purchase_atleast':
            $value_counter++;
            $sql .= " AND `musers`.`purchase_times` >= :value".$value_counter;
            $values[$value_counter] = $conditions[$i]['value'];
            break;
        case 'never_purchase':
            $sql .= ' AND `musers`.`purchase_times` = 0';
            break;
        case 'from_birthdate':
            $value_counter++;
            $sql .= " AND (`musers`.`birthdate` = '0000-00-00' OR `musers`.`birthdate` >= :value".$value_counter." )";
            $values[$value_counter] = date('Y-m-d', strtotime($conditions[$i]['value']));
            break;
        case 'to_birthdate':
            $value_counter++;
            $sql .= " AND (`musers`.`birthdate` = '0000-00-00' OR `musers`.`birthdate` <= :value".$value_counter." )";
            $values[$value_counter] = date('Y-m-d', strtotime($conditions[$i]['value']));
            break;
        case 'from_creation_date':
            $value_counter++;
            $sql .= " AND (`musers`.`creation_date` = '0000-00-00' OR `musers`.`creation_date` >= :value".$value_counter." )";
            $values[$value_counter] = date('Y-m-d', strtotime($conditions[$i]['value']));
            break;
        case 'to_creation_date':
            $value_counter++;
            $sql .= " AND (`musers`.`creation_date` = '0000-00-00' OR `musers`.`creation_date` <= :value".$value_counter." )";
            $values[$value_counter] = date('Y-m-d', strtotime($conditions[$i]['value']));
            break;
        case 'user_id':
            $value_counter++;
            $sql .= ' AND `musers`.`id` = :value'.$value_counter;
            $values[$value_counter] = $conditions[$i]['value'];
            break;
        case 'visit_app_aleast':
            $value_counter++;
            $sql .= ' AND `musers`.`visit_app_times` >= :value'.$value_counter;
            $values[$value_counter] = $conditions[$i]['value'];
            break;
        case 'visit_app_less':
            $value_counter++;
            $sql .= ' AND `musers`.`visit_app_times` < :value'.$value_counter;
            $values[$value_counter] = $conditions[$i]['value'];
            break;
        case 'total_calls_atleast':
            $value_counter++;
            $sql .= ' AND `musers`.`total_calls` >= :value'.$value_counter;
            $values[$value_counter] = $conditions[$i]['value'];
            break;
        case 'total_calls_less':
            $value_counter++;
            $sql .= ' AND `musers`.`total_calls` < :value'.$value_counter;
            $values[$value_counter] = $conditions[$i]['value'];
            break;
        case 'hourdiff':
            $value_counter++;
            $sql .= ' AND `hourdiff` = :value'.$value_counter;
            $values[$value_counter] = $conditions[$i]['value'];
            break;
    }
}
4

1 回答 1

-1

通常,如果要避免代码重复,请在函数或对象方法中提取重复的代码。即使对于 3 行代码,如果重复,也值得创建一个函数并在需要这 3 行代码的任何地方调用它。

在您的特定情况下,还有另一种方法:将机箱堆叠在您的开关结构中。

switch($conditions[$i]['condition']) {
    case 'version':
    case 'except_version':
    case 'from_credits':
        $value_counter++;
        $sql .= ' AND `musers`.`app_version` = :value'.$value_counter;
        $values[$value_counter] = $conditions[$i]['value'];
        break;
     case 'no_credits':
        $sql .= ' AND `musers`.`credits` = 0';
        break;
     // etc.
}

查看“switch”文档以获取更多解释:)

于 2013-10-03T12:17:13.120 回答