postgresql7.2
insert into portal_elements
(element_id, datasource_id, name, page_id, region, sort_key, shadeable_p, hideable_p)
select :element_id,
:datasource_id,
:name,
:page_id,
:region,
coalesce((select max(portal_elements.sort_key) + 1
from portal_elements
where page_id = :page_id
and region = :region), 1),
portal_datasources.shadeable_p,
portal_datasources.hideable_p
from portal_datasources
where portal_datasources.datasource_id = :datasource_id
select sort_key as other_sort_key,
element_id as other_element_id
from (select *
from portal_elements
where page_id = :page_id
and region = :region
and sort_key < :sort_key
and state != 'pinned'
order by sort_key desc) as other_element
limit 1
select sort_key as other_sort_key,
element_id as other_element_id
from (select *
from portal_elements
where page_id = :page_id
and region = :region
and sort_key > :sort_key
and state != 'pinned'
order by sort_key asc) as other_element
limit 1
update portal_elements
set region = :region,
sort_key = (select coalesce((select max(sort_key) + 1
from portal_elements
where page_id = :page_id
and region = :region),
1)
from dual)
where element_id = :element_id
update portal_elements
set page_id = :page_id,
region = :region,
sort_key = (select coalesce((select max(sort_key) + 1
from portal_elements
where page_id = :page_id
and region = :region),
1)
from dual)
where element_id = :element_id