11 The
<filename>isn
</filename> module provides data types for the following
12 international product numbering standards: EAN13, UPC, ISBN (books), ISMN
13 (music), and ISSN (serials). Numbers are validated on input, and correctly
18 <title>Data types
</title>
21 <title><filename>isn
</filename> data types
</title>
25 <entry>Data type
</entry>
26 <entry>Description
</entry>
32 <entry><type>EAN13
</type></entry>
34 European Article Numbers, always displayed in the EAN13 display format
39 <entry><type>ISBN13
</type></entry>
41 International Standard Book Numbers to be displayed in
42 the new EAN13 display format
47 <entry><type>ISMN13
</type></entry>
49 International Standard Music Numbers to be displayed in
50 the new EAN13 display format
54 <entry><type>ISSN13
</type></entry>
56 International Standard Serial Numbers to be displayed in the new
61 <entry><type>ISBN
</type></entry>
63 International Standard Book Numbers to be displayed in the old
68 <entry><type>ISMN
</type></entry>
70 International Standard Music Numbers to be displayed in the
71 old short display format
75 <entry><type>ISSN
</type></entry>
77 International Standard Serial Numbers to be displayed in the
78 old short display format
82 <entry><type>UPC
</type></entry>
84 Universal Product Codes
97 <para>ISBN13, ISMN13, ISSN13 numbers are all EAN13 numbers.
</para>
100 <para>EAN13 numbers aren't always ISBN13, ISMN13 or ISSN13 (some
104 <para>Some ISBN13 numbers can be displayed as ISBN.
</para>
107 <para>Some ISMN13 numbers can be displayed as ISMN.
</para>
110 <para>Some ISSN13 numbers can be displayed as ISSN.
</para>
113 <para>UPC numbers are a subset of the EAN13 numbers (they are basically
114 EAN13 without the first
<literal>0<
/> digit).
</para>
117 <para>All UPC, ISBN, ISMN and ISSN numbers can be represented as EAN13
123 Internally, all these types use the same representation (a
64-bit
124 integer), and all are interchangeable. Multiple types are provided
125 to control display formatting and to permit tighter validity checking
126 of input that is supposed to denote one particular type of number.
130 The
<type>ISBN<
/>,
<type>ISMN<
/>, and
<type>ISSN<
/> types will display the
131 short version of the number (ISxN
10) whenever it's possible, and will show
132 ISxN
13 format for numbers that do not fit in the short version.
133 The
<type>EAN13
</type>,
<type>ISBN13
</type>,
<type>ISMN13
</type> and
134 <type>ISSN13
</type> types will always display the long version of the ISxN
143 The
<filename>isn<
/> module provides the following pairs of type casts:
149 ISBN13
<=
> EAN13
154 ISMN13
<=
> EAN13
159 ISSN13
<=
> EAN13
184 ISBN
<=
> ISBN13
189 ISMN
<=
> ISMN13
194 ISSN
<=
> ISSN13
200 When casting from
<type>EAN13<
/> to another type, there is a run-time
201 check that the value is within the domain of the other type, and an error
202 is thrown if not. The other casts are simply relabelings that will
208 <title>Functions and Operators
</title>
211 The
<filename>isn<
/> module provides the standard comparison operators,
212 plus btree and hash indexing support for all these datatypes. In
213 addition there are several specialized functions; shown in
<xref linkend=
"isn-functions">.
215 <type>isn<
/> means any one of the module's data types.
218 <table id=
"isn-functions">
219 <title><filename>isn<
/> functions
</title>
223 <entry>Function
</entry>
224 <entry>Returns
</entry>
225 <entry>Description
</entry>
231 <entry><function>isn_weak(boolean)
</function></entry>
232 <entry><type>boolean
</type></entry>
233 <entry>Sets the weak input mode (returns new setting)
</entry>
236 <entry><function>isn_weak()
</function></entry>
237 <entry><type>boolean
</type></entry>
238 <entry>Gets the current status of the weak mode
</entry>
241 <entry><function>make_valid(isn)
</function></entry>
242 <entry><type>isn
</type></entry>
243 <entry>Validates an invalid number (clears the invalid flag)
</entry>
246 <entry><function>is_valid(isn)
</function></entry>
247 <entry><type>boolean
</type></entry>
248 <entry>Checks for the presence of the invalid flag
</entry>
255 <firstterm>Weak
</firstterm> mode is used to be able to insert invalid data
256 into a table. Invalid means the check digit is wrong, not that there are
261 Why would you want to use the weak mode? Well, it could be that
262 you have a huge collection of ISBN numbers, and that there are so many of
263 them that for weird reasons some have the wrong check digit (perhaps the
264 numbers were scanned from a printed list and the OCR got the numbers wrong,
265 perhaps the numbers were manually captured... who knows). Anyway, the point
266 is you might want to clean the mess up, but you still want to be able to
267 have all the numbers in your database and maybe use an external tool to
268 locate the invalid numbers in the database so you can verify the
269 information and validate it more easily; so for example you'd want to
270 select all the invalid numbers in the table.
274 When you insert invalid numbers in a table using the weak mode, the number
275 will be inserted with the corrected check digit, but it will be displayed
276 with an exclamation mark (
<literal>!<
/>) at the end, for example
277 <literal>0-
11-
000322-
5!<
/>. This invalid marker can be checked with
278 the
<function>is_valid<
/> function and cleared with the
279 <function>make_valid<
/> function.
283 You can also force the insertion of invalid numbers even when not in the
284 weak mode, by appending the
<literal>!<
/> character at the end of the
289 Another special feature is that during input, you can write
290 <literal>?<
/> in place of the check digit, and the correct check digit
291 will be inserted automatically.
296 <title>Examples
</title>
299 --Using the types directly:
300 SELECT isbn('
978-
0-
393-
04002-
9');
301 SELECT isbn13('
0901690546');
302 SELECT issn('
1436-
4522');
305 -- note that you can only cast from ean13 to another type when the
306 -- number would be valid in the realm of the target type;
307 -- thus, the following will NOT work: select isbn(ean13('
0220356483481'));
309 SELECT upc(ean13('
0220356483481'));
310 SELECT ean13(upc('
220356483481'));
312 --Create a table with a single column to hold ISBN numbers:
313 CREATE TABLE test (id isbn);
314 INSERT INTO test VALUES('
9780393040029');
316 --Automatically calculate check digits (observe the '?'):
317 INSERT INTO test VALUES('
220500896?');
318 INSERT INTO test VALUES('
978055215372?');
320 SELECT issn('
3251231?');
321 SELECT ismn('
979047213542?');
323 --Using the weak mode:
324 SELECT isn_weak(true);
325 INSERT INTO test VALUES('
978-
0-
11-
000533-
4');
326 INSERT INTO test VALUES('
9780141219307');
327 INSERT INTO test VALUES('
2-
205-
00876-X');
328 SELECT isn_weak(false);
330 SELECT id FROM test WHERE NOT is_valid(id);
331 UPDATE test SET id = make_valid(id) WHERE id = '
2-
205-
00876-X!';
335 SELECT isbn13(id) FROM test;
340 <title>Bibliography
</title>
343 The information to implement this module was collected from
344 several sites, including:
347 http://www.isbn-international.org/
349 http://www.ismn-international.org/
350 http://www.wikipedia.org/
354 The prefixes used for hyphenation were also compiled from:
357 http://www.gs1.org/productssolutions/idkeys/support/prefix_list.html
358 http://www.isbn-international.org/en/identifiers.html
359 http://www.ismn-international.org/ranges.html
363 Care was taken during the creation of the algorithms and they
364 were meticulously verified against the suggested algorithms
365 in the official ISBN, ISMN, ISSN User Manuals.
370 <title>Author
</title>
372 Germán Méndez Bravo (Kronuz),
2004 -
2006
376 This module was inspired by Garrett A. Wollman's