public function addWhere($column, $value = null, $operator = null, $conj = null, $group = null, $join = false) { PHPWS_DB::touchDB(); $where = new PHPWS_DB_Where(); $where->setJoin($join); $operator = strtoupper($operator); // If passed in value was an array, loop over the array and call this method once for each column name if (is_array($column)) { foreach ($column as $new_column => $new_value) { $result = $this->addWhere($new_column, $new_value, $operator, $conj, $group); if (PHPWS_Error::isError($result)) { return $result; } } return true; } else { // Single column name passed in, check column name if (!PHPWS_DB::allowed($column) || preg_match('[^\\w\\.]', $column)) { return PHPWS_Error::get(PHPWS_DB_BAD_COL_NAME, 'core', 'PHPWS_DB::addWhere', $column); } } // If non-empty array of values passed in for this column name if (is_array($value) && !empty($value)) { if (!empty($operator) && $operator != 'IN' && $operator != 'NOT IN' && $operator != 'BETWEEN' && $operator != 'NOT BETWEEN') { $search_in = true; } else { if (empty($operator)) { $operator = 'IN'; } $search_in = false; } foreach ($value as $newVal) { if ($search_in) { $result = $this->addWhere($column, $newVal, $operator, $conj, $group); if (PHPWS_Error::isError($result)) { return $result; } } else { $newVal = $GLOBALS['PHPWS_DB']['connection']->escape($newVal); $new_value_list[] = $newVal; } } if (!$search_in && isset($new_value_list)) { $value =& $new_value_list; } else { return true; } } else { // Single value passed in if (is_null($value) || is_string($value) && strtoupper($value) == 'NULL') { if (empty($operator) || $operator != 'IS NOT' && $operator != '!=') { $operator = 'IS'; } else { $operator = 'IS NOT'; } $value = 'NULL'; } else { $value = $GLOBALS['PHPWS_DB']['connection']->escape($value); } } $source_table = $this->getSourceTable(); //$source_table = $this->tables[0]; if (is_string($column)) { if (substr_count($column, '.') == 1) { list($join_table, $join_column) = explode('.', $column); if (isset($this->table_as[$join_table])) { $source_table = $join_table; $column =& $join_column; } elseif (PHPWS_DB::inDatabase($join_table, $join_column)) { $source_table = $join_table; /*** * Commented out because this is trying to work too hard. * If you (as a developer) haven't selected from or joined * the table you're trying to add a 'WHERE' expression for, * then I can't help you. The query will fail, and you'll figure it out. */ //$this->addTable($join_table); } } } //TODO what do we do if $column isn't a string? $where->setColumn($column); $where->setTable($source_table); if (is_string($value)) { if (substr_count($value, '.') == 1) { list($join_table, $join_column) = explode('.', $value); if (isset($this->table_as[$join_table])) { $where->setJoin(true); } elseif ($this->inDatabase($join_table, $join_column)) { $where->setJoin(true); $this->addTable($join_table); } } } $where->setValue($value); $where->setConj($conj); $where->setOperator($operator); if (isset($group)) { $this->where[$group]['values'][] = $where; } else { $this->where[0]['values'][] = $where; } }
public function addWhere($column, $value = null, $operator = null, $conj = null, $group = null, $join = false) { PHPWS_DB::touchDB(); $where = new PHPWS_DB_Where(); $where->setJoin($join); $operator = strtoupper($operator); if (is_array($column)) { foreach ($column as $new_column => $new_value) { $result = $this->addWhere($new_column, $new_value, $operator, $conj, $group); if (PHPWS_Error::isError($result)) { return $result; } } return true; } else { if (!PHPWS_DB::allowed($column) || preg_match('[^\\w\\.]', $column)) { return PHPWS_Error::get(PHPWS_DB_BAD_COL_NAME, 'core', 'PHPWS_DB::addWhere', $column); } } if (is_array($value) && !empty($value)) { if (!empty($operator) && $operator != 'IN' && $operator != 'NOT IN' && $operator != 'BETWEEN' && $operator != 'NOT BETWEEN') { $search_in = true; } else { if (empty($operator)) { $operator = 'IN'; } $search_in = false; } foreach ($value as $newVal) { if ($search_in) { $result = $this->addWhere($column, $newVal, $operator, $conj, $group); if (PHPWS_Error::isError($result)) { return $result; } } else { $newVal = $GLOBALS['PHPWS_DB']['connection']->escape($newVal); $new_value_list[] = $newVal; } } if (!$search_in && isset($new_value_list)) { $value =& $new_value_list; } else { return true; } } else { if (is_null($value) || is_string($value) && strtoupper($value) == 'NULL') { if (empty($operator) || $operator != 'IS NOT' && $operator != '!=') { $operator = 'IS'; } else { $operator = 'IS NOT'; } $value = 'NULL'; } else { $value = $GLOBALS['PHPWS_DB']['connection']->escape($value); } } $source_table = $this->tables[0]; if (is_string($column)) { if (substr_count($column, '.') == 1) { list($join_table, $join_column) = explode('.', $column); if (isset($this->table_as[$join_table])) { $source_table = $join_table; $column =& $join_column; } elseif (PHPWS_DB::inDatabase($join_table, $join_column)) { $column =& $join_column; $source_table = $join_table; $this->addTable($join_table); } } } $where->setColumn($column); $where->setTable($source_table); if (is_string($value)) { if (substr_count($value, '.') == 1) { list($join_table, $join_column) = explode('.', $value); if (!empty($this->table_as) && isset($this->table_as[$join_table])) { $where->setJoin(true); } else { if ($this->inDatabase($join_table, $join_column)) { $where->setJoin(true); $this->addTable($join_table); } } } } $where->setValue($value); $where->setConj($conj); $where->setOperator($operator); if (isset($group)) { $this->where[$group]['values'][] = $where; } else { $this->where[0]['values'][] = $where; } }