Search in sources :

Example 1 with Field

use of io.mycat.route.parser.primitive.Model.Field in project Mycat-Server by MyCATApache.

the class FunctionParser method parseFunction.

public static Function parseFunction(String function) throws SQLNonTransientException {
    StringBuilder buffer = new StringBuilder();
    Stack<Function> functions = new Stack<>();
    int flag = 0;
    for (int i = 0; i < function.length(); i++) {
        char current = function.charAt(i);
        switch(current) {
            case Commons.LEFT_BRACKET:
                if (flag == 0) {
                    String currentIdentifier = buffer.toString().trim();
                    buffer = new StringBuilder();
                    if (!StringUtil.isEmpty(currentIdentifier)) {
                        Function function1 = new Function(currentIdentifier);
                        if (!functions.empty() && functions.peek() != null) {
                            functions.peek().getArguments().add(function1);
                        }
                        functions.push(function1);
                    }
                    break;
                }
                buffer.append(current);
                break;
            case Commons.ARGUMENT_SEPARATOR:
                if (flag == 0 || flag == 3) {
                    String currentIdentifier = buffer.toString().trim();
                    buffer = new StringBuilder();
                    if (!StringUtil.isEmpty(currentIdentifier)) {
                        if (flag == 3) {
                            flag = 0;
                            Identifier identifier = new Identifier(currentIdentifier);
                            functions.peek().getArguments().add(identifier);
                        } else {
                            Field field = new Field(currentIdentifier);
                            functions.peek().getArguments().add(field);
                        }
                    }
                    break;
                }
                buffer.append(current);
                break;
            case Commons.RIGHT_BRACKET:
                if (flag != 1 && flag != 2) {
                    String currentIdentifier = buffer.toString().trim();
                    buffer = new StringBuilder();
                    if (!StringUtil.isEmpty(currentIdentifier)) {
                        if (flag == 3) {
                            flag = 0;
                            Identifier identifier = new Identifier(currentIdentifier);
                            functions.peek().getArguments().add(identifier);
                        } else {
                            Field field = new Field(currentIdentifier);
                            functions.peek().getArguments().add(field);
                        }
                    }
                    if (flag == 0) {
                        if (functions.size() == 1) {
                            return functions.pop();
                        } else {
                            functions.pop();
                        }
                    }
                    break;
                }
                buffer.append(current);
                break;
            case Commons.QUOTE:
                if (flag == 0) {
                    flag = 1;
                } else if (flag == 1) {
                    flag = 3;
                }
            case Commons.DOUBLE_QUOTE:
                if (flag == 0) {
                    flag = 2;
                } else if (flag == 2) {
                    flag = 3;
                }
            default:
                buffer.append(current);
        }
    }
    throw new SQLNonTransientException("Function is not in right format!");
}
Also used : Function(io.mycat.route.parser.primitive.Model.Function) Field(io.mycat.route.parser.primitive.Model.Field) SQLNonTransientException(java.sql.SQLNonTransientException) Identifier(io.mycat.route.parser.primitive.Model.Identifier) Stack(java.util.Stack)

Aggregations

Field (io.mycat.route.parser.primitive.Model.Field)1 Function (io.mycat.route.parser.primitive.Model.Function)1 Identifier (io.mycat.route.parser.primitive.Model.Identifier)1 SQLNonTransientException (java.sql.SQLNonTransientException)1 Stack (java.util.Stack)1