use of org.apache.gora.cassandra.bean.Field in project gora by apache.
the class CassandraQueryFactory method processFieldsForCreateTableQuery.
private static void processFieldsForCreateTableQuery(List<Field> fields, boolean isCommaNeeded, StringBuilder stringBuilder) {
for (Field field : fields) {
if (isCommaNeeded) {
stringBuilder.append(", ");
}
stringBuilder.append(field.getColumnName()).append(" ").append(field.getType());
boolean isStaticColumn = Boolean.parseBoolean(field.getProperty("static"));
boolean isPrimaryKey = Boolean.parseBoolean(field.getProperty("primarykey"));
if (isStaticColumn) {
stringBuilder.append(" STATIC");
}
if (isPrimaryKey) {
stringBuilder.append(" PRIMARY KEY ");
}
isCommaNeeded = true;
}
}
use of org.apache.gora.cassandra.bean.Field in project gora by apache.
the class CassandraQueryFactory method getSelectObjectWithFieldsQuery.
/**
* This method returns CQL Select query to retrieve data from the table with given fields.
* This method is used for Native Serialization
* refer: http://docs.datastax.com/en/cql/3.3/cql/cql_reference/cqlSelect.html
*
* @param mapping Cassandra Mapping {@link CassandraMapping}
* @param fields Given fields to retrieve
* @return CQL Query
*/
static String getSelectObjectWithFieldsQuery(CassandraMapping mapping, String[] fields) {
String cqlQuery = null;
String[] columnNames = getColumnNames(mapping, Arrays.asList(fields));
Select select = QueryBuilder.select(columnNames).from(mapping.getKeySpace().getName(), mapping.getCoreName());
if (Boolean.parseBoolean(mapping.getProperty("allowFiltering"))) {
select.allowFiltering();
}
CassandraKey cKey = mapping.getCassandraKey();
if (cKey != null) {
Select.Where query = null;
boolean isWhereNeeded = true;
for (Field field : cKey.getFieldList()) {
if (isWhereNeeded) {
query = select.where(QueryBuilder.eq(field.getColumnName(), "?"));
isWhereNeeded = false;
} else {
query = query.and(QueryBuilder.eq(field.getColumnName(), "?"));
}
}
cqlQuery = query != null ? query.getQueryString() : null;
} else {
for (Field field : mapping.getFieldList()) {
boolean isPrimaryKey = Boolean.parseBoolean(field.getProperty("primarykey"));
if (isPrimaryKey) {
cqlQuery = select.where(QueryBuilder.eq(field.getColumnName(), "?")).getQueryString();
break;
}
}
}
return cqlQuery;
}
use of org.apache.gora.cassandra.bean.Field in project gora by apache.
the class CassandraQueryFactory method getColumnNames.
private static String[] getColumnNames(CassandraMapping mapping, List<String> fields) {
ArrayList<String> columnNames = new ArrayList<>();
for (String field : fields) {
Field fieldBean = mapping.getFieldFromFieldName(field);
CassandraKey cassandraKey = mapping.getCassandraKey();
Field keyBean = null;
if (cassandraKey != null) {
keyBean = cassandraKey.getFieldFromFieldName(field);
}
if (fieldBean != null) {
columnNames.add(fieldBean.getColumnName());
} else if (keyBean != null) {
columnNames.add(keyBean.getColumnName());
} else {
LOG.warn("{} field is ignored, couldn't find relevant field in the persistent mapping", field);
}
}
return columnNames.toArray(new String[0]);
}
use of org.apache.gora.cassandra.bean.Field in project gora by apache.
the class NativeSerializer method getKey.
private K getKey(T object) {
String keyField = null;
for (Field field : mapping.getFieldList()) {
boolean isPrimaryKey = Boolean.parseBoolean(field.getProperty("primarykey"));
if (isPrimaryKey) {
keyField = field.getFieldName();
break;
}
}
K key;
Method keyMethod = null;
try {
for (Method method : this.persistentClass.getMethods()) {
if (method.getName().equalsIgnoreCase("get" + keyField)) {
keyMethod = method;
}
}
key = (K) keyMethod.invoke(object);
} catch (Exception e) {
try {
key = (K) this.persistentClass.getField(keyField).get(object);
} catch (Exception e1) {
throw new RuntimeException("Field" + keyField + " is not accessible in " + persistentClass + " : " + e1.getMessage());
}
}
return key;
}
use of org.apache.gora.cassandra.bean.Field in project gora by apache.
the class NativeSerializer method analyzePersistent.
/**
* {@inheritDoc}
*
* @throws Exception
*/
@Override
protected void analyzePersistent() throws Exception {
userDefineTypeMaps = new HashMap<>();
for (Field field : mapping.getFieldList()) {
String fieldType = field.getType();
if (fieldType.contains("frozen")) {
String udtType = fieldType.substring(fieldType.indexOf("<") + 1, fieldType.indexOf(">"));
String createQuery = CassandraQueryFactory.getCreateUDTTypeForNative(mapping, persistentClass, udtType, field.getFieldName());
userDefineTypeMaps.put(udtType, createQuery);
}
}
}
Aggregations