Package com.taobao.tdhs.client.easy

Examples of com.taobao.tdhs.client.easy.Query


        return currentResultSet;
    }

    private void doSelectOrCount(com.taobao.tdhs.client.statement.Statement s, ParseSQL parseSQL, String tableName,
                                 String dbName) throws SQLException {
        Query query = preprocessGet(s, parseSQL, tableName, dbName);
        List<Entry<String, String>> columns = parseSQL.getColumns();
        if (columns == null || columns.isEmpty()) {
            throw new TDHSSQLException("no columns to select!", parseSQL.getSql());
        }
        String[] field = new String[columns.size()];
        String[] alias = new String[columns.size()];
        int i = 0;
        for (Entry<String, String> e : columns) {
            String f = StringUtil.escapeField(e.getValue());
            if (f == null) {
                throw new TDHSSQLException("error in field[" + e.getValue() + "]", parseSQL.getSql());
            }
            field[i] = f;
            String a = StringUtil.escapeField(e.getKey());
            if (a == null) {
                throw new TDHSSQLException("error in alias[" + e.getKey() + "]", parseSQL.getSql());
            }
            alias[i++] = a;
        }
        if (field.length == 1 && field[0].equalsIgnoreCase("count(*)") && !columns.get(0).getValue().startsWith("`")) {
            TDHSResponse response = null;
            try {
                response = query.count();
            } catch (TDHSException e) {
                throw new SQLException(e);
            }
            processResponse(response, alias, false, false);

        } else {
            query.select(field);
            TDHSResponse response = null;
            try {
                response = query.get();
            } catch (TDHSException e) {
                throw new SQLException(e);
            }
            processResponse(response, alias, false, false);
        }
View Full Code Here


        }
    }

    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;
    }
View Full Code Here

        return kk;
    }

    private void doDelete(com.taobao.tdhs.client.statement.Statement s, ParseSQL parseSQL, String tableName,
                          String dbName) throws SQLException {
        Query query = preprocessGet(s, parseSQL, tableName, dbName);
        TDHSResponse response = null;
        try {
            response = query.delete();
        } catch (TDHSException e) {
            throw new SQLException(e);
        }
        processResponse(response, true);
    }
View Full Code Here

        processResponse(response, true);
    }

    private void doUpdate(com.taobao.tdhs.client.statement.Statement s, ParseSQL parseSQL, String tableName,
                          String dbName) throws SQLException {
        Query query = preprocessGet(s, parseSQL, tableName, dbName);
        List<Entry<String, String>> updateEntries = parseSQL.getUpdateEntries();
        if (updateEntries == null || updateEntries.isEmpty()) {
            throw new TDHSSQLException("no value to update!", parseSQL.getSql());
        }

        for (Entry<String, String> e : updateEntries) {
            if (StringUtils.isBlank(e.getKey()) || StringUtils.isBlank(e.getValue())) {
                throw new TDHSSQLException("insert column and values can't be empty!", parseSQL.getSql());
            }
            String field = StringUtil.escapeField(StringUtils.trim(e.getKey()));
            if (field == null) {
                throw new TDHSSQLException("insert column is error!", parseSQL.getSql());
            }
            String value = StringUtils.trim(e.getValue());
            if (StringUtils.equalsIgnoreCase("null", value)) {
                query.set().field(field).setNull();
            } else if (StringUtils.equalsIgnoreCase("now()", value)) {
                query.set().field(field).setNow();
            } else {
                if (StringUtils.startsWith(value, field)) {
                    value = value.substring(field.length());
                    value = StringUtils.trim(value);
                    if (StringUtils.startsWith(value, "+")) {
                        value = value.substring(1);
                        value = StringUtils.trim(value);
                        if (StringUtil.isLong(value)) {
                            query.set().field(field).add(Long.valueOf(value));
                        } else {
                            throw new TDHSSQLException("update value is error ,is not long", parseSQL.getSql());
                        }
                    } else if (StringUtils.startsWith(value, "-")) {
                        value = value.substring(1);
                        value = StringUtils.trim(value);
                        if (StringUtil.isLong(value)) {
                            query.set().field(field).sub(Long.valueOf(value));
                        } else {
                            throw new TDHSSQLException("update value is error ,is not long", parseSQL.getSql());
                        }

                    } else {
                        throw new TDHSSQLException("update value is error maybe can't support!", parseSQL.getSql());
                    }
                } else {
                    value = StringUtil.escapeValue(value);
                    if (value == null) {
                        throw new TDHSSQLException("update value is error!", parseSQL.getSql());
                    }

                    if (StringUtils.startsWith(value, BYTE_PARAMETER_PREFIX)) {
                        int pidx =
                                ConvertUtil.safeConvertInt(StringUtils.substring(value, BYTE_PARAMETER_PREFIX.length()),
                                        -1);
                        if (byteParameters.containsKey(pidx)) {
                            query.set().field(field).set(byteParameters.get(pidx));
                        } else {
                            query.set().field(field).set(value);
                        }
                    } else {
                        query.set().field(field).set(value);
                    }
                }
            }
        }
        TDHSResponse response = null;
        try {
            response = query.update();
        } catch (TDHSException e) {
            throw new SQLException(e);
        }
        processResponse(response, true);
    }
View Full Code Here

TOP

Related Classes of com.taobao.tdhs.client.easy.Query

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.