krummas / DrizzleJDBC

A BSD licensed JDBC driver for Drizzle and MySQL
BSD 3-Clause "New" or "Revised" License
31 stars 22 forks source link

fractional seconds with MySql #20

Open archmage74 opened 11 years ago

archmage74 commented 11 years ago

drizzle-jdbc drops fractions within timestamps of a MySql database. The tables were created to support fractional seconds.

We changed the listed methods as stated below. It worked for us, but we tested it only against MySQL Server (not Drizzle Server):

org.drizzle.jdbc.internal.common.AbstractValueObject:

public Timestamp getTimestamp() throws ParseException {
    if (rawBytes == null) {
        return null;
    }
    String rawValue = getString();
    SimpleDateFormat sdf;

    if (rawValue.length() > 20) {
        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    } else if (rawValue.length() >= 19) {
        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    } else {
        sdf = new SimpleDateFormat("yyyy-MM-dd");
    }
    sdf.setLenient(false);
    final java.util.Date utilTime = sdf.parse(rawValue);
    return new Timestamp(utilTime.getTime());
}

org.drizzle.jdbc.internal.common.query.parameters.TimeParameter:

public TimeParameter(final long timestamp) {
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
    byteRepresentation = ("'" + sdf.format(new Date(timestamp)) + "'").getBytes();
}

org.drizzle.jdbc.internal.common.query.parameters.TimestampParameter:

public TimestampParameter(final long timestamp) {
    final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    byteRepresentation = String.valueOf("'" + sdf.format(new Date(timestamp)) + "'").getBytes();
}

public TimestampParameter(final long timestamp, final Calendar cal) {
    final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    sdf.setCalendar(cal);
    byteRepresentation = String.valueOf("'" + sdf.format(new Date(timestamp)) + "'").getBytes();

}

org.drizzle.jdbc.internal.mysql.MySQLValueObject:

@Override
public Time getTime() throws ParseException {
    if (getBytes() == null) {
        return null;
    }
    final String rawValue = getString();
    final SimpleDateFormat sdf;
    if (rawValue.length() > 8) {
        sdf = new SimpleDateFormat("HH:mm:ss.SSS");

    } else {
        sdf = new SimpleDateFormat("HH:mm:ss");
    }
    final java.util.Date utilTime = sdf.parse(rawValue);
    return new Time(utilTime.getTime());
}