/*
 * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

/*
 * @(#)fnSchema.java	1.10 02/01/12
 */


package com.sun.cts.tests.jdbc.ee.common;

import java.io.*;
import java.util.*;
import java.sql.*;

import com.sun.cts.util.*;
import com.sun.cts.harness.*;

import java.rmi.RemoteException;


/**
 * The fnSchema class creates the database using 
 * Sun's J2EE Reference Implementation.
 * @author  
 * @version 1.7, 06/16/99
 */

public class fnSchema extends ServiceEETest  
{
			  
	public void createTable(Properties props, Connection conn) throws RemoteException 
	{  
		String insertString1 = null;
		String insertString2 = null;

		TestUtil.logTrace("createTable");

		
		// drop Table if it exists
		try
		{
			dropTable(props,conn);
		} 
		catch (Exception e) 
		{
			TestUtil.logTrace("SQLException encountered while deleting rows from tables:" + e.getMessage() );
		}

		try
		{
			Statement stmt = conn.createStatement(); 

			insertString1 = props.getProperty("Fnschema_Tab1_Insert","");

			TestUtil.logTrace("insertString1: " + insertString1);
		        TestUtil.logMsg("Executed: '" + insertString1 + "' successfully");
			stmt.executeUpdate(insertString1);

			insertString2 = props.getProperty("Fnschema_Tab2_Insert","");

			TestUtil.logTrace("insertString2: " + insertString2);
		        TestUtil.logMsg("Executed: '" + insertString2 + "' successfully");
			stmt.executeUpdate(insertString2);

			stmt.close();
		}
		catch (SQLException e) 
		{ 
			TestUtil.logErr("SQLException while inserting data");
			TestUtil.logErr(e.getMessage());
			dropTable(props, conn);
			throw new RemoteException( e.getMessage() );
		}      
	}
	public void dropTable(Properties props, Connection conn) throws RemoteException 
	{   
		TestUtil.logTrace("dropTable");	
		// Delete the Tables
		String dropString1 = props.getProperty("Fnschema_Tab1_Delete","");
		String dropString2 = props.getProperty("Fnschema_Tab2_Delete","");
		try
		{
			//Since the scrollable resultSet is optional, the
			//parameters are commented out.
			Statement stmt = conn.createStatement(/*ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY*/);
			stmt.executeUpdate(dropString1);
		        TestUtil.logMsg("Executed: '" + dropString1 + "' successfully");
			stmt.executeUpdate(dropString2);
		        TestUtil.logMsg("Executed: '" + dropString2 + "' successfully");
			stmt.close();
		} 
		catch (SQLException e) 
		{ 
			throw new RemoteException( e.getMessage() );
		}      
	}

	public void dbUnConnect(Connection conn) throws RemoteException 
	{
		TestUtil.logTrace("dbUnConnect");
		// Close the DB connections
		try
		{
			conn.close();
			TestUtil.logMsg("Closed the Database connection");
		} 
		catch (Exception e) 
		{
			TestUtil.logErr("Exception occured while trying to close the DB connection");
			throw new RemoteException( e.getMessage() );
		}
	}

	public boolean isStringFuncFound(String funcName, Connection conn) throws RemoteException
	{
		TestUtil.logTrace("isStringFuncFound");
		//Check if the String Function is supported by the DBMS
			
		try
		{
			DatabaseMetaData dbmd=null;
			dbmd=conn.getMetaData();
			String strFuncList=dbmd.getStringFunctions();
			String formatStrFuncList=',' + strFuncList +  ',';
			String formatFuncName=',' + funcName + ',';
			int index;
			index=formatStrFuncList.indexOf(formatFuncName);
			if(index>=0)
				return true;
			else
				return false;
		}
		catch(Exception e)
		{
			TestUtil.logErr("Exception thrown while trying to check the presence of the string func.");
			throw new RemoteException(e.getMessage());
		}
	}

	public boolean isSystemFuncFound(String funcName, Connection conn) throws RemoteException
	{
		TestUtil.logTrace("isSystemFuncFound");
		//Check if the System Function is supported by the DBMS
			
		try
		{
			DatabaseMetaData dbmd=null;
			dbmd=conn.getMetaData();
			String systemFuncList=dbmd.getSystemFunctions();
			String formatSystemFuncList=',' + systemFuncList +  ',';
			String formatFuncName=',' + funcName + ',';
			int index;
			index=formatSystemFuncList.indexOf(formatFuncName);
			if(index>=0)
				return true;
			else
				return false;
		}
		catch(Exception e)
		{
			TestUtil.logErr("Exception thrown while trying to check the presence of the system func.");
			throw new RemoteException(e.getMessage());
		}
	}

	public boolean isNumericFuncFound(String funcName, Connection conn) throws RemoteException
	{
		TestUtil.logTrace("isNumericFuncFound");
		//Check if the Numeric Function is supported by the DBMS
			
		try
		{
			DatabaseMetaData dbmd=null;
			dbmd=conn.getMetaData();
			String numericFuncList=dbmd.getNumericFunctions();
			String formatNumericFuncList=',' + numericFuncList +  ',';
			String formatFuncName=',' + funcName + ',';
			int index;
			index=formatNumericFuncList.indexOf(formatFuncName);
			if(index>=0)
				return true;
			else
				return false;
		}
		catch(Exception e)
		{
			TestUtil.logErr("Exception thrown while trying to check the presence of the Numeric func.");
			throw new RemoteException(e.getMessage());
		}
	}

	public boolean isTimeDateFuncFound(String funcName, Connection conn) throws RemoteException
	{
		TestUtil.logTrace("isTimeDateFuncFound");
		//Check if the Time & Date Function is supported by the DBMS
			
		try
		{
			DatabaseMetaData dbmd=null;
			dbmd=conn.getMetaData();
			String timeDateFuncList=dbmd.getTimeDateFunctions();
			String formatTimeDateFuncList=',' + timeDateFuncList +  ',';
			String formatFuncName=',' + funcName + ',';
			int index;
			index=formatTimeDateFuncList.indexOf(formatFuncName);
			if(index>=0)
				return true;
			else
				return false;
		}
		catch(Exception e)
		{
			TestUtil.logErr("Exception thrown while trying to check the presence of the Time & Date func.");
			throw new RemoteException(e.getMessage());
		}
	}
}
