cmdrunner.java

来自「数据仓库展示程序」· Java 代码 · 共 1,855 行 · 第 1/5 页

JAVA
1,855
字号
        //this.connection.execute(query);

        // assume member,dimension,hierarchy,level
        Exp exp = Util.lookup(query, Util.explode(trimmed));

        CmdRunner.debug("parseParameter. exp="
            +((exp == null) ? "null" : exp.getClass().getName()));

        if (exp instanceof Member) {
            Member member = (Member) exp;
            return new Expr(member, Expr.MEMBER_TYPE);
        } else if (exp instanceof mondrian.olap.Level) {
            mondrian.olap.Level level = (mondrian.olap.Level) exp;
            return new Expr(level, Expr.MEMBER_TYPE);
        } else if (exp instanceof Hierarchy) {
            Hierarchy hier = (Hierarchy) exp;
            return new Expr(hier, Expr.MEMBER_TYPE);
        } else if (exp instanceof Dimension) {
            Dimension dim = (Dimension) exp;
            return new Expr(dim, Expr.MEMBER_TYPE);
        }
        return null;
    }

    public static void listParameterNameValues(StringBuffer buf) {
        Iterator it = CmdRunner.paraNameValues.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry e = (Map.Entry) it.next();
            buf.append(e.getKey());
            buf.append('=');
            buf.append(e.getValue());
            buf.append(nl);
        }
    }
    public static void listParam(String name, StringBuffer buf) {
        String v = (String) CmdRunner.paraNameValues.get(name);
        buf.append(v);
    }
    public static boolean isParam(String name) {
        String v = (String) CmdRunner.paraNameValues.get(name);
        return (v != null);
    }
    public static void setParameter(String name, String value) {
        if (name == null) {
            CmdRunner.paraNameValues.clear();
        } else {
            if (value == null) {
                CmdRunner.paraNameValues.remove(name);
            } else {
                CmdRunner.paraNameValues.put(name, value);
            }
        }
    }

    /////////////////////////////////////////////////////////////////////////
    //
    // cubes
    //
    public Cube[] getCubes() {
        Connection conn = getConnection();
        Cube[] cubes = conn.getSchemaReader().getCubes();
        return cubes;
    }
    public Cube getCube(String name) {
        Cube[] cubes = getCubes();
        for (int i = 0; i < cubes.length; i++) {
            Cube cube = cubes[i];
            if (cube.getName().equals(name)) {
                return cube;
            }
        }
        return null;
    }
    public void listCubeName(StringBuffer buf) {
        Connection conn = getConnection();
        Cube[] cubes = getCubes();
        for (int i = 0; i < cubes.length; i++) {
            Cube cube = cubes[i];
            buf.append(cube.getName());
            buf.append(nl);
        }
    }
    public void listCubeAttribues(String name, StringBuffer buf) {
        Cube cube = getCube(name);
        if (cube == null) {
            buf.append("No cube found with name \"");
            buf.append(name);
            buf.append("\"");
        } else {
            RolapCube rcube = (RolapCube) cube;
            buf.append("facttable=");
            buf.append(rcube.getStar().getFactTable().getAlias());
            buf.append(nl);
            buf.append("caching=");
            buf.append(rcube.isCache());
            buf.append(nl);
        }
    }
    public void executeCubeCommand(String cubename,
                                   String command,
                                   StringBuffer buf) {
        Cube cube = getCube(cubename);
        if (cube == null) {
            buf.append("No cube found with name \"");
            buf.append(cubename);
            buf.append("\"");
        } else {
            if (command.equals("clearCache")) {
                RolapCube rcube = (RolapCube) cube;
                rcube.clearCache();
            } else {
                buf.append("For cube \"");
                buf.append(cubename);
                buf.append("\" there is no command \"");
                buf.append(command);
                buf.append("\"");
            }
        }
    }
    public void setCubeAttribute(String cubename,
                                 String name,
                                 String value,
                                 StringBuffer buf) {
        Cube cube = getCube(cubename);
        if (cube == null) {
            buf.append("No cube found with name \"");
            buf.append(cubename);
            buf.append("\"");
        } else {
            if (name.equals("caching")) {
                RolapCube rcube = (RolapCube) cube;
                rcube.setCache(Boolean.valueOf(value).booleanValue());
            } else {
                buf.append("For cube \"");
                buf.append(cubename);
                buf.append("\" there is no attribute \"");
                buf.append(name);
                buf.append("\"");
            }
        }
    }
    //
    /////////////////////////////////////////////////////////////////////////

    /**
     * Executes a query and returns the result as a string.
     *
     * @param queryString MDX query text
     * @return result String
     */
    public String execute(String queryString) {
        Result result = runQuery(queryString, true);
        String resultString = toString(result);
        return resultString;
    }

    /**
     * Executes a query and returns the result.
     *
     * @param queryString MDX query text
     * @return a {@link Result} object
     */
    public Result runQuery(String queryString, boolean loadParams) {
        CmdRunner.debug("CmdRunner.runQuery: TOP");
        Result result = null;
        long start = 0;
        try {
            this.connection = getConnection();
            CmdRunner.debug("CmdRunner.runQuery: AFTER getConnection");
            Query query = this.connection.parseQuery(queryString);
            CmdRunner.debug("CmdRunner.runQuery: AFTER parseQuery");
            if (loadParams) {
                loadParameters(query);
            }
            start = System.currentTimeMillis();
            result = this.connection.execute(query);
        } finally {
            CmdRunner.debug("CmdRunner.runQuery: BOTTOM");
        }
        queryTime = (System.currentTimeMillis() - start);
        totalQueryTime += queryTime;
        return result;
    }


    /**
     * Converts a {@link Result} object to a string
     *
     * @param result
     * @return String version of mondrian Result object.
     */
    public String toString(Result result) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        result.print(pw);
        pw.flush();
        return sw.toString();
    }


    public void makeConnectString() {
        String connectString = CmdRunner.getConnectStringProperty();
        CmdRunner.debug("CmdRunner.makeConnectString: connectString="+connectString);

        Util.PropertyList connectProperties = null;
        if (connectString == null || connectString.equals("")) {
            // create new and add provider
            connectProperties = new Util.PropertyList();
            connectProperties.put(RolapConnectionProperties.Provider,"mondrian");
        } else {
            // load with existing connect string
            connectProperties = Util.parseConnectString(connectString);
        }

        // override jdbc url
        String jdbcURL = CmdRunner.getJdbcURLProperty();

        CmdRunner.debug("CmdRunner.makeConnectString: jdbcURL="+jdbcURL);

        if (jdbcURL != null) {
            // add jdbc url to connect string
            connectProperties.put(RolapConnectionProperties.Jdbc, jdbcURL);
        }

        // override jdbc drivers
        String jdbcDrivers = CmdRunner.getJdbcDriversProperty();

        CmdRunner.debug("CmdRunner.makeConnectString: jdbcDrivers="+jdbcDrivers);
        if (jdbcDrivers != null) {
            // add jdbc drivers to connect string
            connectProperties.put(RolapConnectionProperties.JdbcDrivers, jdbcDrivers);
        }

        // override catalog url
        String catalogURL = CmdRunner.getCatalogURLProperty();

        CmdRunner.debug("CmdRunner.makeConnectString: catalogURL="+catalogURL);

        if (catalogURL != null) {
            // add catalog url to connect string
            connectProperties.put(RolapConnectionProperties.Catalog, catalogURL);
        }

        // override JDBC user
        String jdbcUser = CmdRunner.getJdbcUserProperty();

        CmdRunner.debug("CmdRunner.makeConnectString: jdbcUser="+jdbcUser);

        if (jdbcUser != null) {
            // add user to connect string
            connectProperties.put(RolapConnectionProperties.JdbcUser, jdbcUser);
        }

        // override JDBC password
        String jdbcPassword = CmdRunner.getJdbcPasswordProperty();

        CmdRunner.debug("CmdRunner.makeConnectString: jdbcPassword="+jdbcPassword);

        if (jdbcPassword != null) {
            // add password to connect string
            connectProperties.put(RolapConnectionProperties.JdbcPassword, jdbcPassword);
        }

        CmdRunner.debug("CmdRunner.makeConnectString: connectProperties="+connectProperties);

        this.connectString = connectProperties.toString();
    }

    /**
     * Gets a connection to Mondrian.
     *
     * @return Mondrian {@link Connection}
     */
    public Connection getConnection() {
        return getConnection(CmdRunner.RELOAD_CONNECTION);
    }

    /**
     * Gets a Mondrian connection, creating a new one if fresh is true.
     *
     * @param fresh
     * @return mondrian Connection.
     */
    public synchronized Connection getConnection(boolean fresh) {
        if (this.connectString == null) {
            makeConnectString();
        }
        if (fresh) {
            return DriverManager.getConnection(this.connectString, null, fresh);
        } else if (this.connection == null) {
            this.connection =
                DriverManager.getConnection(this.connectString, null, fresh);
        }
        return this.connection;
    }

    /////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////
    //
    // static methods
    //
    /////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////
    private static boolean debug = false;

    protected static void debug(String msg) {
        if (CmdRunner.debug) {
            System.out.println(msg);
        }
    }

    /////////////////////////////////////////////////////////////////////////
    // properties
    /////////////////////////////////////////////////////////////////////////
    protected static String getConnectStringProperty() {
        return MondrianProperties.instance().TestConnectString.get();
    }
    protected static String getJdbcURLProperty() {
        return MondrianProperties.instance().TestJdbcURL.get();
    }
    
    protected static String getJdbcUserProperty() {
        return MondrianProperties.instance().TestJdbcUser.get();
    }
    
    protected static String getJdbcPasswordProperty() {
        return MondrianProperties.instance().TestJdbcPassword.get();
    }
    protected static String getCatalogURLProperty() {
        return MondrianProperties.instance().CatalogURL.get();
    }
    protected static String getJdbcDriversProperty() {
        return MondrianProperties.instance().JdbcDrivers.get();
    }

    /////////////////////////////////////////////////////////////////////////
    // command loop
    /////////////////////////////////////////////////////////////////////////

    protected void commandLoop(boolean interactive) throws IOException {
        commandLoop(System.in, interactive);
    }

    protected void commandLoop(File file) throws IOException {
        // If we open a stream, then we close it.
        InputStream in = new FileInputStream(file);
        try {
            commandLoop(in, false);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (Exception ex) {
                    // ignore
                }
            }
        }
    }

    protected void commandLoop(String mdxCmd, boolean interactive)
        throws IOException {

        InputStream is = new ByteArrayInputStream(mdxCmd.getBytes());
        commandLoop(is, interactive);
    }

    private static final String COMMAND_PROMPT_START = "> ";
    private static final String COMMAND_PROMPT_MID = "? ";

    /**
     * The Command Loop where lines are read from the InputStream and

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?