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

src-main.org.awakefw.sql.api.util.SqlUtil Maven / Gradle / Ivy

/*
 * Awake File: Easy file upload & download through HTTP with Java
 * Awake SQL: Remote JDBC access through HTTP.                                    
 * Copyright (C) 2012, Kawan Softwares S.A.S.
 * (http://www.awakeframework.org). All rights reserved.                                
 *                                                                               
 * Awake File/SQL 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 2.1 of the License, or (at your option) any later version.            
 *                                                                               
 * Awake File/SQL 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             
 * Lesser General Public License for more details.                               
 *                                                                               
 * You should have received a copy of the GNU Lesser General Public              
 * License along with this library; if not, write to the Free Software           
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
 * 02110-1301  USA
 *
 * Any modifications to this file must keep this entire header
 * intact.
 */

// Last Updates: 

// 10/10/11 11:40 NDP : Add SQL product info extractor
// 14/10/11 13:15 NDP : Add DB2
// 15/11/11 15:50 NDP : SQL constants are moved from SqlUtil to StatementAnalyser
// 07/12/11 18:40 NDP : SqlUtil: clean javadoc
// 15/02/12 17:50 NDP : SqlUtil: add ADAPTIVE_SERVER_ENTERPRISE & INFORMIX
// 22/02/12 15:30 NDP : SqlUtil: add SQL_ANYWHERE
// 27/02/12 19:20 NDP : SqlUtil: add HyperSQL
// 05/03/12 17:35 NDP : SqlUtil : clean database names in Javadoc

package org.awakefw.sql.api.util;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.List;

/**
 * Provides methods to get database product info & other utilities to format SQL statements.
 * 
 * @author Nicolas de Pomereu
 * @since 1.0
 */

public class SqlUtil {

    // Database product constants 
    /** Constant for defining Sybase  Adaptive Server Enterprise product */   
    public static final String ADAPTIVE_SERVER_ENTERPRISE = "Adaptive Server Enterprise";   
    
    /** Constant for defining DB2 product */      
    public static final String DB2 = "DB2";
    
    /** Constant for defining HyperSQL */   
    public static final String HSQLDB = "HSQL Database Engine";  
    
    /** Constant for defining Informix */   
    public static final String INFORMIX = "Informix Dynamic Server";  

    /** Constant for defining MySQL product */
    public static final String MYSQL = "MySQL";
    
    /** Constant for defining Oracle product */
    public static final String ORACLE = "Oracle";
    
    /** Constant for defining PostgreSQL product */
    public static final String POSTGRESQL = "PostgreSQL";
    
    /** Constant for defining Sybase  SQL Anywhere product */
    public static final String SQL_ANYWHERE = "SQL Anywhere";
    
    /** Constant for defining SQL Server product */    
    public static final String SQL_SERVER = "SQL Server";


    /** used to get the database product name */
    private String databaseProductName = null;

    /**
     * Constructor.
     * 
     * @param connection
     *            the JDBC Connection
     */
    public SqlUtil(Connection connection) throws SQLException {
	DatabaseMetaData databaseMetaData = connection.getMetaData();
	databaseProductName = databaseMetaData.getDatabaseProductName();
    }

    /**
     * Returns the database product name.
     * 
     * @return the database product name
     */
    public String getDatabaseProductName() {
	return this.databaseProductName;
    }

    /**
     * Returns true if the database engine is Adaptive Server Enterprise.
     * 
     * @return true if the database engine is Adaptive Server Enterprise
     */
    public boolean isAdaptiveServerEnterprise() {
	return isProduct(ADAPTIVE_SERVER_ENTERPRISE);
    }
    
    /**
     * Returns true if the database engine is DB2.
     * 
     * @return true if the database engine is DB2
     */

    public boolean isDB2() {
	return isProduct(DB2);
    }

    /**
     * Returns true if the database engine is HSQLDB (HyperSQL Database).
     * 
     * @return true if the database engine is HSQLDB (HyperSQL Database)
     */

    public boolean isHSQLDB() {
	return isProduct(HSQLDB);
    }

    /**
     * Returns true if the database engine is Informix.
     * 
     * @return true if the database engine is Informix
     */
    public boolean isInformix() {
	return isProduct(INFORMIX);
    }
    
    
    /**
     * Returns true if the database engine is MySQL.
     * 
     * @return true if the database engine is MySQL
     */
    public boolean isMySQL() {
	return isProduct(MYSQL);
    }

    /**
     * Returns true if the database engine is Oracle Database.
     * 
     * @return true if the database engine is Oracle Database
     */
    public boolean isOracle() {
	return isProduct(ORACLE);
    }
    
    /**
     * Returns true if the database engine is PostgreSQL.
     * 
     * @return true if the database engine is PostgreSQL
     */
    public boolean isPostgreSQL() {
	return isProduct(POSTGRESQL);
    }

    /**
     * Returns true if the database engine is SQL Anywhere.
     * 
     * @return true if the database engine is SQL Anywhere
     */

    public boolean isSQLAnywhere() {
	return isProduct(SQL_ANYWHERE);
    }    

    /**
     * Returns true if the database engine is SQL Server.
     * 
     * @return true if the database engine is SQL Server
     */

    public boolean isSQLServer() {
	return isProduct(SQL_SERVER);
    }

   
    /**
     * Returns true if the passed product (database engine) is the current
     * product.
     * 
     * @param product
     *            the product name to test
     * @return true if the actual loaded database is the product
     */
    private boolean isProduct(String product) {
	return databaseProductName.toLowerCase()
		.contains(product.toLowerCase()) ? true : false;
    }

    /**
     * Transforms a List into a SQL {@code 'IN(..., ...)'} of objects separated
     * with commas.
     * 
     * @param objects
     *            the List of objects
     * @return String of objects separated by commas for SQL {@code 'IN(..., ...)'} format.
     */

    public static String listToSqlList(List objects) {
	StringBuffer sb = new StringBuffer();

	String sQuote = (objects.get(0) instanceof String) ? "'" : "";

	for (int i = 0; i < objects.size(); i++) {
	    if (i != 0) {
		sb.append(", ");
	    }

	    sb.append(sQuote);
	    sb.append(objects.get(i));
	    sb.append(sQuote);
	}

	return sb.toString();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy