Skip to content
This repository was archived by the owner on Dec 3, 2019. It is now read-only.

Add support for MySQL BINARY/VARBINARY types #55

Merged
merged 1 commit into from
Dec 1, 2013

Conversation

kxbmap
Copy link
Contributor

@kxbmap kxbmap commented Oct 30, 2013

No description provided.

@mauricio
Copy link
Owner

Can you elaborate on this change? Why are strings not strings anymore?

@mauricio
Copy link
Owner

mauricio commented Nov 3, 2013

ping @kxbmap

@kxbmap
Copy link
Contributor Author

kxbmap commented Nov 7, 2013

Hi. Sorry for late response.

I want to store UUID and InetAddress as binary with index.

DDL is

CREATE TABLE t (
  id BIGINT NOT NULL AUTO_INCREMENT,
  uuid BINARY(16) NOT NULL,
  address VARBINARY(16) NOT NULL,
  PRIMARY KEY (id),
  INDEX idx_t_uuid (uuid),
  INDEX idx_t_address (address)
);

and code for insert is

val insert = "INSERT INTO t (uuid, address) VALUES (?, ?)"
val uuid = MyUUIDUtil.asByteArray(UUID.randomUUID())
val address = InetAddress.getLocalHost.getAddress

Await.result(connection.sendPreparedStatement(insert, Seq(uuid, address)), 5.seconds)

This code works fine. uuid and address are stored in DB as binary.

Problem is

connection.sendQuery("SELECT * FROM t") andThen {
  case Success(result) =>
    val row = result.rows.get(0)
    println(row(0))
    println(MyUUIDUtil.fromByteArray(row(1).asInstanceOf[Array[Byte]])
    println(InetAddress.getByAddress(row(2).asInstanceOf[Array[Byte]])
}

This code throws ClassCastException because row(1) (and row(2)) returns String.

row(1).asInstanceOf[String].getBytes("UTF-8")
// or
row(1).asInstanceOf[String].getBytes("ISO-8859-1")

And this code returns bytes to differ from original bytes.

I seem to there is no way to get the original bytes.

This commit will solve the problem.
But I don't have confidence. I wrote change by printf debug.

@mauricio
Copy link
Owner

Cool, gonna check that.

mauricio added a commit that referenced this pull request Dec 1, 2013
Add support for MySQL BINARY/VARBINARY types
@mauricio mauricio merged commit fcd53aa into mauricio:master Dec 1, 2013
@mauricio
Copy link
Owner

mauricio commented Dec 1, 2013

And it's finally in, sorry for the delay.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants