001: /*
002: * MetaDataSqlManager.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.io.BufferedInputStream;
015: import java.io.File;
016: import java.util.HashMap;
017: import workbench.log.LogMgr;
018: import workbench.resource.Settings;
019: import workbench.util.StringUtil;
020: import workbench.util.WbPersistence;
021:
022: /**
023: * @author support@sql-workbench.net
024: */
025: public class MetaDataSqlManager {
026: public static final String TABLE_NAME_PLACEHOLDER = "%tablename%";
027: public static final String INDEX_TYPE_PLACEHOLDER = "%indextype% ";
028: public static final String INDEX_NAME_PLACEHOLDER = "%indexname%";
029: public static final String PK_NAME_PLACEHOLDER = "%pk_name%";
030: public static final String UNIQUE_PLACEHOLDER = "%unique_key% ";
031: public static final String COLUMN_NAME_PLACEHOLDER = "%column%";
032: public static final String COLUMN_LIST_PLACEHOLDER = "%columnlist%";
033: public static final String FK_NAME_PLACEHOLDER = "%constraintname%";
034: public static final String FK_TARGET_TABLE_PLACEHOLDER = "%targettable%";
035: public static final String FK_TARGET_COLUMNS_PLACEHOLDER = "%targetcolumnlist%";
036: public static final String COMMENT_TABLE_PLACEHOLDER = "%table%";
037: public static final String COMMENT_COLUMN_PLACEHOLDER = COLUMN_NAME_PLACEHOLDER;
038: public static final String COMMENT_PLACEHOLDER = "%comment%";
039: public static final String FK_UPDATE_RULE = "%fk_update_rule%";
040: public static final String FK_DELETE_RULE = "%fk_delete_rule%";
041: public static final String DEFERRABLE = "%deferrable%";
042: public static final String GENERAL_SQL = "All";
043:
044: private String productName;
045: private static final GetMetaDataSql MARKER = new GetMetaDataSql();
046: private static final String NO_STRING = "";
047:
048: private GetMetaDataSql procedureSource = MARKER;
049: private GetMetaDataSql viewSource = MARKER;
050: private GetMetaDataSql listTrigger = MARKER;
051: private GetMetaDataSql triggerSource = MARKER;
052:
053: private String primaryKeyTemplate = NO_STRING;
054: private String indexTemplate = NO_STRING;
055: private String foreignKeyTemplate = NO_STRING;
056: private String columnCommentTemplate = NO_STRING;
057: private String tableCommentTemplate = NO_STRING;
058:
059: public MetaDataSqlManager(String product) {
060: this .productName = product;
061: }
062:
063: public GetMetaDataSql getProcedureSourceSql() {
064: if (this .procedureSource == MARKER) {
065: synchronized (MARKER) {
066: HashMap sql = this
067: .readStatementTemplates("ProcSourceStatements.xml");
068: this .procedureSource = (GetMetaDataSql) sql
069: .get(this .productName);
070: }
071: }
072: return this .procedureSource;
073: }
074:
075: public GetMetaDataSql getViewSourceSql() {
076: if (this .viewSource == MARKER) {
077: synchronized (MARKER) {
078: HashMap sql = this
079: .readStatementTemplates("ViewSourceStatements.xml");
080: this .viewSource = (GetMetaDataSql) sql
081: .get(this .productName);
082: }
083: }
084: return this .viewSource;
085: }
086:
087: public GetMetaDataSql getListTriggerSql() {
088: if (this .listTrigger == MARKER) {
089: synchronized (MARKER) {
090: HashMap sql = this
091: .readStatementTemplates("ListTriggersStatements.xml");
092: this .listTrigger = (GetMetaDataSql) sql
093: .get(this .productName);
094: }
095: }
096: return this .listTrigger;
097: }
098:
099: public GetMetaDataSql getTriggerSourceSql() {
100: if (this .triggerSource == MARKER) {
101: synchronized (MARKER) {
102: HashMap sql = this
103: .readStatementTemplates("TriggerSourceStatements.xml");
104: this .triggerSource = (GetMetaDataSql) sql
105: .get(this .productName);
106: }
107: }
108: return this .triggerSource;
109: }
110:
111: public String getPrimaryKeyTemplate() {
112: if (this .primaryKeyTemplate == NO_STRING) {
113: synchronized (NO_STRING) {
114: HashMap sql = this
115: .readStatementTemplates("CreatePkStatements.xml");
116: this .primaryKeyTemplate = (String) sql
117: .get(this .productName);
118: if (this .primaryKeyTemplate == null) {
119: this .primaryKeyTemplate = (String) sql
120: .get(GENERAL_SQL);
121: }
122: }
123: }
124: return this .primaryKeyTemplate;
125: }
126:
127: public String getForeignKeyTemplate(boolean createInline) {
128: if (this .foreignKeyTemplate == NO_STRING) {
129: synchronized (MARKER) {
130: HashMap sql = this
131: .readStatementTemplates("CreateFkStatements.xml");
132: String template = (String) sql.get(this .productName);
133: if (template == null) {
134: if (createInline) {
135: template = (String) sql.get("All-Inline");
136: } else {
137: template = (String) sql.get(GENERAL_SQL);
138: }
139: }
140: this .foreignKeyTemplate = template;
141: }
142: }
143: return this .foreignKeyTemplate;
144: }
145:
146: public String getIndexTemplate() {
147: if (this .indexTemplate == NO_STRING) {
148: synchronized (NO_STRING) {
149: HashMap sql = this
150: .readStatementTemplates("CreateIndexStatements.xml");
151: this .indexTemplate = (String) sql.get(this .productName);
152: if (indexTemplate == null) {
153: this .indexTemplate = (String) sql.get(GENERAL_SQL);
154: }
155: }
156: }
157: return this .indexTemplate;
158: }
159:
160: public String getColumnCommentSql() {
161: if (this .columnCommentTemplate == NO_STRING) {
162: synchronized (NO_STRING) {
163: HashMap sql = this
164: .readStatementTemplates("ColumnCommentStatements.xml");
165: this .columnCommentTemplate = (String) sql
166: .get(this .productName);
167: if (columnCommentTemplate == null) {
168: this .columnCommentTemplate = (String) sql
169: .get(GENERAL_SQL);
170: }
171: }
172: }
173: return this .columnCommentTemplate;
174: }
175:
176: public String getTableCommentSql() {
177: if (this .tableCommentTemplate == NO_STRING) {
178: synchronized (NO_STRING) {
179: HashMap sql = this
180: .readStatementTemplates("TableCommentStatements.xml");
181: this .tableCommentTemplate = (String) sql
182: .get(this .productName);
183: if (tableCommentTemplate == null) {
184: this .tableCommentTemplate = (String) sql
185: .get(GENERAL_SQL);
186: }
187: }
188: }
189: return this .tableCommentTemplate;
190: }
191:
192: public static String removePlaceholder(String sql,
193: String placeholder, boolean withNL) {
194: String s = null;
195: if (withNL) {
196: StringBuilder b = new StringBuilder(
197: placeholder.length() + 10);
198: b.append("[ \\t]*");
199: b.append(StringUtil.quoteRegexMeta(placeholder));
200: b.append("[\n|\r\n]?");
201: s = b.toString();
202: } else {
203: s = StringUtil.quoteRegexMeta(placeholder);
204: }
205: return sql.replaceAll(s, StringUtil.EMPTY_STRING);
206: }
207:
208: private HashMap readStatementTemplates(String aFilename) {
209: HashMap result = null;
210:
211: BufferedInputStream in = new BufferedInputStream(
212: DbMetadata.class.getResourceAsStream(aFilename));
213: Object value;
214: try {
215: WbPersistence reader = new WbPersistence();
216: value = reader.readObject(in);
217: } catch (Exception e) {
218: LogMgr.logError(
219: "MetaDataSqlManager.readStatementTemplate()",
220: "Error reading templates file " + aFilename, e);
221: value = null;
222: }
223:
224: if (value instanceof HashMap) {
225: result = (HashMap) value;
226: }
227:
228: // Try to read the file in the current directory.
229: File f = new File(aFilename);
230: if (!f.exists()) {
231: f = new File(Settings.getInstance().getConfigDir(),
232: aFilename);
233: }
234: if (f.exists()) {
235: LogMgr.logInfo("DbMetadata.readStatementTemplates()",
236: "Reading user defined template file "
237: + f.getAbsolutePath());
238: // try to read additional definitions from local file
239: try {
240: WbPersistence reader = new WbPersistence(aFilename);
241: value = reader.readObject();
242: } catch (Exception e) {
243: LogMgr.logDebug(
244: "MetaDataSqlManager.readStatementTemplate()",
245: "Error reading template file " + aFilename, e);
246: }
247: if (value instanceof HashMap) {
248: HashMap m = (HashMap) value;
249: if (result != null) {
250: result.putAll(m);
251: } else {
252: result = m;
253: }
254: }
255: } else {
256: LogMgr.logDebug(
257: "MetaDataSqlManager.readStatementTemplates()",
258: "No user defined template file found for "
259: + aFilename);
260: }
261: return result;
262: }
263:
264: public static void main(String args[]) {
265: String sql = "ALTER TABLE CONFIGURATION \n"
266: + " ADD CONSTRAINT FK_CONFIG_RES FOREIGN KEY (RESOURCE_KEY) \n"
267: + " REFERENCES %targettable% (%targetcolumnlist%) \n"
268: + " %fk_delete_rule%\n" + " %deferrable%";
269:
270: sql = removePlaceholder(sql, FK_DELETE_RULE, true);
271: System.out.println("|" + sql + "|");
272: //sql = removePlaceholder(sql, DEFERRABLE, true);
273: sql = StringUtil.replace(sql, DEFERRABLE,
274: "deferrable inititially");
275: System.out.println("|" + sql + "|");
276: }
277:
278: }
|