Skip to content

Commit 33f7d0b

Browse files
committed
Use legobricks as example and test custom units
1 parent 20635b9 commit 33f7d0b

File tree

4 files changed

+73
-8
lines changed

4 files changed

+73
-8
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ DATA = unit_prefixes.data unit_units.data \
55
unit--1.sql unit--1--2.sql \
66
unit--2.sql
77
DATA_built = unit--2--3.sql unit--3.sql
8-
REGRESS = extension tables unit prefix units temperature functions derived compare aggregate iec
8+
REGRESS = extension tables unit prefix units temperature functions derived compare aggregate iec custom
99
EXTRA_CLEAN = unitparse.yy.* powers powers.o unit-*.dump # unitparse.tab.*
1010

1111
PG_CONFIG = pg_config

README.md

+6-7
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ User-Defined Units
171171
To create custom prefixes and units, insert new rows into the tables:
172172

173173
```
174-
# select '1 foobar'::unit;
174+
# SELECT '1 foobar'::unit;
175175
ERROR: unit "foobar" is not known
176176
# INSERT INTO unit_prefixes VALUES ('foo', 42);
177177
INSERT 0 1
@@ -180,12 +180,11 @@ INSERT 0 1
180180
---------
181181
4.2 MPa
182182
183-
# INSERT INTO unit_units VALUES ('footballfieldsize', '68m * 105m');
184-
INSERT 0 1
185-
# SELECT '1 hectare'::unit @ 'footballfieldsize' AS one_hectare;
186-
one_hectare
187-
------------------------------------
188-
1.40056022408964 footballfieldsize
183+
# INSERT INTO unit_units VALUES ('legobrick', '9.6 mm');
184+
# SELECT '1 m'::unit @ 'legobricks' AS one_meter;
185+
one_meter
186+
-----------------------------
187+
104.166666666667 legobricks
189188
```
190189

191190
Input Syntax

expected/custom.out

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
SELECT '1 foobar'::unit;
2+
ERROR: unit "foobar" is not known
3+
LINE 1: SELECT '1 foobar'::unit;
4+
^
5+
SELECT unit_is_hashed('foobar');
6+
unit_is_hashed
7+
----------------
8+
f
9+
(1 row)
10+
11+
INSERT INTO unit_prefixes VALUES ('foo', 42);
12+
SELECT '1 foobar'::unit;
13+
unit
14+
---------
15+
4.2 MPa
16+
(1 row)
17+
18+
SELECT unit_is_hashed('foobar');
19+
unit_is_hashed
20+
----------------
21+
t
22+
(1 row)
23+
24+
SELECT * FROM unit_prefixes WHERE dump;
25+
prefix | factor | definition | dump
26+
--------+--------+------------+------
27+
foo | 42 | | t
28+
(1 row)
29+
30+
INSERT INTO unit_units VALUES ('legobrick', '9.6 mm');
31+
SELECT unit_is_hashed('legobricks');
32+
unit_is_hashed
33+
----------------
34+
f
35+
(1 row)
36+
37+
SELECT '1 m'::unit @ 'legobricks' AS one_meter;
38+
one_meter
39+
-----------------------------
40+
104.166666666667 legobricks
41+
(1 row)
42+
43+
SELECT unit_is_hashed('legobricks');
44+
unit_is_hashed
45+
----------------
46+
t
47+
(1 row)
48+
49+
SELECT * FROM unit_units WHERE dump;
50+
name | unit | shift | definition | dump
51+
-----------+--------+-------+------------+------
52+
legobrick | 9.6 mm | | | t
53+
(1 row)
54+

sql/custom.sql

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
SELECT '1 foobar'::unit;
2+
SELECT unit_is_hashed('foobar');
3+
INSERT INTO unit_prefixes VALUES ('foo', 42);
4+
SELECT '1 foobar'::unit;
5+
SELECT unit_is_hashed('foobar');
6+
SELECT * FROM unit_prefixes WHERE dump;
7+
8+
INSERT INTO unit_units VALUES ('legobrick', '9.6 mm');
9+
SELECT unit_is_hashed('legobricks');
10+
SELECT '1 m'::unit @ 'legobricks' AS one_meter;
11+
SELECT unit_is_hashed('legobricks');
12+
SELECT * FROM unit_units WHERE dump;

0 commit comments

Comments
 (0)