@Override public Sequence execute(final StaticContext sctx, final QueryContext ctx, final Sequence[] args) throws QueryException { try { final String collName = FunUtil.getString(args, 0, "collName", "collection", null, true); final Sequence nodes = args[2]; if (nodes == null) throw new QueryException(new QNm("No sequence of nodes specified!")); final boolean createNew = args.length == 4 ? args[3].booleanValue() : true; final String resName = FunUtil.getString(args, 1, "resName", "resource", null, createNew ? false : true); final DBStore store = (DBStore) ctx.getStore(); if (createNew) { create(store, collName, resName, nodes); } else { try { final DBCollection coll = (DBCollection) store.lookup(collName); add(store, coll, resName, nodes); } catch (DocumentException e) { // collection does not exist create(store, collName, resName, nodes); } } return null; } catch (final Exception e) { throw new QueryException(new QNm(e.getMessage()), e); } }
private void create( final DBStore store, final String collName, final String resName, final Sequence nodes) throws DocumentException, IOException { if (nodes instanceof Node) { final Node<?> n = (Node<?>) nodes; store.create(collName, Optional.of(resName), new StoreParser(n)); } else { store.create(collName, new ParserStream(nodes)); } }