Skip to content

Commit 8230644

Browse files
committed
Add unit_load() function to load/update the data tables.
1 parent ef456fb commit 8230644

File tree

5 files changed

+63
-6
lines changed

5 files changed

+63
-6
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,13 @@ The [definitions.units](definitions.units) file is an interesting read on its
189189
own due to extensive comments explaining the history and origin of the units
190190
covered.
191191

192-
User-Defined Units
193-
------------------
192+
The `unit_load()` function can be used to reload the `unit_prefixes.data` and
193+
`unit_units.data` files from disk. Extension upgrades use it to load newly
194+
added or changed definitions, but it is also user-callable. User-defined
195+
entries are preserved.
196+
197+
User-Defined Prefixes and Units
198+
-------------------------------
194199

195200
To create custom prefixes and units, insert new rows into the tables:
196201

@@ -276,6 +281,7 @@ function stddev_samp(unit)
276281
function stddev(unit)
277282
function sum(unit)
278283
function unit_is_hashed(cstring)
284+
function unit_load()
279285
function unit_reset()
280286
function value(unit)
281287
function variance(unit)

debian/changelog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ postgresql-unit (6.0-1) UNRELEASED; urgency=medium
44
as double precision number.
55
* Fix parsing of addition/subtraction in unit values.
66
* Grant SELECT on unit prefixes and units table to public.
7+
* Add unit_load() function to load/update the data tables.
78

89
-- Christoph Berg <[email protected]> Thu, 11 Jan 2018 20:49:44 +0100
910

unit--5--6.sql.in

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
GRANT SELECT ON unit_prefixes, unit_units TO PUBLIC;
22

3+
CREATE OR REPLACE FUNCTION unit_load()
4+
RETURNS VOID
5+
LANGUAGE plpgsql
6+
AS $$BEGIN
7+
DELETE FROM unit_prefixes WHERE dump IS NOT TRUE;
8+
CREATE TEMP TABLE tmp_prefixes (LIKE unit_prefixes) ON COMMIT DROP;
9+
COPY tmp_prefixes (prefix, factor, definition, dump) FROM '@MODULEDIR@/unit_prefixes.data';
10+
INSERT INTO unit_prefixes
11+
SELECT * FROM tmp_prefixes t WHERE NOT EXISTS
12+
(SELECT prefix FROM unit_prefixes u WHERE u.prefix = t.prefix);
13+
DROP TABLE tmp_prefixes;
14+
15+
DELETE FROM unit_units WHERE dump IS NOT TRUE;
16+
CREATE TEMP TABLE tmp_units (LIKE unit_units) ON COMMIT DROP;
17+
COPY unit_units (name, unit, shift, definition, dump) FROM '@MODULEDIR@/unit_units.data';
18+
INSERT INTO unit_units
19+
SELECT * FROM tmp_units t WHERE NOT EXISTS
20+
(SELECT name FROM unit_units u WHERE u.name = t.name);
21+
DROP TABLE tmp_units;
22+
END;$$;
23+
24+
COMMENT ON FUNCTION unit_load() IS 'Loads/upgrades the unit_units and unit_prefixes contents from the data files on disk';
25+
26+
SELECT unit_load();
27+
328
CREATE FUNCTION unit_at_double(unit, text)
429
RETURNS double precision
530
SET search_path = @extschema@

unit--6.sql.in

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ CREATE TABLE unit_prefixes (
4545

4646
SELECT pg_catalog.pg_extension_config_dump('unit_prefixes', 'WHERE dump');
4747

48-
COPY unit_prefixes (prefix, factor, definition, dump) FROM '@MODULEDIR@/unit_prefixes.data';
49-
5048
CREATE TABLE unit_units (
5149
name varchar(32) PRIMARY KEY,
5250
unit unit NOT NULL,
@@ -57,9 +55,32 @@ CREATE TABLE unit_units (
5755

5856
SELECT pg_catalog.pg_extension_config_dump('unit_units', 'WHERE dump');
5957

58+
GRANT SELECT ON unit_prefixes, unit_units TO PUBLIC;
59+
60+
CREATE OR REPLACE FUNCTION unit_load()
61+
RETURNS VOID
62+
LANGUAGE plpgsql
63+
AS $$BEGIN
64+
DELETE FROM unit_prefixes WHERE dump IS NOT TRUE;
65+
CREATE TEMP TABLE tmp_prefixes (LIKE unit_prefixes) ON COMMIT DROP;
66+
COPY tmp_prefixes (prefix, factor, definition, dump) FROM '@MODULEDIR@/unit_prefixes.data';
67+
INSERT INTO unit_prefixes
68+
SELECT * FROM tmp_prefixes t WHERE NOT EXISTS
69+
(SELECT prefix FROM unit_prefixes u WHERE u.prefix = t.prefix);
70+
DROP TABLE tmp_prefixes;
71+
72+
DELETE FROM unit_units WHERE dump IS NOT TRUE;
73+
CREATE TEMP TABLE tmp_units (LIKE unit_units) ON COMMIT DROP;
6074
COPY unit_units (name, unit, shift, definition, dump) FROM '@MODULEDIR@/unit_units.data';
75+
INSERT INTO unit_units
76+
SELECT * FROM tmp_units t WHERE NOT EXISTS
77+
(SELECT name FROM unit_units u WHERE u.name = t.name);
78+
DROP TABLE tmp_units;
79+
END;$$;
6180

62-
GRANT SELECT ON unit_prefixes, unit_units TO PUBLIC;
81+
COMMENT ON FUNCTION unit_load() IS 'Loads/upgrades the unit_units and unit_prefixes contents from the data files on disk';
82+
83+
SELECT unit_load();
6384

6485
-- constructors
6586

unit.control

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
default_version = '6'
2-
comment = 'unit extension'
2+
comment = 'SI units extension'
3+
# the unit_prefixes/unit_units tables can be installed in an arbitrary schema,
4+
# but can not be relocated later:
35
relocatable = false
6+
# unit_load() is written in plpgsql:
7+
requires = 'plpgsql'

0 commit comments

Comments
 (0)