HHH-3216 : ParameterParser + escaped function call syntax
git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@15200 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
parent
8390813565
commit
b20825d1c9
|
@ -46,8 +46,10 @@ public class ParameterParser {
|
||||||
public void other(char character);
|
public void other(char character);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Direct instantiation of ParameterParser disallowed.
|
||||||
|
*/
|
||||||
private ParameterParser() {
|
private ParameterParser() {
|
||||||
// disallow instantiation
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -60,12 +62,10 @@ public class ParameterParser {
|
||||||
*
|
*
|
||||||
* @param sqlString The string to be parsed/tokenized.
|
* @param sqlString The string to be parsed/tokenized.
|
||||||
* @param recognizer The thing which handles recognition events.
|
* @param recognizer The thing which handles recognition events.
|
||||||
* @throws QueryException
|
* @throws QueryException Indicates unexpected parameter conditions.
|
||||||
*/
|
*/
|
||||||
public static void parse(String sqlString, Recognizer recognizer) throws QueryException {
|
public static void parse(String sqlString, Recognizer recognizer) throws QueryException {
|
||||||
boolean hasMainOutputParameter = sqlString.indexOf( "call" ) > 0 &&
|
boolean hasMainOutputParameter = startsWithEscapeCallTemplate( sqlString );
|
||||||
sqlString.indexOf( "?" ) < sqlString.indexOf( "call" ) &&
|
|
||||||
sqlString.indexOf( "=" ) < sqlString.indexOf( "call" );
|
|
||||||
boolean foundMainOutputParam = false;
|
boolean foundMainOutputParam = false;
|
||||||
|
|
||||||
int stringLength = sqlString.length();
|
int stringLength = sqlString.length();
|
||||||
|
@ -89,8 +89,9 @@ public class ParameterParser {
|
||||||
int chopLocation = right < 0 ? sqlString.length() : right;
|
int chopLocation = right < 0 ? sqlString.length() : right;
|
||||||
String param = sqlString.substring( indx + 1, chopLocation );
|
String param = sqlString.substring( indx + 1, chopLocation );
|
||||||
if ( StringHelper.isEmpty( param ) ) {
|
if ( StringHelper.isEmpty( param ) ) {
|
||||||
throw new QueryException("Space is not allowed after parameter prefix ':' '"
|
throw new QueryException(
|
||||||
+ sqlString + "'");
|
"Space is not allowed after parameter prefix ':' [" + sqlString + "]"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
recognizer.namedParameter( param, indx );
|
recognizer.namedParameter( param, indx );
|
||||||
indx = chopLocation - 1;
|
indx = chopLocation - 1;
|
||||||
|
@ -129,4 +130,34 @@ public class ParameterParser {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean startsWithEscapeCallTemplate(String sqlString) {
|
||||||
|
if ( ! ( sqlString.startsWith( "{" ) && sqlString.endsWith( "}" ) ) ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int chopLocation = sqlString.indexOf( "call" );
|
||||||
|
if ( chopLocation <= 0 ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
final String checkString = sqlString.substring( 1, chopLocation + 4 );
|
||||||
|
final String fixture = "?=call";
|
||||||
|
int fixturePosition = 0;
|
||||||
|
boolean matches = true;
|
||||||
|
for ( int i = 0, max = checkString.length(); i < max; i++ ) {
|
||||||
|
final char c = Character.toLowerCase( checkString.charAt( i ) );
|
||||||
|
if ( Character.isWhitespace( c ) ) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if ( c == fixture.charAt( fixturePosition ) ) {
|
||||||
|
fixturePosition++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
matches = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return matches;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
/*
|
||||||
|
* Hibernate, Relational Persistence for Idiomatic Java
|
||||||
|
*
|
||||||
|
* Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
|
||||||
|
* indicated by the @author tags or express copyright attribution
|
||||||
|
* statements applied by the authors. All third-party contributions are
|
||||||
|
* distributed under license by Red Hat Middleware LLC.
|
||||||
|
*
|
||||||
|
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||||
|
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||||
|
* Lesser General Public License, as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||||
|
* for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
|
* along with this distribution; if not, write to:
|
||||||
|
* Free Software Foundation, Inc.
|
||||||
|
* 51 Franklin Street, Fifth Floor
|
||||||
|
* Boston, MA 02110-1301 USA
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.hibernate.engine.query;
|
||||||
|
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unit tests of the ParameterParser class
|
||||||
|
*
|
||||||
|
* @author Steve Ebersole
|
||||||
|
*/
|
||||||
|
public class ParameterParserTest extends TestCase {
|
||||||
|
public void testEscapeCallRecognition() {
|
||||||
|
assertTrue( ParameterParser.startsWithEscapeCallTemplate( "{ ? = call abc(?) }" ) );
|
||||||
|
assertFalse( ParameterParser.startsWithEscapeCallTemplate( "from User u where u.userName = ? and u.userType = 'call'" ) );
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue