cmdrunner.java
来自「数据仓库展示程序」· Java 代码 · 共 1,855 行 · 第 1/5 页
JAVA
1,855 行
/*
// $Id: //open/mondrian/src/main/mondrian/tui/CmdRunner.java#18 $
// This software is subject to the terms of the Common Public License
// Agreement, available at the following URL:
// http://www.opensource.org/licenses/cpl.html.
// (C) Copyright 2005-2005 Julian Hyde and others
// All Rights Reserved.
// You must accept the terms of that agreement to use this software.
*/
package mondrian.tui;
import mondrian.olap.Category;
import mondrian.olap.*;
import mondrian.olap.Hierarchy;
import mondrian.olap.fun.FunInfo;
import mondrian.rolap.RolapConnectionProperties;
import mondrian.rolap.RolapCube;
import org.apache.log4j.Level;
import org.apache.log4j.*;
import org.eigenbase.util.property.*;
import org.eigenbase.util.property.Property;
import java.io.*;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
/**
* Command line utility which reads and executes MDX commands.
*
* <p>TODO: describe how to use this class.</p>
*
* @author Richard Emberson
* @version $Id: //open/mondrian/src/main/mondrian/tui/CmdRunner.java#18 $
*/
public class CmdRunner {
private static final String nl = Util.nl;
private static boolean RELOAD_CONNECTION = true;
private static final Map paraNameValues = new HashMap();
private boolean timeQueries;
private long queryTime;
private long totalQueryTime;
private String filename;
private String mdxCmd;
private String mdxResult;
private String error;
private String stack;
private String connectString;
private Connection connection;
/**
* Creates a <code>CmdRunner</code>.
*/
public CmdRunner() {
this.filename = null;
this.mdxCmd = null;
this.mdxResult = null;
this.error = null;
this.queryTime = -1;
}
public void setTimeQueries(boolean timeQueries) {
this.timeQueries = timeQueries;
}
public boolean getTimeQueries() {
return timeQueries;
}
public long getQueryTime() {
return queryTime;
}
public long getTotalQueryTime() {
return totalQueryTime;
}
public void noCubeCaching() {
Cube[] cubes = getCubes();
for (int i = 0; i < cubes.length; i++) {
Cube cube = cubes[i];
RolapCube rcube = (RolapCube) cube;
rcube.setCache(false);
}
}
void setError(String s) {
this.error = s;
}
void setError(Throwable t) {
this.error = formatError(t);
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
pw.flush();
this.stack = sw.toString();
}
void clearError() {
this.error = null;
this.stack = null;
}
private String formatError(Throwable mex) {
String message = mex.getMessage();
if (message == null) {
message = mex.toString();
}
if (mex.getCause() != null && mex.getCause() != mex) {
message = message + nl + formatError(mex.getCause());
}
return message;
}
public static void listPropertyNames(StringBuffer buf) {
PropertyInfo propertyInfo =
new PropertyInfo(MondrianProperties.instance());
for (int i = 0; i < propertyInfo.size(); i++) {
buf.append(propertyInfo.getProperty(i).getPath());
buf.append(nl);
}
}
public static void listPropertiesAll(StringBuffer buf) {
PropertyInfo propertyInfo =
new PropertyInfo(MondrianProperties.instance());
for (int i = 0; i < propertyInfo.size(); i++) {
String propertyName = propertyInfo.getPropertyName(i);
String propertyValue = propertyInfo.getProperty(i).getString();
buf.append(propertyName);
buf.append('=');
buf.append(propertyValue);
buf.append(nl);
}
}
/**
* Returns the value of a property, or null if it is not set.
*/
private static String getPropertyValue(String propertyName) {
final Property property = PropertyInfo.lookupProperty(
MondrianProperties.instance(),
propertyName);
return property.isSet() ?
property.getString() :
null;
}
public static void listProperty(String propertyName, StringBuffer buf) {
buf.append(getPropertyValue(propertyName));
}
public static boolean isProperty(String propertyName) {
final Property property = PropertyInfo.lookupProperty(
MondrianProperties.instance(),
propertyName);
return property != null;
}
public static boolean setProperty(String name, String value) {
final Property property = PropertyInfo.lookupProperty(
MondrianProperties.instance(),
name);
String oldValue = property.getString();
if (! Util.equals(oldValue, value)) {
property.setString(value);
return true;
} else {
return false;
}
}
public void loadParameters(Query query) {
Parameter[] params = query.getParameters();
for (int i = 0; i < params.length; i++) {
Parameter param = params[i];
loadParameter(query, param);
}
}
/**
* Looks up the definition of a property with a given name.
*/
private static class PropertyInfo {
private final List propertyList = new ArrayList();
private final List propertyNameList = new ArrayList();
PropertyInfo(MondrianProperties properties) {
final Class clazz = properties.getClass();
final Field[] fields = clazz.getFields();
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
if (!Modifier.isPublic(field.getModifiers()) ||
Modifier.isStatic(field.getModifiers()) ||
!Property.class.isAssignableFrom(
field.getType())) {
continue;
}
final Property property;
try {
property = (Property) field.get(properties);
} catch (IllegalAccessException e) {
continue;
}
propertyList.add(property);
propertyNameList.add(field.getName());
}
}
public int size() {
return propertyList.size();
}
public Property getProperty(int i) {
return (Property) propertyList.get(i);
}
public String getPropertyName(int i) {
return (String) propertyNameList.get(i);
}
/**
* Looks up the definition of a property with a given name.
*/
public static Property lookupProperty(
MondrianProperties properties,
String propertyName)
{
final Class clazz = properties.getClass();
final Field field;
try {
field = clazz.getField(propertyName);
} catch (NoSuchFieldException e) {
return null;
}
if (!Modifier.isPublic(field.getModifiers()) ||
Modifier.isStatic(field.getModifiers()) ||
!Property.class.isAssignableFrom(field.getType())) {
return null;
}
try {
return (Property) field.get(properties);
} catch (IllegalAccessException e) {
return null;
}
}
}
private static class Expr {
static final int STRING_TYPE = 1;
static final int NUMERIC_TYPE = 2;
static final int MEMBER_TYPE = 3;
static String typeName(int type) {
switch (type) {
case STRING_TYPE:
return "STRING_TYPE";
case NUMERIC_TYPE:
return "NUMERIC_TYPE";
case MEMBER_TYPE:
return "MEMBER_TYPE";
default:
return "UNKNOWN_TYPE";
}
}
Object value;
int type;
Expr(Object value, int type) {
this.value = value;
this.type = type;
}
}
public void loadParameter(Query query, Parameter param) {
int pType = param.getType();
String name = param.getName();
String value = (String) CmdRunner.paraNameValues.get(name);
CmdRunner.debug("loadParameter: name=" +name+ ", value=" + value);
if (value == null) {
return;
}
Expr expr = parseParameter(value);
if (expr == null) {
return;
}
int type = expr.type;
// found the parameter with the given name in the query
switch (pType) {
case Category.Numeric :
if (type != Expr.NUMERIC_TYPE) {
String msg = "For parameter named \""
+ name
+ "\" of Catetory.Numeric, "
+ "the value was type \""
+ Expr.typeName(type)
+ "\"";
throw new IllegalArgumentException(msg);
}
if (expr.value instanceof Double) {
param.setValue(expr.value);
} else {
Number n = (Number) expr.value;
param.setValue(new Double(n.doubleValue()));
}
break;
case Category.String :
if (type != Expr.STRING_TYPE) {
String msg = "For parameter named \""
+ name
+ "\" of Catetory.String, "
+ "the value was type \""
+ Expr.typeName(type)
+ "\"";
throw new IllegalArgumentException(msg);
}
param.setValue(value);
break;
case Category.Member :
if (type != Expr.MEMBER_TYPE) {
String msg = "For parameter named \""
+ name
+ "\" of Catetory.Member, "
+ "the value was type \""
+ Expr.typeName(type)
+ "\"";
throw new IllegalArgumentException(msg);
}
param.setValue(expr.value);
break;
}
}
static NumberFormat nf = NumberFormat.getInstance();
// this is taken from JPivot
public Expr parseParameter(String value) {
// is it a String (enclose in double or single quotes ?
String trimmed = value.trim();
int len = trimmed.length();
if (trimmed.charAt(0) == '"' && trimmed.charAt(len - 1) == '"') {
CmdRunner.debug("parseParameter. STRING_TYPE: " +trimmed);
return new Expr(trimmed.substring(1, trimmed.length() - 1),
Expr.STRING_TYPE);
}
if (trimmed.charAt(0) == '\'' && trimmed.charAt(len - 1) == '\'') {
CmdRunner.debug("parseParameter. STRING_TYPE: " +trimmed);
return new Expr(trimmed.substring(1, trimmed.length() - 1),
Expr.STRING_TYPE);
}
// is it a Number ?
Number number = null;
try {
number = nf.parse(trimmed);
} catch (ParseException pex) {
// nothing to do, should be member
}
if (number != null) {
CmdRunner.debug("parseParameter. NUMERIC_TYPE: " +number);
return new Expr(number, Expr.NUMERIC_TYPE);
}
CmdRunner.debug("parseParameter. MEMBER_TYPE: " +trimmed);
Query query = this.connection.parseQuery(this.mdxCmd);
// dont have to execute
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?