001: /*
002: * ObjectScripter.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.db;
013:
014: import java.util.List;
015: import workbench.resource.ResourceMgr;
016: import workbench.util.ExceptionUtil;
017:
018: import workbench.interfaces.ScriptGenerationMonitor;
019: import workbench.interfaces.Scripter;
020: import workbench.resource.Settings;
021:
022: /**
023: *
024: * @author support@sql-workbench.net
025: */
026: public class ObjectScripter implements Scripter {
027: public static final String TYPE_SEQUENCE = "sequence";
028: public static final String TYPE_TABLE = "table";
029: public static final String TYPE_VIEW = "view";
030: public static final String TYPE_SYNONYM = "synonym";
031: public static final String TYPE_INSERT = "insert";
032: public static final String TYPE_SELECT = "select";
033: public static final String TYPE_PROC = "procedure";
034: public static final String TYPE_PKG = "package";
035: public static final String TYPE_FUNC = "function";
036: public static final String TYPE_TRG = "trigger";
037: public static final String TYPE_MVIEW = DbMetadata.MVIEW_NAME
038: .toLowerCase();
039:
040: private List<? extends DbObject> objectList;
041: private StringBuilder script;
042: private ScriptGenerationMonitor progressMonitor;
043: private WbConnection dbConnection;
044: private boolean cancel;
045: private String nl = Settings.getInstance()
046: .getInternalEditorLineEnding();
047:
048: public ObjectScripter(List<? extends DbObject> objectList,
049: WbConnection aConnection) {
050: this .objectList = objectList;
051: this .dbConnection = aConnection;
052: }
053:
054: public void setProgressMonitor(ScriptGenerationMonitor aMonitor) {
055: this .progressMonitor = aMonitor;
056: }
057:
058: public String getScript() {
059: if (this .script == null)
060: this .generateScript();
061: return this .script.toString();
062: }
063:
064: public boolean isCancelled() {
065: return this .cancel;
066: }
067:
068: public void generateScript() {
069: try {
070: this .dbConnection.setBusy(true);
071: this .cancel = false;
072: this .script = new StringBuilder(
073: this .objectList.size() * 500);
074: if (!cancel)
075: this .appendObjectType(TYPE_SEQUENCE);
076: if (!cancel)
077: this .appendObjectType(TYPE_TABLE);
078: if (!cancel)
079: this .appendForeignKeys();
080: if (!cancel)
081: this .appendObjectType(TYPE_VIEW);
082: if (!cancel)
083: this .appendObjectType(TYPE_SYNONYM);
084: if (!cancel)
085: this .appendObjectType(TYPE_MVIEW);
086: if (!cancel)
087: this .appendObjectType(TYPE_INSERT);
088: if (!cancel)
089: this .appendObjectType(TYPE_SELECT);
090: if (!cancel)
091: this .appendObjectType(TYPE_FUNC);
092: if (!cancel)
093: this .appendObjectType(TYPE_PROC);
094: if (!cancel)
095: this .appendObjectType(TYPE_PKG);
096: if (!cancel)
097: this .appendObjectType(TYPE_TRG);
098: } finally {
099: this .dbConnection.setBusy(false);
100: }
101: }
102:
103: public void cancel() {
104: this .cancel = true;
105: }
106:
107: private void appendForeignKeys() {
108: if (this .progressMonitor != null) {
109: this .progressMonitor.setCurrentObject(ResourceMgr
110: .getString("TxtScriptProcessFk"));
111: }
112: boolean first = true;
113: for (DbObject dbo : objectList) {
114: if (cancel)
115: break;
116: String type = dbo.getObjectType();
117: if (!type.equalsIgnoreCase(TYPE_TABLE))
118: continue;
119:
120: TableIdentifier tbl = (TableIdentifier) dbo;
121: tbl.adjustCase(this .dbConnection);
122: StringBuilder source = dbConnection.getMetadata()
123: .getFkSource(tbl);
124: if (source != null && source.length() > 0) {
125: if (first) {
126: this .script.append("-- BEGIN FOREIGN KEYS --" + nl);
127: first = false;
128: }
129: script.append(source);
130: }
131: }
132: if (!first) {
133: // no table was found, so no FK was added --> do not add separator
134: this .script.append("-- END FOREIGN KEYS --" + nl + nl);
135: }
136: }
137:
138: private void appendObjectType(String typeFilter) {
139: for (DbObject dbo : objectList) {
140: if (cancel)
141: break;
142: String type = dbo.getObjectType();
143:
144: if (!type.equalsIgnoreCase(typeFilter))
145: continue;
146:
147: CharSequence source = null;
148:
149: if (this .progressMonitor != null) {
150: this .progressMonitor.setCurrentObject(dbo
151: .getObjectName());
152: }
153:
154: try {
155: source = dbo.getSource(dbConnection);
156: } catch (Exception e) {
157: this .script.append("\nError creating script for "
158: + dbo.getObjectName() + " "
159: + ExceptionUtil.getDisplay(e));
160: }
161:
162: if (source != null && source.length() > 0) {
163: boolean useSeparator = !type.equalsIgnoreCase("insert")
164: && !type.equalsIgnoreCase("select");
165: if (useSeparator)
166: this .script.append("-- BEGIN " + type + " "
167: + dbo.getObjectName() + nl);
168: this .script.append(source);
169: if (useSeparator)
170: this .script.append("-- END " + type + " "
171: + dbo.getObjectName() + nl);
172: this.script.append(nl);
173: }
174: }
175: }
176:
177: }
|