1 <!-- doc/src/sgml/isn.sgml -->
3 <sect1 id=
"isn" xreflabel=
"isn">
4 <title>isn
— data types for international standard numbers (ISBN, EAN, UPC, etc.)
</title>
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 according to a
14 hard-coded list of prefixes; this list of prefixes is also used to hyphenate
15 numbers on output. Since new prefixes are assigned from time to time, the
16 list of prefixes may be out of date. It is hoped that a future version of
17 this module will obtain the prefix list from one or more tables that
18 can be easily updated by users as needed; however, at present, the
19 list can only be updated by modifying the source code and recompiling.
20 Alternatively, prefix validation and hyphenation support may be
21 dropped from a future version of this module.
25 This module is considered
<quote>trusted
</quote>, that is, it can be
26 installed by non-superusers who have
<literal>CREATE
</literal> privilege
27 on the current database.
30 <sect2 id=
"isn-data-types">
31 <title>Data Types
</title>
34 <xref linkend=
"isn-datatypes"/> shows the data types provided by
35 the
<filename>isn
</filename> module.
38 <table id=
"isn-datatypes">
39 <title><filename>isn
</filename> Data Types
</title>
41 <colspec colname=
"col1" colwidth=
"1*"/>
42 <colspec colname=
"col2" colwidth=
"2*"/>
45 <entry>Data Type
</entry>
46 <entry>Description
</entry>
52 <entry><type>EAN13
</type></entry>
54 European Article Numbers, always displayed in the EAN13 display format
59 <entry><type>ISBN13
</type></entry>
61 International Standard Book Numbers to be displayed in
62 the new EAN13 display format
67 <entry><type>ISMN13
</type></entry>
69 International Standard Music Numbers to be displayed in
70 the new EAN13 display format
74 <entry><type>ISSN13
</type></entry>
76 International Standard Serial Numbers to be displayed in the new
81 <entry><type>ISBN
</type></entry>
83 International Standard Book Numbers to be displayed in the old
88 <entry><type>ISMN
</type></entry>
90 International Standard Music Numbers to be displayed in the
91 old short display format
95 <entry><type>ISSN
</type></entry>
97 International Standard Serial Numbers to be displayed in the
98 old short display format
102 <entry><type>UPC
</type></entry>
104 Universal Product Codes
117 <para>ISBN13, ISMN13, ISSN13 numbers are all EAN13 numbers.
</para>
120 <para>EAN13 numbers aren't always ISBN13, ISMN13 or ISSN13 (some
124 <para>Some ISBN13 numbers can be displayed as ISBN.
</para>
127 <para>Some ISMN13 numbers can be displayed as ISMN.
</para>
130 <para>Some ISSN13 numbers can be displayed as ISSN.
</para>
133 <para>UPC numbers are a subset of the EAN13 numbers (they are basically
134 EAN13 without the first
<literal>0</literal> digit).
</para>
137 <para>All UPC, ISBN, ISMN and ISSN numbers can be represented as EAN13
143 Internally, all these types use the same representation (a
64-bit
144 integer), and all are interchangeable. Multiple types are provided
145 to control display formatting and to permit tighter validity checking
146 of input that is supposed to denote one particular type of number.
150 The
<type>ISBN
</type>,
<type>ISMN
</type>, and
<type>ISSN
</type> types will display the
151 short version of the number (ISxN
10) whenever it's possible, and will show
152 ISxN
13 format for numbers that do not fit in the short version.
153 The
<type>EAN13
</type>,
<type>ISBN13
</type>,
<type>ISMN13
</type> and
154 <type>ISSN13
</type> types will always display the long version of the ISxN
159 <sect2 id=
"isn-casts">
163 The
<filename>isn
</filename> module provides the following pairs of type casts:
169 ISBN13
<=
> EAN13
174 ISMN13
<=
> EAN13
179 ISSN13
<=
> EAN13
204 ISBN
<=
> ISBN13
209 ISMN
<=
> ISMN13
214 ISSN
<=
> ISSN13
220 When casting from
<type>EAN13
</type> to another type, there is a run-time
221 check that the value is within the domain of the other type, and an error
222 is thrown if not. The other casts are simply relabelings that will
227 <sect2 id=
"isn-funcs-ops">
228 <title>Functions and Operators
</title>
231 The
<filename>isn
</filename> module provides the standard comparison operators,
232 plus B-tree and hash indexing support for all these data types. In
233 addition there are several specialized functions; shown in
<xref linkend=
"isn-functions"/>.
235 <type>isn
</type> means any one of the module's data types.
238 <table id=
"isn-functions">
239 <title><filename>isn
</filename> Functions
</title>
243 <entry role=
"func_table_entry"><para role=
"func_signature">
254 <entry role=
"func_table_entry"><para role=
"func_signature">
255 <indexterm><primary>isn_weak
</primary></indexterm>
256 <function>isn_weak
</function> (
<type>boolean
</type> )
257 <returnvalue>boolean
</returnvalue>
260 Sets the weak input mode, and returns new setting.
265 <entry role=
"func_table_entry"><para role=
"func_signature">
266 <function>isn_weak
</function> ()
267 <returnvalue>boolean
</returnvalue>
270 Returns the current status of the weak mode.
275 <entry role=
"func_table_entry"><para role=
"func_signature">
276 <indexterm><primary>make_valid
</primary></indexterm>
277 <function>make_valid
</function> (
<type>isn
</type> )
278 <returnvalue>isn
</returnvalue>
281 Validates an invalid number (clears the invalid flag).
286 <entry role=
"func_table_entry"><para role=
"func_signature">
287 <indexterm><primary>is_valid
</primary></indexterm>
288 <function>is_valid
</function> (
<type>isn
</type> )
289 <returnvalue>boolean
</returnvalue>
292 Checks for the presence of the invalid flag.
300 <firstterm>Weak
</firstterm> mode is used to be able to insert invalid data
301 into a table. Invalid means the check digit is wrong, not that there are
306 Why would you want to use the weak mode? Well, it could be that
307 you have a huge collection of ISBN numbers, and that there are so many of
308 them that for weird reasons some have the wrong check digit (perhaps the
309 numbers were scanned from a printed list and the OCR got the numbers wrong,
310 perhaps the numbers were manually captured... who knows). Anyway, the point
311 is you might want to clean the mess up, but you still want to be able to
312 have all the numbers in your database and maybe use an external tool to
313 locate the invalid numbers in the database so you can verify the
314 information and validate it more easily; so for example you'd want to
315 select all the invalid numbers in the table.
319 When you insert invalid numbers in a table using the weak mode, the number
320 will be inserted with the corrected check digit, but it will be displayed
321 with an exclamation mark (
<literal>!
</literal>) at the end, for example
322 <literal>0-
11-
000322-
5!
</literal>. This invalid marker can be checked with
323 the
<function>is_valid
</function> function and cleared with the
324 <function>make_valid
</function> function.
328 You can also force the insertion of invalid numbers even when not in the
329 weak mode, by appending the
<literal>!
</literal> character at the end of the
334 Another special feature is that during input, you can write
335 <literal>?
</literal> in place of the check digit, and the correct check digit
336 will be inserted automatically.
340 <sect2 id=
"isn-examples">
341 <title>Examples
</title>
344 --Using the types directly:
345 SELECT isbn('
978-
0-
393-
04002-
9');
346 SELECT isbn13('
0901690546');
347 SELECT issn('
1436-
4522');
350 -- note that you can only cast from ean13 to another type when the
351 -- number would be valid in the realm of the target type;
352 -- thus, the following will NOT work: select isbn(ean13('
0220356483481'));
354 SELECT upc(ean13('
0220356483481'));
355 SELECT ean13(upc('
220356483481'));
357 --Create a table with a single column to hold ISBN numbers:
358 CREATE TABLE test (id isbn);
359 INSERT INTO test VALUES('
9780393040029');
361 --Automatically calculate check digits (observe the '?'):
362 INSERT INTO test VALUES('
220500896?');
363 INSERT INTO test VALUES('
978055215372?');
365 SELECT issn('
3251231?');
366 SELECT ismn('
979047213542?');
368 --Using the weak mode:
369 SELECT isn_weak(true);
370 INSERT INTO test VALUES('
978-
0-
11-
000533-
4');
371 INSERT INTO test VALUES('
9780141219307');
372 INSERT INTO test VALUES('
2-
205-
00876-X');
373 SELECT isn_weak(false);
375 SELECT id FROM test WHERE NOT is_valid(id);
376 UPDATE test SET id = make_valid(id) WHERE id = '
2-
205-
00876-X!';
380 SELECT isbn13(id) FROM test;
384 <sect2 id=
"isn-bibliography">
385 <title>Bibliography
</title>
388 The information to implement this module was collected from
389 several sites, including:
391 <listitem><para><ulink url=
"https://www.isbn-international.org/"></ulink></para></listitem>
392 <listitem><para><ulink url=
"https://www.issn.org/"></ulink></para></listitem>
393 <listitem><para><ulink url=
"https://www.ismn-international.org/"></ulink></para></listitem>
394 <listitem><para><ulink url=
"https://www.wikipedia.org/"></ulink></para></listitem>
397 The prefixes used for hyphenation were also compiled from:
399 <listitem><para><ulink url=
"https://www.gs1.org/standards/id-keys"></ulink></para></listitem>
400 <listitem><para><ulink url=
"https://en.wikipedia.org/wiki/List_of_ISBN_registration_groups"></ulink></para></listitem>
401 <listitem><para><ulink url=
"https://www.isbn-international.org/content/isbn-users-manual"></ulink></para></listitem>
402 <listitem><para><ulink url=
"https://en.wikipedia.org/wiki/International_Standard_Music_Number"></ulink></para></listitem>
403 <listitem><para><ulink url=
"https://www.ismn-international.org/ranges.html"></ulink></para></listitem>
406 Care was taken during the creation of the algorithms and they
407 were meticulously verified against the suggested algorithms
408 in the official ISBN, ISMN, ISSN User Manuals.
412 <sect2 id=
"isn-author">
413 <title>Author
</title>
415 Germ
án M
éndez Bravo (Kronuz),
2004–2006
419 This module was inspired by Garrett A. Wollman's
420 <filename>isbn_issn
</filename> code.