Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions api/src/main/java/org/eclipse/daanse/xmla/api/RowStream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2026 Contributors to the Eclipse Foundation.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Stefan Bischof (bipolis.org) - initial
*/
package org.eclipse.daanse.xmla.api;

import java.util.Iterator;
import java.util.List;

/**
* An Execute result written row by row as the rows come, not at once -- what {@code Subscribe} to a trace answers: an
* open rowset, one row for each event, until the subscription ends.
* <p>
* The transport writes the envelope and the inline schema at once, then each row as {@link #rows()} gives it,
* flushing after each, and closes the document when the iterator ends. {@code hasNext()} may block while no row is
* there. The transport closes the stream when it has written the last row or cannot write any more because the client
* is gone.
* <p>
* A column has the name a client reads it by, as {@code sql:field}, and a short element name of its own, as Analysis
* Services writes a trace's rows: {@code <row><a>10</a><b>6</b><c>SELECT …</c></row>} under
* {@code <xsd:element sql:field="EventClass" name="a" type="xsd:int"/>}.
*/
public abstract class RowStream extends org.eclipse.emf.ecore.impl.MinimalEObjectImpl.Container
implements AutoCloseable {

/**
* One column of the rows.
*
* @param field the name a client reads the column by
* @param name the element name of its values
* @param xsdType its type, e.g. {@code xsd:int}; {@code null} for none
*/
public record Column(String field, String name, String xsdType) {
}

/** The columns, in the order the rows hold their values. */
public abstract List<Column> columns();

/** The rows, each a value per column, {@code null} for one it does not have; {@code hasNext()} may block. */
public abstract Iterator<List<Object>> rows();

/** Ends what the stream reads from; called once the transport has written its last row. */
@Override
public void close() {
// nothing held by default
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,50 @@ public enum Side {
CLIENT, SERVER
}

/**
* One error of a refusal, as the fault's {@code detail} lists it.
*
* @param code the unsigned 32-bit number Analysis Services gives it
* @param description its sentence
*/
public record Error(long code, String description) {
}

private final Side side;

private final java.util.List<Error> errors;

/** A refusal this server has no Analysis Services number for: the fault then carries no {@code Error} element. */
public XmlaRefusedException(Side side, String message) {
this(side, null, message);
}

/**
* A refusal with the number Analysis Services answers the same refusal with.
* <p>
* The number is what the client sees as {@code ErrorCode}, and the transport writes the fault code in the form
* SSAS uses for it, {@code XMLAnalysisError.0x<hex>}. Only the number Analysis Services gives the same refusal
* belongs here; a refusal it has no number for keeps the other constructor rather than an invented code.
*
* @param errorCode the unsigned 32-bit number, e.g. {@code 0xC1140002L}; {@code null} for none
*/
public XmlaRefusedException(Side side, Long errorCode, String message) {
super(message);
this.side = side;
this.errors = errorCode == null ? java.util.List.of() : java.util.List.of(new Error(errorCode, message));
}

/**
* A refusal Analysis Services answers with several errors -- an event number it does not know is a range error and
* then a parse error naming where the number stood. The fault lists them in this order; its code and its
* {@code faultstring} are the last one's, as there.
*
* @param errors at least one
*/
public XmlaRefusedException(Side side, java.util.List<Error> errors) {
super(errors.getLast().description());
this.side = side;
this.errors = java.util.List.copyOf(errors);
}

/**
Expand Down Expand Up @@ -73,4 +112,17 @@ public static XmlaRefusedException sessionNotOpened() {
public Side side() {
return side;
}

/**
* The Analysis Services number of this refusal -- of its last error where it has several -- or {@code null} when
* this server has none for it.
*/
public Long errorCode() {
return errors.isEmpty() ? null : errors.getLast().code();
}

/** The errors of this refusal in the order the fault lists them; none when this server has no number for it. */
public java.util.List<Error> errors() {
return errors;
}
}
16 changes: 16 additions & 0 deletions api/src/main/java/org/eclipse/daanse/xmla/api/XmlaSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public final class XmlaSession {
private volatile Instant lastUsedAt;
private volatile Instant lastCommandStartedAt;
private volatile Instant lastCommandEndedAt;
private volatile String lastCommand;
private volatile AuthenticatedIdentity identity;

XmlaSession(String id, Instant startedAt) {
Expand Down Expand Up @@ -66,6 +67,14 @@ public Optional<Instant> lastCommandEndedAt() {
return Optional.ofNullable(lastCommandEndedAt);
}

/**
* The text of the command the session ran last, or runs now: a statement as written, the request type of a
* Discover, the name of any other command -- what {@code SESSION_LAST_COMMAND} and {@code COMMAND_TEXT} report.
*/
public Optional<String> lastCommand() {
return Optional.ofNullable(lastCommand);
}

/**
* The identity the in-band handshake bound, or empty for a session that carries
* none.
Expand Down Expand Up @@ -94,8 +103,15 @@ void identity(AuthenticatedIdentity bound) {
this.identity = bound;
}

/** A command begins in the session; its text is not known. */
public void commandStarted() {
commandStarted(null);
}

/** A command of this text begins in the session. */
public void commandStarted(String text) {
Instant now = Instant.now();
lastCommand = text;
lastCommandStartedAt = now;
lastUsedAt = now;
commandCount.incrementAndGet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,11 @@ void aSessionReportsWhatDiscoverSessionsAsksFor() {
assertThat(session.busy()).isFalse();
assertThat(session.idle(session.startedAt())).isZero();

session.commandStarted();
assertThat(session.lastCommand()).isEmpty();
session.commandStarted("SELECT FROM [Sales]");
assertThat(session.busy()).isTrue();
assertThat(session.commandCount()).isEqualTo(1);
assertThat(session.lastCommand()).contains("SELECT FROM [Sales]");

session.commandEnded();
assertThat(session.busy()).isFalse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,32 @@ public enum Unknown {
private static final Map<EClass, Map<String, EStructuralFeature>> ATTRIBUTES = new ConcurrentHashMap<>();

private final Unknown unknown;
/**
* The element names from the envelope down to where the reader stands, while it records where it read what; null
* when it does not.
*/
private final java.util.List<String> path;

public EcoreXmlReader(Unknown unknown) {
this(unknown, null);
}

/**
* A reader that records where it read each object ({@link ElementLocation}), its elements standing below the given
* path, and refuses an unknown element, under {@link Unknown#FAIL}, in the words Analysis Services uses.
*
* @param above the element names above the element read, from the envelope down; {@code null} records nothing
*/
public EcoreXmlReader(Unknown unknown, java.util.List<String> above) {
this.unknown = unknown;
this.path = above == null ? null : new java.util.ArrayList<>(above);
}

private ElementLocation.Position here(XMLStreamReader in, String child) {
var at = in.getLocation();
String joined = String.join("/", path);
return new ElementLocation.Position(child == null ? joined : joined + "/" + child, in.getNamespaceURI(),
at.getLineNumber(), at.getColumnNumber());
}

/**
Expand All @@ -83,6 +106,12 @@ public EcoreXmlReader(Unknown unknown) {
*/
public EObject read(XMLStreamReader in, EClass target) throws XMLStreamException {
EObject object = target.getEPackage().getEFactoryInstance().create(target);
ElementLocation location = null;
if (path != null) {
path.add(in.getLocalName());
location = new ElementLocation(here(in, null));
object.eAdapters().add(location);
}
readAttributes(in, target, object);
// A type with simple content keeps the element's own text, as
// <FmtValue>$1,234.00</FmtValue> and <Value xsi:type="xsd:int">42</Value>
Expand All @@ -104,6 +133,10 @@ public EObject read(XMLStreamReader in, EClass target) throws XMLStreamException
if (text != null && !text.isEmpty()) {
set(object, simple, text.toString(), in);
}
if (location != null) {
location.end(here(in, null));
path.removeLast();
}
return object;
}
depth--;
Expand Down Expand Up @@ -173,6 +206,12 @@ private void readChild(XMLStreamReader in, EClass target, EObject object) throws
return;
}
if (unknown == Unknown.FAIL) {
if (path != null) {
var at = in.getLocation();
throw new XmlaParseException(XmlaParseException.CANNOT_APPEAR, "The " + name + " element at line "
+ at.getLineNumber() + ", column " + at.getColumnNumber() + " (namespace "
+ in.getNamespaceURI() + ") cannot appear under " + String.join("/", path) + ".");
}
throw new XmlaCodecException("element <" + name + "> is not a feature of " + target.getName(), in);
}
skipSubtree(in);
Expand All @@ -186,6 +225,10 @@ private void readChild(XMLStreamReader in, EClass target, EObject object) throws
// XML and kept as the column's value.
set(object, feature, XmlFragment.read(in), in);
} else {
if (path != null) {
ElementLocation.Position at = here(in, name);
ElementLocation.of(object).ifPresent(location -> location.child(name, at));
}
set(object, feature, in.getElementText(), in);
}
}
Expand All @@ -207,6 +250,19 @@ private static EStructuralFeature wrappedBy(EClass target, String name) {
* Reads the children of a wrapper as though they had been written without it.
*/
private void readWrapped(XMLStreamReader in, EClass target, EObject object) throws XMLStreamException {
if (path != null) {
path.add(in.getLocalName());
}
try {
readWrappedChildren(in, target, object);
} finally {
if (path != null) {
path.removeLast();
}
}
}

private void readWrappedChildren(XMLStreamReader in, EClass target, EObject object) throws XMLStreamException {
int depth = 1;
while (in.hasNext() && depth > 0) {
int event = in.next();
Expand Down
Loading
Loading