001: /*
002: * Copyright (C) 2006 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;
020:
021: import java.sql.SQLException;
022: import java.util.ArrayList;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.ResourceBundle;
026:
027: import net.sourceforge.squirrel_sql.client.session.ISession;
028: import net.sourceforge.squirrel_sql.client.session.MockSession;
029: import net.sourceforge.squirrel_sql.fw.dialects.DialectFactory;
030: import net.sourceforge.squirrel_sql.fw.dialects.UserCancelledOperationException;
031: import net.sourceforge.squirrel_sql.fw.sql.DatabaseObjectType;
032: import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
033: import net.sourceforge.squirrel_sql.fw.sql.ISQLConnection;
034: import net.sourceforge.squirrel_sql.fw.sql.ITableInfo;
035: import net.sourceforge.squirrel_sql.fw.sql.MockDatabaseObjectInfo;
036: import net.sourceforge.squirrel_sql.fw.sql.SQLDatabaseMetaData;
037: import net.sourceforge.squirrel_sql.fw.sql.TableInfo;
038: import net.sourceforge.squirrel_sql.plugins.dbcopy.util.DBUtil;
039:
040: public class MockSessionInfoProvider implements SessionInfoProvider {
041:
042: ISession sourceSession = null;
043:
044: ISession destSession = null;
045:
046: ArrayList<ITableInfo> selectedDatabaseObjects = new ArrayList<ITableInfo>();
047:
048: IDatabaseObjectInfo destSelectedDatabaseObject = null;
049:
050: ResourceBundle bundle = null;
051:
052: String sourceSchema = null;
053: String sourceCatalog = null;
054: String destSchema = null;
055: String destCatalog = null;
056:
057: boolean dropOnly = false;
058:
059: public MockSessionInfoProvider(String propertyFile, boolean dropOnly)
060: throws Exception {
061: this .dropOnly = dropOnly;
062: initialize(propertyFile);
063: }
064:
065: private void initialize(String propertyFile) throws Exception {
066: bundle = ResourceBundle.getBundle(propertyFile);
067: String sourceDriver = bundle.getString("sourceDriver");
068: String sourceJdbcUrl = bundle.getString("sourceJdbcUrl");
069: String sourceUser = bundle.getString("sourceUser");
070: String sourcePass = bundle.getString("sourcePass");
071:
072: sourceSession = new MockSession(sourceDriver, sourceJdbcUrl,
073: sourceUser, sourcePass);
074: sourceSchema = fixCase(bundle.getString("sourceSchema"),
075: sourceSession);
076: sourceCatalog = fixCase(bundle.getString("sourceCatalog"),
077: sourceSession);
078: String destDriver = bundle.getString("destDriver");
079: String destJdbcUrl = bundle.getString("destJdbcUrl");
080: String destUser = bundle.getString("destUser");
081: String destPass = bundle.getString("destPass");
082: destSession = new MockSession(destDriver, destJdbcUrl,
083: destUser, destPass);
084: destCatalog = fixCase(bundle.getString("destCatalog"),
085: destSession);
086: destSchema = fixCase(bundle.getString("destSchema"),
087: destSession);
088: initializeDBObjs();
089: }
090:
091: private void initializeDBObjs() throws SQLException,
092: UserCancelledOperationException {
093: List<ITableInfo> tables = getTableNames(sourceSession);
094: String destSchema = fixCase(bundle.getString("destSchema"),
095: destSession);
096: if (tables.size() == 0) {
097: throw new SQLException("No tables found to copy");
098: }
099:
100: for (ITableInfo info : tables) {
101: String sourceTable = fixCase(info.getSimpleName(),
102: sourceSession);
103: if (!shouldIncludeTable(sourceTable)) {
104: continue;
105: }
106: dropDestinationTable(sourceTable, destSchema);
107: if (!dropOnly) {
108: selectedDatabaseObjects.add(info);
109: }
110: }
111: /*
112: if (DialectFactory.isMySQLSession(sourceSession)) {
113: destSelectedDatabaseObject =
114: new MockDatabaseObjectInfo(destSchema, null, destSchema);
115: } else {
116: destSelectedDatabaseObject =
117: new MockDatabaseObjectInfo(destSchema, destSchema, null);
118: }
119: */
120: destSelectedDatabaseObject = new MockDatabaseObjectInfo(
121: destSchema, destSchema, destCatalog);
122: System.out.println("destSelectedDatabaseObject: "
123: + destSelectedDatabaseObject);
124: }
125:
126: private boolean shouldIncludeTable(String tableName) {
127: boolean result = true;
128: // Hack to deal with Ingres IIE* meta tables.
129: if (tableName.startsWith("IIE") || tableName.startsWith("iie")) {
130: result = false;
131: }
132: // Hack to deal with Axion AXION_* tables.
133: if (tableName.startsWith("AXION")
134: || tableName.startsWith("axion")) {
135: result = false;
136: }
137: // Hack to deal with Firebird's RDB meta tables.
138: if (tableName.startsWith("RDB$")) {
139: result = false;
140: }
141: // Hack to deal with Sybase's sys tables
142: if (tableName.startsWith("sys")) {
143: //result = false;
144: }
145:
146: return result;
147: }
148:
149: private void dropDestinationTable(String tableName, String schema)
150: throws SQLException, UserCancelledOperationException {
151: String destTable = fixCase(tableName, destSession);
152: if (DialectFactory.isFrontBase(destSession.getMetaData())) {
153: DBUtil.dropTable(destTable, schema, null, destSession,
154: true, DialectFactory.DEST_TYPE);
155: } else {
156: DBUtil.dropTable(destTable, schema, null, destSession,
157: false, DialectFactory.DEST_TYPE);
158: }
159: }
160:
161: private List<ITableInfo> getTableNames(ISession sourceSession)
162: throws SQLException {
163: List<ITableInfo> result = null;
164: String tableStr = bundle.getString("tablesToCopy");
165: if ("*".equals(tableStr)) {
166: result = getAllTables(sourceSession);
167: } else {
168: result = new ArrayList<ITableInfo>();
169: String[] tableNames = tableStr.split(",");
170: for (int i = 0; i < tableNames.length; i++) {
171: String tableName = tableNames[i];
172: TableInfo info = new TableInfo(sourceCatalog,
173: sourceSchema, tableName, "TABLE", "", null);
174:
175: result.add(info);
176: }
177: }
178: return result;
179: }
180:
181: private List<ITableInfo> getAllTables(ISession sourceSession)
182: throws SQLException {
183: ISQLConnection sourceConn = sourceSession.getSQLConnection();
184: SQLDatabaseMetaData data = sourceConn.getSQLMetaData();
185: ITableInfo[] tableInfos = data.getTables(sourceCatalog,
186: sourceSchema, "%", new String[] { "TABLE" }, null);
187:
188: ArrayList<ITableInfo> tables = new ArrayList<ITableInfo>();
189: for (int i = 0; i < tableInfos.length; i++) {
190: String tiSchema = tableInfos[i].getSchemaName();
191: if (sourceSchema.equals(tiSchema)
192: || ("".equals(sourceSchema) && tiSchema == null)) {
193: if (tableInfos[i].getDatabaseObjectType() == DatabaseObjectType.TABLE) {
194: System.out.println("Adding table "
195: + tableInfos[i].getSimpleName());
196: tables.add(tableInfos[i]);
197: }
198: }
199: }
200: return tables;
201: }
202:
203: private String fixCase(String identifier, ISession session)
204: throws SQLException {
205: ISQLConnection con = session.getSQLConnection();
206: String result = identifier;
207: if (con.getSQLMetaData().getJDBCMetaData()
208: .storesUpperCaseIdentifiers()
209: && !DialectFactory.isFrontBase(session.getMetaData())) {
210: result = identifier.toUpperCase();
211: }
212: return result;
213: }
214:
215: public void setCopySourceSession(ISession session) {
216: sourceSession = session;
217: }
218:
219: public ISession getCopySourceSession() {
220: return sourceSession;
221: }
222:
223: public IDatabaseObjectInfo[] getSourceSelectedDatabaseObjects() {
224: int size = selectedDatabaseObjects.size();
225: IDatabaseObjectInfo[] result = new IDatabaseObjectInfo[size];
226: Iterator<?> i = selectedDatabaseObjects.iterator();
227: int index = 0;
228: while (i.hasNext()) {
229: result[index++] = (IDatabaseObjectInfo) i.next();
230: }
231: return result;
232: }
233:
234: public void setDestCopySession(ISession session) {
235: destSession = session;
236: }
237:
238: public ISession getCopyDestSession() {
239: return destSession;
240: }
241:
242: /* (non-Javadoc)
243: * @see net.sourceforge.squirrel_sql.plugins.dbcopy.SessionInfoProvider#getDestSelectedDatabaseObject()
244: */
245: public IDatabaseObjectInfo getDestSelectedDatabaseObject() {
246: return destSelectedDatabaseObject;
247: }
248:
249: /* (non-Javadoc)
250: * @see net.sourceforge.squirrel_sql.plugins.dbcopy.SessionInfoProvider#setDestSelectedDatabaseObject(net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo)
251: */
252: public void setDestSelectedDatabaseObject(IDatabaseObjectInfo info) {
253: destSelectedDatabaseObject = info;
254: }
255:
256: }
|