package com.uas.search.console.b2b.jms;

import java.sql.SQLException;

import oracle.jdbc.OracleTypes;
import oracle.jdbc.driver.OracleConnection;
import oracle.jpub.runtime.MutableStruct;
import oracle.sql.CustomDatum;
import oracle.sql.CustomDatumFactory;
import oracle.sql.Datum;
import oracle.sql.STRUCT;

/**
 * 对数据库的数据格式进行转换
 * 
 * @author sunyj
 * @since 2016年7月7日 下午8:49:44
 */
@SuppressWarnings("deprecation")
public class QueueMessageTypeFactory implements CustomDatum, CustomDatumFactory {

	public static final int SQL_TYPECODE = OracleTypes.STRUCT;

	MutableStruct struct;

	// 12表示字符串
	static int[] sqlType = { 12 };
	static CustomDatumFactory[] factory = new CustomDatumFactory[1];
	static final QueueMessageTypeFactory messageFactory = new QueueMessageTypeFactory();

	public QueueMessageTypeFactory() {
		struct = new MutableStruct(new Object[1], sqlType, factory);
	}

	public static CustomDatumFactory getFactory() {
		return messageFactory;
	}

	@Override
	public CustomDatum create(Datum datum, int sqlType) throws SQLException {
		if (datum == null) {
			return null;
		}
		QueueMessageTypeFactory queueMessageType = new QueueMessageTypeFactory();
		queueMessageType.struct = new MutableStruct((STRUCT) datum, QueueMessageTypeFactory.sqlType, factory);
		return queueMessageType;
	}

	@Override
	public Datum toDatum(OracleConnection connection) throws SQLException {
		return struct.toDatum(connection, "QueueMessageTypeFactory");
	}

	public String getMessage() throws SQLException {
		return (String) struct.getAttribute(0);
	}

}
