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 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  	public String getServerUrl(String url) {
68  		// By default just return the existing url
69  		return url;
70  	}
71  
72  	public String getSchemaName(String artifactId) {
73  		String suffix = "-db";
74  		if (artifactId.endsWith(suffix)) {
75  			int length = artifactId.length();
76  			artifactId = artifactId.substring(0, length - suffix.length());
77  		}
78  		return StringUtils.remove(artifactId, "-");
79  	}
80  
81  	/**
82  	 * @see Platform#getMaxColumnNameLength()
83  	 */
84  	public int getMaxColumnNameLength() {
85  		return 64;
86  	}
87  
88  	/**
89  	 * @see Platform#getNativeIdMethod()
90  	 */
91  	public String getNativeIdMethod() {
92  		return Platform.IDENTITY;
93  	}
94  
95  	/**
96  	 * @see Platform#getDomainForSchemaType(SchemaType)
97  	 */
98  	public Domain getDomainForSchemaType(SchemaType jdbcType) {
99  		return schemaDomainMap.get(jdbcType);
100 	}
101 
102 	/**
103 	 * @return Only produces a SQL fragment if null values are disallowed.
104 	 * @see Platform#getNullString(boolean)
105 	 */
106 	public String getNullString(boolean notNull) {
107 		// TODO: Check whether this is true for all DBs. Also verify
108 		// the old Sybase templates.
109 		return (notNull ? "NOT NULL" : "");
110 	}
111 
112 	/**
113 	 * @see Platform#getAutoIncrement()
114 	 */
115 	public String getAutoIncrement() {
116 		return "IDENTITY";
117 	}
118 
119 	/**
120 	 * @see Platform#hasScale(String) TODO collect info for all platforms
121 	 */
122 	public boolean hasScale(String sqlType) {
123 		return true;
124 	}
125 
126 	/**
127 	 * @see Platform#hasSize(String) TODO collect info for all platforms
128 	 */
129 	public boolean hasSize(String sqlType) {
130 		return true;
131 	}
132 
133 	/**
134 	 * @see Platform#createNotNullBeforeAutoincrement()
135 	 */
136 	public boolean createNotNullBeforeAutoincrement() {
137 		return true;
138 	}
139 
140 	public String filterInvalidDefaultValues(String defaultValue) {
141 		return defaultValue;
142 	}
143 
144 	public boolean isSpecialDefault(String defaultValue) {
145 		return false;
146 	}
147 
148 	public Long getSequenceNextVal(Connection con, String schema, String sequenceName) {
149 		throw new UnsupportedOperationException("getSequenceDefinition");
150 	}
151 
152 	public String getViewDefinition(Connection con, String schema, String viewName) {
153 		throw new UnsupportedOperationException("getViewDefinition");
154 	}
155 
156 	/**
157 	 * Retrieves a list of the columns composing the primary key for a given table.
158 	 * 
159 	 * @param dbMeta
160 	 *            JDBC metadata.
161 	 * @param tableName
162 	 *            Table from which to retrieve PK information.
163 	 * @return A list of the primary key parts for <code>tableName</code>.
164 	 * @throws SQLException
165 	 */
166 	public List<String> getPrimaryKeys(DatabaseMetaData dbMeta, String dbSchema, String tableName) throws SQLException {
167 		List<String> pk = new ArrayList<String>();
168 		ResultSet parts = null;
169 		try {
170 			parts = dbMeta.getPrimaryKeys(null, dbSchema, tableName);
171 			while (parts.next()) {
172 				pk.add(parts.getString(4));
173 			}
174 		} finally {
175 			if (parts != null) {
176 				parts.close();
177 			}
178 		}
179 		return pk;
180 	}
181 
182 	/**
183 	 * Get all the table names in the current database that are not system tables.
184 	 * 
185 	 * @param dbMeta
186 	 *            JDBC database metadata.
187 	 * @return The list of all the tables in a database.
188 	 * @throws SQLException
189 	 */
190 	public List<String> getTableNames(DatabaseMetaData dbMeta, String databaseSchema) throws SQLException {
191 		// System.out.println("Getting table list...");
192 		List<String> tables = new ArrayList<String>();
193 		ResultSet tableNames = null;
194 		// these are the entity types we want from the database
195 		String[] types = { "TABLE" }; // JHK: removed views from list
196 		try {
197 			tableNames = dbMeta.getTables(null, databaseSchema, null, types);
198 			while (tableNames.next()) {
199 				String name = tableNames.getString(3);
200 				tables.add(name);
201 			}
202 		} finally {
203 			if (tableNames != null) {
204 				tableNames.close();
205 			}
206 		}
207 		// System.out.println("Found " + tables.size() + " tables.");
208 		return tables;
209 	}
210 }