All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.torque.engine.platform.PlatformMysqlImpl Maven / Gradle / Ivy

package org.apache.torque.engine.platform;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.torque.engine.database.model.Domain;
import org.apache.torque.engine.database.model.SchemaType;
import org.kuali.common.impex.service.MySqlProducer;
import org.kuali.common.impex.service.SqlProducer;

/**
 * MySql Platform implementation.
 *
 * @author Martin Poeschl
 * @version $Id: PlatformMysqlImpl.java,v 1.1.6.1 2008-04-18 17:04:37 jkeller Exp $
 */
public class PlatformMysqlImpl extends PlatformDefaultImpl {
	/**
	 * Default constructor.
	 */
	public PlatformMysqlImpl() {
		super();
		initialize();
	}

	/**
	 * Initializes db specific domain mapping.
	 */
	private void initialize() {
		setSchemaDomainMapping(new Domain(SchemaType.NUMERIC, "DECIMAL"));
		setSchemaDomainMapping(new Domain(SchemaType.LONGVARCHAR, "MEDIUMTEXT"));
		setSchemaDomainMapping(new Domain(SchemaType.DATE, "DATETIME"));
		setSchemaDomainMapping(new Domain(SchemaType.BINARY, "BLOB"));
		setSchemaDomainMapping(new Domain(SchemaType.VARBINARY, "MEDIUMBLOB"));
		setSchemaDomainMapping(new Domain(SchemaType.LONGVARBINARY, "LONGBLOB"));
		setSchemaDomainMapping(new Domain(SchemaType.BLOB, "LONGBLOB"));
		setSchemaDomainMapping(new Domain(SchemaType.CLOB, "LONGTEXT"));
		setSchemaDomainMapping(new Domain(SchemaType.TIMESTAMP, "DATETIME"));
	}

	/**
	 * @see Platform#getAutoIncrement()
	 */
	@Override
	public String getAutoIncrement() {
		return "AUTO_INCREMENT";
	}

	/**
	 * @see Platform#hasSize(String)
	 */
	@Override
	public boolean hasSize(String sqlType) {
		return !("MEDIUMTEXT".equals(sqlType) || "LONGTEXT".equals(sqlType) || "BLOB".equals(sqlType) || "MEDIUMBLOB".equals(sqlType) || "LONGBLOB".equals(sqlType));
	}

	protected String getBaseUrl(String url) {
		if (url == null) {
			return null;
		}
		url = url.trim();
		if (!url.startsWith("jdbc:mysql://")) {
			return null;
		}
		int count = StringUtils.countMatches(url, "/");
		if (count != 3) {
			return null;
		}
		int pos = url.lastIndexOf("/");
		return url.substring(0, pos + 1);
	}

	protected String getOptions(String url) {
		int pos = url.indexOf("?");
		if (pos == -1) {
			return "";
		}
		return url.substring(pos);
	}

	/**
	 * jdbc:mysql://[host:port],[host:port]/[database][?property1][=value1][&property2][=value2]
	 */
	@Override
	public String getServerUrl(String url) {
		String baseUrl = getBaseUrl(url);
		if (baseUrl == null) {
			return url;
		}
		String options = getOptions(url);
		return baseUrl + options;
	}

	@Override
	public String getSchemaName(String artifactId) {
		String s = super.getSchemaName(artifactId);
		return s.toLowerCase();
	}

	@Override
	public String filterInvalidDefaultValues(String defaultValue) {
		if (defaultValue == null) {
			return null;
		}
		defaultValue = defaultValue.replace("SYS_GUID()", "");
		defaultValue = defaultValue.replace("SYSDATE", "");
		defaultValue = defaultValue.replace("USERENV(\'SESSIONID\')", "");
		return defaultValue.trim();
	}

	@Override
	public Long getSequenceNextVal(Connection con, String schema, String sequenceName) {
		try {
			PreparedStatement ps = con.prepareStatement("SELECT auto_increment FROM information_schema.tables WHERE table_schema = ? AND table_name = ?");
			Long nextVal = 0L;
			ps.setString(1, schema);
			ps.setString(2, sequenceName);
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {
				nextVal = rs.getLong(1);
			}
			rs.close();
			ps.close();
			System.out.println("Next Val for " + schema + "." + sequenceName + "=" + nextVal);
			return nextVal;
		} catch (SQLException ex) {
			System.err.println("Unable to extract sequence definition: " + schema + "." + sequenceName);
			ex.printStackTrace();
			return 0L;
		}
	}

	@Override
	public String getViewDefinition(Connection con, String schema, String viewName) {
		try {
			PreparedStatement ps = con.prepareStatement("SELECT view_definition FROM information_schema.views WHERE table_schema = ? AND table_name = ?");
			String definition = "";
			ps.setString(1, schema);
			ps.setString(2, viewName);
			ResultSet rs = ps.executeQuery();
			if (rs.next()) {
				definition = rs.getString(1);
			}
			rs.close();
			ps.close();
			return definition;
		} catch (SQLException ex) {
			System.err.println("Unable to extract view definition: " + schema + "." + viewName);
			ex.printStackTrace();
			return "";
		}
	}

	private boolean isSequence(String sequenceName) {
		return sequenceName.toUpperCase().startsWith("SEQ_") || sequenceName.toUpperCase().startsWith("SEQUENCE_") || sequenceName.toUpperCase().endsWith("_SEQ")
		        || sequenceName.toUpperCase().endsWith("_SEQUENCE") || sequenceName.toUpperCase().endsWith("_ID") || sequenceName.toUpperCase().endsWith("_S");
	}

	@Override
	public List getSequenceNames(DatabaseMetaData dbMetaData, String databaseSchema) throws SQLException {
		// intentionally not calling the super implementation.

		List sequenceList = new ArrayList();

		List tableList = getTableNames(dbMetaData, databaseSchema);

		for (String tableName : tableList) {

			if (isSequence(tableName))
				sequenceList.add(tableName);

		}
		return sequenceList;
	}

	@Override
	public SqlProducer getSqlProducer() {
		return new MySqlProducer();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy