org.eclipse.jnosql.databases.cassandra.communication.CassandraConverter Maven / Gradle / Ivy
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/
package org.eclipse.jnosql.databases.cassandra.communication;
import com.datastax.oss.driver.api.core.CqlIdentifier;
import com.datastax.oss.driver.api.core.cql.ColumnDefinition;
import com.datastax.oss.driver.api.core.cql.Row;
import com.datastax.oss.driver.api.core.data.UdtValue;
import com.datastax.oss.driver.api.core.type.DataType;
import com.datastax.oss.driver.api.core.type.UserDefinedType;
import com.datastax.oss.driver.api.core.type.codec.TypeCodec;
import com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry;
import com.datastax.oss.protocol.internal.ProtocolConstants;
import org.eclipse.jnosql.communication.Value;
import org.eclipse.jnosql.communication.semistructured.CommunicationEntity;
import org.eclipse.jnosql.communication.semistructured.Element;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.StreamSupport;
import static java.util.stream.Collectors.toList;
final class CassandraConverter {
private CassandraConverter() {
}
public static CommunicationEntity toDocumentEntity(Row row) {
List columns = new ArrayList<>();
String columnFamily = "";
for (ColumnDefinition definition : row.getColumnDefinitions()) {
columnFamily = definition.getTable().asInternal();
Object result = CassandraConverter.get(definition, row);
if (Objects.nonNull(result)) {
columns.add(getColumn(definition, result));
}
}
return CommunicationEntity.of(columnFamily, columns);
}
private static Element getColumn(ColumnDefinition definition, Object result) {
final DataType type = definition.getType();
return switch (type.getProtocolCode()) {
case ProtocolConstants.DataType.UDT -> Element.class.cast(result);
case ProtocolConstants.DataType.LIST, ProtocolConstants.DataType.SET -> {
if (isUDTIterable(result)) {
yield UDT.builder(getUserType(result)).withName(definition.getName().asInternal())
.addUDTs(getColumns(definition, result)).build();
}
yield Element.of(definition.getName().asInternal(), Value.of(result));
}
default -> Element.of(definition.getName().asInternal(), Value.of(result));
};
}
static Object get(ColumnDefinition definition, Row row) {
String name = definition.getName().asInternal();
final DataType type = definition.getType();
if (type instanceof UserDefinedType) {
return getUDT(definition, row.getUdtValue(name));
}
final TypeCodec