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

Denis Laxalde denis.laxalde at logilab.fr
Thu Feb 22 08:44:28 CET 2018


Philippe Pepiot a écrit :
> 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.

The assumption is only in table definition, not in this function unless
I'm missing something.

> 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 '')

It seems to me that the problem with NULL as default is that it prevents
usage of respective column in the primary key constraint. Am I wrong?
(IIRC, this is about SQL standard being ambiguous in this respect.)

Your other suggestions are very good, thanks.



More information about the saem-devel mailing list