001: /*
002: * Copyright (C) 2005 Rob Manning
003: * manningr@users.sourceforge.net
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package net.sourceforge.squirrel_sql.plugins.dbcopy.util;
020:
021: import java.io.File;
022: import java.io.FileOutputStream;
023: import java.io.PrintWriter;
024:
025: import net.sourceforge.squirrel_sql.client.session.ISession;
026: import net.sourceforge.squirrel_sql.client.util.ApplicationFiles;
027: import net.sourceforge.squirrel_sql.fw.util.StringManager;
028: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
029: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
030: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
031: import net.sourceforge.squirrel_sql.plugins.dbcopy.prefs.DBCopyPreferenceBean;
032: import net.sourceforge.squirrel_sql.plugins.dbcopy.prefs.PreferencesManager;
033:
034: /**
035: * This class accomplishes the task of saving a database copy session (all the
036: * sql statements that were executed) to a file, and for debugging to the
037: * squirrel log file.
038: */
039: public class ScriptWriter {
040:
041: private final static ILogger s_log = LoggerController
042: .createLogger(ScriptWriter.class);
043:
044: /** local logger that writes to a file */
045: private static PrintWriter out = null;
046:
047: private static String scriptsDirName = null;
048:
049: private static DBCopyPreferenceBean prefs = PreferencesManager
050: .getPreferences();
051:
052: /** Internationalized strings for this class */
053: private static final StringManager s_stringMgr = StringManagerFactory
054: .getStringManager(ScriptWriter.class);
055:
056: /**
057: * Writes the specified stmt to the script file.
058: * @param stmt
059: */
060: public static void write(String stmt) {
061: if (!prefs.isWriteScript() || out == null) {
062: return;
063: }
064: out.println(stmt);
065: }
066:
067: public static void write(String pstmt, String[] bindVars) {
068: if (!prefs.isWriteScript()) {
069: return;
070: }
071: write(getBoundStatement(pstmt, bindVars));
072: }
073:
074: public static String getBoundStatement(String pstmt,
075: String[] bindVars) {
076: String[] parts = pstmt.split("\\?");
077: StringBuffer result = new StringBuffer(parts[0]);
078: for (int i = 0; i < bindVars.length; i++) {
079: String nextVal = bindVars[i];
080:
081: try {
082: if (nextVal != null) {
083: Double.parseDouble(nextVal);
084: } else {
085: nextVal = "null";
086: }
087: } catch (NumberFormatException e) {
088: nextVal = "'" + nextVal + "'";
089: }
090:
091: // replace the next question mark with nextVal
092: result.append(nextVal);
093: result.append(parts[i + 1]);
094: }
095: return result.toString();
096: }
097:
098: public static void open(ISession source, ISession dest) {
099: if (!prefs.isWriteScript()) {
100: return;
101: }
102: try {
103: setupScriptsDir();
104: if (scriptsDirName == null) {
105: return;
106: }
107: String filename = constructFilename(source, dest);
108: File f = new File(filename);
109: if (f.exists()) {
110: f.delete();
111: }
112: out = new PrintWriter(new FileOutputStream(filename));
113: } catch (Exception e) {
114: s_log.error("", e);
115: }
116: }
117:
118: public static void close() {
119: if (!prefs.isWriteScript() || out == null) {
120: return;
121: }
122: out.close();
123: }
124:
125: private static String constructFilename(ISession source,
126: ISession dest) {
127: StringBuffer result = new StringBuffer(scriptsDirName);
128: result.append(File.separator);
129: result.append(source.getAlias().getUserName());
130: result.append("_to_");
131: result.append(dest.getAlias().getUserName());
132: result.append(".sql");
133: return result.toString();
134: }
135:
136: private static void setupScriptsDir() {
137: String userHomeDir = System.getProperty("user.home");
138: if (userHomeDir != null) {
139: StringBuffer scriptsDir = new StringBuffer();
140: ApplicationFiles appFiles = new ApplicationFiles();
141: // user.home/.squirrel-sql/plugins
142: scriptsDir.append(appFiles
143: .getPluginsUserSettingsDirectory());
144: scriptsDir.append(File.separator);
145: // user.home/.squirrel-sql/plugins/dbcopy
146: scriptsDir.append("dbcopy");
147: if (mkdir(scriptsDir.toString())) {
148: scriptsDir.append(File.separator);
149: // user.home/.squirrel-sql/plugins/dbcopy/scripts
150: scriptsDir.append("scripts");
151: if (mkdir(scriptsDir.toString())) {
152: scriptsDirName = scriptsDir.toString();
153: }
154: }
155: } else {
156: //i18n[ScriptWriter.error.nouserhome=The System property <user.home>
157: // is null. Cannot determine where to write DBCopy SQL scripts.
158: // Disabling writing SQL scripts for DBCopy operations.]
159: String msg = s_stringMgr
160: .getString("ScriptWriter.error.nouserhome");
161: s_log.error(msg);
162: scriptsDirName = null;
163: }
164: }
165:
166: private static boolean mkdir(String directory) {
167: boolean result = true;
168: File f = new File(directory);
169: if (!f.exists()) {
170: if (!f.mkdir()) {
171: result = false;
172: }
173: } else {
174: if (!f.isDirectory()) {
175: result = false;
176: }
177: }
178: return result;
179: }
180: }
|