postgresql7.2
select sort_key as other_sort_key,
element_id as other_element_id
from (select *
from layout_elements
where page_id = :page_id
and page_column = :page_column
and sort_key < :sort_key
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 layout_elements
where page_id = :page_id
and page_column = :page_column
and sort_key > :sort_key
order by sort_key asc) as other_element
limit 1
update layout_elements
set page_column = :page_column,
sort_key = (select coalesce((select max(sort_key) + 1
from layout_elements
where page_id = :page_id
and page_column = :page_column),
1)
from dual)
where element_id = :element_id
update layout_elements
set page_id = :page_id,
page_column = :page_column,
sort_key = (select coalesce((select max(sort_key) + 1
from layout_elements
where page_id = :page_id
and page_column = :page_column),
1)
from dual)
where element_id = :element_id