diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..394611b --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +*~ +*.swp +db.sqlite3 +bower_components +*.pyc +\#*# +.#* +catalogia/settings.hy diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..17d186a --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +Copyright (c) 2016 Elf M. Sternberg + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + + - Elf M. Sternberg diff --git a/README.md b/README.md deleted file mode 100644 index 95aa0d5..0000000 --- a/README.md +++ /dev/null @@ -1 +0,0 @@ -# postgres-basename-dirname diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..9be7d0c --- /dev/null +++ b/README.rst @@ -0,0 +1,18 @@ +Synopsis +-------- + +`basename.sql` contains functions which provide the POSIX-equivalent +functions `basename` and `dirname` of PostgreSQL. The functions are +written in fairly standard PL/pgSQL, and should work with any version of +PostgreSQL after version 6. + +I make no claim that these functions are SQL/PSM-compliant. + +LICENSE AND COPYRIGHT NOTICE: NO WARRANTY GRANTED OR IMPLIED +------------------------------------------------------------ + +Copyright ⓒ 2016 Elf M. Sternberg + +Released under The MIT License. Please see the LICENSE file for more +details. All rights not clearly enumerated by the LICENSE are reserved +to the authors. diff --git a/basename.sql b/basename.sql new file mode 100644 index 0000000..876cf60 --- /dev/null +++ b/basename.sql @@ -0,0 +1,47 @@ +CREATE OR REPLACE FUNCTION basename(fullpath text, + suffix text DEFAULT NULL, + separator char DEFAULT '/') + RETURNS text AS +$$ +DECLARE + basename text; + suffixre text; + separare text; +BEGIN + separare := '^.*' || separator; + basename := regexp_replace(fullpath, separare, ''); + IF suffix IS NOT NULL THEN + suffixre := suffix + '$'; + basename := regexp_replace(basename, suffixre, ''); + END IF; + RETURN basename; +END; +$$ +LANGUAGE 'plpgsql' IMMUTABLE; + + +CREATE OR REPLACE FUNCTION dirname(fullpath text, + separator char DEFAULT '/') + RETURNS text AS +$$ +DECLARE + separare text; + dirname text; + compname text; +BEGIN + IF position(separator in fullpath) = 0 THEN + return ''; + END IF; + separare := '^(.*' || separator || ').*$'; + dirname = regexp_replace(fullpath, separare, '\1'); + IF length(dirname) != 0 THEN + compname := lpad('', length(dirname), separator); + IF compname != dirname THEN + dirname = rtrim(dirname, separator); + END IF; + END IF; + return dirname; +END; +$$ +LANGUAGE 'plpgsql' IMMUTABLE; + diff --git a/basename_tests.sql b/basename_tests.sql new file mode 100644 index 0000000..339c335 --- /dev/null +++ b/basename_tests.sql @@ -0,0 +1,17 @@ +CREATE TABLE testdata (name TEXT, direxpected TEXT, baseexpected TEXT); + +INSERT INTO testdata VALUES ('/foo/bar', '/foo', 'bar'); +INSERT INTO testdata VALUES ('/', '/', ''); +INSERT INTO testdata VALUES ('foo', '', 'foo'); +INSERT INTO testdata VALUES ('////foo', '////' ,'foo'); +INSERT INTO testdata VALUES ('//foo//bar', '//foo' ,'bar'); +INSERT INTO testdata VALUES ('/foo/bar', '/foo', 'bar'); +INSERT INTO testdata VALUES ('/foo/bar/', '/foo/bar', ''); + +SELECT name, basename(name) AS basetest, baseexpected, dirname(name) AS dirtest, direxpected, + CASE WHEN basename(name) = baseexpected THEN 'PASS' ELSE 'FAIL' END AS base, + CASE WHEN dirname(name) = direxpected THEN 'PASS' ELSE 'FAIL' END AS dir FROM testdata; + +DROP TABLE testdata; +DROP FUNCTION IF EXISTS basename(text, text, char); +DROP FUNCTION IF EXISTS dirname(text, char);