}
}
private Query preprocessGet(com.taobao.tdhs.client.statement.Statement s, ParseSQL parseSQL, String tableName,
String dbName) throws TDHSSQLException {
Query query = s.query().use(dbName).from(tableName);
List<OperationStruct> operation = parseSQL.getListOperationStructs();
if (StringUtils.isNotBlank(parseSQL.getErrmsg())) {
throw new TDHSSQLException(parseSQL.getErrmsg(), parseSQL.getSql());
}
if (operation == null || operation.isEmpty()) {
throw new TDHSSQLException("must have some where condition!", parseSQL.getSql());
}
List<OperationStruct> keys = new ArrayList<OperationStruct>(2);
HintStruct hint = parseSQL.getHint();
if (hint != null && StringUtils.isBlank(hint.getErrmsg()) && StringUtils.isNotBlank(hint.getIndexName())) {
String index = hint.getIndexName();
List<String> listIndexColumns = hint.getListIndexColumns();
if (listIndexColumns == null || listIndexColumns.isEmpty()) {
throw new TDHSSQLException("can't get columns from hint", parseSQL.getSql());
}
Map<String, List<OperationStruct>> mapOperationStructs = parseSQL.getMapOperationStructs();
for (String hintKey : listIndexColumns) {
List<OperationStruct> k = mapOperationStructs.get(hintKey);
if ((k == null || k.isEmpty())) {
if (keys.isEmpty()) {
throw new TDHSSQLException("don't find key in Index!", parseSQL.getSql());
} else {
break;
}
}
if (keys.isEmpty()) {
keys.add(k.get(0));
if ("in".equalsIgnoreCase(k.get(0).getOper())) {
//in的话只处理一个
break;
}
} else {
if (keys.get(0).getOper().equalsIgnoreCase(k.get(0).getOper())) {
keys.add(k.get(0));
} else {
break;
}
}
}
List<String> orderByColumn = parseSQL.getOrderByColumn();
if (!keys.get(0).getOper().equalsIgnoreCase("=") &&
((orderByColumn != null && !orderByColumn.isEmpty()) || parseSQL.getSortMethod() != null)) {
throw new TDHSSQLException("can't support this orderBy!", parseSQL.getSql());
}
if (orderByColumn != null) {
if (orderByColumn.size() > listIndexColumns.size()) {
throw new TDHSSQLException("too many orderBy columns for this index!", parseSQL.getSql());
}
int j = 0;
for (String oc : orderByColumn) {
String indexField = listIndexColumns.get(listIndexColumns.size() - orderByColumn.size() + j);
if (!indexField.equalsIgnoreCase(oc)) {
throw new TDHSSQLException(
"can't support this orderBy columns which is not in index or error sort!",
parseSQL.getSql());
}
}
}
Where where = query.where().index(index);
processKey(parseSQL, keys, where);
} else {
if (hint != null && StringUtils.isNotBlank(hint.getErrmsg())) {
logger.warn("TDHS: JDBC hint error:" + hint.getErrmsg() + " , SQL:" + parseSQL.getSql());
}
List<String> orderByColumn = parseSQL.getOrderByColumn();
if ((orderByColumn != null && !orderByColumn.isEmpty()) || parseSQL.getSortMethod() != null) {
throw new TDHSSQLException("can't support orderBy without hint!", parseSQL.getSql());
}
OperationStruct firstKey = operation.get(0);
keys.add(firstKey);
String firstField = StringUtil.escapeField(firstKey.getColumnName());
if (firstField == null) {
throw new TDHSSQLException("error field [" + firstKey.getColumnName() + "]", parseSQL.getSql());
}
Where where = query.where().fields(firstField);
processKey(parseSQL, keys, where);
}
for (OperationStruct o : operation) {
if (!keys.contains(o)) {
String op = o.getOper();
if ("=".equalsIgnoreCase(op)) {
PerprocessFilter perprocessFilter = new PerprocessFilter(parseSQL, o).invoke();
String f = perprocessFilter.getF();
String v = perprocessFilter.getV();
query.and().field(f).equal(v);
} else if ("!=".equalsIgnoreCase(op)) {
PerprocessFilter perprocessFilter = new PerprocessFilter(parseSQL, o).invoke();
String f = perprocessFilter.getF();
String v = perprocessFilter.getV();
query.and().field(f).not(v);
} else if (">".equalsIgnoreCase(op)) {
PerprocessFilter perprocessFilter = new PerprocessFilter(parseSQL, o).invoke();
String f = perprocessFilter.getF();
String v = perprocessFilter.getV();
query.and().field(f).greaterThan(v);
} else if (">=".equalsIgnoreCase(op)) {
PerprocessFilter perprocessFilter = new PerprocessFilter(parseSQL, o).invoke();
String f = perprocessFilter.getF();
String v = perprocessFilter.getV();
query.and().field(f).greaterEqual(v);
} else if ("<".equalsIgnoreCase(op)) {
PerprocessFilter perprocessFilter = new PerprocessFilter(parseSQL, o).invoke();
String f = perprocessFilter.getF();
String v = perprocessFilter.getV();
query.and().field(f).lessThan(v);
} else if ("<=".equalsIgnoreCase(op)) {
PerprocessFilter perprocessFilter = new PerprocessFilter(parseSQL, o).invoke();
String f = perprocessFilter.getF();
String v = perprocessFilter.getV();
query.and().field(f).lessEqual(v);
} else if ("in".equalsIgnoreCase(op)) {
throw new TDHSSQLException("can't support IN in this postion!", parseSQL.getSql());
} else {
throw new TDHSSQLException("error operation!", parseSQL.getSql());
}
}
}
int limit = parseSQL.getLimit();
int start = parseSQL.getLimitOffset();
if (limit < 0 || start < 0) {
throw new TDHSSQLException("limit can't be negative!", parseSQL.getSql());
}
query.limit(start, limit);
return query;
}