/* JFox, the OpenSource J2EE Application Server
 *
 * Copyright (C) 2002 huihoo.com
 * Distributable under GNU LGPL license
 * See the GNU Lesser General Public License for more details.
 */

package org.jfox.jdbc.xa.microsoft;

import javax.sql.XADataSource;

import com.microsoft.jdbcx.sqlserver.SQLServerDataSource;
import org.jfox.jdbc.xa.XADataSourceFactory;

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

public class MICROSOFTXADataSourceFactory extends XADataSourceFactory {

  public XADataSource newXADataSource(String url) throws Exception {
    // url example: jdbc:microsoft:sqlserver://localhost:1433;user=x;password=y;databaseName=db;
    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;

  }

  public static void main(String[] args) {

  }
}