Example #1
0
 /**
  * Applies additional filters to this query based on the given SearchInfo.
  * 
  * The SearchInfo defines a search string and a list of columns. 
  * This translates into an additional 'where' clause, in which we filter for 
  * all records which match the search string on ANY of those columns.
  * 
  * For example, given SearchInfo: 
  *     {
  *         searchString: "john", 
  *         searchColumns: ["name", "age"]
  *     }
  * The following filter clause will be added:
  *     sqlBuilder.filter('name like ? or age like ?', 'ss', 'john', 'john');
  *
  * @see SQLBuilder::filter which construct adds a 'where' condition
  * @param SearchInfo $search contains the filtering info
  */
 private function applySearchFilter($search)
 {
     $searchString = $search->getSearchString();
     $searchColumns = $search->getSearchColumns();
     $numColumns = count($searchColumns);
     if (empty($searchString) || $numColumns < 1) {
         return;
     }
     if ($this->sql instanceof SQLBuilder) {
         /* 
          * Given a list of column names and search string, populate
          * 1. $condition - SQL clause
          * 2. $vartypes - types of parameters and
          * 3. $args - list of search strings
          * to be used for prepared statements.
          */
         $conditions = array();
         foreach ($searchColumns as $column) {
             $conditions[] = $column . " like ?";
         }
         $varTypes = str_repeat('s', $numColumns);
         // types of parameters passed
         $args = array_fill(0, $numColumns, "%{$searchString}%");
         // list of search strings repeated up to number of columns
         call_user_func_array(array($this->sql, "filter"), array_merge(array(implode(" or ", $conditions), $varTypes), $args));
     } else {
         throw IllegalStateException("Cannot support sql objects other than SQLBuilder.");
     }
 }
Example #2
0
 /**
  * Stores the columns on which filtering has to be applied
  * @param String[] $searchColumns
  */
 public function setSearchColumns($searchColumns)
 {
     $this->searchInfo->setSearchColumns($searchColumns);
 }