001/* 
002 * JKNIV, SQLegance keeping queries maintainable.
003 * 
004 * Copyright (C) 2017, the original author or authors.
005 *
006 * This library is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 2.1 of the License.
010 * 
011 * This library is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 * 
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with this library; if not, write to the Free Software Foundation, Inc., 
018 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
019 */
020package net.sf.jkniv.sqlegance.params;
021
022import java.util.ArrayList;
023import java.util.List;
024import java.util.regex.Matcher;
025import java.util.regex.Pattern;
026
027public class ParamParserColonMark extends AbstractParamParser
028{
029    private static final Pattern PATTERN_PARAMS = Pattern.compile(REGEX_COLON_MARK, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
030    private static final ParamParser INSTANCE = new ParamParserColonMark();
031    
032    private ParamParserColonMark()
033    {
034        //super(REGEX_COLON_MARK);
035    }
036
037    public static ParamParser getInstance()
038    {
039        return INSTANCE;
040    }
041    
042    @Override
043    Pattern getPatternParams()
044    {
045        return PATTERN_PARAMS;
046    }
047
048    @Override
049    public String[] find(String query)
050    {
051        return parserColon(query).toArray(new String[0]);
052    }
053    
054    @Override
055    public ParamMarkType getType()
056    {
057        return ParamMarkType.COLON;
058    }
059    
060    private List<String> parserColon(final String sentence)
061    {
062        List<String> params = new ArrayList<String>();
063        Matcher matcherTwoDots = PATTERN_PARAMS.matcher(sentence);
064        int i = 0;
065        while (matcherTwoDots.find())
066        {
067            if (matcherTwoDots.group().startsWith("'"))
068            {
069                continue;
070            }
071            else if (matcherTwoDots.group().startsWith(":in:"))
072            {
073                params.add(i++, sentence.subSequence(matcherTwoDots.start() + 1, matcherTwoDots.end()).toString());
074                continue;
075            }
076            //System.out.printf("group[%s] [%s]\n", matcherTwoDots.group(), sentence.subSequence(matcherTwoDots.start() + 1, matcherTwoDots.end()).toString());
077            params.add(i++, sentence.subSequence(matcherTwoDots.start() + 1, matcherTwoDots.end()).toString());
078        }
079        
080        return params;
081    }   
082}