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

ru.curs.celesta.score.View Maven / Gradle / Ivy

The newest version!
package ru.curs.celesta.score;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * View object in metadata.
 */
public class View extends AbstractView {

    Map> columnTypes = null;


    View(GrainPart grainPart, String name) throws ParseException {
        super(grainPart, name);
        getGrain().addElement(this);
    }

    public View(GrainPart grainPart, String name, String sql) throws ParseException {
        this(grainPart, name);
        try (StringReader sr = new StringReader(sql)) {
            CelestaParser parser = new CelestaParser(sr);
            try {
                parser.unionAll(this);
            } catch (ParseException e) {
                delete();
                throw e;
            }
        }
    }

    @Override
    String viewType() {
        return "view";
    }

    @Override
    AbstractSelectStmt newSelectStatement() {
        return new ViewSelectStmt(this);
    }


    @Override
    public final Map> getColumns() {
        if (!getSegments().isEmpty()) {
            if (columnTypes == null) {
                columnTypes = new LinkedHashMap<>();
                for (Map.Entry e : getSegments().get(0).columns.entrySet()) {
                    ViewColumnMeta meta = e.getValue().getMeta();
                    meta.setName(e.getKey());
                    columnTypes.put(e.getKey(), meta);
                }
            }
            return columnTypes;
        } else {
            return Collections.emptyMap();
        }
    }

    /**
     * Creates CREATE VIEW script in different SQL dialects by using 'visitor' pattern.
     *
     * @param bw  stream that the saving is performed into
     * @param gen generator-visitor
     * @throws IOException error on writing to stream
     */
    public void createViewScript(PrintWriter bw, SQLGenerator gen) throws IOException {
        bw.println(gen.preamble(this));
        selectScript(bw, gen);
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy