Skip to content

Commit

Permalink
ugh
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Van Dyk committed Mar 19, 2012
1 parent 831a535 commit 2697c99
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions slug.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
--begin;

create extension if not exists hstore;

--begin;

-- Test table
drop table if exists foos;
create table foos (
Expand Down Expand Up @@ -32,6 +33,7 @@ declare
new_slug_value text;
already_exists integer;
the_same boolean;
results foos%rowtype;
begin
slug_column := TG_ARGV[0];

Expand All @@ -44,18 +46,13 @@ begin
end if;
end if;


execute 'select coalesce(max(x), 0) from (select unnest(avals(slug_history)::integer[]) x from ' || tg_table_name || ' where search_slug_history($1, slug_history)) f'
execute 'select count(*) from (select unnest(avals(slug_history)::integer[]) x from ' || tg_table_name || ' where search_slug_history($1, slug_history)) f'
into already_exists using new_slug_value;

raise notice 'exists: %', already_exists;

if already_exists then
new_slug_value := new_slug_value || '--' || already_exists + 1;
end if;
already_exists := already_exists + 1;

NEW.slug := new_slug_value;
NEW.slug_history := NEW.slug_history || ARRAY[NEW.slug, already_exists::text]::hstore;
NEW.slug := new_slug_value || '--' || already_exists;
NEW.slug_history := NEW.slug_history || ARRAY[new_slug_value, already_exists::text]::hstore;

return NEW;
end;
Expand Down

0 comments on commit 2697c99

Please sign in to comment.