|
| 1 | +/* |
| 2 | + * eXist-db Open Source Native XML Database |
| 3 | + * Copyright (C) 2001 The eXist-db Authors |
| 4 | + * |
| 5 | + |
| 6 | + * http://www.exist-db.org |
| 7 | + * |
| 8 | + * This library is free software; you can redistribute it and/or |
| 9 | + * modify it under the terms of the GNU Lesser General Public |
| 10 | + * License as published by the Free Software Foundation; either |
| 11 | + * version 2.1 of the License, or (at your option) any later version. |
| 12 | + * |
| 13 | + * This library is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | + * Lesser General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU Lesser General Public |
| 19 | + * License along with this library; if not, write to the Free Software |
| 20 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | + */ |
| 22 | +package org.exist.xquery.modules.sql; |
| 23 | + |
| 24 | +import com.evolvedbinary.j8fu.tuple.Tuple2; |
| 25 | +import org.exist.EXistException; |
| 26 | +import org.exist.dom.memtree.ElementImpl; |
| 27 | +import org.exist.security.PermissionDeniedException; |
| 28 | +import org.exist.source.Source; |
| 29 | +import org.exist.source.StringSource; |
| 30 | +import org.exist.storage.BrokerPool; |
| 31 | +import org.exist.storage.DBBroker; |
| 32 | +import org.exist.storage.txn.Txn; |
| 33 | +import org.exist.test.ExistEmbeddedServer; |
| 34 | +import org.exist.xquery.XPathException; |
| 35 | +import org.exist.xquery.XQueryContext; |
| 36 | +import org.exist.xquery.value.Sequence; |
| 37 | +import org.exist.xquery.value.Type; |
| 38 | +import org.junit.Rule; |
| 39 | +import org.junit.Test; |
| 40 | +import org.w3c.dom.Element; |
| 41 | + |
| 42 | +import java.io.IOException; |
| 43 | + |
| 44 | +import static com.evolvedbinary.j8fu.tuple.Tuple.Tuple; |
| 45 | +import static org.exist.xquery.modules.sql.Util.executeQuery; |
| 46 | +import static org.exist.xquery.modules.sql.Util.withCompiledQuery; |
| 47 | +import static org.junit.Assert.assertEquals; |
| 48 | +import static org.junit.Assert.assertTrue; |
| 49 | + |
| 50 | +/** |
| 51 | + * SQL Execute Integration Tests. |
| 52 | + * |
| 53 | + * @author <a href="mailto:[email protected]">Adam Retter</a> |
| 54 | + */ |
| 55 | +public class ExecuteIT { |
| 56 | + |
| 57 | + @Rule |
| 58 | + public final ExistEmbeddedServer existEmbeddedServer = new ExistEmbeddedServer(true, true); |
| 59 | + |
| 60 | + @Rule |
| 61 | + public final H2DatabaseResource h2Database = new H2DatabaseResource(); |
| 62 | + |
| 63 | + @Test |
| 64 | + public void executeResultsInSqlNS() throws EXistException, XPathException, PermissionDeniedException, IOException { |
| 65 | + executeForNS(SQLModule.NAMESPACE_URI, SQLModule.PREFIX); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void executeResultsInCustomNS() throws EXistException, XPathException, PermissionDeniedException, IOException { |
| 70 | + executeForNS("http://custom/ns", "custom"); |
| 71 | + } |
| 72 | + |
| 73 | + private void executeForNS(final String namespace, final String prefix) throws EXistException, XPathException, PermissionDeniedException, IOException { |
| 74 | + final String mainQuery = |
| 75 | + "import module namespace sql = \"http://exist-db.org/xquery/sql\";\n" + |
| 76 | + "let $conn := sql:get-connection(\"" + h2Database.getDriverClass().getName() + "\", \"" + h2Database.getUrl() + "\", \"" + h2Database.getUser() + "\", \"" + h2Database.getPassword() + "\")\n" + |
| 77 | + "return\n" + |
| 78 | + " sql:execute($conn, \"SELECT 'Hello World' FROM DUAL;\", true(), \"" + namespace + "\", \"" + prefix + "\")"; |
| 79 | + |
| 80 | + final BrokerPool pool = existEmbeddedServer.getBrokerPool(); |
| 81 | + final Source mainQuerySource = new StringSource(mainQuery); |
| 82 | + try (final DBBroker broker = pool.getBroker(); |
| 83 | + final Txn transaction = pool.getTransactionManager().beginTransaction()) { |
| 84 | + |
| 85 | + final Tuple2<String, String> namespaceAndPrefix = withCompiledQuery(broker, mainQuerySource, mainCompiledQuery -> { |
| 86 | + final XQueryContext mainQueryContext = mainCompiledQuery.getContext(); |
| 87 | + |
| 88 | + // execute the query |
| 89 | + final Sequence result = executeQuery(broker, mainCompiledQuery); |
| 90 | + |
| 91 | + // check that the namespace of the result element is in the 'sql' namespace |
| 92 | + assertEquals(1, result.getItemCount()); |
| 93 | + assertTrue(result.itemAt(0) instanceof Element); |
| 94 | + assertEquals(Type.ELEMENT, result.itemAt(0).getType()); |
| 95 | + final Element element = (ElementImpl) result.itemAt(0); |
| 96 | + |
| 97 | + return Tuple(element.getNamespaceURI(), element.getPrefix()); |
| 98 | + }); |
| 99 | + |
| 100 | + assertEquals(namespace, namespaceAndPrefix._1); |
| 101 | + assertEquals(prefix, namespaceAndPrefix._2); |
| 102 | + |
| 103 | + transaction.commit(); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments