id generation and updates

Saturday, 18 November 2017 · 8 messages · last reply 27 Nov 2017
EJ
E. Wray Johnson18 Nov 2017 02:44 #
How can I white a single XQUERY statement that assigns an auto-generated id to a node (using either generate-id(), or random:uuid() ) then inserts the node into a database and then returns the auto-generated id. I can do the id assignment and insert operation just fine using { }, I just don't know how to return the id afterward
CG
Christian Grün18 Nov 2017 10:00 #
You might want to try db:output. See our documentation on performing returning updates and returning output.
Quoted text · E. Wray Johnson · 8 lines
Am 18.11.2017 01:44 schrieb "E. Wray Johnson" <wray.johnson at gmail.com>: > How can I white a single XQUERY statement that assigns an auto-generated > id to a node (using either generate-id(), or random:uuid() ) then inserts > the node into a database and then returns the auto-generated id. > > I can do the id assignment and insert operation just fine using { }, I > just don't know how to return the id afterward >
XS
Xavier-Laurent SALVADOR18 Nov 2017 10:19 #
Hi all, I don't know if that helps. *1- *Here is how we (Isilex) do multiple tests before inserting a node while we redirect at the same time the user to another web Page using db:output :
try {
     *db:output(*<rest:redirect>/</rest:redirect>),   (:Redirection:)
if($isi:testid2)
then
if (db:open('site')//texts[name=$idPage]/text[@lang=$lg])
then
for $x in db:open('site')//texts[name=$idPage]/text[@lang=$lg]
return* replace node $x* with html:parse($node, map { 'nons': true()
}) *(:Insert
1:)*
else if (db:open('pages')//page[name=$idPage])
then
*replace node* db:open('pages')//page[name=$idPage] with
html:parse(normalize-space($node), map { 'nons': true() })
else if (not(fetch:xml(normalize-space($node), map{'chop': true()})//name =
('','Titre de la page','Page title','Nom','Name')))
then
*insert node *html:parse(normalize-space($node), map{'nons': true()}) into
db:open('pages')/root   *(:Insert 2:)*
else *db:output*(<rest:redirect>/err:save</rest:redirect>)
                           else *db:output*
(isi:template(isi:t('unauthorized_access')))
}
catch * { fn:error(xs:QName('err:save'), $node) } };
*2-* Here is how we save a file onto a disk and at the same time we redirect a user to another web page: (db:output(<rest:redirect>/myUrl</rest:redirect>) , file:write($filePath,$css,map { "method": "text"}) ) Hope We helped, 2017-11-18 10:00 GMT+01:00 Christian Grün <christian.gruen at gmail.com>:
Quoted text · Christian Grün · 15 lines
> You might want to try db:output. See our documentation on performing > returning updates and returning output. > > > > Am 18.11.2017 01:44 schrieb "E. Wray Johnson" <wray.johnson at gmail.com>: > >> How can I white a single XQUERY statement that assigns an auto-generated >> id to a node (using either generate-id(), or random:uuid() ) then inserts >> the node into a database and then returns the auto-generated id. >> >> I can do the id assignment and insert operation just fine using { }, I >> just don't know how to return the id afterward >> >
-- Ce message peut contenir des informations réservées exclusivement à son destinataire. Toute diffusion sans autorisation est interdite. Si vous n'en êtes pas le destinataire, merci de prendre contact avec l'expéditeur et de détruire ce message. *This email may contain material for the sole use of the intended recipient. Any forwarding without express permission is prohibited. If you are not the intended recipient, please contact the sender and delete all copies*.
EJ
E. Wray Johnson18 Nov 2017 23:03 #
That should work. Thanks. Wray Johnson (m) 704-293-9008
Quoted text · Xavier-Laurent SALVADOR · 57 lines
> On Nov 18, 2017, at 4:19 AM, Xavier-Laurent SALVADOR <xavierlaurent.salvador at gmail.com> wrote: > > Hi all, > I don't know if that helps. > > 1- Here is how we (Isilex) do multiple tests before inserting a node while we redirect at the same time the user to another web Page using db:output : > > try { > db:output(<rest:redirect>/</rest:redirect>), (:Redirection:) > if($isi:testid2) > then > if (db:open('site')//texts[name=$idPage]/text[@lang=$lg]) > then > for $x in db:open('site')//texts[name=$idPage]/text[@lang=$lg] > return replace node $x with html:parse($node, map { 'nons': true() }) (:Insert 1:) > > else if (db:open('pages')//page[name=$idPage]) > > then > replace node db:open('pages')//page[name=$idPage] with > html:parse(normalize-space($node), map { 'nons': true() }) > else if (not(fetch:xml(normalize-space($node), map{'chop': true()})//name = ('','Titre de la page','Page title','Nom','Name'))) > then > insert node html:parse(normalize-space($node), map{'nons': true()}) into db:open('pages')/root (:Insert 2:) > else db:output(<rest:redirect>/err:save</rest:redirect>) > else db:output(isi:template(isi:t('unauthorized_access'))) > } > catch * { fn:error(xs:QName('err:save'), $node) } }; > > 2- Here is how we save a file onto a disk and at the same time we redirect a user to another web page: > > (db:output(<rest:redirect>/myUrl</rest:redirect>) > , > file:write($filePath,$css,map { "method": "text"}) > ) > > Hope We helped, > > > > > 2017-11-18 10:00 GMT+01:00 Christian Grün <christian.gruen at gmail.com>: >> You might want to try db:output. See our documentation on performing returning updates and returning output. >> >> >> >> Am 18.11.2017 01:44 schrieb "E. Wray Johnson" <wray.johnson at gmail.com>: >>> How can I white a single XQUERY statement that assigns an auto-generated id to a node (using either generate-id(), or random:uuid() ) then inserts the node into a database and then returns the auto-generated id. >>> >>> I can do the id assignment and insert operation just fine using { }, I just don't know how to return the id afterward > > > > -- > Ce message peut contenir des informations réservées exclusivement à son destinataire. Toute diffusion sans autorisation est interdite. Si vous n'en êtes pas le destinataire, merci de prendre contact avec l'expéditeur et de détruire ce message. > > This email may contain material for the sole use of the intended recipient. Any forwarding without express permission is prohibited. If you are not the intended recipient, please contact the sender and delete all copies.
CG
Christian Grünin reply to E. Wray Johnson, 27 Nov 2017, 17:1727 Nov 2017 16:23 #
Hi Wray, As usual, in case you are looking for help, please help your readers first, and provide some examples that one can reproduce. Thanks in advance, Christian
Quoted text · E. Wray Johnson · 23 lines
On Mon, Nov 27, 2017 at 4:17 PM, E. Wray Johnson <wray.johnson at gmail.com> wrote: > Could not get this to work. The element to be inserted was not reachable by > the db:add or the generated id was not reachable by the db:ouput. > > Wray Johnson > (m) 704-293-9008 > > On Nov 18, 2017, at 4:00 AM, Christian Grün <christian.gruen at gmail.com> > wrote: > > You might want to try db:output. See our documentation on performing > returning updates and returning output. > > > > Am 18.11.2017 01:44 schrieb "E. Wray Johnson" <wray.johnson at gmail.com>: >> >> How can I white a single XQUERY statement that assigns an auto-generated >> id to a node (using either generate-id(), or random:uuid() ) then inserts >> the node into a database and then returns the auto-generated id. >> >> I can do the id assignment and insert operation just fine using { }, I >> just don't know how to return the id afterward
EJ
E. Wray Johnsonin reply to Christian Grün, 18 Nov 2017, 10:0027 Nov 2017 17:17 #
Could not get this to work. The element to be inserted was not reachable by the db:add or the generated id was not reachable by the db:ouput. Wray Johnson (m) 704-293-9008
Quoted text · Christian Grün · 10 lines
> On Nov 18, 2017, at 4:00 AM, Christian Grün <christian.gruen at gmail.com> wrote: > > You might want to try db:output. See our documentation on performing returning updates and returning output. > > > > Am 18.11.2017 01:44 schrieb "E. Wray Johnson" <wray.johnson at gmail.com>: >> How can I white a single XQUERY statement that assigns an auto-generated id to a node (using either generate-id(), or random:uuid() ) then inserts the node into a database and then returns the auto-generated id. >> >> I can do the id assignment and insert operation just fine using { }, I just don't know how to return the id afterward
CG
Christian Grünin reply to E. Wray Johnson, 27 Nov 2017, 21:4927 Nov 2017 21:30 #
Thanks for the example.
Quoted text · E. Wray Johnson · 3 lines
On Mon, Nov 27, 2017 at 8:49 PM, E. Wray Johnson <wray.johnson at gmail.com> wrote: > I cannot get db:output to work. > Error is [XPST0008] Undefined variable $c for the following sample code:
There are two things to consider: • You need to use parentheses after the return clause. • The official XQuery Update spec does not allow the return clause of copy/modify/return to be updating (I think it shouldn’t actually cause problems to relax this rule, so maybe I will allow it in BaseX soon). You can wrap this expression into an additional FLWOR expression. This should work:
  let $doc := (
    copy $c := element test { element id { }}
    modify (
      replace value of node $c/id with generate-id($c)
    )
    return $c
  )
  return (
    db:output($doc/id),
    insert node $doc into db:open('sample', 'tests')
  )
However, the following alternative might be a better solution, because the ids created via generate-id() won’t be unique if you restart BaseX:
  let $doc := element test {
    element id { random:uuid() }
  }
  return (
    db:output($doc/id),
    insert node $doc into db:open('sample', 'tests')
  )
If you want to have shorter ids, you could add a meta document in your database and update it every time you add a new document. In the following example, the id will be included in the name of the document. Just a tiny example: Initialization:
    db:create('db', <meta><id>0</id></meta>, 'meta.xml')
Adding new document:
  let $db := 'db'
  let $meta-doc := db:open($db, 'meta.xml') update {
    for $id in meta/id/text()
    return replace node $id with $id + 1
  }
  let $id := string($meta-doc/meta/id)
  let $new-doc := element test {
    element id { $id }
  }
  return (
    db:replace($db, 'meta.xml', $meta-doc),
    db:replace($db, 'doc' || $id || '.xml', $new-doc),
    db:output($id)
 )
Best, Christian
Quoted text · E. Wray Johnson · 5 lines
>> copy $c := element test { element id { }} >> modify ( >> replace value of node $c/id with generate-id($c) >> ) >> return db:output ($c/id), insert node $c into db:open('sample', 'tests')
EJ
E. Wray Johnsonin reply to Christian Grün, 18 Nov 2017, 10:0027 Nov 2017 21:49 #
I cannot get db:output to work. Error is [XPST0008] Undefined variable $c for the following sample code:
Quoted text · Christian Grün · 23 lines
copy $c := element test { element id { }} > modify ( > replace value of node $c/id with generate-id($c) > ) > return db:output ($c/id), insert node $c into db:open('sample', 'tests') > On Sat, Nov 18, 2017 at 4:00 AM, Christian Grün <christian.gruen at gmail.com> wrote: > You might want to try db:output. See our documentation on performing > returning updates and returning output. > > > > Am 18.11.2017 01:44 schrieb "E. Wray Johnson" <wray.johnson at gmail.com>: > >> How can I white a single XQUERY statement that assigns an auto-generated >> id to a node (using either generate-id(), or random:uuid() ) then inserts >> the node into a database and then returns the auto-generated id. >> >> I can do the id assignment and insert operation just fine using { }, I >> just don't know how to return the id afterward >> >

⚡Generated with XQuery