View Javadoc

1   package org.apache.torque.engine.platform;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.sql.Connection;
23  import java.sql.DatabaseMetaData;
24  import java.sql.ResultSet;
25  import java.sql.SQLException;
26  import java.util.ArrayList;
27  import java.util.HashMap;
28  import java.util.Iterator;
29  import java.util.List;
30  import java.util.Map;
31  
32  import org.apache.commons.lang.StringUtils;
33  import org.apache.torque.engine.database.model.Domain;
34  import org.apache.torque.engine.database.model.SchemaType;
35  
36  /**
37   * Default implementation for the Platform interface.
38   *
39   * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
40   * @version $Id: PlatformDefaultImpl.java,v 1.1.6.2 2008-04-18 17:04:37 jkeller Exp $
41   */
42  public abstract class PlatformDefaultImpl implements Platform {
43  	private Map<SchemaType, Domain> schemaDomainMap;
44  
45  	/**
46  	 * Default constructor.
47  	 */
48  	public PlatformDefaultImpl() {
49  		initialize();
50  	}
51  
52  	private void initialize() {
53  		schemaDomainMap = new HashMap<SchemaType, Domain>(30);
54  		Iterator<SchemaType> iter = SchemaType.iterator();
55  		while (iter.hasNext()) {
56  			SchemaType type = iter.next();
57  			schemaDomainMap.put(type, new Domain(type));
58  		}
59  		schemaDomainMap.put(SchemaType.BOOLEANCHAR, new Domain(SchemaType.BOOLEANCHAR, "CHAR"));
60  		schemaDomainMap.put(SchemaType.BOOLEANINT, new Domain(SchemaType.BOOLEANINT, "INTEGER"));
61  	}
62  
63  	protected void setSchemaDomainMapping(Domain domain) {
64  		schemaDomainMap.put(domain.getType(), domain);
65  	}
66  
67  	@Override
68      public String getServerUrl(String url) {
69  		// By default just return the existing url
70  		return url;
71  	}
72  
73  	@Override
74      public String getSchemaName(String artifactId) {
75  		String suffix = "-db";
76  		if (artifactId.endsWith(suffix)) {
77  			int length = artifactId.length();
78  			artifactId = artifactId.substring(0, length - suffix.length());
79  		}
80  		return StringUtils.remove(artifactId, "-");
81  	}
82  
83  	/**
84  	 * @see Platform#getMaxColumnNameLength()
85  	 */
86  	@Override
87      public int getMaxColumnNameLength() {
88  		return 64;
89  	}
90  
91  	/**
92  	 * @see Platform#getNativeIdMethod()
93  	 */
94  	@Override
95      public String getNativeIdMethod() {
96  		return Platform.IDENTITY;
97  	}
98  
99  	/**
100 	 * @see Platform#getDomainForSchemaType(SchemaType)
101 	 */
102 	@Override
103     public Domain getDomainForSchemaType(SchemaType jdbcType) {
104 		return schemaDomainMap.get(jdbcType);
105 	}
106 
107 	/**
108 	 * @return Only produces a SQL fragment if null values are disallowed.
109 	 * @see Platform#getNullString(boolean)
110 	 */
111 	@Override
112     public String getNullString(boolean notNull) {
113 		// TODO: Check whether this is true for all DBs. Also verify
114 		// the old Sybase templates.
115 		return (notNull ? "NOT NULL" : "");
116 	}
117 
118 	/**
119 	 * @see Platform#getAutoIncrement()
120 	 */
121 	@Override
122     public String getAutoIncrement() {
123 		return "IDENTITY";
124 	}
125 
126 	/**
127 	 * @see Platform#hasScale(String) TODO collect info for all platforms
128 	 */
129 	@Override
130     public boolean hasScale(String sqlType) {
131 		return true;
132 	}
133 
134 	/**
135 	 * @see Platform#hasSize(String) TODO collect info for all platforms
136 	 */
137 	@Override
138     public boolean hasSize(String sqlType) {
139 		return true;
140 	}
141 
142 	/**
143 	 * @see Platform#createNotNullBeforeAutoincrement()
144 	 */
145 	@Override
146     public boolean createNotNullBeforeAutoincrement() {
147 		return true;
148 	}
149 
150 	@Override
151     public String filterInvalidDefaultValues(String defaultValue) {
152 		return defaultValue;
153 	}
154 
155 	@Override
156     public boolean isSpecialDefault(String defaultValue) {
157 		return false;
158 	}
159 
160 	@Override
161     public Long getSequenceNextVal(Connection con, String schema, String sequenceName) {
162 		throw new UnsupportedOperationException("getSequenceDefinition");
163 	}
164 
165 	@Override
166     public String getViewDefinition(Connection con, String schema, String viewName) {
167 		throw new UnsupportedOperationException("getViewDefinition");
168 	}
169 
170 	/**
171 	 * Retrieves a list of the columns composing the primary key for a given table.
172 	 *
173 	 * @param dbMeta
174 	 *            JDBC metadata.
175 	 * @param tableName
176 	 *            Table from which to retrieve PK information.
177 	 * @return A list of the primary key parts for <code>tableName</code>.
178 	 * @throws SQLException
179 	 */
180 	@Override
181     public List<String> getPrimaryKeys(DatabaseMetaData dbMeta, String dbSchema, String tableName) throws SQLException {
182 		List<String> pk = new ArrayList<String>();
183 		ResultSet parts = null;
184 		try {
185 			parts = dbMeta.getPrimaryKeys(null, dbSchema, tableName);
186 			while (parts.next()) {
187 				pk.add(parts.getString(4));
188 			}
189 		} finally {
190 			if (parts != null) {
191 				parts.close();
192 			}
193 		}
194 		return pk;
195 	}
196 
197 	protected List<String> getTableNames(DatabaseMetaData dbMeta, String databaseSchema, String tableType) throws SQLException {
198 		// System.out.println("Getting table list...");
199 		List<String> tables = new ArrayList<String>();
200 		ResultSet tableNames = null;
201 		// these are the entity types we want from the database
202 		String[] types = { tableType }; // JHK: removed views from list
203 		try {
204 			tableNames = dbMeta.getTables(null, databaseSchema, null, types);
205 			while (tableNames.next()) {
206 				String name = tableNames.getString(3);
207 				tables.add(name);
208 			}
209 		} finally {
210 			if (tableNames != null) {
211 				tableNames.close();
212 			}
213 		}
214 		// System.out.println("Found " + tables.size() + " tables.");
215 		return tables;
216 	}
217 
218 	/**
219 	 * Get all the table names in the current database that are not system tables.
220 	 *
221 	 * @param dbMeta
222 	 *            JDBC database metadata.
223 	 * @return The list of all the tables in a database.
224 	 * @throws SQLException
225 	 */
226 	@Override
227     public List<String> getTableNames(DatabaseMetaData dbMeta, String databaseSchema) throws SQLException {
228 		return this.getTableNames(dbMeta, databaseSchema, "TABLE");
229 	}
230 
231 	@Override
232     public List<String> getSequenceNames(DatabaseMetaData dbMeta, String databaseSchema) throws SQLException {
233 		return this.getTableNames(dbMeta, databaseSchema, "SEQUENCE");
234 	}
235 }