提交 e0ffeed0 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Do not convert standard TRIM function to non-standard functions

上级 5fe7ec4c
...@@ -4509,7 +4509,7 @@ RTRIM(NAME) ...@@ -4509,7 +4509,7 @@ RTRIM(NAME)
" "
"Functions (String)","TRIM"," "Functions (String)","TRIM","
TRIM ( [ { LEADING | TRAILING | BOTH } [ string ] FROM ] string ) TRIM ( [ [ LEADING | TRAILING | BOTH ] [ string ] FROM ] string )
"," ","
Removes all leading spaces, trailing spaces, or spaces at both ends, from a string. Removes all leading spaces, trailing spaces, or spaces at both ends, from a string.
Other characters can be removed as well. Other characters can be removed as well.
......
...@@ -3582,32 +3582,40 @@ public class Parser { ...@@ -3582,32 +3582,40 @@ public class Parser {
break; break;
} }
case Function.TRIM: { case Function.TRIM: {
Expression space = null; int flags;
boolean needFrom = false;
if (readIf("LEADING")) { if (readIf("LEADING")) {
function = Function.getFunction(database, "LTRIM"); flags = Function.TRIM_LEADING;
if (!readIf(FROM)) { needFrom = true;
space = readExpression();
read(FROM);
}
} else if (readIf("TRAILING")) { } else if (readIf("TRAILING")) {
function = Function.getFunction(database, "RTRIM"); flags = Function.TRIM_TRAILING;
if (!readIf(FROM)) { needFrom = true;
space = readExpression(); } else {
read(FROM); needFrom = readIf("BOTH");
flags = Function.TRIM_LEADING | Function.TRIM_TRAILING;
} }
} else if (readIf("BOTH")) { Expression p0, space = null;
function.setFlags(flags);
if (needFrom) {
if (!readIf(FROM)) { if (!readIf(FROM)) {
space = readExpression(); space = readExpression();
read(FROM); read(FROM);
} }
} p0 = readExpression();
Expression p0 = readExpression(); } else {
if (readIf(COMMA)) { if (readIf(FROM)) {
space = readExpression(); p0 = readExpression();
} else if (readIf(FROM)) { } else {
p0 = readExpression();
if (readIf(FROM)) {
space = p0; space = p0;
p0 = readExpression(); p0 = readExpression();
} }
}
}
if (!needFrom && space == null && readIf(COMMA)) {
space = readExpression();
}
function.setParameter(0, p0); function.setParameter(0, p0);
if (space != null) { if (space != null) {
function.setParameter(1, space); function.setParameter(1, space);
......
...@@ -153,6 +153,16 @@ public class Function extends Expression implements FunctionCall { ...@@ -153,6 +153,16 @@ public class Function extends Expression implements FunctionCall {
*/ */
public static final int H2VERSION = 231; public static final int H2VERSION = 231;
/**
* The flags for TRIM(LEADING ...) function.
*/
public static final int TRIM_LEADING = 1;
/**
* The flags for TRIM(TRAILING ...) function.
*/
public static final int TRIM_TRAILING = 2;
protected static final int VAR_ARGS = -1; protected static final int VAR_ARGS = -1;
private static final HashMap<String, FunctionInfo> FUNCTIONS = new HashMap<>(256); private static final HashMap<String, FunctionInfo> FUNCTIONS = new HashMap<>(256);
...@@ -162,6 +172,7 @@ public class Function extends Expression implements FunctionCall { ...@@ -162,6 +172,7 @@ public class Function extends Expression implements FunctionCall {
protected final FunctionInfo info; protected final FunctionInfo info;
private ArrayList<Expression> varArgs; private ArrayList<Expression> varArgs;
private int flags;
protected TypeInfo type; protected TypeInfo type;
private final Database database; private final Database database;
...@@ -573,6 +584,24 @@ public class Function extends Expression implements FunctionCall { ...@@ -573,6 +584,24 @@ public class Function extends Expression implements FunctionCall {
} }
} }
/**
* Set the flags for this function.
*
* @param flags the flags to set
*/
public void setFlags(int flags) {
this.flags = flags;
}
/**
* Returns the flags.
*
* @return the flags
*/
public int getFlags() {
return flags;
}
@Override @Override
public Value getValue(Session session) { public Value getValue(Session session) {
return getValueWithArgs(session, args); return getValueWithArgs(session, args);
...@@ -1343,7 +1372,7 @@ public class Function extends Expression implements FunctionCall { ...@@ -1343,7 +1372,7 @@ public class Function extends Expression implements FunctionCall {
break; break;
case TRIM: case TRIM:
result = ValueString.get(StringUtils.trim(v0.getString(), result = ValueString.get(StringUtils.trim(v0.getString(),
true, true, v1 == null ? " " : v1.getString()), (flags & TRIM_LEADING) != 0, (flags & TRIM_TRAILING) != 0, v1 == null ? " " : v1.getString()),
database.getMode().treatEmptyStringsAsNull); database.getMode().treatEmptyStringsAsNull);
break; break;
case RTRIM: case RTRIM:
...@@ -2602,6 +2631,21 @@ public class Function extends Expression implements FunctionCall { ...@@ -2602,6 +2631,21 @@ public class Function extends Expression implements FunctionCall {
builder.append('('); builder.append('(');
} }
switch (info.type) { switch (info.type) {
case TRIM: {
switch (flags) {
case TRIM_LEADING:
builder.append("LEADING ");
break;
case TRIM_TRAILING:
builder.append("TRAILING ");
break;
}
if (args.length > 1) {
args[1].getSQL(builder, alwaysQuote).append(" FROM ");
}
args[0].getSQL(builder, alwaysQuote);
break;
}
case CAST: { case CAST: {
args[0].getSQL(builder, alwaysQuote).append(" AS ").append(new Column(null, type).getCreateSQL()); args[0].getSQL(builder, alwaysQuote).append(" AS ").append(new Column(null, type).getCreateSQL());
break; break;
......
...@@ -3,14 +3,23 @@ ...@@ -3,14 +3,23 @@
-- Initial Developer: H2 Group -- Initial Developer: H2 Group
-- --
create memory table test(id int primary key, name varchar(255)); CREATE TABLE TEST(ID INT PRIMARY KEY, A VARCHAR, B VARCHAR, C VARCHAR) AS VALUES (1, '__A__', ' B ', 'xAx');
> ok > ok
insert into test values(1, 'Hello'); SELECT TRIM(BOTH '_' FROM A), '|' || TRIM(LEADING FROM B) || '|', TRIM(TRAILING 'x' FROM C) FROM TEST;
> update count: 1 > TRIM('_' FROM A) ('|' || TRIM(LEADING B)) || '|' TRIM(TRAILING 'x' FROM C)
> ---------------- ------------------------------- -------------------------
> A |B | xA
> rows: 1
select TRIM(BOTH '_' FROM '__A__') A, TRIM(LEADING FROM ' B ') BS, TRIM(TRAILING 'x' FROM 'xAx') XA from test; SELECT LENGTH(TRIM(B)), LENGTH(TRIM(FROM B)) FROM TEST;
> A BS XA > LENGTH(TRIM(B)) LENGTH(TRIM(B))
> - -- -- > --------------- ---------------
> A B xA > 1 1
> rows: 1 > rows: 1
SELECT TRIM(BOTH B) FROM TEST;
> exception SYNTAX_ERROR_2
DROP TABLE TEST;
> ok
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论