<commit_before>package data_objects.drivers;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.Map;
import java.util.Properties;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.jruby.Ruby;
import org.jruby.RubyBigDecimal;
import org.jruby.RubyBignum;
import org.jruby.RubyClass;
import org.jruby.RubyFixnum;
import org.jruby.RubyFloat;
import org.jruby.RubyHash;
import org.jruby.RubyNumeric;
import org.jruby.RubyObjectAdapter;
import org.jruby.RubyProc;
import org.jruby.RubyRegexp;
import org.jruby.RubyString;
import org.jruby.RubyTime;
import org.jruby.exceptions.RaiseException;
import org.jruby.javasupport.JavaEmbedUtils;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.marshal.UnmarshalStream;
import org.jruby.util.ByteList;

import data_objects.RubyType;
import java.lang.UnsupportedOperationException;

/**
 *
 * @author alexbcoles
 * @author mkristian
 */
public abstract class AbstractDriverDefinition implements DriverDefinition {

    // assuming that API is thread safe
    protected static final RubyObjectAdapter API = JavaEmbedUtils
            .newObjectAdapter();

    protected final static DateTimeFormatter DATE_TIME_FORMAT = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");

    private final String scheme;
    private final String jdbcScheme;
    private final String moduleName;

    protected AbstractDriverDefinition(String scheme, String moduleName) {
        this(scheme, scheme, moduleName);
    }

    protected AbstractDriverDefinition(String scheme, String jdbcScheme,
            String moduleName) {
        this.scheme = scheme;
        this.jdbcScheme = jdbcScheme;
        this.moduleName = moduleName;
    }

    public String getModuleName() {
        return this.moduleName;
    }

    public String getErrorName() {
        return this.moduleName + "Error";
    }

    @SuppressWarnings("unchecked")
    public URI parseConnectionURI(IRubyObject connection_uri)
            throws URISyntaxException, UnsupportedEncodingException {
        URI uri;

        if ("DataObjects::URI".equals(connection_uri.getType().getName())) {
            String query;
            StringBuffer userInfo = new StringBuffer();

            verifyScheme(stringOrNull(API.callMethod(connection_uri, "scheme")));

            String user = stringOrNull(API.callMethod(connection_uri, "user"));
            String password = stringOrNull(API.callMethod(connection_uri,
                    "password"));
            String host = stringOrNull(API.callMethod(connection_uri, "host"));
            int port = intOrMinusOne(API.callMethod(connection_uri, "port"));
            String path = stringOrNull(API.callMethod(connection_uri, "path"));
            IRubyObject query_values = API.callMethod(connection_uri, "query");
            String fragment = stringOrNull(API.callMethod(connection_uri,
                    "fragment"));

            if (user != null && !"".equals(user)) {
                userInfo.append(user);
                if (password != null && !"".equals(password)) {
                    userInfo.append(":").append(password);
                }
            }

            if (query_values.isNil()) {
                query = null;
            } else if (query_values instanceof RubyHash) {
                query = mapToQueryString(query_values.convertToHash());
            } else {
                query = API.callMethod(query_values, "to_s").asJavaString();
            }

            if (host != null && !"".equals(host)) {
                // a client/server database (e.g. MySQL, PostgreSQL, MS
                // SQLServer)
                uri = new URI(this.jdbcScheme, userInfo.toString(), host, port,
                        path, query, fragment);
            } else {
                // an embedded / file-based database (e.g. SQLite3, Derby
                // (embedded mode), HSQLDB - use opaque uri
                uri = new URI(this.jdbcScheme, path, fragment);
            }<commit_msg>[do_jdbc] Fix path handling when connecting with a URI<commit_after>package data_objects.drivers;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.Map;
import java.util.Properties;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.jruby.Ruby;
import org.jruby.RubyBigDecimal;
import org.jruby.RubyBignum;
import org.jruby.RubyClass;
import org.jruby.RubyFixnum;
import org.jruby.RubyFloat;
import org.jruby.RubyHash;
import org.jruby.RubyNumeric;
import org.jruby.RubyObjectAdapter;
import org.jruby.RubyProc;
import org.jruby.RubyRegexp;
import org.jruby.RubyString;
import org.jruby.RubyTime;
import org.jruby.exceptions.RaiseException;
import org.jruby.javasupport.JavaEmbedUtils;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.marshal.UnmarshalStream;
import org.jruby.util.ByteList;

import data_objects.RubyType;
import java.lang.UnsupportedOperationException;

/**
 *
 * @author alexbcoles
 * @author mkristian
 */
public abstract class AbstractDriverDefinition implements DriverDefinition {

    // assuming that API is thread safe
    protected static final RubyObjectAdapter API = JavaEmbedUtils
            .newObjectAdapter();

    protected final static DateTimeFormatter DATE_TIME_FORMAT = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");

    private final String scheme;
    private final String jdbcScheme;
    private final String moduleName;

    protected AbstractDriverDefinition(String scheme, String moduleName) {
        this(scheme, scheme, moduleName);
    }

    protected AbstractDriverDefinition(String scheme, String jdbcScheme,
            String moduleName) {
        this.scheme = scheme;
        this.jdbcScheme = jdbcScheme;
        this.moduleName = moduleName;
    }

    public String getModuleName() {
        return this.moduleName;
    }

    public String getErrorName() {
        return this.moduleName + "Error";
    }

    @SuppressWarnings("unchecked")
    public URI parseConnectionURI(IRubyObject connection_uri)
            throws URISyntaxException, UnsupportedEncodingException {
        URI uri;

        if ("DataObjects::URI".equals(connection_uri.getType().getName())) {
            String query;
            StringBuffer userInfo = new StringBuffer();

            verifyScheme(stringOrNull(API.callMethod(connection_uri, "scheme")));

            String user = stringOrNull(API.callMethod(connection_uri, "user"));
            String password = stringOrNull(API.callMethod(connection_uri,
                    "password"));
            String host = stringOrNull(API.callMethod(connection_uri, "host"));
            int port = intOrMinusOne(API.callMethod(connection_uri, "port"));
            String path = stringOrNull(API.callMethod(connection_uri, "path"));
            IRubyObject query_values = API.callMethod(connection_uri, "query");
            String fragment = stringOrNull(API.callMethod(connection_uri,
                    "fragment"));

            if (user != null && !"".equals(user)) {
                userInfo.append(user);
                if (password != null && !"".equals(password)) {
                    userInfo.append(":").append(password);
                }
            }

            if (query_values.isNil()) {
                query = null;
            } else if (query_values instanceof RubyHash) {
                query = mapToQueryString(query_values.convertToHash());
            } else {
                query = API.callMethod(query_values, "to_s").asJavaString();
            }

            if (host != null && !"".equals(host)) {
                // a client/server database (e.g. MySQL, PostgreSQL, MS
                // SQLServer)
                String normalizedPath;
                if (path != null && path.length() > 0 && path.charAt(0) != '/') {
                    normalizedPath = '/' + path;
                } else {
                    normalizedPath = path;
                }
                uri = new URI(this.jdbcScheme, userInfo.toString(), host, port,
                        normalizedPath, query, fragment);
            } else {
                // an embedded / file-based database (e.g. SQLite3, Derby
                // (embedded mode), HSQLDB - use opaque uri
                uri = new URI(this.jdbcScheme, path, fragment);
            }
{'fraction_non_alphanumeric': 0.10254450147428197, 'fraction_numerical': 0.00043682428743038114, 'mean_word_length': 2.7137064071370642, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2016942', 'n_tokens_mistral': 2645, 'n_tokens_neox': 2463, 'n_words': 555}