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

xdev.db.mssql2008.jdbc.MsSql2008DDLMapper Maven / Gradle / Ivy

The newest version!
package xdev.db.mssql2008.jdbc;

/*-
 * #%L
 * SqlEngine Database Adapter MsSQL 2008
 * %%
 * Copyright (C) 2003 - 2021 XDEV Software
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */


import java.util.Hashtable;

import com.xdev.jadoth.sqlengine.SQL;
import com.xdev.jadoth.sqlengine.SQL.DATATYPE;
import com.xdev.jadoth.sqlengine.SQL.INDEXTYPE;
import com.xdev.jadoth.sqlengine.dbms.standard.StandardDDLMapper;
import com.xdev.jadoth.sqlengine.internal.tables.SqlTableIdentity;



/**
 * The Class MsSql2008DDLMapper.
 * 
 * @author Thomas Muenz
 */
public class MsSql2008DDLMapper extends StandardDDLMapper
{
	// /////////////////////////////////////////////////////////////////////////
	// constants //
	// ///////////////////
	
	/** The Constant DATATYPE_BIT. */
	public static final String							DATATYPE_BIT		= "BIT";
	
	/** The Constant DATATYPE_DATETIME. */
	public static final String							DATATYPE_DATETIME	= "DATETIME";
	
	/** The Constant DATATYPE_TEXT. */
	public static final String							DATATYPE_TEXT		= "TEXT";
	
	/** The Constant DATATYPE_IMAGE. */
	public static final String							DATATYPE_IMAGE		= "IMAGE";
	
	/** The Constant dataTypeStrings. */
	private static final Hashtable	dataTypeStrings		= createDataTypeStrings();
	
	
	/**
	 * Creates the data type strings.
	 * 
	 * @return the hashtable
	 */
	private static final Hashtable createDataTypeStrings()
	{
		final Hashtable c = new Hashtable<>(10);
		
		c.put(DATATYPE_BIT,SQL.DATATYPE.BOOLEAN);
		
		c.put(DATATYPE_DATETIME,SQL.DATATYPE.TIMESTAMP);
		
		c.put(DATATYPE_TEXT,SQL.DATATYPE.LONGVARCHAR);
		c.put(DATATYPE_IMAGE,SQL.DATATYPE.LONGVARBINARY);
		
		c.put(SQL.DATATYPE.FLOAT.name(),SQL.DATATYPE.DOUBLE);
		
		// replicate the whole stuff for lower case, just in case
		for(final String s : c.keySet().toArray(new String[c.size()]))
		{
			c.put(s.toLowerCase(),c.get(s));
		}
		
		return c;
	}
	
	
	// /////////////////////////////////////////////////////////////////////////
	// constructors //
	// ///////////////////
	
	/**
	 * Instantiates a new ms sql2008 ddl mapper.
	 * 
	 * @param dbmsAdaptor
	 *            the dbms adaptor
	 */
	protected MsSql2008DDLMapper(final MsSql2008Dbms dbmsAdaptor)
	{
		super(dbmsAdaptor);
	}
	
	
	// /////////////////////////////////////////////////////////////////////////
	// override methods //
	// ///////////////////
	
	/**
	 * Map data type.
	 * 
	 * @param dataTypeString
	 *            the data type string
	 * @return the dATATYPE
	 * @return
	 * @see com.xdev.jadoth.sqlengine.dbms.DbmsDDLMapper.Implementation#mapDataType(java.lang.String)
	 */
	@Override
	public DATATYPE mapDataType(final String dataTypeString)
	{
		final String upperCaseDataType = dataTypeString.toUpperCase();
		
		final DATATYPE dbmsType = dataTypeStrings.get(upperCaseDataType);
		if(dbmsType != null)
		{
			return dbmsType;
		}
		
		return super.mapDataType(upperCaseDataType);
	}
	
	
	/**
	 * Map index type.
	 * 
	 * @param indexTypeString
	 *            the index type string
	 * @return the iNDEXTYPE
	 * @return
	 * @see com.xdev.jadoth.sqlengine.dbms.DbmsDDLMapper.Implementation#mapIndexType(java.lang.String)
	 */
	@Override
	public INDEXTYPE mapIndexType(final String indexTypeString)
	{
		if(indexTypeString == null)
		{
			return SQL.INDEXTYPE.NORMAL;
		}
		
		if(indexTypeString.contains("unique"))
		{
			return SQL.INDEXTYPE.UNIQUE;
		}
		
		return SQL.INDEXTYPE.NORMAL;
	}
	
	
	/**
	 * Lookup ddbms data type mapping.
	 * 
	 * @param type
	 *            the type
	 * @param table
	 *            the table
	 * @return the string
	 * @return
	 * @see com.xdev.jadoth.sqlengine.dbms.DbmsDDLMapper.Implementation#lookupDdbmsDataTypeMapping(com.xdev.jadoth.sqlengine.SQL.DATATYPE,
	 *      com.xdev.jadoth.sqlengine.internal.tables.SqlTableIdentity)
	 */
	@Override
	public String lookupDdbmsDataTypeMapping(final DATATYPE type, final SqlTableIdentity table)
	{
		switch(type)
		{
			case BOOLEAN:
				return DATATYPE_BIT;
			case TIMESTAMP:
				return DATATYPE_DATETIME;
			case LONGVARCHAR:
				return DATATYPE_TEXT;
			case DOUBLE:
				return SQL.DATATYPE.FLOAT.name();
			case FLOAT:
				return SQL.DATATYPE.REAL.name();
			default:
				return super.lookupDdbmsDataTypeMapping(type,table);
		}
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy