first = false;
String[] columns = ClassInfo.getColumnNames(f);
if("IN".equals(op)) {
if(!Collection.class.isAssignableFrom(value.getClass()))
throw new SienaException("Collection needed when using IN operator in filter() query");
StringBuilder s = new StringBuilder();
Collection<?> col = (Collection<?>) value;
for (Object object : col) {
// TODO manages model collection
// TO BE VERIFIED: SHOULD BE MANAGED by toString!!!
if(object != null){
s.append(","+SimpleDB.quote(toString(f, object)));
}else{
throw new SienaException("Can't use NULL in collection for IN operator");
}
}
String column = null;
if(ClassInfo.isId(f)) {
column = ITEM_NAME;
} else {
column = ClassInfo.getColumnNames(f)[0];
}
q.append(column+" in("+s.toString().substring(1)+")");
} else if(ClassInfo.isModel(f.getType())) {
// TODO could manage other ops here
if(!op.equals("=")) {
throw new SienaException("Unsupported operator for relationship: "+op);
}
ClassInfo relInfo = ClassInfo.getClassInfo(f.getType());
int i = 0;
for (Field key : relInfo.keys) {
if(value == null) {
q.append(columns[i++] + IS_NULL);
} else {
q.append(columns[i++] + op + SimpleDB.quote(objectFieldToString(value, key)));
}
}
} else {
String column = null;
if(ClassInfo.isId(f)) {
column = "itemName()";
if(value == null && op.equals("=")) {
throw new SienaException("SDB filter on @Id field with 'IS NULL' is not possible");
}
} else {
column = ClassInfo.getColumnNames(f)[0];
}
if(value == null && op.equals("=")) {
q.append(column + IS_NULL);
} else if(value == null && op.equals("!=")) {
q.append(column + IS_NOT_NULL);
} else {
q.append(column + op + SimpleDB.quote(toString(f, value)));
}
}
}else if(QueryFilterSearch.class.isAssignableFrom(filter.getClass())){
Class<T> clazz = query.getQueriedClass();
QueryFilterSearch qf = (QueryFilterSearch)filter;
//if(qf.fields.length>1)
// throw new SienaException("Search not possible for several fields in SDB: only one field");
try {
//Field field = Util.getField(clazz, qf.fields[0]);
//if(field.isAnnotationPresent(Unindexed.class)){
// throw new SienaException("Cannot search the @Unindexed field "+field.getName());
//}
// cuts match into words
String[] words = qf.match.split("\\s");
// if several words, then only OR operator represented by IN GAE
Pattern pNormal = Pattern.compile("[\\%]*(\\w+)[\\%]*");
if(!first) {
q.append(AND);
}
// forces true
first = true;
q.append(" ( ");
for(String f: qf.fields){
Field field = Util.getField(clazz, f);
if(!first) {
q.append(OR);
}
first = false;
q.append(" ( ");
String column = null;
if(ClassInfo.isId(field)) {
column = "itemName()";
} else {
column = ClassInfo.getColumnNames(field)[0];
}
first = true;
for(String word:words){
if(!first) {
q.append(OR);
}
first = false;
if(!pNormal.matcher(word).matches()){
throw new SienaException("'"+word+"' doesn't match pattern [\\%]*(\\w+)[\\%]*");
}
if(word.contains("%")){
q.append(column + LIKE + SimpleDB.quote(word));
}else {
q.append(column + EQ + SimpleDB.quote(word));
}
}
q.append(" ) ");
}
q.append(" ) ");
}catch(Exception e){
throw new SienaException(e);
}
}
}
}