001: package net.sourceforge.squirrel_sql.plugins.mysql.tokenizer;
002:
003: /*
004: * Copyright (C) 2007 Rob Manning
005: * manningr@users.sourceforge.net
006: *
007: * Based on initial work from Johan Compagner.
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022: */
023: import java.util.ArrayList;
024: import java.util.Iterator;
025: import java.util.regex.Pattern;
026:
027: import net.sourceforge.squirrel_sql.fw.preferences.IQueryTokenizerPreferenceBean;
028: import net.sourceforge.squirrel_sql.fw.sql.IQueryTokenizer;
029: import net.sourceforge.squirrel_sql.fw.sql.ITokenizerFactory;
030: import net.sourceforge.squirrel_sql.fw.sql.QueryTokenizer;
031: import net.sourceforge.squirrel_sql.fw.util.StringUtilities;
032: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
033: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
034:
035: /**
036: * This class is loaded by the MySQL Plugin and registered with all MySQL
037: * Sessions as the query tokenizer if the plugin is loaded. It handles some
038: * of the syntax allowed in MySQL scripts that would be hard to parse in a
039: * generic way for any database. It handles create statements for stored
040: * procedures, triggers, and functions. It can also
041: * handle "/" as the statement terminator in leiu of or in addition to the
042: * default statement separator which is ";".
043: *
044: * @author manningr
045: */
046: public class MysqlQueryTokenizer extends QueryTokenizer implements
047: IQueryTokenizer {
048: /** Logger for this class. */
049: private final static ILogger s_log = LoggerController
050: .createLogger(MysqlQueryTokenizer.class);
051:
052: private static final String PROCEDURE_PATTERN = "^\\s*CREATE\\s+PROCEDURE.*";
053:
054: private static final String FUNCTION_PATTERN = "^\\s*CREATE\\s+FUNCTION.*";
055:
056: private static final String TRIGGER_PATTERN = "^\\s*CREATE\\s+TRIGGER.*";
057:
058: private Pattern procPattern = Pattern.compile(PROCEDURE_PATTERN,
059: Pattern.DOTALL);
060:
061: private Pattern funcPattern = Pattern.compile(FUNCTION_PATTERN,
062: Pattern.DOTALL);
063:
064: private Pattern triggerPattern = Pattern.compile(TRIGGER_PATTERN,
065: Pattern.DOTALL);
066:
067: private IQueryTokenizerPreferenceBean _prefs = null;
068:
069: public MysqlQueryTokenizer(IQueryTokenizerPreferenceBean prefs) {
070: super (prefs.getStatementSeparator(), prefs.getLineComment(),
071: prefs.isRemoveMultiLineComments());
072: _prefs = prefs;
073: }
074:
075: public void setScriptToTokenize(String script) {
076: super .setScriptToTokenize(script);
077:
078: // Since it is likely to have the procedure separator on it's own line,
079: // and it is key to letting us know that proceeding statements form a
080: // multi-statement procedure or function, it deserves it's own place in
081: // the _queries arraylist. If it is followed by other procedure or
082: // function creation blocks, we may fail to detect that, so this just
083: // goes through the list and breaks apart statements on newline so that
084: // this cannot happen.
085: breakApartNewLines();
086:
087: // MySQL allows statement separators in procedure blocks. The process
088: // of tokenizing above renders these procedure blocks as separate
089: // statements, which are not valid to be executed separately. Here, we
090: // re-assemble any create procedure/function/trigger statements that we
091: // find using the beginning procedure block pattern and the procedure
092: // separator.
093: joinFragments(procPattern, false);
094: joinFragments(funcPattern, false);
095: joinFragments(triggerPattern, false);
096:
097: _queryIterator = _queries.iterator();
098: }
099:
100: /**
101: * Sets the ITokenizerFactory which is used to create additional instances
102: * of the IQueryTokenizer - this is used for handling file includes
103: * recursively.
104: */
105: protected void setFactory() {
106: _tokenizerFactory = new ITokenizerFactory() {
107: public IQueryTokenizer getTokenizer() {
108: return new MysqlQueryTokenizer(_prefs);
109: }
110: };
111: }
112:
113: /**
114: * This will loop through _queries and break apart lines that look like
115: *
116: * <sep>\n\ncreate proc...
117: * into
118: *
119: * <sep>
120: * create proc...
121: */
122: private void breakApartNewLines() {
123: ArrayList<String> tmp = new ArrayList<String>();
124: String procSep = _prefs.getProcedureSeparator();
125: for (Iterator<String> iter = _queries.iterator(); iter
126: .hasNext();) {
127: String next = iter.next();
128: if (next.startsWith(procSep)) {
129: tmp.add(procSep);
130: String[] parts = next.split(procSep + "\\n+");
131: for (int i = 0; i < parts.length; i++) {
132: if (!"".equals(parts[i])
133: && !procSep.equals(parts[i])) {
134: tmp.add(parts[i]);
135: }
136: }
137: } else if (next.endsWith(procSep)) {
138: String chopped = StringUtilities.chop(next);
139: tmp.add(chopped);
140: tmp.add(procSep);
141: } else if (next.indexOf(procSep) != -1) {
142: String[] parts = next.split("\\" + procSep);
143: for (int i = 0; i < parts.length; i++) {
144: tmp.add(parts[i]);
145: if (i < parts.length - 1) {
146: tmp.add(procSep);
147: }
148: }
149: } else {
150: tmp.add(next);
151: }
152: }
153: _queries = tmp;
154: }
155:
156: /**
157: * This will scan the _queries list looking for fragments matching the
158: * specified pattern and will combine successive fragments until the
159: * procedure separator is found, indicating the end of the code block.
160: *
161: * @param skipStraySep if we find a procedure separator before matching a
162: * pattern and this is true, we will exclude it from
163: * our list of sql queries.
164: */
165: private void joinFragments(Pattern pattern, boolean skipStraySep) {
166:
167: boolean inMultiSQLStatement = false;
168: StringBuilder collector = null;
169: ArrayList<String> tmp = new ArrayList<String>();
170: String procSep = _prefs.getProcedureSeparator();
171: String stmtSep = _prefs.getStatementSeparator();
172: for (Iterator<String> iter = _queries.iterator(); iter
173: .hasNext();) {
174: String next = iter.next();
175:
176: // DELIMITER sets the separator that tells us when a procedure.
177: // This is MySQL-specific
178: if (next.startsWith("DELIMITER")) {
179: String[] parts = StringUtilities.split(next, ' ', true);
180: if (parts.length == 2) {
181: procSep = parts[1];
182: } else {
183: s_log.error("Found DELIMITER keyword, followed by "
184: + (parts.length - 1)
185: + " elements; expected only one: " + next
186: + "\nSkipping DELIMITER directive.");
187: }
188: }
189:
190: if (pattern.matcher(next.toUpperCase()).matches()) {
191: inMultiSQLStatement = true;
192: collector = new StringBuilder(next);
193: collector.append(stmtSep);
194: continue;
195: }
196: if (next.startsWith(procSep)) {
197: inMultiSQLStatement = false;
198: if (collector != null) {
199: tmp.add(collector.toString());
200: collector = null;
201: } else {
202: if (skipStraySep) {
203: // Stray sep - or we failed to find pattern
204: if (s_log.isDebugEnabled()) {
205: s_log
206: .debug("Detected stray proc separator("
207: + procSep + "). Skipping");
208: }
209: } else {
210: tmp.add(next);
211: }
212: }
213: continue;
214: }
215: if (inMultiSQLStatement) {
216: collector.append(next);
217: collector.append(stmtSep);
218: continue;
219: }
220: tmp.add(next);
221: }
222: // We got to the end of the script without finding a proc separator.
223: // Just add it as if we had.
224: if (collector != null && inMultiSQLStatement) {
225: tmp.add(collector.toString());
226: }
227: _queries = tmp;
228: }
229: }
|