Strabon

changeset 766:733b824d894f

Improved addLimit function to take into account some more cases.
author Stella Giannakopoulou <sgian@di.uoa.gr>
date Wed Dec 05 19:05:40 2012 +0200 (2012-12-05)
parents ca4221c776e2
children 3b0d9bcd9a1d
files endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/StrabonBeanWrapper.java
line diff
     1.1 --- a/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/StrabonBeanWrapper.java	Tue Dec 04 22:53:27 2012 +0200
     1.2 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/StrabonBeanWrapper.java	Wed Dec 05 19:05:40 2012 +0200
     1.3 @@ -350,6 +350,7 @@
     1.4  	 * */
     1.5  	public String addLimit(String queryString, String maxLimit){
     1.6  		String limitedQuery = queryString;
     1.7 +		String lowerLimit = null;
     1.8  		int max;
     1.9  		
    1.10  		if(maxLimit == null)
    1.11 @@ -360,22 +361,25 @@
    1.12  		if(max > 0)
    1.13  		{	
    1.14  			queryString = queryString.trim();		
    1.15 -			Pattern limitPattern = Pattern.compile(".*limit (\\d+)", Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
    1.16 +			Pattern limitPattern = Pattern.compile("limit(\\s*)(\\d+)(\\s*)(offset(\\s*)\\d+)?$", Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
    1.17  			Matcher limitMatcher = limitPattern.matcher(queryString);
    1.18 -						
    1.19 +			
    1.20  			// check whether the query contains a limit clause
    1.21 -			if(limitMatcher.matches())		
    1.22 -			{								
    1.23 -				Pattern rowsNumberPattern = Pattern.compile("\\d+$");				
    1.24 -				Matcher rowsNumberMatcher = rowsNumberPattern.matcher(queryString);
    1.25 -				rowsNumberMatcher.find();				
    1.26 +			if(limitMatcher.find())
    1.27 +			{					
    1.28 +				Pattern rowsNumberPattern = Pattern.compile("\\d+");
    1.29 +				Matcher rowsNumberMatcher = rowsNumberPattern.matcher(limitMatcher.group());
    1.30 +				rowsNumberMatcher.find();
    1.31  				
    1.32  				// if the initial limit is greater than the maximum, set it to the maximum
    1.33  				if(Integer.valueOf(rowsNumberMatcher.group()) > max)
    1.34 -					limitedQuery = rowsNumberMatcher.replaceAll(String.valueOf(max));			
    1.35 -			}	 
    1.36 +				{	
    1.37 +					lowerLimit = rowsNumberMatcher.replaceFirst(String.valueOf(max));					
    1.38 +					limitedQuery = limitMatcher.replaceFirst(lowerLimit); 					
    1.39 +				}								
    1.40 +			}	
    1.41  			else // add a limit to the query 
    1.42 -				limitedQuery = queryString+" limit "+max;		
    1.43 +				limitedQuery = queryString+" limit "+max;			
    1.44  		}
    1.45  		return limitedQuery;
    1.46  	}