Exemplo n.º 1
0
 /**
  * _get_where_clause 依照 $search 條件以及指定的 $table 回傳 WHERE 的 SQL 
  * 
  * @param Pix_Table_Search $search 
  * @param Pix_Table $table 
  * @access protected
  * @return string
  */
 protected function _get_where_clause($search)
 {
     $terms = array();
     $table = $search->getTable();
     foreach ($search->getSearchCondictions() as $condiction) {
         switch ($condiction[0]) {
             case 'map':
                 $terms[] = "(" . $this->column_quote($condiction[1]) . ' = ' . $this->quoteWithColumn($table, $condiction[2], $condiction[1]) . ")";
                 break;
             case 'string':
                 $terms[] = "(" . $condiction[1] . ")";
                 break;
             default:
                 throw new Pix_Table_Exception('不知名的狀態');
         }
     }
     if ($search->after() or $search->before()) {
         $orders = $search->order() ?: array_fill_keys($table->getPrimaryColumns(), 'asc');
         if (!is_array($orders)) {
             throw new Pix_Table_Exception("指定的 ORDER 無法使用 after 或是 before");
         }
         if (!($row = $search->after())) {
             // 如果指定 before 的話,順序要調過來
             $row = $search->before();
             $orders = Pix_Table_Search::reverseOrder($orders);
         }
         $equal_orders = array();
         $or_terms = array();
         foreach ($orders as $order => $way) {
             $and_terms = array();
             foreach ($equal_orders as $equal_order) {
                 $and_terms[] = $this->column_quote($equal_order) . " = " . $this->quoteWithColumn($table, $row->{$equal_order}, $equal_order);
             }
             $and_terms[] = $this->column_quote($order) . ('asc' == $way ? '>' : '<') . " " . $this->quoteWithColumn($table, $row->{$order}, $order);
             $or_terms[] = '(' . implode(' AND ', $and_terms) . ')';
             $equal_orders[] = $order;
         }
         $terms[] = '(' . implode(' OR ', $or_terms) . ')';
     }
     if (!$terms) {
         return '1 = 1';
     }
     return implode(' AND ', $terms);
 }