📄 mquery.java
字号:
*/
public String getOperator (int index)
{
if (index < 0 || index >= m_list.size())
return null;
Restriction r = (Restriction)m_list.get(index);
return r.Operator;
} // getOperator
/**
* Get Restriction Display of index
* @param index index
* @return Restriction Display
*/
public String getInfoDisplay (int index)
{
if (index < 0 || index >= m_list.size())
return null;
Restriction r = (Restriction)m_list.get(index);
return r.InfoDisplay;
} // getOperator
/**
* Get TO Restriction Display of index
* @param index index
* @return Restriction Display
*/
public String getInfoDisplay_to (int index)
{
if (index < 0 || index >= m_list.size())
return null;
Restriction r = (Restriction)m_list.get(index);
return r.InfoDisplay_to;
} // getOperator
/**
* Get Info Name
* @param index index
* @return Info Name
*/
public String getInfoName(int index)
{
if (index < 0 || index >= m_list.size())
return null;
Restriction r = (Restriction)m_list.get(index);
return r.InfoName;
} // getInfoName
/**
* Get Info Operator
* @param index index
* @return info Operator
*/
public String getInfoOperator(int index)
{
if (index < 0 || index >= m_list.size())
return null;
Restriction r = (Restriction)m_list.get(index);
return r.getInfoOperator();
} // getInfoOperator
/**
* Get Display with optional To
* @param index index
* @return info display
*/
public String getInfoDisplayAll (int index)
{
if (index < 0 || index >= m_list.size())
return null;
Restriction r = (Restriction)m_list.get(index);
return r.getInfoDisplayAll();
} // getInfoDisplay
/**
* String representation
* @return info
*/
public String toString()
{
if (isActive())
return getWhereClause(true);
return "MQuery[" + m_TableName + "]";
} // toString
/**
* Clone Query
* @return Query
*/
public MQuery deepCopy()
{
MQuery newQuery = new MQuery(m_TableName);
for (int i = 0; i < m_list.size(); i++)
newQuery.addRestriction((Restriction)m_list.get(i));
return newQuery;
} // clone
/*************************************************************************/
/**
* Create simple Equal Query.
* Creates columnName=value or columnName='value'
* @param columnName columnName
* @param value value
* @return quary
*/
public static MQuery getEqualQuery (String columnName, Object value)
{
MQuery query = new MQuery();
query.addRestriction(columnName, EQUAL, value);
return query;
} // getEqualQuery
/**
* Create simple Equal Query.
* Creates columnName=value
* @param columnName columnName
* @param value value
* @return quary
*/
public static MQuery getEqualQuery (String columnName, int value)
{
MQuery query = new MQuery();
query.addRestriction(columnName, EQUAL, new Integer(value));
return query;
} // getEqualQuery
} // MQuery
/*****************************************************************************/
/**
* Query Restriction
*/
class Restriction implements Serializable
{
/**
* Restriction
* @param ColumnName ColumnName
* @param Operator Operator, e.g. = != ..
* @param Code Code, e.g 0, All%
* @param InfoName Display Name
* @param InfoDisplay Display of Code (Lookup)
*/
Restriction (String ColumnName, String Operator,
Object Code, String InfoName, String InfoDisplay)
{
this.ColumnName = ColumnName.trim();
if (InfoName != null)
this.InfoName = InfoName;
else
this.InfoName = this.ColumnName;
//
this.Operator = Operator;
// clean code
this.Code = Code;
if (this.Code instanceof String)
{
if (this.Code.toString().startsWith("'"))
this.Code = this.Code.toString().substring(1);
if (this.Code.toString().endsWith("'"))
this.Code = this.Code.toString().substring(0, this.Code.toString().length()-2);
}
if (InfoDisplay != null)
this.InfoDisplay = InfoDisplay.trim();
else if (this.Code != null)
this.InfoDisplay = this.Code.toString();
} // Restriction
/**
* Range Restriction (BETWEEN)
* @param ColumnName ColumnName
* @param Code Code, e.g 0, All%
* @param Code_to Code, e.g 0, All%
* @param InfoName Display Name
* @param InfoDisplay Display of Code (Lookup)
* @param InfoDisplay_to Display of Code (Lookup)
*/
Restriction (String ColumnName,
Object Code, Object Code_to,
String InfoName, String InfoDisplay, String InfoDisplay_to)
{
this (ColumnName, MQuery.BETWEEN, Code, InfoName, InfoDisplay);
// Code_to
this.Code_to = Code_to;
if (this.Code_to instanceof String)
{
if (this.Code_to.toString().startsWith("'"))
this.Code_to = this.Code_to.toString().substring(1);
if (this.Code_to.toString().endsWith("'"))
this.Code_to = this.Code_to.toString().substring(0, this.Code_to.toString().length()-2);
}
// InfoDisplay_to
if (InfoDisplay_to != null)
this.InfoDisplay_to = InfoDisplay_to.trim();
else if (this.Code_to != null)
this.InfoDisplay_to = this.Code_to.toString();
} // Restriction
/**
* Create Restriction with dircet WHERE clause
* @param whereClause SQL WHERE Clause
*/
Restriction (String whereClause)
{
DircetWhereClause = whereClause;
} // Restriction
/** Direct Where Clause */
protected String DircetWhereClause = null;
/** Column Name */
protected String ColumnName;
/** Name */
protected String InfoName;
/** Operator */
protected String Operator;
/** SQL Where Code */
protected Object Code;
/** Info */
protected String InfoDisplay;
/** SQL Where Code To */
protected Object Code_to;
/** Info To */
protected String InfoDisplay_to;
/** And/Or Condition */
protected boolean andCondition = true;
/**
* Return SQL construct for this restriction
* @param tableName optional table name
* @return SQL WHERE construct
*/
public String getSQL (String tableName)
{
if (DircetWhereClause != null)
return DircetWhereClause;
//
StringBuffer sb = new StringBuffer();
if (tableName != null && tableName.length() > 0)
{
// Assumes - REPLACE(INITCAP(variable),'s','X') or UPPER(variable)
int pos = ColumnName.lastIndexOf('(')+1; // including (
int end = ColumnName.indexOf(')');
// We have a Function in the ColumnName
if (pos != -1 && end != -1)
sb.append(ColumnName.substring(0, pos))
.append(tableName).append(".").append(ColumnName.substring(pos, end))
.append(ColumnName.substring(end));
else
sb.append(tableName).append(".").append(ColumnName);
}
else
sb.append(ColumnName);
//
sb.append(Operator);
if (Code instanceof String)
sb.append(DB.TO_STRING(Code.toString()));
else if (Code instanceof Timestamp)
sb.append(DB.TO_DATE((Timestamp)Code));
else
sb.append(Code);
// Between
// if (Code_to != null && InfoDisplay_to != null)
if (MQuery.BETWEEN.equals(Operator))
{
sb.append(" AND ");
if (Code_to instanceof String)
sb.append(DB.TO_STRING(Code_to.toString()));
else if (Code_to instanceof Timestamp)
sb.append(DB.TO_DATE((Timestamp)Code_to));
else
sb.append(Code_to);
}
return sb.toString();
} // getSQL
/**
* Get String Representation
* @return info
*/
public String toString()
{
return getSQL(null);
} // toString
/**
* Get Info Name
* @return Info Name
*/
public String getInfoName()
{
return InfoName;
} // getInfoName
/**
* Get Info Operator
* @return info Operator
*/
public String getInfoOperator()
{
for (int i = 0; i < MQuery.OPERATORS.length; i++)
{
if (MQuery.OPERATORS[i].getValue().equals(Operator))
return MQuery.OPERATORS[i].getName();
}
return Operator;
} // getInfoOperator
/**
* Get Display with optional To
* @return info display
*/
public String getInfoDisplayAll()
{
if (InfoDisplay_to == null)
return InfoDisplay;
StringBuffer sb = new StringBuffer(InfoDisplay);
sb.append(" - ").append(InfoDisplay_to);
return sb.toString();
} // getInfoDisplay
} // Restriction
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -