All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.simpleflatmapper.datastax.DatastaxCrudDSL Maven / Gradle / Ivy

There is a newer version: 8.2.3
Show newest version
package org.simpleflatmapper.datastax;

import com.datastax.driver.core.Session;
import com.datastax.driver.core.TableMetadata;
import org.simpleflatmapper.datastax.impl.DatastaxCrudFactory;
import org.simpleflatmapper.reflect.meta.AliasProviderService;
import org.simpleflatmapper.reflect.meta.Table;
import org.simpleflatmapper.reflect.meta.DefaultPropertyNameMatcher;
import org.simpleflatmapper.util.TypeHelper;

import java.lang.reflect.Type;

public class DatastaxCrudDSL {
    private final DatastaxMapperFactory datastaxMapperFactory;
    private final Type targetType;
    private final Type keyType;

    public DatastaxCrudDSL(Type targetType, Type keyType, DatastaxMapperFactory datastaxMapperFactory) {
        this.targetType = targetType;
        this.keyType = keyType;
        this.datastaxMapperFactory = datastaxMapperFactory;
    }


    public DatastaxCrud to(Session session) {
        Table table =
                AliasProviderService
                        .getAliasProvider()
                        .getTable(TypeHelper.toClass(targetType));
        return to(session, keyspace(session, table), table(session, table, targetType));
    }

    private String table(Session session, Table keyspaceTable, Type targetType) {
        String table = keyspaceTable.table();

        if (table == null) {
            final String className = TypeHelper.toClass(targetType).getSimpleName();
            for(TableMetadata metadata : session.getCluster().getMetadata().getKeyspace(keyspace(session, keyspaceTable)).getTables()) {
                if (DefaultPropertyNameMatcher.of(metadata.getName()).matches(className)) {
                    return metadata.getName();
                }
            }
        } else {
            return table;
        }

        throw new IllegalArgumentException("No table define on type " + targetType);
    }

    private String keyspace(Session session, Table table) {
        String keyspace = table.schema();
        if (keyspace == null) {
            keyspace = session.getLoggedKeyspace();
        }
        return keyspace;
    }

    public DatastaxCrud to(Session session, String table) {
        Table keyspaceTable =
                AliasProviderService
                        .getAliasProvider()
                        .getTable(TypeHelper.toClass(targetType));

        return to(session, keyspace(session, keyspaceTable), table);
    }

    public DatastaxCrud to(Session session, String keyspace,  String table) {
        TableMetadata tableMetadata =
                session.getCluster().getMetadata().getKeyspace(keyspace).getTable(table);

        return DatastaxCrudFactory.newInstance(targetType,
                keyType,
                tableMetadata,
                session,
                datastaxMapperFactory);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy