[PATCH 1 of 2 saem_ref v2] [ark] Add an insert_ark helper function

Philippe Pepiot philippe.pepiot at logilab.fr
Wed Feb 21 18:25:44 CET 2018


On 21/02/2018, Denis Laxalde wrote:
> # HG changeset patch
> # User Denis Laxalde <denis.laxalde at logilab.fr>
> # Date 1519227628 -3600
> #      Wed Feb 21 16:40:28 2018 +0100
> # Node ID 00a7985081f1e0c376e9dd2d7de605ce9e7e3bd7
> # Parent  4e2b908a791f0250f1bc26f460ecad8b5bb3574b
> # Available At http://hg.logilab.org/review/cubes/saem_ref
> #              hg pull http://hg.logilab.org/review/cubes/saem_ref -r 00a7985081f1
> # EXP-Topic ark/unique-overall
> [ark] Add an insert_ark helper function
> 
> diff --git a/cubicweb_saem_ref/ark.py b/cubicweb_saem_ref/ark.py
> --- a/cubicweb_saem_ref/ark.py
> +++ b/cubicweb_saem_ref/ark.py
> @@ -74,3 +74,18 @@ def generate_qualified_ark(cnx, naan, na
>      )
>      qualifier, = cu.fetchone()
>      return qualifier
> +
> +
> +def insert_ark(cnx, naan, name, qualifier=None):
> +    """Insert a record in "ark" table from specified values."""
> +    if qualifier is None:
> +        qs = 'INSERT INTO ark VALUES (%s, %s);'
> +        values = (naan, name)
> +    else:
> +        qs = 'INSERT INTO ark VALUES (%s, %s, %s);'
> +        values = (naan, name, qualifier)
> +    cnx.system_sql(qs, values)
> +    ark = u'/'.join([naan, name])
> +    if qualifier is not None:
> +        ark += u'/' + qualifier
> +    return ark

This make an assumption on default qualifier to be '' (empty string)
which is fine to me but you seems to avoid that in the previous patch.

To make this assumption more explicit you can set qualifier='' by default in
insert_ark() and use a single sql query..

Given an INSERT clause you cannot determine what is actually written in
the database (because of DEFAULT, triggers etc). The best practice to
avoid such side effect is to re-read the inserted item with RETURNING:

INSERT INTO ark (naan, name) VALUES ('foo', 'bar') RETURNING naan, name,
qualifier;

If qualifier was defaulting to NULL you could even write:

INSERT INTO ark (naan, name) VALUES ('foo', 'bar') RETURNING
array_to_string(ARRAY[naan, name, qualifier], '/') as ark;

(note that I don't have strong opinion on NULL vs '')


> diff --git a/test/test_ark.py b/test/test_ark.py
> --- a/test/test_ark.py
> +++ b/test/test_ark.py
> @@ -26,6 +26,7 @@ from cubicweb.devtools import (
>  
>  from cubicweb_saem_ref.ark import (
>      ARK_RGX,
> +    insert_ark,
>      match as ark_match,
>  )
>  import testutils
> @@ -173,6 +174,22 @@ class ArkServiceTC(testlib.CubicWebTC):
>                  (_, _, qualifier), = cu.fetchall()
>          self.assertEqual(len(qualifier), 3, qualifier)
>  
> +    def test_insert_ark(self):
> +        with self.admin_access.cnx() as cnx:
> +            ark = insert_ark(cnx, '12345', u'bob')
> +            cnx.commit()
> +            self.assertEqual(ark, u'12345/bob')
> +            results = cnx.system_sql('SELECT naan, name, qualifier FROM ark;').fetchall()
> +            self.assertEqual(results, [(12345, u'bob', u'')])
> +
> +    def test_insert_ark_qualifier(self):
> +        with self.admin_access.cnx() as cnx:
> +            ark = insert_ark(cnx, '12345', u'bob', u'1')
> +            cnx.commit()
> +            self.assertEqual(ark, u'12345/bob/1')
> +            results = cnx.system_sql('SELECT naan, name, qualifier FROM ark;').fetchall()
> +            self.assertEqual(results, [(12345, u'bob', u'1')])
> +
>  
>  if __name__ == '__main__':
>      import unittest
> 



More information about the saem-devel mailing list