01: /*
02: * Copyright (C) 2005 Rob Manning
03: * manningr@users.sourceforge.net
04: *
05: * This program is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU General Public License
07: * as published by the Free Software Foundation; either version 2
08: * of the License, or any later version.
09: *
10: * This program is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13: * GNU General Public License for more details.
14: *
15: * You should have received a copy of the GNU General Public License
16: * along with this program; if not, write to the Free Software
17: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18: */
19: package net.sourceforge.squirrel_sql.plugins.dbcopy;
20:
21: import net.sourceforge.squirrel_sql.client.session.ISession;
22: import net.sourceforge.squirrel_sql.plugins.dbcopy.event.CopyEvent;
23: import net.sourceforge.squirrel_sql.plugins.dbcopy.event.CopyTableAdaptor;
24: import net.sourceforge.squirrel_sql.plugins.dbcopy.event.CopyTableListener;
25: import net.sourceforge.squirrel_sql.plugins.dbcopy.event.StatementEvent;
26: import net.sourceforge.squirrel_sql.plugins.dbcopy.util.ScriptWriter;
27:
28: /**
29: * This class listens for copy table events that represent SQL statements that
30: * should be recorded in the script file if so configured.
31: */
32: public class CopyScripter extends CopyTableAdaptor implements
33: CopyTableListener {
34:
35: /* (non-Javadoc)
36: * @see net.sourceforge.squirrel_sql.plugins.dbcopy.event.CopyTableListener#copyStarted(net.sourceforge.squirrel_sql.plugins.dbcopy.event.CopyEvent)
37: */
38: public void copyStarted(CopyEvent e) {
39: initializeScript(e.getSessionInfoProvider());
40: }
41:
42: /* (non-Javadoc)
43: * @see net.sourceforge.squirrel_sql.plugins.dbcopy.event.CopyTableListener#statementExecuted(net.sourceforge.squirrel_sql.plugins.dbcopy.event.StatementEvent)
44: */
45: public void statementExecuted(StatementEvent e) {
46: String sql = e.getStatement();
47: if (e.getStatementType() == StatementEvent.INSERT_RECORD_TYPE) {
48: String[] values = e.getBindValues();
49: ScriptWriter.write(sql, values);
50: } else {
51: ScriptWriter.write(sql);
52: }
53: }
54:
55: /* (non-Javadoc)
56: * @see net.sourceforge.squirrel_sql.plugins.dbcopy.event.CopyTableListener#copyFinished(int)
57: */
58: public void copyFinished(int seconds) {
59: finalizeScript();
60: }
61:
62: /**
63: * Open the script.
64: * @param prov
65: */
66: private void initializeScript(SessionInfoProvider prov) {
67: ISession source = prov.getCopySourceSession();
68: ISession dest = prov.getCopyDestSession();
69: ScriptWriter.open(source, dest);
70: }
71:
72: /**
73: * close the script.
74: */
75: private void finalizeScript() {
76: ScriptWriter.close();
77: }
78: }
|