From cee16532fed0bb60f4b1a226a8e2e53a8eaabb12 Mon Sep 17 00:00:00 2001 From: miker Date: Sat, 16 Oct 2010 15:32:32 +0000 Subject: [PATCH] use a function to wrap up escaping of solidus and casting to bytea, propogate to indexing and where/order_by git-svn-id: svn://svn.open-ils.org/ILS/trunk@18364 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- .../Application/Storage/Publisher/asset.pm | 16 ++++++++-------- .../perlmods/OpenILS/Application/SuperCat.pm | 8 ++++---- Open-ILS/src/sql/Pg/002.functions.config.sql | 4 ++++ Open-ILS/src/sql/Pg/002.schema.config.sql | 2 +- Open-ILS/src/sql/Pg/040.schema.asset.sql | 4 ++-- Open-ILS/src/sql/Pg/1.6.1-2.0-upgrade-db.sql | 2 +- .../0439.schema.function-bytea-index-label.sql | 17 +++++++++++++++++ 7 files changed, 37 insertions(+), 16 deletions(-) create mode 100644 Open-ILS/src/sql/Pg/upgrade/0439.schema.function-bytea-index-label.sql diff --git a/Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/asset.pm b/Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/asset.pm index 81a9b1bd58..77b213da20 100644 --- a/Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/asset.pm +++ b/Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/asset.pm @@ -230,9 +230,9 @@ sub cn_browse_pagedown { $table cn where not deleted - and (cast(upper(label) as bytea) > ? or ( cn.id > ? and cast(upper(label) as bytea) = ? )) + and (oils_text_as_bytea(upper(label)) > ? or ( cn.id > ? and oils_text_as_bytea(upper(label)) = ? )) and owning_lib in ($orgs) - order by cast(upper(label) as bytea), 4, 2 + order by oils_text_as_bytea(upper(label)), 4, 2 limit $size; SQL @@ -285,9 +285,9 @@ sub cn_browse_pageup { $table cn where not deleted - and (cast(upper(label) as bytea) < ? or ( cn.id < ? and cast(upper(label) as bytea) = ? )) + and (oils_text_as_bytea(upper(label)) < ? or ( cn.id < ? and oils_text_as_bytea(upper(label)) = ? )) and owning_lib in ($orgs) - order by cast(upper(label) as bytea) desc, 4 desc, 2 desc + order by oils_text_as_bytea(upper(label)) desc, 4 desc, 2 desc limit $size ) as bar order by 1,4,2; @@ -343,9 +343,9 @@ sub cn_browse_target { $table cn where not deleted - and cast(upper(label) as bytea) < ? + and oils_text_as_bytea(upper(label)) < ? and owning_lib in ($orgs) - order by cast(upper(label) as bytea) desc, 4 desc, 2 desc + order by oils_text_as_bytea(upper(label)) desc, 4 desc, 2 desc limit $topsize ) as bar order by 1,4,2; @@ -361,9 +361,9 @@ sub cn_browse_target { $table cn where not deleted - and cast(upper(label) as bytea) >= ? + and oils_text_as_bytea(upper(label)) >= ? and owning_lib in ($orgs) - order by cast(upper(label) as bytea),4,2 + order by oils_text_as_bytea(upper(label)),4,2 limit $bottomsize; SQL diff --git a/Open-ILS/src/perlmods/OpenILS/Application/SuperCat.pm b/Open-ILS/src/perlmods/OpenILS/Application/SuperCat.pm index 53847925ba..5c6f1d02e0 100644 --- a/Open-ILS/src/perlmods/OpenILS/Application/SuperCat.pm +++ b/Open-ILS/src/perlmods/OpenILS/Application/SuperCat.pm @@ -305,7 +305,7 @@ sub cn_browse { }, { flesh => 1, flesh_fields => { acn => [qw/record owning_lib/] }, - order_by => { acn => "cast(label_sortkey as bytea) desc, upper(label) desc, id desc, owning_lib desc" }, + order_by => { acn => "oils_text_as_bytea(label_sortkey) desc, upper(label) desc, id desc, owning_lib desc" }, limit => $before_limit, offset => abs($page) * $page_size - $before_offset, } @@ -323,7 +323,7 @@ sub cn_browse { }, { flesh => 1, flesh_fields => { acn => [qw/record owning_lib/] }, - order_by => { acn => "cast(label_sortkey as bytea), upper(label), id, owning_lib" }, + order_by => { acn => "oils_text_as_bytea(label_sortkey), upper(label), id, owning_lib" }, limit => $after_limit, offset => abs($page) * $page_size - $after_offset, } @@ -428,7 +428,7 @@ sub cn_startwith { }, { flesh => 1, flesh_fields => { acn => [qw/record owning_lib/] }, - order_by => { acn => "cast(label_sortkey as bytea) desc, upper(label) desc, id desc, owning_lib desc" }, + order_by => { acn => "oils_text_as_bytea(label_sortkey) desc, upper(label) desc, id desc, owning_lib desc" }, limit => $limit, offset => $offset, } @@ -446,7 +446,7 @@ sub cn_startwith { }, { flesh => 1, flesh_fields => { acn => [qw/record owning_lib/] }, - order_by => { acn => "cast(label_sortkey as bytea), upper(label), id, owning_lib" }, + order_by => { acn => "oils_text_as_bytea(label_sortkey), upper(label), id, owning_lib" }, limit => $limit, offset => $offset, } diff --git a/Open-ILS/src/sql/Pg/002.functions.config.sql b/Open-ILS/src/sql/Pg/002.functions.config.sql index 46daf9e777..cc97d92f42 100644 --- a/Open-ILS/src/sql/Pg/002.functions.config.sql +++ b/Open-ILS/src/sql/Pg/002.functions.config.sql @@ -597,5 +597,9 @@ if ($create or $munge) { return; $func$ LANGUAGE PLPERLU; +CREATE OR REPLACE FUNCTION oils_text_as_bytea (TEXT) RETURNS BYTEA AS $_$ + SELECT CAST(REGEXP_REPLACE($1, $$\\$$, $$\\\\$$, 'g') AS BYTEA); +$_$ LANGUAGE SQL IMMUTABLE; + COMMIT; diff --git a/Open-ILS/src/sql/Pg/002.schema.config.sql b/Open-ILS/src/sql/Pg/002.schema.config.sql index 58bc8fc41f..85f7a4edc6 100644 --- a/Open-ILS/src/sql/Pg/002.schema.config.sql +++ b/Open-ILS/src/sql/Pg/002.schema.config.sql @@ -70,7 +70,7 @@ CREATE TABLE config.upgrade_log ( install_date TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW() ); -INSERT INTO config.upgrade_log (version) VALUES ('0438'); -- miker +INSERT INTO config.upgrade_log (version) VALUES ('0439'); -- miker CREATE TABLE config.bib_source ( id SERIAL PRIMARY KEY, diff --git a/Open-ILS/src/sql/Pg/040.schema.asset.sql b/Open-ILS/src/sql/Pg/040.schema.asset.sql index 5509df6b1c..6b604df2a9 100644 --- a/Open-ILS/src/sql/Pg/040.schema.asset.sql +++ b/Open-ILS/src/sql/Pg/040.schema.asset.sql @@ -293,8 +293,8 @@ CREATE INDEX asset_call_number_record_idx ON asset.call_number (record); CREATE INDEX asset_call_number_creator_idx ON asset.call_number (creator); CREATE INDEX asset_call_number_editor_idx ON asset.call_number (editor); CREATE INDEX asset_call_number_dewey_idx ON asset.call_number (public.call_number_dewey(label)); -CREATE INDEX asset_call_number_upper_label_id_owning_lib_idx ON asset.call_number (cast(upper(label) as bytea),id,owning_lib); -CREATE INDEX asset_call_number_label_sortkey ON asset.call_number(cast(label_sortkey as bytea)); +CREATE INDEX asset_call_number_upper_label_id_owning_lib_idx ON asset.call_number (oils_text_as_bytea(upper(label)),id,owning_lib); +CREATE INDEX asset_call_number_label_sortkey ON asset.call_number(oils_text_as_bytea(label_sortkey)); CREATE UNIQUE INDEX asset_call_number_label_once_per_lib ON asset.call_number (record, owning_lib, label) WHERE deleted = FALSE OR deleted IS FALSE; CREATE RULE protect_cn_delete AS ON DELETE TO asset.call_number DO INSTEAD UPDATE asset.call_number SET deleted = TRUE WHERE OLD.id = asset.call_number.id; CREATE TRIGGER asset_label_sortkey_trigger diff --git a/Open-ILS/src/sql/Pg/1.6.1-2.0-upgrade-db.sql b/Open-ILS/src/sql/Pg/1.6.1-2.0-upgrade-db.sql index 99d3b403d2..3a6b9523bf 100644 --- a/Open-ILS/src/sql/Pg/1.6.1-2.0-upgrade-db.sql +++ b/Open-ILS/src/sql/Pg/1.6.1-2.0-upgrade-db.sql @@ -17770,7 +17770,7 @@ ALTER TABLE asset.call_number ADD COLUMN label_sortkey TEXT; CREATE INDEX asset_call_number_label_sortkey - ON asset.call_number(cast(label_sortkey as bytea)); + ON asset.call_number(oils_text_as_bytea(label_sortkey)); ALTER TABLE auditor.asset_call_number_history ADD COLUMN label_class BIGINT; diff --git a/Open-ILS/src/sql/Pg/upgrade/0439.schema.function-bytea-index-label.sql b/Open-ILS/src/sql/Pg/upgrade/0439.schema.function-bytea-index-label.sql new file mode 100644 index 0000000000..ae850cd4f7 --- /dev/null +++ b/Open-ILS/src/sql/Pg/upgrade/0439.schema.function-bytea-index-label.sql @@ -0,0 +1,17 @@ + +BEGIN; + +INSERT INTO config.upgrade_log (version) VALUES ('0439'); -- miker + +CREATE OR REPLACE FUNCTION oils_text_as_bytea (TEXT) RETURNS BYTEA AS $_$ + SELECT CAST(REGEXP_REPLACE($1, $$\\$$, $$\\\\$$, 'g') AS BYTEA); +$_$ LANGUAGE SQL IMMUTABLE; + + +DROP INDEX asset.asset_call_number_upper_label_id_owning_lib_idx; +CREATE INDEX asset_call_number_upper_label_id_owning_lib_idx ON asset.call_number (oils_text_as_bytea(label),id,owning_lib); + +DROP INDEX asset.asset_call_number_label_sortkey; +CREATE INDEX asset_call_number_label_sortkey ON asset.call_number(oils_text_as_bytea(label_sortkey)); + +COMMIT; -- 2.43.2