F.11. isn

The isn module adds data types for the following international-standard namespaces: EAN13, UPC, ISBN (books), ISMN (music), and ISSN (serials). This module is inspired by Garrett A. Wollman's isbn_issn code.

This module validates, and automatically adds the correct hyphenations to the numbers. Also, it supports the new ISBN-13 numbers to be used starting in January 2007.

Premises:

  1. ISBN13, ISMN13, ISSN13 numbers are all EAN13 numbers

  2. EAN13 numbers aren't always ISBN13, ISMN13 or ISSN13 (some are)

  3. some ISBN13 numbers can be displayed as ISBN

  4. some ISMN13 numbers can be displayed as ISMN

  5. some ISSN13 numbers can be displayed as ISSN

  6. all UPC, ISBN, ISMN and ISSN can be represented as EAN13 numbers

Note: All types are internally represented as 64 bit integers, and internally all are consistently interchangeable.

Note: We have two operator classes (for btree and for hash) so each data type can be indexed for faster access.

F.11.1. Data types

We have the following data types:

Table F-8. Data types

Data type

Description

EAN13

European Article Numbers. This type will always show the EAN13-display format. Te output function for this is ean13_out()

ISBN13

For International Standard Book Numbers to be displayed in the new EAN13-display format.

ISMN13

For International Standard Music Numbers to be displayed in the new EAN13-display format.

ISSN13

For International Standard Serial Numbers to be displayed in the new EAN13-display format.

ISBN

For International Standard Book Numbers to be displayed in the current short-display format.

ISMN

For International Standard Music Numbers to be displayed in the current short-display format.

ISSN

For International Standard Serial Numbers to be displayed in the current short-display format. These types will display the short version of the ISxN (ISxN 10) whenever it's possible, and it will show ISxN 13 when it's impossible to show the short version. The output function to do this is isn_out()

UPC

For Universal Product Codes. UPC numbers are a subset of the EAN13 numbers (they are basically EAN13 without the first '0' digit.) The output function to do this is also isn_out()

Note: EAN13, ISBN13, ISMN13 and ISSN13 types will always display the long version of the ISxN (EAN13). The output function to do this is ean13_out().

The need for these types is just for displaying in different ways the same data: ISBN13 is actually the same as ISBN, ISMN13=ISMN and ISSN13=ISSN.

F.11.2. Input functions

We have the following input functions:

Table F-9. Input functions

FunctionDescription

ean13_in()

To take a string and return an EAN13.

isbn_in()

To take a string and return valid ISBN or ISBN13 numbers.

ismn_in()

To take a string and return valid ISMN or ISMN13 numbers.

issn_in()

To take a string and return valid ISSN or ISSN13 numbers.

upc_in()

To take a string and return an UPC codes.

F.11.3. Casts

We are able to cast from:

F.11.4. C API

The C API is implemented as:

   extern Datum isn_out(PG_FUNCTION_ARGS);
   extern Datum ean13_out(PG_FUNCTION_ARGS);
   extern Datum ean13_in(PG_FUNCTION_ARGS);
   extern Datum isbn_in(PG_FUNCTION_ARGS);
   extern Datum ismn_in(PG_FUNCTION_ARGS);
   extern Datum issn_in(PG_FUNCTION_ARGS);
   extern Datum upc_in(PG_FUNCTION_ARGS);
  

On success:

(on failure, the functions 'ereport' the error)

F.11.5. Testing functions

Table F-10. Testing functions

Function

Description

isn_weak(boolean)

Sets the weak input mode.

isn_weak()

Gets the current status of the weak mode.

make_valid()

Validates an invalid number (deleting the invalid flag).

is_valid()

Checks for the invalid flag prsence.

Weak mode is used to be able to insert invalid data to a table. Invalid as in the check digit being wrong, not missing numbers.

Why would you want to use the weak mode? Well, it could be that you have a huge collection of ISBN numbers, and that there are so many of them that for weird reasons some have the wrong check digit (perhaps the numbers where scanned from a printed list and the OCR got the numbers wrong, perhaps the numbers were manually captured... who knows.) Anyway, the thing is you might want to clean the mess up, but you still want to be able to have all the numbers in your database and maybe use an external tool to access the invalid numbers in the database so you can verify the information and validate it more easily; as selecting all the invalid numbers in the table.

When you insert invalid numbers in a table using the weak mode, the number will be inserted with the corrected check digit, but it will be flagged with an exclamation mark ('!') at the end (i.e. 0-11-000322-5!)

You can also force the insertion of invalid numbers even not in the weak mode, appending the '!' character at the end of the number.

F.11.6. Examples

--Using the types directly:
SELECT isbn('978-0-393-04002-9');
SELECT isbn13('0901690546');
SELECT issn('1436-4522');

--Casting types:
-- note that you can only cast from ean13 to other type when the casted 
-- number would be valid in the realm of the casted type; 
-- thus, the following will NOT work: select isbn(ean13('0220356483481')); 
-- but these will: 
SELECT upc(ean13('0220356483481')); 
SELECT ean13(upc('220356483481')); 

--Create a table with a single column to hold ISBN numbers:
CREATE TABLE test ( id isbn );
INSERT INTO test VALUES('9780393040029');

--Automatically calculating check digits (observe the '?'):
INSERT INTO test VALUES('220500896?');
INSERT INTO test VALUES('978055215372?');

SELECT issn('3251231?');
SELECT ismn('979047213542?');

--Using the weak mode:
SELECT isn_weak(true);
INSERT INTO test VALUES('978-0-11-000533-4');
INSERT INTO test VALUES('9780141219307');
INSERT INTO test VALUES('2-205-00876-X');
SELECT isn_weak(false);
  
SELECT id FROM test WHERE NOT is_valid(id);
UPDATE test SET id=make_valid(id) WHERE id = '2-205-00876-X!';
  
SELECT * FROM test;

SELECT isbn13(id) FROM test;
  

F.11.7. Bibliography

The information to implement this module was collected through several sites, including:

   http://www.isbn-international.org/
   http://www.issn.org/
   http://www.ismn-international.org/
   http://www.wikipedia.org/
  

the prefixes used for hyphenation where also compiled from:

   http://www.gs1.org/productssolutions/idkeys/support/prefix_list.html
   http://www.isbn-international.org/en/identifiers.html
   http://www.ismn-international.org/ranges.html
  

Care was taken during the creation of the algorithms and they were meticulously verified against the suggested algorithms in the official ISBN, ISMN, ISSN User Manuals.

F.11.8. Author

Germán Méndez Bravo (Kronuz), 2004 - 2006