/* JFox, the OpenSource J2EE Application Server
 *
 * Distributable under GNU LGPL license by gun.org
 * more details please visit http://www.huihoo.org/jfox
 */

package org.jfox.jdbc.xa;

import javax.sql.XADataSource;

import com.microsoft.jdbcx.sqlserver.SQLServerDataSource;

/**
 * 支持 SQL Server JDBC 驱动
 *
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class MICROSOFTXADataSource extends AbstractXADataSource {

    public MICROSOFTXADataSource(String url) {
        super(url);
    }

    public XADataSource init(String url) {
        // url example: jdbc:microsoft:sqlserver://localhost:1433;user=x;password=y;databaseName=db;
        try {
            SQLServerDataSource sqlXaDS = new SQLServerDataSource();

            URLProperties urlProp = parseSqlURL(url);

            if(urlProp.containsKey("serverName")) {
                sqlXaDS.setServerName(urlProp.getProperty("serverName"));
            }

            if(urlProp.containsKey("portNumber")) {
                sqlXaDS.setPortNumber(Integer.parseInt(urlProp.getProperty("portNumber")));
            }

            if(urlProp.containsKey("databaseName")) {
                sqlXaDS.setDatabaseName(urlProp.getProperty("databaseName"));
            }

            if(urlProp.containsKey("embedded")) {
                sqlXaDS.setEmbedded(new Boolean(urlProp.getProperty("embedded")).booleanValue());
            }

            if(urlProp.containsKey("batchPerformanceWorkaround")) {
                sqlXaDS.setBatchPerformanceWorkaround(new Boolean(urlProp.getProperty("portNumber")).booleanValue());
            }

            if(urlProp.containsKey("selectMethod")) {
                sqlXaDS.setSelectMethod(urlProp.getProperty("selectMethod"));
            }
            else {
                // sqlserver 要使用 jta，必须设置 selectMethod=cursor
                sqlXaDS.setSelectMethod("cursor");
            }
            return sqlXaDS;
        }
        catch(Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    public static void main(String[] args) {

    }
}