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