postgresql7.2
select lv.variable_id,
lv.name,
lv.unit,
lv.type
from logger_variables lv
where (exists (select 1
from logger_project_var_map lpvm,
logger_project_pkg_map lppm
where lv.variable_id = lpvm.variable_id
and lpvm.project_id = lppm.project_id
and lppm.package_id = :package_id
)
or lv.package_id = :package_id
or lv.package_id is null)
and acs_permission__permission_p(lv.variable_id, :user_id, 'read') = 't'
$extra_where_clause