All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mysema.query.sql.WindowRows Maven / Gradle / Ivy

There is a newer version: 3.7.4
Show newest version
/*
 * Copyright 2013, Mysema Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mysema.query.sql;

import java.util.List;

import com.google.common.collect.Lists;
import com.mysema.query.types.ConstantImpl;
import com.mysema.query.types.Expression;

/**
 * WindowRows provides the building of the rows/range part of the window function expression
 *
 * @author tiwe
 *
 * @param 
 */
public class WindowRows {

    private static final String AND = " and";

    private static final String BETWEEN = " between";

    private static final String CURRENT_ROW = " current row";

    private static final String FOLLOWING = " following";

    private static final String PRECEDING = " preceding";

    private static final String UNBOUNDED = " unbounded";

    public class Between {

        public BetweenAnd unboundedPreceding() {
            str.append(UNBOUNDED);
            str.append(PRECEDING);
            return new BetweenAnd();
        }

        public BetweenAnd currentRow() {
            str.append(CURRENT_ROW);
            return new BetweenAnd();
        }

        public BetweenAnd preceding(Expression expr) {
            args.add(expr);
            str.append(PRECEDING);
            str.append(" {" + (offset++) + "}");
            return new BetweenAnd();
        }

        public BetweenAnd preceding(int i) {
            return preceding(ConstantImpl.create(i));
        }

        public BetweenAnd following(Expression expr) {
            args.add(expr);
            str.append(FOLLOWING);
            str.append(" {" + (offset++) + "}");
            return new BetweenAnd();
        }

        public BetweenAnd following(int i) {
            return following(ConstantImpl.create(i));
        }
    }

    public class BetweenAnd {

        public BetweenAnd() {
            str.append(AND);
        }

        public WindowFunction unboundedFollowing() {
            str.append(UNBOUNDED);
            str.append(FOLLOWING);
            return rv.withRowsOrRange(str.toString(), args);
        }

        public WindowFunction currentRow() {
            str.append(CURRENT_ROW);
            return rv.withRowsOrRange(str.toString(), args);
        }

        public WindowFunction preceding(Expression expr) {
            args.add(expr);
            str.append(PRECEDING);
            str.append(" {" + (offset++) + "}");
            return rv.withRowsOrRange(str.toString(), args);
        }

        public WindowFunction preceding(int i) {
            return preceding(ConstantImpl.create(i));
        }

        public WindowFunction following(Expression expr) {
            args.add(expr);
            str.append(FOLLOWING);
            str.append(" {" + (offset++) + "}");
            return rv.withRowsOrRange(str.toString(), args);
        }

        public WindowFunction following(int i) {
            return following(ConstantImpl.create(i));
        }
    }

    private final WindowFunction rv;

    private final StringBuilder str = new StringBuilder();

    private final List> args = Lists.newArrayList();

    private int offset;

    public WindowRows(WindowFunction windowFunction, String prefix, int offset) {
        this.rv = windowFunction;
        this.offset = offset;
        str.append(prefix);
    }

    public Between between() {
        str.append(BETWEEN);
        return new Between();
    }

    public WindowFunction unboundedPreceding() {
        str.append(UNBOUNDED);
        str.append(PRECEDING);
        return rv.withRowsOrRange(str.toString(), args);
    }

    public WindowFunction currentRow() {
        str.append(CURRENT_ROW);
        return rv.withRowsOrRange(str.toString(), args);
    }

    public WindowFunction preceding(Expression expr) {
        args.add(expr);
        str.append(PRECEDING);
        str.append(" {" + (offset++) + "}");
        return rv.withRowsOrRange(str.toString(), args);
    }

    public WindowFunction preceding(int i) {
        return preceding(ConstantImpl.create(i));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy