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.PreparedStatement;
25  import java.sql.ResultSet;
26  import java.sql.SQLException;
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  import org.apache.commons.lang.StringUtils;
31  import org.apache.torque.engine.database.model.Domain;
32  import org.apache.torque.engine.database.model.SchemaType;
33  
34  /**
35   * MySql Platform implementation.
36   * 
37   * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
38   * @version $Id: PlatformMysqlImpl.java,v 1.1.6.1 2008-04-18 17:04:37 jkeller Exp $
39   */
40  public class PlatformMysqlImpl extends PlatformDefaultImpl {
41  	/**
42  	 * Default constructor.
43  	 */
44  	public PlatformMysqlImpl() {
45  		super();
46  		initialize();
47  	}
48  
49  	/**
50  	 * Initializes db specific domain mapping.
51  	 */
52  	private void initialize() {
53  		setSchemaDomainMapping(new Domain(SchemaType.NUMERIC, "DECIMAL"));
54  		setSchemaDomainMapping(new Domain(SchemaType.LONGVARCHAR, "MEDIUMTEXT"));
55  		setSchemaDomainMapping(new Domain(SchemaType.DATE, "DATETIME"));
56  		setSchemaDomainMapping(new Domain(SchemaType.BINARY, "BLOB"));
57  		setSchemaDomainMapping(new Domain(SchemaType.VARBINARY, "MEDIUMBLOB"));
58  		setSchemaDomainMapping(new Domain(SchemaType.LONGVARBINARY, "LONGBLOB"));
59  		setSchemaDomainMapping(new Domain(SchemaType.BLOB, "LONGBLOB"));
60  		setSchemaDomainMapping(new Domain(SchemaType.CLOB, "LONGTEXT"));
61  	}
62  
63  	/**
64  	 * @see Platform#getAutoIncrement()
65  	 */
66  	public String getAutoIncrement() {
67  		return "AUTO_INCREMENT";
68  	}
69  
70  	/**
71  	 * @see Platform#hasSize(String)
72  	 */
73  	public boolean hasSize(String sqlType) {
74  		return !("MEDIUMTEXT".equals(sqlType) || "LONGTEXT".equals(sqlType) || "BLOB".equals(sqlType) || "MEDIUMBLOB".equals(sqlType) || "LONGBLOB".equals(sqlType));
75  	}
76  
77  	protected String getBaseUrl(String url) {
78  		if (url == null) {
79  			return null;
80  		}
81  		url = url.trim();
82  		if (!url.startsWith("jdbc:mysql://")) {
83  			return null;
84  		}
85  		int count = StringUtils.countMatches(url, "/");
86  		if (count != 3) {
87  			return null;
88  		}
89  		int pos = url.lastIndexOf("/");
90  		return url.substring(0, pos + 1);
91  	}
92  
93  	protected String getOptions(String url) {
94  		int pos = url.indexOf("?");
95  		if (pos == -1) {
96  			return "";
97  		}
98  		return url.substring(pos);
99  	}
100 
101 	/**
102 	 * jdbc:mysql://[host:port],[host:port]/[database][?property1][=value1][&property2][=value2]
103 	 */
104 	@Override
105 	public String getServerUrl(String url) {
106 		String baseUrl = getBaseUrl(url);
107 		if (baseUrl == null) {
108 			return url;
109 		}
110 		String options = getOptions(url);
111 		return baseUrl + options;
112 	}
113 
114 	@Override
115 	public String getSchemaName(String artifactId) {
116 		String s = super.getSchemaName(artifactId);
117 		return s.toLowerCase();
118 	}
119 
120 	@Override
121 	public String filterInvalidDefaultValues(String defaultValue) {
122 		if (defaultValue == null) {
123 			return null;
124 		}
125 		defaultValue = defaultValue.replace("SYS_GUID()", "");
126 		defaultValue = defaultValue.replace("SYSDATE", "");
127 		defaultValue = defaultValue.replace("USERENV(\'SESSIONID\')", "");
128 		return defaultValue.trim();
129 	}
130 
131 	@Override
132 	public Long getSequenceNextVal(Connection con, String schema, String sequenceName) {
133 		try {
134 			PreparedStatement ps = con.prepareStatement("SELECT auto_increment FROM information_schema.tables WHERE table_schema = ? AND table_name = ?");
135 			Long nextVal = 0L;
136 			ps.setString(1, schema);
137 			ps.setString(2, sequenceName);
138 			ResultSet rs = ps.executeQuery();
139 			if (rs.next()) {
140 				nextVal = rs.getLong(1);
141 			}
142 			rs.close();
143 			ps.close();
144 			System.out.println("Next Val for " + schema + "." + sequenceName + "=" + nextVal);
145 			return nextVal;
146 		} catch (SQLException ex) {
147 			System.err.println("Unable to extract sequence definition: " + schema + "." + sequenceName);
148 			ex.printStackTrace();
149 			return 0L;
150 		}
151 	}
152 
153 	@Override
154 	public String getViewDefinition(Connection con, String schema, String viewName) {
155 		try {
156 			PreparedStatement ps = con.prepareStatement("SELECT view_definition FROM information_schema.views WHERE table_schema = ? AND table_name = ?");
157 			String definition = "";
158 			ps.setString(1, schema);
159 			ps.setString(2, viewName);
160 			ResultSet rs = ps.executeQuery();
161 			if (rs.next()) {
162 				definition = rs.getString(1);
163 			}
164 			rs.close();
165 			ps.close();
166 			return definition;
167 		} catch (SQLException ex) {
168 			System.err.println("Unable to extract view definition: " + schema + "." + viewName);
169 			ex.printStackTrace();
170 			return "";
171 		}
172 	}
173 	
174 	private boolean isSequence(String sequenceName) {
175 		return sequenceName.toUpperCase().startsWith("SEQ_") || sequenceName.toUpperCase().startsWith("SEQUENCE_")
176 				|| sequenceName.toUpperCase().endsWith("_SEQ") || sequenceName.toUpperCase().endsWith("_SEQUENCE")
177 				|| sequenceName.toUpperCase().endsWith("_ID") || sequenceName.toUpperCase().endsWith("_S");
178 	}
179 
180 
181 	@Override
182 	public List<String> getSequenceNames(DatabaseMetaData dbMetaData,
183 			String databaseSchema) throws SQLException {
184 		// intentionally not calling the super implementation.
185 		
186 		List<String> sequenceList = new ArrayList<String>();
187 		
188 		List<String> tableList = getTableNames(dbMetaData, databaseSchema);
189 		
190 		for (String tableName : tableList) {
191 			
192 			if (isSequence(tableName))
193 				sequenceList.add(tableName);
194 			
195 		}
196 		return sequenceList;
197 	}
198 	
199 	
200 
201 }