package usda.weru.weps.reports.query;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.DriverPropertyInfo;
import java.sql.SQLException;
// LEW - Jim Ascough's programmer's changes to allow compilation/running under Java 7
import java.sql.SQLFeatureNotSupportedException;
import java.util.Properties;
// LEW - Jim Ascough's programmer's changes to allow compilation/running under Java 7
import java.util.logging.Logger;
import org.apache.log4j.BasicConfigurator;

/**
 *
 * @author joelevin
 */
public class WepsDriver implements Driver {

    /**
     * The database URL prefix of this database.
     */
    public static final String URL_PREFIX = "jdbc:weps:";

    /**
     *
     * @param url
     * @return
     */
    @Override
    public boolean acceptsURL(String url) {
        return url != null && url.startsWith(URL_PREFIX);
    }

    /**
     *
     * @param url
     * @param info
     * @return
     * @throws SQLException
     */
    @Override
    public Connection connect(String url, Properties info) throws SQLException {
        try {
            if (info == null) {
                info = new Properties();
            }
            if (!acceptsURL(url)) {
                return null;
            }
            return new WepsConnection(url, info, "name");

        } catch (SQLException e) {
            throw new SQLException(e);
        }
    }

    /**
     *
     * @return
     */
    @Override
    public int getMajorVersion() {
        return 1;
    }

    /**
     *
     * @return
     */
    @Override
    public int getMinorVersion() {
        return 0;
    }

    /**
     *
     * @param url
     * @param info
     * @return
     * @throws SQLException
     */
    @Override
    public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
        return new DriverPropertyInfo[0];
    }

    /**
     *
     * @return
     */
    @Override
    public boolean jdbcCompliant() {
        return true;
    }

    /**
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        try {
            BasicConfigurator.configure();
            DriverManager.registerDriver(new WepsDriver());
            //String url = "jdbc:weps:C:\\temp\\cotton_corn\\Cotton-Corn_5.wjr,
            //C:\\usr\\joelevin\\projects\\reporting-rewrite\\weps1.install\\projects\\DisturbedLands;units=US";
            String url = "jdbc:weps:" + (args.length > 0 ? args[0] : "");

            org.h2.tools.Shell.main(new String[]{"-url", url});
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
    // LEW - Jim Ascough's programmer's changes to allow compilation/running under Java 7
    //@Override

    /**
     *
     * @return
     * @throws SQLFeatureNotSupportedException
     */
    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
