#include "UPC.h"
#include "fmgr.h"
#include "isn.h"
+#include "utils/guc.h"
PG_MODULE_MAGIC;
static const char *const isn_names[] = {"EAN13/UPC/ISxN", "EAN13/UPC/ISxN", "EAN13", "ISBN", "ISMN", "ISSN", "UPC"};
+/* GUC value */
static bool g_weak = false;
if (!check_table(UPC_range, UPC_index))
elog(ERROR, "UPC failed check");
}
+
+ /* Define a GUC variable for weak mode. */
+ DefineCustomBoolVariable("isn.weak",
+ "Accept input with invalid ISN check digits.",
+ NULL,
+ &g_weak,
+ false,
+ PGC_USERSET,
+ 0,
+ NULL,
+ NULL,
+ NULL);
+
+ MarkGUCPrefixReserved("isn");
}
/* isn_out
/* this function temporarily sets weak input flag
* (to lose the strictness of check digit acceptance)
- * It's a helper function, not intended to be used!!
*/
PG_FUNCTION_INFO_V1(accept_weak_input);
Datum
accept_weak_input(PG_FUNCTION_ARGS)
{
-#ifdef ISN_WEAK_MODE
- g_weak = PG_GETARG_BOOL(0);
-#else
- /* function has no effect */
-#endif /* ISN_WEAK_MODE */
+ bool newvalue = PG_GETARG_BOOL(0);
+
+ (void) set_config_option("isn.weak", newvalue ? "on" : "off",
+ PGC_USERSET, PGC_S_SESSION,
+ GUC_ACTION_SET, true, 0, false);
PG_RETURN_BOOL(g_weak);
}
<para>
The <filename>isn</filename> 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 <xref linkend="isn-functions"/>.
+ addition, there are several specialized functions, shown in <xref linkend="isn-functions"/>.
In this table,
<type>isn</type> means any one of the module's data types.
</para>
<tbody>
<row>
<entry role="func_table_entry"><para role="func_signature">
- <indexterm><primary>isn_weak</primary></indexterm>
- <function>isn_weak</function> ( <type>boolean</type> )
- <returnvalue>boolean</returnvalue>
+ <indexterm><primary>make_valid</primary></indexterm>
+ <function>make_valid</function> ( <type>isn</type> )
+ <returnvalue>isn</returnvalue>
</para>
<para>
- Sets the weak input mode, and returns new setting.
+ Clears the invalid-check-digit flag of the value.
</para></entry>
</row>
<row>
<entry role="func_table_entry"><para role="func_signature">
- <function>isn_weak</function> ()
+ <indexterm><primary>is_valid</primary></indexterm>
+ <function>is_valid</function> ( <type>isn</type> )
<returnvalue>boolean</returnvalue>
</para>
<para>
- Returns the current status of the weak mode.
+ Checks for the presence of the invalid-check-digit flag.
</para></entry>
</row>
<row>
<entry role="func_table_entry"><para role="func_signature">
- <indexterm><primary>make_valid</primary></indexterm>
- <function>make_valid</function> ( <type>isn</type> )
- <returnvalue>isn</returnvalue>
+ <indexterm><primary>isn_weak</primary></indexterm>
+ <function>isn_weak</function> ( <type>boolean</type> )
+ <returnvalue>boolean</returnvalue>
</para>
<para>
- Validates an invalid number (clears the invalid flag).
+ Sets the weak input mode, and returns the new setting.
+ This function is retained for backward compatibility.
+ The recommended way to set weak mode is via
+ the <varname>isn.weak</varname> configuration parameter.
</para></entry>
</row>
<row>
<entry role="func_table_entry"><para role="func_signature">
- <indexterm><primary>is_valid</primary></indexterm>
- <function>is_valid</function> ( <type>isn</type> )
+ <function>isn_weak</function> ()
<returnvalue>boolean</returnvalue>
</para>
<para>
- Checks for the presence of the invalid flag.
+ Returns the current status of the weak mode.
+ This function is retained for backward compatibility.
+ The recommended way to check weak mode is via
+ the <varname>isn.weak</varname> configuration parameter.
</para></entry>
</row>
</tbody>
</tgroup>
</table>
+ </sect2>
- <para>
- <firstterm>Weak</firstterm> 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.
- </para>
+ <sect2 id="isn-configuration-parameters">
+ <title>Configuration Parameters</title>
+
+ <variablelist>
+ <varlistentry id="isn-configuration-parameters-weak">
+ <term>
+ <varname>isn.weak</varname> (<type>boolean</type>)
+ <indexterm>
+ <primary><varname>isn.weak</varname> configuration parameter</primary>
+ </indexterm>
+ </term>
+ <listitem>
+ <para>
+ <varname>isn.weak</varname> enables the weak input mode, which allows
+ ISN input values to be accepted even when their check digit is wrong.
+ The default is <literal>false</literal>, which rejects invalid check
+ digits.
+ </para>
+ </listitem>
+ </varlistentry>
+ </variablelist>
<para>
Why would you want to use the weak mode? Well, it could be that
</para>
<para>
- You can also force the insertion of invalid numbers even when not in the
- weak mode, by appending the <literal>!</literal> character at the end of the
- number.
+ You can also force the insertion of marked-as-invalid numbers even when not
+ in the weak mode, by appending the <literal>!</literal> character at the
+ end of the number.
</para>
<para>
SELECT ismn('979047213542?');
--Using the weak mode:
-SELECT isn_weak(true);
+SET isn.weak TO 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);
+SET isn.weak TO false;
SELECT id FROM test WHERE NOT is_valid(id);
UPDATE test SET id = make_valid(id) WHERE id = '2-205-00876-X!';