[PATCH 2 of 4 saem_ref v2] [ark] Rename gen_ark_name function into gen_ark_part

Denis Laxalde denis.laxalde at logilab.fr
Thu Feb 15 15:04:04 CET 2018


# HG changeset patch
# User Denis Laxalde <denis.laxalde at logilab.fr>
# Date 1518702250 -3600
#      Thu Feb 15 14:44:10 2018 +0100
# Node ID 97dd205f44f9ba98f5ed3d8960970e0602532f59
# Parent  828385bddd63694089ede151c4b309a8d0683f1f
# Available At http://hg.logilab.org/review/cubes/saem_ref
#              hg pull http://hg.logilab.org/review/cubes/saem_ref -r 97dd205f44f9
# EXP-Topic ark/qualifiers
[ark] Rename gen_ark_name function into gen_ark_part

This function actually handles any "part" (name or qualifier) of an ARK
identifier; adjusting returned name accordingly.

Related to extranet #46880051.

diff --git a/cubicweb_saem_ref/schema/ark.postgres.sql b/cubicweb_saem_ref/schema/ark.postgres.sql
--- a/cubicweb_saem_ref/schema/ark.postgres.sql
+++ b/cubicweb_saem_ref/schema/ark.postgres.sql
@@ -4,13 +4,13 @@ CREATE TABLE ark (
     CONSTRAINT qualified_name PRIMARY KEY (name, qualifier)
 );
 
--- Return a part of an ARK identifier of `len` length with `prefix` prefix and
--- `control_char` trailing character.
+-- Return a part (name or qualifier) of an ARK identifier of `len` length with
+-- `prefix` prefix and `control_char` trailing character.
 CREATE OR REPLACE
-FUNCTION gen_ark_name(len INTEGER, prefix TEXT, control_char TEXT)
+FUNCTION gen_ark_part(len INTEGER, prefix TEXT, control_char TEXT)
 RETURNS text AS $$
 DECLARE
-    ark TEXT;
+    result TEXT;
     letters TEXT;
     numbers TEXT;
     characters TEXT;
@@ -22,25 +22,25 @@ BEGIN
     letters := 'bcdfghjkmnpqrstvwxz';
     numbers := '0123456789';
     characters := letters || numbers;
-    -- Length of "ark" at which we break the loop.
+    -- Length of "result" at which we break the loop.
     break_length = len - length(control_char);
-    -- Prefix ARK identifier.
-    ark := prefix;
+    -- Start with "prefix".
+    result := prefix;
     LOOP
-        tail := substring(ark, length(ark) - 2, 3);
+        tail := substring(result, length(result) - 2, 3);
         -- No more than 3 consecutive consonants.
         IF tail ~ '([a-z]){3}' THEN
             next_char := random_char(numbers);
         ELSE
             next_char := random_char(characters);
         END IF;
-        ark := ark || next_char;
-        IF length(ark) = break_length THEN
+        result := result || next_char;
+        IF length(result) = break_length THEN
             EXIT;
         END IF;
     END LOOP;
     -- Add control character.
-    RETURN ark || control_char;
+    RETURN result || control_char;
 END;
 $$ language 'plpgsql';
 
@@ -62,7 +62,7 @@ BEGIN
     LOOP
         BEGIN
             INSERT INTO ark
-                VALUES (gen_ark_name(len, prefix, control_char), DEFAULT)
+                VALUES (gen_ark_part(len, prefix, control_char), DEFAULT)
                 RETURNING name INTO ark_name;
             RETURN ark_name;
         EXCEPTION



More information about the saem-devel mailing list