Skip to content

Commit

Permalink
Use LEN for String.length instead of CHAR_LENGTH for SQLServer. Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
andyjefferson committed Nov 28, 2016
1 parent f57f6cc commit 7f7fa34
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
@@ -0,0 +1,61 @@
/**********************************************************************
Copyright (c) 2016 Andy Jefferson and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Contributors:
...
**********************************************************************/
package org.datanucleus.store.rdbms.sql.method;

import java.util.ArrayList;
import java.util.List;

import org.datanucleus.exceptions.NucleusException;
import org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping;
import org.datanucleus.store.rdbms.sql.expression.IntegerLiteral;
import org.datanucleus.store.rdbms.sql.expression.NumericExpression;
import org.datanucleus.store.rdbms.sql.expression.ParameterLiteral;
import org.datanucleus.store.rdbms.sql.expression.SQLExpression;
import org.datanucleus.store.rdbms.sql.expression.StringExpression;
import org.datanucleus.store.rdbms.sql.expression.StringLiteral;
import org.datanucleus.util.Localiser;

/**
* Expression handler to evaluate {stringExpression}.length().
* Returns a NumericExpression <pre>LEN({stringExpr})</pre>.
*/
public class StringLengthMethod4 extends AbstractSQLMethod
{
/* (non-Javadoc)
* @see org.datanucleus.store.rdbms.sql.method.SQLMethod#getExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression, java.util.List)
*/
public SQLExpression getExpression(SQLExpression expr, List<SQLExpression> args)
{
if (!expr.isParameter() && expr instanceof StringLiteral)
{
JavaTypeMapping m = exprFactory.getMappingForType(int.class, false);
String val = (String)((StringLiteral)expr).getValue();
return new IntegerLiteral(stmt, m, Integer.valueOf(val.length()), null);
}
else if (expr instanceof StringExpression || expr instanceof ParameterLiteral)
{
ArrayList funcArgs = new ArrayList();
funcArgs.add(expr);
return new NumericExpression(stmt, getMappingForClass(int.class), "LEN", funcArgs);
}
else
{
throw new NucleusException(Localiser.msg("060001", "length", expr));
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/plugin.xml
Expand Up @@ -900,6 +900,7 @@ Contributors:
<sql-method class="java.lang.String" method="length" datastore="oracle" evaluator="org.datanucleus.store.rdbms.sql.method.StringLength3Method"/>
<sql-method class="java.lang.String" method="length" datastore="sapdb" evaluator="org.datanucleus.store.rdbms.sql.method.StringLength3Method"/>
<sql-method class="java.lang.String" method="length" datastore="sqlite" evaluator="org.datanucleus.store.rdbms.sql.method.StringLength3Method"/>
<sql-method class="java.lang.String" method="length" datastore="sqlserver" evaluator="org.datanucleus.store.rdbms.sql.method.StringLength4Method"/>
<sql-method class="java.lang.String" method="matches" evaluator="org.datanucleus.store.rdbms.sql.method.StringMatchesMethod"/>
<sql-method class="java.lang.String" method="matches" datastore="derby" evaluator="org.datanucleus.store.rdbms.sql.method.StringMatchesDerbyMethod"/>
<sql-method class="java.lang.String" method="replaceAll" evaluator="org.datanucleus.store.rdbms.sql.method.StringReplaceAllMethod"/>
Expand Down

3 comments on commit 7f7fa34

@beikov
Copy link
Contributor

@beikov beikov commented on 7f7fa34 Nov 28, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you watching my build output? 😆
I just wanted to report that issue. Are you testing MSSQL support currently? I got some other issues if you want. Just tell me and I'll write up an email or create separate issues if you want.

@andyjefferson
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. Not aware of your build output, where is it?
I had it reported a couple of weeks ago by a user, but haven't been able to do it til this weekend. Not aware of anything else on SQLServer so if you have problems, report them. But be aware that I don't have that database so cannot test it ;-)

@beikov
Copy link
Contributor

@beikov beikov commented on 7f7fa34 Nov 28, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well you should try out the linux docker image for SQL Server 2016. Good enough for testing.
I will create issues but here is the output if you are interested: https://travis-ci.org/Blazebit/blaze-persistence/jobs/179432801

Please sign in to comment.