Postgresql 中文操作指南
F.21. isn — data types for international standard numbers (ISBN, EAN, UPC, etc.) #
isn 模块为以下国际产品编号标准提供数据类型:EAN13、UPC、ISBN(书籍)、ISMN(音乐)和 ISSN(连续出版物)。输入时,该模块根据一个硬编码的前缀列表对号进行验证;此前缀列表还用于在输出时对号进行连字符分隔。由于会时不时分配新的前缀,因此前缀列表可能会过时。我们希望此模块的未来版本将从一个或多个表中获取前缀列表,根据需要,用户可以轻松地对其进行更新;然而,当前,仅当修改源代码并重新编译后才能更新前缀列表。或者,在将来版本的此模块中,可能会取消前缀验证和连字符分隔支持。
The isn module provides data types for the following international product numbering standards: EAN13, UPC, ISBN (books), ISMN (music), and ISSN (serials). Numbers are validated on input according to a hard-coded list of prefixes; this list of prefixes is also used to hyphenate numbers on output. Since new prefixes are assigned from time to time, the list of prefixes may be out of date. It is hoped that a future version of this module will obtain the prefix list from one or more tables that can be easily updated by users as needed; however, at present, the list can only be updated by modifying the source code and recompiling. Alternatively, prefix validation and hyphenation support may be dropped from a future version of this module.
此模块被认为是“受信任的”,也就是说,它可以由在当前数据库上具有 CREATE 权限的非超级用户安装。
This module is considered “trusted”, that is, it can be installed by non-superusers who have CREATE privilege on the current database.
F.21.1. Data Types #
Table F.11显示了_isn_模块提供的的数据类型。
Table F.11 shows the data types provided by the isn module.
Table F.11. isn Data Types
Table F.11. isn Data Types
Data Type |
Description |
EAN13 |
European Article Numbers, always displayed in the EAN13 display format |
ISBN13 |
International Standard Book Numbers to be displayed in the new EAN13 display format |
ISMN13 |
International Standard Music Numbers to be displayed in the new EAN13 display format |
ISSN13 |
International Standard Serial Numbers to be displayed in the new EAN13 display format |
ISBN |
International Standard Book Numbers to be displayed in the old short display format |
ISMN |
International Standard Music Numbers to be displayed in the old short display format |
ISSN |
International Standard Serial Numbers to be displayed in the old short display format |
UPC |
Universal Product Codes |
一些事项:
Some notes:
在内部,所有这些类型都使用相同的表示(一个 64 位整数),并且所有类型都是可互换的。提供多个类型以控制显示格式,并允许对应该表示某一特定类型的数字的输入进行更严格的有效性检查。
Internally, all these types use the same representation (a 64-bit integer), and all are interchangeable. Multiple types are provided to control display formatting and to permit tighter validity checking of input that is supposed to denote one particular type of number.
ISBN、ISMN 和 ISSN 类型会在任何可能的情况下显示该数字的短版本(ISxN 10),并且对于不符合短版本的数字,将显示 ISxN 13 格式。EAN13、ISBN13、ISMN13 和 ISSN13 类型将始终显示 ISxN(EAN13)的长版本。
The ISBN, ISMN, and ISSN types will display the short version of the number (ISxN 10) whenever it’s possible, and will show ISxN 13 format for numbers that do not fit in the short version. The EAN13, ISBN13, ISMN13 and ISSN13 types will always display the long version of the ISxN (EAN13).
F.21.2. Casts #
isn 模块提供以下类型转换对:
The isn module provides the following pairs of type casts:
从 EAN13 转换为另一个类型时,系统会在运行时检查该值是否在另一类型的域内,如果不是,则会抛出错误。其它类型转换仅仅是重标记,它始终都会成功。
When casting from EAN13 to another type, there is a run-time check that the value is within the domain of the other type, and an error is thrown if not. The other casts are simply relabelings that will always succeed.
F.21.3. Functions and Operators #
isn 模块提供标准比较运算符,以及 B 树和哈希索引支持以便处理所有这些数据类型。另外还有一些专门函数,如图 Table F.12 中所示。在此数据表中,_isn_表示模块的其中一种数据类型。
The isn module provides the standard comparison operators, plus B-tree and hash indexing support for all these data types. In addition there are several specialized functions; shown in Table F.12. In this table, isn means any one of the module’s data types.
Table F.12. isn Functions
Table F.12. isn Functions
Function Description |
isn_weak ( boolean ) → boolean Sets the weak input mode, and returns new setting. |
isn_weak () → boolean Returns the current status of the weak mode. |
make_valid ( isn ) → isn Validates an invalid number (clears the invalid flag). |
is_valid ( isn ) → boolean Checks for the presence of the invalid flag. |
使用 Weak 模式以便能够将无效数据插入表中。无效表示校验位错误,而不是缺少数字。
Weak mode is used to be able to insert invalid data into a table. Invalid means the check digit is wrong, not that there are missing numbers.
为什么要使用弱模式?好吧,可能是您有一大批 ISBN 号码,而且它们的数量如此之多,出于奇怪的原因,有些号码的校验位错误(也许这些号码是从印刷清单中扫描的,OCR 识别错误,也许这些号码是手动捕获的… 谁知道呢)。无论如何,重点是您可能想清理这些混乱,但您仍然希望能够拥有数据库中的所有号码,并可能使用外部工具查找数据库中的无效号码,以便您可以验证信息并更轻松地对其进行验证;因此,例如,您将希望选择表中的所有无效号码。
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 were scanned from a printed list and the OCR got the numbers wrong, perhaps the numbers were manually captured… who knows). Anyway, the point 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 locate the invalid numbers in the database so you can verify the information and validate it more easily; so for example you’d want to select all the invalid numbers in the table.
当您使用弱模式将无效号码插入表中时,该号码将使用更正后的校验位插入,但它将在结尾处显示感叹号 (!),例如 0-11-000322-5!。可以使用 is_valid 函数检查此无效标记,并可以使用 make_valid 函数将其清除。
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 displayed with an exclamation mark (!) at the end, for example 0-11-000322-5!. This invalid marker can be checked with the is_valid function and cleared with the make_valid function.
您还可以强制插入无效号码,即使不在弱模式下,方法是在数字末尾添加 ! 字符。
You can also force the insertion of invalid numbers even when not in the weak mode, by appending the ! character at the end of the number.
另一个特殊功能是,在输入期间,您可以在校验位的位置写 ?,正确的校验位将自动插入。
Another special feature is that during input, you can write ? in place of the check digit, and the correct check digit will be inserted automatically.
F.21.4. 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 another type when the
-- number would be valid in the realm of the target 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 calculate 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.21.5. Bibliography #
有关实现此模块的信息是从多个站点收集的,包括:
The information to implement this module was collected from several sites, including:
连字符使用的前缀也编译自:
The prefixes used for hyphenation were also compiled from:
在创建算法的过程中非常谨慎,并根据官方 ISBN、ISMN、ISSN 用户手册中的建议算法对其进行了细致的验证。
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.21.6. Author #
Germán Méndez Bravo (Kronuz),2004-2006
Germán Méndez Bravo (Kronuz), 2004–2006
此模块的灵感来自于 Garrett A. Wollman 的 isbn_issn 代码。
This module was inspired by Garrett A. Wollman’s isbn_issn code.