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