Hi Christian,
I have question about updates.
I use updating function upsertVehicles in loop. Function downloadVehicles download from url and pass to upsertVehicles function.
declare %updating function cfv:downloadData () as empty-sequence() { for $URL in doc("config.xml")/urls return (cfv:downloadVehicles($URL) => cfv:upsertVehicles(), admin:write-log("Inserted: " || serialize(update:cache()))) };
When does cache is purged ? In my case cache contains information from previous iterations. If upsertVehicles fail only updates from this function are reverted. What is the scope of the PUL ? Is this query, function or all invocation chain of updating functions ?
I have problem with update:cache(). What is the return type ? Sequence of items or sequence of sequence [....] of items ? When I try get one item like this: update:cache()[1] I receive error: Item expected, sequence found: (0, 0).
Best Regards Bogdan Bogucki
-----Wiadomość oryginalna----- Od: BaseX-Talk [mailto:basex-talk-bounces@mailman.uni-konstanz.de] W imieniu Bogdan Bogucki Wysłano: 25 sierpnia 2018 17:58 Do: 'Christian Grün' DW: 'BaseX' Temat: [basex-talk] ODP: Update functions - Error handling and counting
Hi Christian,
Thank you it helps me a lot.
I found that update:output-cache() is in module db. Wiki describe output-cache() in update module.
Best Regards Bogdan Bogucki
-----Wiadomość oryginalna----- Od: Christian Grün [mailto:christian.gruen@gmail.com] Wysłano: 24 sierpnia 2018 23:41 Do: Bogdan Bogucki DW: BaseX Temat: Re: [basex-talk] Update functions - Error handling and counting
Hi Bogdan,
- XQuery is functional language and updating function can return empty sequence. How can I count and return result how many nodes was inserted or updated ?
BaseX provides the update:output function for that purpose [1]. A solution could look as follows:
declare %updating function cfv:upsertVehicles( $vehicles as element(car)* ) as empty-sequence() { let $db := db:open("db")/cars let $existing := $vehicles[$db/car/vin = vin] return ( (: replace existing vehicles :) for $updated in $existing let $old := $db/car[vin = $updated/vin] return replace node $old with $updated, (: insert new vehicles :) insert node ($vehicles except $existing) into $db, (: return count :) update:output(count($vehicles)) ) };
I have similar problem with error handling. How can I catch and return any error.
Due to the semantics of XQuery Update, logical errors will be raised before your updates are eventually executed [2]. No updates will be performed if the execution is expected to fail.
Best, Christian
[1] http://docs.basex.org/wiki/Update_Module#update:output [2] http://docs.basex.org/wiki/XQuery_Update#Error_Messages