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

org.apache.ctakes.ytex.dao.DBUtil Maven / Gradle / Ivy

The newest version!
/**
 * 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.
 */
package org.apache.ctakes.ytex.dao;

import org.slf4j.LoggerFactory;
import org.slf4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Oracle differs from sql server & mysql in handling of empty string. In
 * oracle, empty string is null, in other platforms, empty string is non-null.
 * We need a stand-in for the empty string in oracle; we use the string with one
 * blank in its place.
 * 
 * @author vijay
 * 
 */
public class DBUtil {

	static private final Logger LOGGER = LoggerFactory.getLogger( "DBUtil" );
	private static Properties ytexProperties;
	private static boolean oracle;
	private static boolean mysql;
	private static boolean mssql;
	private static boolean hsql;
	private static String escapeBegin;
	private static String escapeEnd;

	static {
		InputStream ytexPropsIn = null;
		try {
			ytexPropsIn = DBUtil.class.getResourceAsStream("/org/apache/ctakes/ytex/ytex.properties");
			ytexProperties = new Properties();
			ytexProperties.load(ytexPropsIn);
			oracle = "orcl".equals(ytexProperties.getProperty("db.type"));
			mysql = "mysql".equals(ytexProperties.getProperty("db.type"));
			hsql = "hsql".equals(ytexProperties.getProperty("db.type"));
			
			if (mssql) {
				escapeBegin = "[";
				escapeEnd = "]";
			} else if (mysql) {
				escapeBegin = "`";
				escapeEnd = "`";
			} else  {
				escapeBegin = "";
				escapeEnd = "";
			} 
		} catch (Exception e) {
			LOGGER.error("initalizer", e);
		} finally {
			if (ytexPropsIn != null) {
				try {
					ytexPropsIn.close();
				} catch (IOException e) {
				}
			}
		}
	}

	public static String getYTEXTablePrefix() {
		if (mssql)
			return ytexProperties.getProperty("db.schema", "dbo") + ".";
		else
			return "";
	}

	public static String getEmptyString() {
		if (oracle)
			return " ";
		else
			return "";
	}

	public static String getEscapeBegin() {
		return escapeBegin;
	}

	public static String getEscapeEnd() {
		return escapeEnd;
	}

	public static String nullToEmptyString(String param) {
		if (param == null)
			return getEmptyString();
		else
			return param;
	}

	public static String formatFieldName(String fieldName) {
		return getEscapeBegin() + fieldName + getEscapeEnd();
	}

	public static String formatTableName(String tableName) {
		return getEscapeBegin()
				+ tableName.replaceAll("\\.", getEscapeEnd() + '.'
						+ getEscapeBegin()) + getEscapeEnd();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy