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 027/** 028 * 029 * @author Alisson Gomes 030 * @since 0.6.0 031 */ 032public class ParamParserQuestionMark extends AbstractParamParser 033{ 034 private static final Pattern PATTERN_PARAMS = Pattern.compile(REGEX_QUESTION_MARK, 035 Pattern.MULTILINE | Pattern.CASE_INSENSITIVE); 036 037 private static ParamParser INSTANCE = new ParamParserQuestionMark(); 038 039 private ParamParserQuestionMark() 040 { 041 //super(REGEX_QUESTION_MARK); 042 } 043 044 public static ParamParser getInstance() 045 { 046 return INSTANCE; 047 } 048 049 @Override 050 Pattern getPatternParams() 051 { 052 return PATTERN_PARAMS; 053 } 054 055 @Override 056 public String[] find(String query) 057 { 058 return parserQuestion(query).toArray(new String[0]); 059 } 060 061 @Override 062 public ParamMarkType getType() 063 { 064 return ParamMarkType.QUESTION; 065 } 066 067 private List<String> parserQuestion(final String sentence) 068 { 069 List<String> params = new ArrayList<String>(); 070 Matcher matcherQuestion = PATTERN_PARAMS.matcher(sentence); 071 int i = 0; 072 while (matcherQuestion.find()) 073 { 074 if (matcherQuestion.group().startsWith("'")) 075 { 076 continue; 077 } 078 else if (matcherQuestion.group().startsWith(":in:")) 079 { 080 params.add(i++, sentence.subSequence(matcherQuestion.start() + 1, matcherQuestion.end()).toString()); 081 continue; 082 } 083 //System.out.printf("group[%s] [%s]\n", matcherQuestion.group(), sentence.subSequence(matcherQuestion.start() + 1, matcherQuestion.end()).toString()); 084 params.add(i++, sentence.subSequence(matcherQuestion.start(), matcherQuestion.end()).toString()); 085 } 086 return params; 087 } 088}