/** * Creates a container for the specified node values. * * @param doc document */ protected static void create(final String doc) { try { new CreateDB(Util.className(SandboxTest.class), doc).execute(context); } catch (final BaseXException ex) { Util.notExpected(ex); } }
/** * Creates query plans. * * @param comp compiled flag */ private void plan(final boolean comp) { if (comp != options.get(MainOptions.COMPPLAN)) return; // show dot plan try { if (options.get(MainOptions.DOTPLAN)) { final String path = options.get(MainOptions.QUERYPATH); final String dot = path.isEmpty() ? "plan.dot" : new IOFile(path).name().replaceAll("\\..*?$", ".dot"); try (final BufferOutput bo = new BufferOutput(dot)) { try (final DOTSerializer d = new DOTSerializer(bo, options.get(MainOptions.DOTCOMPACT))) { d.serialize(qp.plan()); } } } // show XML plan if (options.get(MainOptions.XMLPLAN)) { info(NL + QUERY_PLAN + COL); info(qp.plan().serialize().toString()); } } catch (final Exception ex) { Util.stack(ex); } }
/** * Creates the error message from the specified text and extension array. * * @param text text message with optional placeholders * @param ext info extensions * @return argument */ private static String message(final String text, final Object[] ext) { final int es = ext.length; for (int e = 0; e < es; e++) { if (ext[e] instanceof ExprInfo) ext[e] = chop(((ExprInfo) ext[e]).toErrorString(), null); } return Util.info(text, ext); }
/** * Returns a string representation of all found arguments. * * @param args array with arguments * @return string representation */ static String foundArgs(final Value[] args) { // compose found arguments final StringBuilder sb = new StringBuilder(); for (final Value v : args) { if (sb.length() != 0) sb.append(", "); sb.append(v instanceof Jav ? Util.className(((Jav) v).toJava()) : v.seqType()); } return sb.toString(); }
/** Tests the specified instance. */ @Test public void test() { final StringBuilder sb = new StringBuilder(); int fail = 0; for (final Object[] qu : queries) { final boolean correct = qu.length == 3; final String query = qu[correct ? 2 : 1].toString(); final Value cmp = correct ? (Value) qu[1] : null; final QueryProcessor qp = new QueryProcessor(query, context); try { final Value val = qp.value(); if (!correct || !new DeepCompare().equal(val, cmp)) { sb.append("[" + qu[0] + "] " + query); String s = correct && cmp.size() != 1 ? "#" + cmp.size() : ""; sb.append("\n[E" + s + "] "); if (correct) { final String cp = cmp.toString(); sb.append('\''); sb.append(cp.length() > 1000 ? cp.substring(0, 1000) + "..." : cp); sb.append('\''); } else { sb.append("error"); } final TokenBuilder types = new TokenBuilder(); for (final Item it : val) types.add(it.type.toString()).add(" "); s = val.size() == 1 ? "" : "#" + val.size(); sb.append("\n[F" + s + "] '" + val + "', " + types + details() + '\n'); ++fail; } } catch (final Exception ex) { final String msg = ex.getMessage(); if (correct || msg == null || msg.contains("mailman")) { final String cp = correct && cmp.data() != null ? cmp.toString() : "()"; sb.append( "[" + qu[0] + "] " + query + "\n[E] " + cp + "\n[F] " + (msg == null ? Util.className(ex) : msg.replaceAll("\r\n?|\n", " ")) + ' ' + details() + '\n'); ex.printStackTrace(); ++fail; } } finally { qp.close(); } } if (fail != 0) fail(fail + " Errors. [E] = expected, [F] = found:\n" + sb.toString().trim()); }
@Override public Iter iter(final QueryContext qc) throws QueryException { final Value seq = qc.value(ts); for (final TypeCase tc : cases) { final Iter iter = tc.iter(qc, seq); if (iter != null) return iter; } // will never happen throw Util.notExpected(); }
/** * Adds parameters from the passed on request body. * * @param body request body * @param params map parameters */ private static void addParams(final String body, final Map<String, String[]> params) { for (final String nv : body.split("&")) { final String[] parts = nv.split("=", 2); if (parts.length < 2) continue; try { params.put(parts[0], new String[] {URLDecoder.decode(parts[1], Token.UTF8)}); } catch (final Exception ex) { Util.notexpected(ex); } } }
/** * Returns the serialization parameters. * * @param ctx context * @return serialization parameters */ public String parameters(final Context ctx) { try { qp(args[0], ctx); parse(null); return qp.qc.serParams().toString(); } catch (final QueryException ex) { error(Util.message(ex)); } finally { qp = null; } return SerializerMode.DEFAULT.get().toString(); }
/** * Checks if the query possibly performs updates. * * @param ctx database context * @param query query string * @return result of check */ final boolean updates(final Context ctx, final String query) { try { final Performance p = new Performance(); qp(query, ctx); parse(p); return qp.updating; } catch (final QueryException ex) { Util.debug(ex); exception = ex; qp.close(); return false; } }
/** * Tests writing of request content when @src is set. * * @throws IOException I/O Exception */ @Test public void writeFromResource() throws IOException { // Create a file form which will be read final IOFile file = new IOFile(Prop.TMP, Util.className(FnHttpTest.class)); file.write(token("test")); // Request final HttpRequest req = new HttpRequest(); req.payloadAttrs.put("src", file.url()); req.payloadAttrs.put("method", "binary"); // HTTP connection final FakeHttpConnection fakeConn = new FakeHttpConnection(new URL("http://www.test.com")); HttpClient.setRequestContent(fakeConn.getOutputStream(), req); // Delete file file.delete(); assertEquals(fakeConn.out.toString(Strings.UTF8), "test"); }
/** * Evaluates the specified query. * * @param query query * @return success flag */ final boolean query(final String query) { final Performance p = new Performance(); String error; if (exception != null) { error = Util.message(exception); } else { try { long hits = 0; final boolean run = options.get(MainOptions.RUNQUERY); final boolean serial = options.get(MainOptions.SERIALIZE); final int runs = Math.max(1, options.get(MainOptions.RUNS)); for (int r = 0; r < runs; ++r) { // reuse existing processor instance if (r != 0) qp = null; qp(query, context); parse(p); if (r == 0) plan(false); qp.compile(); info.compiling += p.time(); if (r == 0) plan(true); if (!run) continue; final PrintOutput po = r == 0 && serial ? out : new NullOutput(); try (final Serializer ser = qp.getSerializer(po)) { if (maxResults >= 0) { result = qp.cache(maxResults); info.evaluating += p.time(); result.serialize(ser); hits = result.size(); } else { hits = 0; final Iter ir = qp.iter(); info.evaluating += p.time(); for (Item it; (it = ir.next()) != null; ) { ser.serialize(it); ++hits; checkStop(); } } } qp.close(); info.serializing += p.time(); } // dump some query info // out.flush(); // remove string list if global locking is used and if query is updating if (soptions.get(StaticOptions.GLOBALLOCK) && qp.updating) { info.readLocked = null; info.writeLocked = null; } return info(info.toString(qp, out.size(), hits, options.get(MainOptions.QUERYINFO))); } catch (final QueryException | IOException ex) { exception = ex; error = Util.message(ex); } catch (final ProcException ex) { error = INTERRUPTED; } catch (final StackOverflowError ex) { Util.debug(ex); error = BASX_STACKOVERFLOW.desc; } catch (final RuntimeException ex) { extError(""); Util.debug(info()); throw ex; } finally { // close processor after exceptions if (qp != null) qp.close(); } } return extError(error); }
@Override public Test intersect(final Test other) { throw Util.notExpected(this); }
/** * Constructor, specifying an exception or error. {@link QueryError#BASX_GENERIC_X} will be set as * error code. * * @param cause exception or error */ public QueryException(final Throwable cause) { this(Util.message(cause)); }
/** * Creates an exception with the specified message. * * @param msg message * @param ext error extension * @return exception * @throws QueryException query exception */ QueryException error(final String msg, final Object... ext) throws QueryException { throw new QueryException(function.info, Err.BASX_RESTXQ, Util.info(msg, ext)); }