org.sfm.datastax.DatastaxCrudDSL Maven / Gradle / Ivy
package org.sfm.datastax;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.TableMetadata;
import org.sfm.datastax.impl.DatastaxCrudFactory;
import org.sfm.reflect.meta.AliasProviderFactory;
import org.sfm.reflect.meta.Table;
import org.sfm.reflect.TypeHelper;
import org.sfm.reflect.meta.DefaultPropertyNameMatcher;
import org.sfm.reflect.meta.PropertyNameMatcher;
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 =
AliasProviderFactory
.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 =
AliasProviderFactory
.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 - 2025 Weber Informatics LLC | Privacy Policy