1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
--
-- Test ISN extension
--
CREATE EXTENSION isn;
--
-- test valid conversions
--
SELECT '9780123456786'::EAN13, -- old book
'9790123456785'::EAN13, -- music
'9791234567896'::EAN13, -- new book
'9771234567898'::EAN13, -- serial
'0123456789012'::EAN13, -- upc
'1234567890128'::EAN13;
ean13 | ean13 | ean13 | ean13 | ean13 | ean13
-------------------+-------------------+-----------------+-------------------+-----------------+-----------------
978-0-12-345678-6 | 979-0-1234-5678-5 | 979-123456789-6 | 977-1234-567-89-8 | 012-345678901-2 | 123-456789012-8
(1 row)
SELECT '9780123456786'::ISBN,
'123456789X'::ISBN,
'9780123456786'::ISBN13::ISBN,
'9780123456786'::EAN13::ISBN;
isbn | isbn | isbn | isbn
---------------+---------------+---------------+---------------
0-12-345678-9 | 1-234-56789-X | 0-12-345678-9 | 0-12-345678-9
(1 row)
SELECT -- new books, shown as ISBN13 even for ISBN...
'9791234567896'::ISBN,
'9791234567896'::ISBN13::ISBN,
'9791234567896'::EAN13::ISBN;
isbn | isbn | isbn
-----------------+-----------------+-----------------
979-123456789-6 | 979-123456789-6 | 979-123456789-6
(1 row)
SELECT '9780123456786'::ISBN13,
'123456789X'::ISBN13,
'9791234567896'::ISBN13,
'9791234567896'::EAN13::ISBN13;
isbn13 | isbn13 | isbn13 | isbn13
-------------------+-------------------+-----------------+-----------------
978-0-12-345678-6 | 978-1-234-56789-7 | 979-123456789-6 | 979-123456789-6
(1 row)
SELECT '9790123456785'::ISMN,
'9790123456785'::EAN13::ISMN,
'M123456785'::ISMN,
'M-1234-5678-5'::ISMN;
ismn | ismn | ismn | ismn
---------------+---------------+---------------+---------------
M-1234-5678-5 | M-1234-5678-5 | M-1234-5678-5 | M-1234-5678-5
(1 row)
SELECT '9790123456785'::ISMN13,
'M123456785'::ISMN13,
'M-1234-5678-5'::ISMN13;
ismn13 | ismn13 | ismn13
-------------------+-------------------+-------------------
979-0-1234-5678-5 | 979-0-1234-5678-5 | 979-0-1234-5678-5
(1 row)
SELECT '9771234567003'::ISSN,
'12345679'::ISSN;
issn | issn
-----------+-----------
1234-5679 | 1234-5679
(1 row)
SELECT '9771234567003'::ISSN13,
'12345679'::ISSN13,
'9771234567898'::ISSN13,
'9771234567898'::EAN13::ISSN13;
issn13 | issn13 | issn13 | issn13
-------------------+-------------------+-------------------+-------------------
977-1234-567-00-3 | 977-1234-567-00-3 | 977-1234-567-89-8 | 977-1234-567-89-8
(1 row)
SELECT '0123456789012'::UPC,
'0123456789012'::EAN13::UPC;
upc | upc
--------------+--------------
123456789012 | 123456789012
(1 row)
--
-- test invalid checksums
--
SELECT '1234567890'::ISBN;
ERROR: invalid check digit for ISBN number: "1234567890", should be X
LINE 1: SELECT '1234567890'::ISBN;
^
SELECT 'M123456780'::ISMN;
ERROR: invalid check digit for ISMN number: "M123456780", should be 5
LINE 1: SELECT 'M123456780'::ISMN;
^
SELECT '12345670'::ISSN;
ERROR: invalid check digit for ISSN number: "12345670", should be 9
LINE 1: SELECT '12345670'::ISSN;
^
SELECT '9780123456780'::ISBN;
ERROR: invalid check digit for ISBN number: "9780123456780", should be 6
LINE 1: SELECT '9780123456780'::ISBN;
^
SELECT '9791234567890'::ISBN13;
ERROR: invalid check digit for ISBN number: "9791234567890", should be 6
LINE 1: SELECT '9791234567890'::ISBN13;
^
SELECT '0123456789010'::UPC;
ERROR: invalid check digit for UPC number: "0123456789010", should be 2
LINE 1: SELECT '0123456789010'::UPC;
^
SELECT '1234567890120'::EAN13;
ERROR: invalid check digit for EAN13 number: "1234567890120", should be 8
LINE 1: SELECT '1234567890120'::EAN13;
^
--
-- test invalid conversions
--
SELECT '9790123456785'::ISBN; -- not a book
ERROR: cannot cast ISMN to ISBN for number: "9790123456785"
LINE 1: SELECT '9790123456785'::ISBN;
^
SELECT '9771234567898'::ISBN; -- not a book
ERROR: cannot cast ISSN to ISBN for number: "9771234567898"
LINE 1: SELECT '9771234567898'::ISBN;
^
SELECT '0123456789012'::ISBN; -- not a book
ERROR: cannot cast UPC to ISBN for number: "0123456789012"
LINE 1: SELECT '0123456789012'::ISBN;
^
SELECT '9790123456785'::ISBN13; -- not a book
ERROR: cannot cast ISMN to ISBN for number: "9790123456785"
LINE 1: SELECT '9790123456785'::ISBN13;
^
SELECT '9771234567898'::ISBN13; -- not a book
ERROR: cannot cast ISSN to ISBN for number: "9771234567898"
LINE 1: SELECT '9771234567898'::ISBN13;
^
SELECT '0123456789012'::ISBN13; -- not a book
ERROR: cannot cast UPC to ISBN for number: "0123456789012"
LINE 1: SELECT '0123456789012'::ISBN13;
^
SELECT '9780123456786'::ISMN; -- not music
ERROR: cannot cast ISBN to ISMN for number: "9780123456786"
LINE 1: SELECT '9780123456786'::ISMN;
^
SELECT '9771234567898'::ISMN; -- not music
ERROR: cannot cast ISSN to ISMN for number: "9771234567898"
LINE 1: SELECT '9771234567898'::ISMN;
^
SELECT '9791234567896'::ISMN; -- not music
ERROR: cannot cast ISBN to ISMN for number: "9791234567896"
LINE 1: SELECT '9791234567896'::ISMN;
^
SELECT '0123456789012'::ISMN; -- not music
ERROR: cannot cast UPC to ISMN for number: "0123456789012"
LINE 1: SELECT '0123456789012'::ISMN;
^
SELECT '9780123456786'::ISSN; -- not serial
ERROR: cannot cast ISBN to ISSN for number: "9780123456786"
LINE 1: SELECT '9780123456786'::ISSN;
^
SELECT '9790123456785'::ISSN; -- not serial
ERROR: cannot cast ISMN to ISSN for number: "9790123456785"
LINE 1: SELECT '9790123456785'::ISSN;
^
SELECT '9791234567896'::ISSN; -- not serial
ERROR: cannot cast ISBN to ISSN for number: "9791234567896"
LINE 1: SELECT '9791234567896'::ISSN;
^
SELECT '0123456789012'::ISSN; -- not serial
ERROR: cannot cast UPC to ISSN for number: "0123456789012"
LINE 1: SELECT '0123456789012'::ISSN;
^
SELECT '9780123456786'::UPC; -- not a product
ERROR: cannot cast ISBN to UPC for number: "9780123456786"
LINE 1: SELECT '9780123456786'::UPC;
^
SELECT '9771234567898'::UPC; -- not a product
ERROR: cannot cast ISSN to UPC for number: "9771234567898"
LINE 1: SELECT '9771234567898'::UPC;
^
SELECT '9790123456785'::UPC; -- not a product
ERROR: cannot cast ISMN to UPC for number: "9790123456785"
LINE 1: SELECT '9790123456785'::UPC;
^
SELECT '9791234567896'::UPC; -- not a product
ERROR: cannot cast ISBN to UPC for number: "9791234567896"
LINE 1: SELECT '9791234567896'::UPC;
^
SELECT 'postgresql...'::EAN13;
ERROR: invalid input syntax for EAN13 number: "postgresql..."
LINE 1: SELECT 'postgresql...'::EAN13;
^
SELECT 'postgresql...'::ISBN;
ERROR: invalid input syntax for ISBN number: "postgresql..."
LINE 1: SELECT 'postgresql...'::ISBN;
^
SELECT 9780123456786::EAN13;
ERROR: cannot cast type bigint to ean13
LINE 1: SELECT 9780123456786::EAN13;
^
SELECT 9780123456786::ISBN;
ERROR: cannot cast type bigint to isbn
LINE 1: SELECT 9780123456786::ISBN;
^
--
-- test some comparisons, must yield true
--
SELECT '12345679'::ISSN = '9771234567003'::EAN13 AS "ok",
'M-1234-5678-5'::ISMN = '9790123456785'::EAN13 AS "ok",
'9791234567896'::EAN13 != '123456789X'::ISBN AS "nope";
ok | ok | nope
----+----+------
t | t | t
(1 row)
--
-- cleanup
--
DROP EXTENSION isn;
|