Java SQL Type decodeJavaType(Object pType)

Here you can find the source of decodeJavaType(Object pType)

Description

This is a private helper method that will decode java types to the required SQL formatting.

License

Open Source License

Parameter

Parameter Description
pType The java <code>Object</code>.

Declaration

protected static String decodeJavaType(Object pType) 

Method Source Code

//package com.java2s;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    protected final static String DATE_FORMAT = "yyyy-MM-dd HH24:mi:ss";
    protected final static SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    protected final static SimpleDateFormat SIMPLE_TIMESTAMP_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    protected final static String SQL_BOOLEAN_FALSE = "0";
    protected final static String SQL_BOOLEAN_TRUE = "1";

    /**/*from www . j a  va 2s  .  com*/
     * This is a private helper method that will decode java types to the
     * required SQL formatting.
     * 
     * @param pType The java <code>Object</code>.
     * 
     * @author Stephen Hardy
     */
    protected static String decodeJavaType(Object pType) {
        StringBuffer mSQL = new StringBuffer();
        if (null != pType) {
            if (pType instanceof String) {
                //  needs to be surrounded by ' '
                mSQL.append("'");
                mSQL.append(pType);
                mSQL.append("'");
            } else if (pType instanceof Timestamp) {
                mSQL.append(normalize((Timestamp) pType));
            } else if (pType instanceof Date) {
                //  format as described in DATE_FORMAT
                mSQL.append(normalize((Date) pType));
            } else if (pType instanceof Boolean) {
                mSQL.append(normalize((Boolean) pType));
            } else {
                //  default to the toString of the object
                mSQL.append(pType.toString());
            }
        } else {
            mSQL.append("null");
        }
        return mSQL.toString();
    }

    /**
     * This is a private helper method that will decode a java date to the
     * required SQL formatting.
     * 
     * @param pType The java <code>Object</code>.
     * 
     * @author Stephen Hardy
     */
    private static String normalize(Date pDate) {
        StringBuffer mSqlDate = new StringBuffer();
        if (null != pDate) {
            mSqlDate.append("To_Date('");
            mSqlDate.append(SIMPLE_DATE_FORMAT.format(pDate));
            mSqlDate.append("','");
            mSqlDate.append(DATE_FORMAT);
            mSqlDate.append("')");
        } else {
            mSqlDate.append("null");
        }
        return mSqlDate.toString();
    }

    /**
     * This is a private helper method that will decode a java timestamp to the
     * required SQL formatting.
     * 
     * @param pType The java <code>Object</code>.
     * 
     * @author Stephen Hardy
     */
    private static String normalize(Timestamp pTimeStamp) {
        StringBuffer mSqlDate = new StringBuffer();
        if (null != pTimeStamp) {
            mSqlDate.append("To_Timestamp('");
            mSqlDate.append(SIMPLE_TIMESTAMP_FORMAT.format(pTimeStamp));
            mSqlDate.append("','");
            mSqlDate.append(DATE_FORMAT);
            mSqlDate.append("')");
        } else {
            mSqlDate.append("null");
        }
        return mSqlDate.toString();
    }

    /**
     * This is a private helper method that will decode a java boolean to the
     * required SQL formatting.
     * 
     * @param pType The java <code>Object</code>.
     * 
     * @author Stephen Hardy
     */
    private static String normalize(Boolean pBoolean) {
        StringBuffer mSqlBoolean = new StringBuffer();
        if (null != pBoolean) {
            if (pBoolean.booleanValue()) {
                mSqlBoolean.append(SQL_BOOLEAN_TRUE);
            } else {
                mSqlBoolean.append(SQL_BOOLEAN_FALSE);
            }
        } else {
            mSqlBoolean.append("null");
        }
        return mSqlBoolean.toString();
    }
}

Related

  1. createArrTypSql(String[] arr)
  2. createSignature(Method m)
  3. createSqlDataType(@Nonnull final String javaDataType)
  4. createTable2()
  5. createTestData()
  6. deleteData(Connection conn)
  7. deleteFromAllTables(Connection con)
  8. deserializeSQLTypesToJava(String sqltypename, String value)
  9. dumpTypes(Connection pConnection)