Initial check-in of basename/dirname for PostgreSQL.

This commit is contained in:
Elf M. Sternberg 2016-07-12 15:09:30 -07:00
parent 8fda1123b7
commit 6961710f75
6 changed files with 111 additions and 1 deletions

8
.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
*~
*.swp
db.sqlite3
bower_components
*.pyc
\#*#
.#*
catalogia/settings.hy

21
LICENSE Normal file
View File

@ -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 <elf.sternberg@gmail.com>

View File

@ -1 +0,0 @@
# postgres-basename-dirname

18
README.rst Normal file
View File

@ -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 <elf.sternberg@gmail.com>
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.

47
basename.sql Normal file
View File

@ -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;

17
basename_tests.sql Normal file
View File

@ -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);