$distinct = 'DISTINCT ON (calendar_item.uid) ';
    $params[':path_match'] = '^' . $target_collection->bound_from();
} else {
    $where = ' WHERE caldav_data.collection_id = ' . $target_collection->resource_id();
    $distinct = '';
}
if (is_array($qry_filters)) {
    dbg_log_array("calquery", "qry_filters", $qry_filters, true);
    $components = array();
    $filter_fragment = SqlFilterFragment($qry_filters, $components);
    if ($filter_fragment !== false) {
        $where .= ' ' . $filter_fragment['sql'];
        $params = array_merge($params, $filter_fragment['params']);
    }
}
if ($target_collection->Privileges() != privilege_to_bits('DAV::all')) {
    $where .= " AND (calendar_item.class != 'PRIVATE' OR calendar_item.class IS NULL) ";
}
if (isset($c->hide_TODO) && ($c->hide_TODO === true || is_string($c->hide_TODO) && preg_match($c->hide_TODO, $_SERVER['HTTP_USER_AGENT'])) && !$target_collection->HavePrivilegeTo('all')) {
    $where .= " AND caldav_data.caldav_type NOT IN ('VTODO') ";
}
if (isset($c->hide_older_than) && intval($c->hide_older_than > 0)) {
    $where .= " AND (CASE WHEN caldav_data.caldav_type<>'VEVENT' OR calendar_item.dtstart IS NULL THEN true ELSE calendar_item.dtstart > (now() - interval '" . intval($c->hide_older_than) . " days') END) ";
}
$sql = 'SELECT ' . $distinct . ' caldav_data.*,calendar_item.*  FROM collection INNER JOIN caldav_data USING(collection_id) INNER JOIN calendar_item USING(dav_id) ' . $where;
if (isset($c->strict_result_ordering) && $c->strict_result_ordering) {
    $sql .= " ORDER BY caldav_data.dav_id";
}
$qry = new AwlQuery($sql, $params);
if ($qry->Exec("calquery", __LINE__, __FILE__) && $qry->rows() > 0) {
    while ($dav_object = $qry->Fetch()) {
    $rawurl = rawurldecode($v->GetContent());
    $path_pos = strpos($rawurl, $request->path);
    if ($path_pos === false) {
        $href = $bound_from . $rawurl;
    } else {
        $href = $bound_from . substr($rawurl, $path_pos + strlen($request->path));
    }
    @dbg_error_log("REPORT", 'Reporting on href "%s"', $href);
    $href_in .= $href_in == '' ? '' : ', ';
    $href_in .= ':href' . $k;
    $params[':href' . $k] = $href;
}
$where = " WHERE caldav_data.collection_id = " . $collection->resource_id();
$where .= " AND caldav_data.dav_name IN ( {$href_in} ) ";
if ($mode == 'caldav') {
    if ($collection->Privileges() != privilege_to_bits('DAV::all')) {
        $where .= " AND (calendar_item.class != 'PRIVATE' OR calendar_item.class IS NULL) ";
    }
    if (isset($c->hide_TODO) && $c->hide_TODO && !$collection->Privileges() == privilege_to_bits('all')) {
        $where .= " AND caldav_data.caldav_type NOT IN ('VTODO') ";
    }
}
$sql = 'SELECT calendar_item.*, addressbook_resource.*, caldav_data.* FROM caldav_data
                  LEFT JOIN calendar_item USING(dav_id, user_no, dav_name, collection_id)
                  LEFT JOIN addressbook_resource USING(dav_id)
                  LEFT JOIN collection USING(collection_id)';
/**
 * @todo: Add stanzas for missing rows, so we don't just return a blank multistatus but
 * actually return <response> stanzas with a 404 for each absent href.  We could do
 * this relatively easily with an array_flip($params) and remove each matching dav_name
 * as we process it.