001: /*
002: * $Header: /export/home/cvsroot/MyPersonalizerRepository/MyPersonalizer/Subsystems/Kernel/Sources/es/udc/mypersonalizer/kernel/model/repository/sql/config/DatabaseTemplatesConfig.java,v 1.1.1.1 2004/03/25 12:08:36 fbellas Exp $
003: * $Revision: 1.1.1.1 $
004: * $Date: 2004/03/25 12:08:36 $
005: *
006: * =============================================================================
007: *
008: * Copyright (c) 2003, The MyPersonalizer Development Group
009: * (http://www.tic.udc.es/~fbellas/mypersonalizer/index.html) at
010: * University Of A Coruna
011: * All rights reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions are met:
015: *
016: * - Redistributions of source code must retain the above copyright notice,
017: * this list of conditions and the following disclaimer.
018: *
019: * - Redistributions in binary form must reproduce the above copyright notice,
020: * this list of conditions and the following disclaimer in the documentation
021: * and/or other materials provided with the distribution.
022: *
023: * - Neither the name of the University Of A Coruna nor the names of its
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
028: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
029: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
030: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
031: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
032: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
033: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
034: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
035: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
036: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
037: * POSSIBILITY OF SUCH DAMAGE.
038: *
039: */
040:
041: package es.udc.mypersonalizer.kernel.model.repository.sql.config;
042:
043: /**
044: * Configuration bean containing the templates used to generate SQL code
045: * for this database.
046: *
047: * @author Abel Muinho
048: * @author Fernando Bellas
049: * @since 1.0
050: */
051: public class DatabaseTemplatesConfig {
052: /* --- Keywords used --- */
053: /** Keyword for sequence names. */
054: private static final String SEQUENCE_NAME_KEYWORD = "${sequence}";
055: /** Keyword for table names. */
056: private static final String TABLE_NAME_KEYWORD = "${table}";
057: /** Keyword for column definitions. */
058: private static final String COLUMNS_KEYWORD = "${columns}";
059: /** Keyword for index names. */
060: private static final String INDEX_NAME_KEYWORD = "${index}";
061: /** Keyword for index keys. */
062: private static final String INDEX_KEYS_KEYWORD = "${keys}";
063: /** Keyword for primary keys. */
064: private static final String PK_KEYWORD = "${constraint}";
065: /** Keyword for foreign key constraints. */
066: private static final String FK_KEYWORD = "${constraint}";
067: /** Keyword for foreign key constraints keys. */
068: private static final String FK_KEYS_KEYWORD = "${keys}";
069: /** Keyword for a table referenced in a foreign key constraint. */
070: private static final String REFERENCED_TABLE_KEYWORD = "${table}";
071:
072: private String dropSequenceTemplate = null;
073: private String createSequenceTemplate = null;
074:
075: private String dropTableTemplate = null;
076:
077: private String createTableTemplate = null;
078:
079: private String dropIndexTemplate = null;
080:
081: private String createIndexTemplate = null;
082:
083: private String pkConstraintTemplate = null;
084:
085: private String fkReferenceTemplate = null;
086:
087: private String insertIdTemplate = null;
088:
089: private String nextIdTemplate = null;
090:
091: /**
092: * Obtains the "drop sequence" query for the given sequence name.
093: * @param sequenceName name of the sequence.
094: * @return the "drop sequence".
095: */
096: public String getDropSequence(String sequenceName) {
097: return replace(dropSequenceTemplate, SEQUENCE_NAME_KEYWORD,
098: sequenceName);
099: }
100:
101: /**
102: * Sets the template for generating "drop sequence" queries.
103: * @param string the template.
104: */
105: public void setDropSequenceTemplate(String string) {
106: dropSequenceTemplate = string;
107: }
108:
109: /**
110: * Obtains the "create sequence" query for the given sequence name.
111: * @param sequenceName the name of the sequence.
112: * @return the "create sequence".
113: */
114: public String getCreateSequence(String sequenceName) {
115: return replace(createSequenceTemplate, SEQUENCE_NAME_KEYWORD,
116: sequenceName);
117: }
118:
119: /**
120: * Sets the template for generating "create sequence" queries.
121: * @param string the template.
122: */
123: public void setCreateSequenceTemplate(String string) {
124: createSequenceTemplate = string;
125: }
126:
127: /**
128: * Obtains the "drop table" query for the given table name.
129: * @param tableName the name of the table.
130: * @return the "drop table" query.
131: */
132: public String getDropTable(String tableName) {
133: return replace(dropTableTemplate, TABLE_NAME_KEYWORD, tableName);
134: }
135:
136: /**
137: * Sets the template for generating "drop table" queries.
138: * @param string the template.
139: */
140: public void setDropTableTemplate(String string) {
141: dropTableTemplate = string;
142: }
143:
144: /**
145: * Obtains the "create table" query for the given table name.
146: * @param tableName the name of the table.
147: * @param columns the column definitions.
148: * @return the "create table" query.
149: */
150: public String getCreateTable(String tableName, String columns) {
151: return replace(replace(createTableTemplate, TABLE_NAME_KEYWORD,
152: tableName), COLUMNS_KEYWORD, columns);
153: }
154:
155: /**
156: * Sets the template for the "create table" query.
157: * @param string the template.
158: */
159: public void setCreateTableTemplate(String string) {
160: createTableTemplate = string;
161: }
162:
163: /**
164: * Obtains the "drop index" query for the given index name.
165: * @param indexName the name of the index.
166: * @param tableName the name of the indexed table.
167: * @return the "drop index" query.
168: */
169: public String getDropIndex(String indexName, String tableName) {
170: return replace(replace(dropIndexTemplate, TABLE_NAME_KEYWORD,
171: tableName), INDEX_NAME_KEYWORD, indexName);
172: }
173:
174: /**
175: * Sets the template for the "drop index" query.
176: * @param string the template.
177: */
178: public void setDropIndexTemplate(String string) {
179: dropIndexTemplate = string;
180: }
181:
182: /**
183: * Obtains the "create index" query for the given index name.
184: * @param indexName the name of the index.
185: * @param tableName the name of the indexed table.
186: * @param keys the comma-separated keys that build the index.
187: * @return the "create index" query.
188: */
189: public String getCreateIndex(String indexName, String tableName,
190: String keys) {
191: return replace(replace(replace(createIndexTemplate,
192: TABLE_NAME_KEYWORD, tableName), INDEX_KEYS_KEYWORD,
193: keys), INDEX_NAME_KEYWORD, indexName);
194: }
195:
196: /**
197: * Sets the template for the "create index" query.
198: * @param string the template.
199: */
200: public void setCreateIndexTemplate(String string) {
201: createIndexTemplate = string;
202: }
203:
204: /**
205: * Obtains the "primary key constraint" fragment for the given key and
206: * "name space" name.
207: * @param key The primary key
208: * @return the query fragment.
209: */
210: public String getPkConstraint(String key) {
211: return replace(pkConstraintTemplate, PK_KEYWORD, key);
212: }
213:
214: /**
215: * Sets the template for generating "primary key constraint" query
216: * fragments.
217: * @param string the template.
218: */
219: public void setPkConstraintTemplate(String string) {
220: pkConstraintTemplate = string;
221: }
222:
223: /**
224: * Obtains the "foreign key reference" query fragment.
225: * @param key The foreign key.
226: * @param tableName The table referenced.
227: * @return the query fragment.
228: */
229: public String getFkReference(String key, String tableName,
230: String keys) {
231: return replace(replace(replace(fkReferenceTemplate, FK_KEYWORD,
232: key), FK_KEYS_KEYWORD, keys), REFERENCED_TABLE_KEYWORD,
233: tableName);
234: }
235:
236: /**
237: * Sets the template for generating "foreign key reference" query
238: * fragments.
239: * @param string the template.
240: */
241: public void setFkReferenceTemplate(String string) {
242: fkReferenceTemplate = string;
243: }
244:
245: /**
246: * Obtains the query for retrieving an unique id for the given table.
247: * @param tableName the name of the table.
248: * @return the query for obtaining an unique identifier.
249: */
250: public String getNextId(String tableName) {
251: return replace(nextIdTemplate, SEQUENCE_NAME_KEYWORD, tableName);
252: }
253:
254: /**
255: * Sets the template for generating "next identifier" queries.
256: * @param string the template.
257: */
258: public void setNextIdTemplate(String string) {
259: nextIdTemplate = string;
260: }
261:
262: /**
263: * Obtains the query for generating an identifier when emulating sequences
264: * with tables.
265: *
266: * @param tableName the name of the table
267: * @return the query for generating an identifier
268: */
269: public String getInsertId(String tableName) {
270: return replace(insertIdTemplate, SEQUENCE_NAME_KEYWORD,
271: tableName);
272: }
273:
274: /**
275: * Sets the template query for generating an identifier when emulating
276: * sequences with tables.
277: *
278: * @param string the template query
279: */
280: public void setInsertIdTemplate(String string) {
281: insertIdTemplate = string;
282: }
283:
284: /**
285: * Replace every occurrence of <code>keyword</code> by
286: * the given value in the passed string.
287: *
288: * @param original Original string containing the keywords to be replaced.
289: * @param keyword Keyword to be substituted.
290: * @param replacement String to put in place of keywords to produce the
291: * result string.
292: * @return The original string replacing the instances of the given keyword
293: * by the replacement string.
294: */
295: protected String replace(String original, String keyword,
296: String replacement) {
297:
298: int startPos = 0;
299: int lastFound = 0;
300:
301: StringBuffer result = new StringBuffer(original.length() * 2);
302: while ((lastFound = original.indexOf(keyword, startPos)) != -1) {
303: result.append(original.substring(startPos, lastFound))
304: .append(replacement);
305: startPos = lastFound + keyword.length();
306: }
307: String theResult = result.append(original.substring(startPos))
308: .toString();
309: return theResult;
310: }
311: }
|