/** * Constructs a select query and fetch 1 row using this query, and then process the row * * Internal function, do not override. * @param array @fields_array array of name value pairs used to construct query. * @param boolean $encode Optional, default true, encode fetched data. * @param boolean $deleted Optional, default true, if set to false deleted filter will not be added. * @return object Instance of this bean with fetched data. */ function retrieve_by_string_fields($fields_array, $encode = true, $deleted = true) { $where_clause = $this->get_where($fields_array, $deleted); $custom_join = $this->getCustomJoin(); $query = "SELECT {$this->table_name}.*" . $custom_join['select'] . " FROM {$this->table_name} " . $custom_join['join']; $query .= " {$where_clause}"; $GLOBALS['log']->debug("Retrieve {$this->object_name}: " . $query); //requireSingleResult has been deprecated. //$result = $this->db->requireSingleResult($query, true, "Retrieving record $where_clause:"); $result = $this->db->limitQuery($query, 0, 1, true, "Retrieving record {$where_clause}:"); if (empty($result)) { return null; } $row = $this->db->fetchByAssoc($result, $encode); if (empty($row)) { return null; } // Removed getRowCount-if-clause earlier and insert duplicates_found here as it seems that we have found something // if we didn't return null in the previous clause. $this->duplicates_found = true; $row = $this->convertRow($row); $this->fetched_row = $row; $this->fromArray($row); $this->is_updated_dependent_fields = false; $this->fill_in_additional_detail_fields(); return $this; }
/** * Get query for retrieving beans from this link * @param array $params * optional parameters. Possible Values; * 'return_as_array': returns the query broken into * @return String/Array query to grab just ids for this relationship */ public function getQuery($params = array()) { $query_array['select'] = "SELECT DISTINCT emails.* "; $query_array['from'] = "FROM emails "; $query_array['join'] = $this->getEmailsJoin(); $deleted = !empty($params['deleted']) ? 1 : 0; $query_array['where'] = " WHERE emails.deleted={$deleted} "; // Add any optional where clause if (!empty($params['where'])) { $query_array['where'] .= " AND ({$params['where']}) "; } if (!empty($params['enforce_teams'])) { $seed = BeanFactory::getBean($this->getRelatedModuleName()); $seed->addVisibilityFrom($query_array['join']); $seed->addVisibilityWhere($query_array['where']); } if (!empty($params['return_as_array'])) { return $query_array; } $query = $query_array['select'] . $query_array['from'] . $query_array['join'] . $query_array['where']; if (!empty($params['orderby'])) { $query .= "ORDER BY {$params['orderby']}"; } if (!empty($params['limit']) && $params['limit'] > 0) { $offset = isset($params['offset']) ? $params['offset'] : 0; $query = $this->db->limitQuery($query, $offset, $params['limit'], false, "", false); } return $query; }
public function disabledLimitQuery() { $beanIds = $this->_createRecords(5); $_REQUEST['module'] = 'contacts'; $result = $this->_db->limitQuery("SELECT id From contacts where last_name = 'foobar'", 1, 3); if ($this->_db instanceof MysqliManager) { $this->assertInstanceOf('Mysqli_result', $result); } else { $this->assertTrue(is_resource($result)); } while ($row = $this->_db->fetchByAssoc($result)) { if ($row['id'][0] > 3 || $row['id'][0] < 0) { $this->assertFalse(in_array($row['id'], $beanIds), "Found {$row['id']} in error"); } else { $this->assertTrue(in_array($row['id'], $beanIds), "Didn't find {$row['id']}"); } } unset($_REQUEST['module']); $this->_removeRecords($beanIds); }
function execute_query($query_name = 'query', $result_name = 'result', $row_count_name = 'row_count', $row_start_name = 'row_start', $row_end_name = 'row_end', $limit = false) { // FIXME: needs DB-independent code here if ($limit) { $start_offset = $this->report_offset; if (!$this->db->supports('select_rows')) { if ($start_offset > 0) { $start_offset++; } } $this->{$result_name} = $this->db->limitQuery($this->{$query_name}, $start_offset, $this->report_max, true, "Error executing query "); } else { $this->{$result_name} = $this->db->query($this->{$query_name}, true, "Error executing query "); } if (!empty($row_count_name) && empty($this->{$row_count_name})) { $this->{$row_count_name} = $this->report_offset; $this->{$row_end_name} = $this->report_max; if ($limit && $this->total_count < $this->{$row_end_name} + $this->{$row_count_name}) { $this->{$row_end_name} = $this->total_count - $this->{$row_count_name}; } if ($this->{$row_count_name} > 0) { $this->{$row_start_name} = 1; } } }