@@ -245,7 +245,7 @@ public void transformWith() throws EXistException, RecognitionException, XPathEx
245
245
public void copyModifyExprTest () throws EXistException , RecognitionException , XPathException , TokenStreamException , PermissionDeniedException
246
246
{
247
247
String query = "copy $je := $e\n " +
248
- " modify $je\n " +
248
+ " modify delete node $je/salary \n " +
249
249
" return $je" ;
250
250
251
251
BrokerPool pool = BrokerPool .getInstance ();
@@ -264,11 +264,50 @@ public void copyModifyExprTest() throws EXistException, RecognitionException, XP
264
264
265
265
XQueryTreeParser treeParser = new XQueryTreeParser (context );
266
266
PathExpr expr = new PathExpr (context );
267
- treeParser .expr (ast , expr );
267
+ Expression ret = treeParser .expr (ast , expr );
268
268
if (treeParser .foundErrors ()) {
269
269
fail (treeParser .getErrorMessage ());
270
270
return ;
271
271
}
272
+
273
+ assertTrue (ret instanceof CopyModifyExpression );
274
+ assertEquals ("$je" ,((CopyModifyExpression ) ret ).getReturnExpr ().toString ());
275
+ }
276
+ }
277
+
278
+ @ Test
279
+ public void copyModifyExprTestComplexModify () throws EXistException , RecognitionException , XPathException , TokenStreamException , PermissionDeniedException
280
+ {
281
+ String query = "copy $newx := $oldx\n " +
282
+ " modify (rename node $newx as \" newx\" , \n " +
283
+ " replace value of node $newx with $newx * 2)\n " +
284
+ " return ($oldx, $newx)" ;
285
+
286
+ BrokerPool pool = BrokerPool .getInstance ();
287
+ try (final DBBroker broker = pool .getBroker ()) {
288
+ // parse the query into the internal syntax tree
289
+ XQueryContext context = new XQueryContext (broker .getBrokerPool ());
290
+ XQueryLexer lexer = new XQueryLexer (context , new StringReader (query ));
291
+ XQueryParser xparser = new XQueryParser (lexer );
292
+ xparser .expr ();
293
+ if (xparser .foundErrors ()) {
294
+ fail (xparser .getErrorMessage ());
295
+ return ;
296
+ }
297
+
298
+ XQueryAST ast = (XQueryAST ) xparser .getAST ();
299
+
300
+ XQueryTreeParser treeParser = new XQueryTreeParser (context );
301
+ PathExpr expr = new PathExpr (context );
302
+ Expression ret = treeParser .expr (ast , expr );
303
+ if (treeParser .foundErrors ()) {
304
+ fail (treeParser .getErrorMessage ());
305
+ return ;
306
+ }
307
+
308
+ assertTrue (ret instanceof CopyModifyExpression );
309
+ assertEquals ("( replace $newx \" newx\" , replace $newx VALUE $newx * 2 )" ,((CopyModifyExpression ) ret ).getModifyExpr ().toString ());
310
+ assertEquals ("( $oldx, $newx )" ,((CopyModifyExpression ) ret ).getReturnExpr ().toString ());
272
311
}
273
312
}
274
313
@@ -339,6 +378,48 @@ public void insertExpr() throws EXistException, RecognitionException, XPathExcep
339
378
340
379
assertTrue (expr .getFirst () instanceof InsertExpr );
341
380
assertEquals (Expression .Category .UPDATING , expr .getFirst ().getCategory ());
381
+ assertEquals (InsertExpr .Choice .AFTER , ((InsertExpr ) expr .getFirst ()).getChoice ());
382
+ }
383
+ }
384
+
385
+ @ Test
386
+ public void insertExprAsLast () throws EXistException , RecognitionException , XPathException , TokenStreamException , PermissionDeniedException
387
+ {
388
+ String query =
389
+ "insert node $new-police-report\n " +
390
+ " as last into fn:doc(\" insurance.xml\" )/policies\n " +
391
+ " /policy[id = $pid]\n " +
392
+ " /driver[license = $license]\n " +
393
+ " /accident[date = $accdate]\n " +
394
+ " /police-reports" ;
395
+
396
+ BrokerPool pool = BrokerPool .getInstance ();
397
+ try (final DBBroker broker = pool .getBroker ()) {
398
+ // parse the query into the internal syntax tree
399
+ XQueryContext context = new XQueryContext (broker .getBrokerPool ());
400
+ XQueryLexer lexer = new XQueryLexer (context , new StringReader (query ));
401
+ XQueryParser xparser = new XQueryParser (lexer );
402
+ xparser .expr ();
403
+ if (xparser .foundErrors ()) {
404
+ fail (xparser .getErrorMessage ());
405
+ return ;
406
+ }
407
+
408
+ XQueryAST ast = (XQueryAST ) xparser .getAST ();
409
+
410
+ XQueryTreeParser treeParser = new XQueryTreeParser (context );
411
+ PathExpr expr = new PathExpr (context );
412
+ treeParser .expr (ast , expr );
413
+
414
+ if (treeParser .foundErrors ()) {
415
+ fail (treeParser .getErrorMessage ());
416
+ return ;
417
+ }
418
+
419
+ assertTrue (expr .getFirst () instanceof InsertExpr );
420
+ assertEquals (Expression .Category .UPDATING , expr .getFirst ().getCategory ());
421
+ assertEquals ("$new-police-report" , ((InsertExpr ) expr .getFirst ()).getSourceExpr ().toString ());
422
+ assertEquals (InsertExpr .Choice .LAST , ((InsertExpr ) expr .getFirst ()).getChoice ());
342
423
}
343
424
}
344
425
@@ -348,6 +429,42 @@ public void deleteExpr() throws EXistException, RecognitionException, XPathExcep
348
429
String query =
349
430
"delete node fn:doc(\" bib.xml\" )/books/book[1]/author[last()]" ;
350
431
432
+ BrokerPool pool = BrokerPool .getInstance ();
433
+ try (final DBBroker broker = pool .getBroker ()) {
434
+ // parse the query into the internal syntax tree
435
+ XQueryContext context = new XQueryContext (broker .getBrokerPool ());
436
+ XQueryLexer lexer = new XQueryLexer (context , new StringReader (query ));
437
+ XQueryParser xparser = new XQueryParser (lexer );
438
+ xparser .expr ();
439
+ if (xparser .foundErrors ()) {
440
+ fail (xparser .getErrorMessage ());
441
+ return ;
442
+ }
443
+
444
+ XQueryAST ast = (XQueryAST ) xparser .getAST ();
445
+
446
+ XQueryTreeParser treeParser = new XQueryTreeParser (context );
447
+ PathExpr expr = new PathExpr (context );
448
+ treeParser .expr (ast , expr );
449
+
450
+ if (treeParser .foundErrors ()) {
451
+ fail (treeParser .getErrorMessage ());
452
+ return ;
453
+ }
454
+
455
+ assertTrue (expr .getFirst () instanceof DeleteExpr );
456
+ assertEquals (Expression .Category .UPDATING , expr .getFirst ().getCategory ());
457
+ assertEquals ("doc(\" bib.xml\" )/child::{}books/child::{}book[1]/child::{}author[last()]" , ((DeleteExpr ) expr .getFirst ()).getTargetExpr ().toString ());
458
+ }
459
+ }
460
+
461
+ @ Test
462
+ public void deleteExprComplex () throws EXistException , RecognitionException , XPathException , TokenStreamException , PermissionDeniedException
463
+ {
464
+ String query =
465
+ "delete nodes /email/message\n " +
466
+ " [date > xs:dayTimeDuration(\" P365D\" )]" ;
467
+
351
468
BrokerPool pool = BrokerPool .getInstance ();
352
469
try (final DBBroker broker = pool .getBroker ()) {
353
470
// parse the query into the internal syntax tree
@@ -485,4 +602,40 @@ public void renameExpr() throws EXistException, RecognitionException, XPathExcep
485
602
assertEquals ("\" principal-author\" " ,((RenameExpr ) expr .getFirst ()).getNewNameExpr ().toString ());
486
603
}
487
604
}
605
+
606
+ @ Test
607
+ public void renameExprWithExpr () throws EXistException , RecognitionException , XPathException , TokenStreamException , PermissionDeniedException
608
+ {
609
+ String query =
610
+ "rename node fn:doc(\" bib.xml\" )/books/book[1]/author[1]\n " +
611
+ "as $newname" ;
612
+
613
+ BrokerPool pool = BrokerPool .getInstance ();
614
+ try (final DBBroker broker = pool .getBroker ()) {
615
+ // parse the query into the internal syntax tree
616
+ XQueryContext context = new XQueryContext (broker .getBrokerPool ());
617
+ XQueryLexer lexer = new XQueryLexer (context , new StringReader (query ));
618
+ XQueryParser xparser = new XQueryParser (lexer );
619
+ xparser .expr ();
620
+ if (xparser .foundErrors ()) {
621
+ fail (xparser .getErrorMessage ());
622
+ return ;
623
+ }
624
+
625
+ XQueryAST ast = (XQueryAST ) xparser .getAST ();
626
+
627
+ XQueryTreeParser treeParser = new XQueryTreeParser (context );
628
+ PathExpr expr = new PathExpr (context );
629
+ treeParser .expr (ast , expr );
630
+
631
+ if (treeParser .foundErrors ()) {
632
+ fail (treeParser .getErrorMessage ());
633
+ return ;
634
+ }
635
+
636
+ assertTrue (expr .getFirst () instanceof RenameExpr );
637
+ assertEquals ("doc(\" bib.xml\" )/child::{}books/child::{}book[1]/child::{}author[1]" ,((RenameExpr ) expr .getFirst ()).getTargetExpr ().toString ());
638
+ assertEquals ("$newname" ,((RenameExpr ) expr .getFirst ()).getNewNameExpr ().toString ());
639
+ }
640
+ }
488
641
}
0 commit comments