Skip to content

Commit

Permalink
Add ResultSetCodec.toJsonSeq (#409)
Browse files Browse the repository at this point in the history
  • Loading branch information
xerial committed Mar 2, 2019
1 parent 5b0bc77 commit 61c9627
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,40 +31,91 @@ object JDBCCodec extends LogSupport {
private def columnTypes: IndexedSeq[Int] = (1 to columnCount).map(i => md.getColumnType(i))
private lazy val columnCodecs: IndexedSeq[JDBCColumnCodec] =
(1 to columnCount).map(i => toJDBCColumnCodec(md.getColumnType(i), md.getColumnTypeName(i))).toIndexedSeq
private lazy val columnNames = (1 to columnCount).map(i => md.getColumnName(i))

/**
* Encode the all ResultSet rows as MsgPack map values
*/
def toMsgPack: Array[Byte] = {
val p = MessagePack.newBufferPacker
packAllRows(p)
packAllRowsAsMap(p)
p.toByteArray
}

def packAllRows(p: Packer): Unit = {
/**
* Encode the all ResultSet rows as JSON object values
*/
def toJsonSeq: TraversableOnce[String] = {
mapMsgPackMapRows { msgpack =>
JSONCodec.toJson(msgpack)
}
}

/**
* Pack the all ResultSet rows as MsgPack array values
*/
def packAllRowsAsArray(p: Packer): Unit = {
while (rs.next()) {
packRowAsArray(p)
}
}

/**
* pack the all ResultSet rows as MsgPack map values
*/
def packAllRowsAsMap(p: Packer): Unit = {
while (rs.next()) {
packRow(p)
packRowAsMap(p)
}
}

private class RStoMsgPackIterator[A](f: Array[Byte] => A) extends Iterator[A] {
private class RStoMsgPackIterator[A](f: Array[Byte] => A, packer: Packer => Unit) extends Iterator[A] {
override def hasNext: Boolean = rs.next()
override def next(): A = {
val p = MessagePack.newBufferPacker
packRow(p)
packer(p)
f(p.toByteArray)
}
}

def mapMsgPackRows[U](f: Array[Byte] => U): TraversableOnce[U] = {
new RStoMsgPackIterator[U](f)
/**
* Create an interator for reading ResultSet as a sequence of MsgPack Map values
*/
def mapMsgPackMapRows[U](f: Array[Byte] => U): TraversableOnce[U] = {
new RStoMsgPackIterator[U](f, packer = packRowAsMap(_))
}

/**
* Create an interator for reading ResultSet as a sequence of MsgPack array values
*/
def mapMsgPackArrayRows[U](f: Array[Byte] => U): TraversableOnce[U] = {
new RStoMsgPackIterator[U](f, packer = packRowAsArray(_))
}

def packRow(p: Packer): Unit = {
/**
* Read a row from ResultSet and pack as a MsgPack array
*/
def packRowAsArray(p: Packer): Unit = {
p.packArrayHeader(columnCount)
var col = 1
while (col <= columnCount) {
columnCodecs(col - 1).pack(p, rs, col)
col += 1
}
}

/**
* Read a row from ResultSet and pack as a MsgPack map
*/
def packRowAsMap(p: Packer): Unit = {
p.packMapHeader(columnCount)
var col = 1
while (col <= columnCount) {
p.packString(columnNames(col - 1))
columnCodecs(col - 1).pack(p, rs, col)
col += 1
}
}
}

def toJDBCColumnCodec(sqlType: Int, typeName: String): JDBCColumnCodec = {
Expand Down Expand Up @@ -346,7 +397,7 @@ object JDBCCodec extends LogSupport {
}
}

object JavaSqlArrayCodec extends LogSupport {
object JavaSqlArrayCodec extends MessageCodec[java.sql.Array] with LogSupport {
def pack(p: Packer, v: java.sql.Array): Unit = {
val arr: AnyRef = v.getArray
arr match {
Expand All @@ -369,13 +420,13 @@ object JDBCCodec extends LogSupport {
case a: Array[Boolean] =>
BooleanArrayCodec.pack(p, a)
case a: Array[AnyRef] =>
debug(s"element class: ${a.head.getClass}")
throw new UnsupportedOperationException(
s"Reading array type of ${arr.getClass} is not supported:\n${a.mkString(", ")}")
case other =>
throw new UnsupportedOperationException(s"Reading array type of ${arr.getClass} is not supported: ${arr}")
}
}
override def unpack(u: Unpacker, v: MessageHolder): Unit = ???
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,17 @@ class JDBCCodecTest extends AirframeSpec {
JavaSqlArrayCodec.pack(p, MockArray(Seq(1, 2, 3)))
}
}

"ResultSet to JSON maps" taggedAs working in {
withQuery("""with a(id, name) as
|(select * from (values (1, 'leo'), (2, 'yui')))
|select * from a
|""".stripMargin) { rs =>
val jsonSeq = JDBCCodec(rs).toJsonSeq.toIndexedSeq
jsonSeq(0) shouldBe """{"id":1,"name":"leo"}"""
jsonSeq(1) shouldBe """{"id":2,"name":"yui"}"""
}
}
}

case class MockArray(v: AnyRef) extends java.sql.Array {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ object HttpRecord extends LogSupport {
private[recorder] def read(rs: ResultSet): Seq[HttpRecord] = {
val resultSetCodec = JDBCCodec(rs)
resultSetCodec
.mapMsgPackRows(msgpack => recordCodec.unpackBytes(msgpack))
.mapMsgPackMapRows(msgpack => recordCodec.unpackBytes(msgpack))
.filter(_.isDefined)
.map(_.get)
.toSeq
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ResultSetReader(rs: ResultSet) extends TabletReader with LogSupport {
None
} else {
val p = MessagePack.newBufferPacker
codec.packRow(p)
codec.packRowAsArray(p)
val arr = p.toByteArray
Some(MessagePackRecord(arr))
}
Expand Down

0 comments on commit 61c9627

Please sign in to comment.