Skip to content
This repository was archived by the owner on Dec 3, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2013 Maurício Linhares
*
* Maurício Linhares licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.github.mauricio.async.db.column

import java.util.UUID

object UUIDEncoderDecoder extends ColumnEncoderDecoder {

override def decode(value: String): UUID = UUID.fromString(value)

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ object ColumnTypes {

final val MoneyArray = 791
final val NameArray = 1003
final val UUID = 2950
final val UUIDArray = 2951
final val XMLArray = 143

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class PostgreSQLColumnDecoderRegistry( charset : Charset = CharsetUtil.UTF_8 ) e
private final val timeArrayDecoder = new ArrayDecoder(TimeEncoderDecoder.Instance)
private final val timeWithTimestampArrayDecoder = new ArrayDecoder(TimeWithTimezoneEncoderDecoder)
private final val intervalArrayDecoder = new ArrayDecoder(PostgreSQLIntervalEncoderDecoder)
private final val uuidArrayDecoder = new ArrayDecoder(UUIDEncoderDecoder)

override def decode(kind: ColumnData, value: ByteBuf, charset: Charset): Any = {
decoderFor(kind.dataType).decode(kind, value, charset)
Expand Down Expand Up @@ -108,7 +109,8 @@ class PostgreSQLColumnDecoderRegistry( charset : Charset = CharsetUtil.UTF_8 ) e

case MoneyArray => this.stringArrayDecoder
case NameArray => this.stringArrayDecoder
case UUIDArray => this.stringArrayDecoder
case UUID => UUIDEncoderDecoder
case UUIDArray => this.uuidArrayDecoder
case XMLArray => this.stringArrayDecoder
case ByteA => ByteArrayEncoderDecoder

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class PostgreSQLColumnEncoderRegistry extends ColumnEncoderRegistry {
classOf[BigDecimal] -> (BigDecimalEncoderDecoder -> ColumnTypes.Numeric),
classOf[java.math.BigDecimal] -> (BigDecimalEncoderDecoder -> ColumnTypes.Numeric),

classOf[java.util.UUID] -> (UUIDEncoderDecoder -> ColumnTypes.UUID),

classOf[LocalDate] -> ( DateEncoderDecoder -> ColumnTypes.Date ),
classOf[LocalDateTime] -> (TimestampEncoderDecoder.Instance -> ColumnTypes.Timestamp),
classOf[DateTime] -> (TimestampWithTimezoneEncoderDecoder -> ColumnTypes.TimestampWithTimezone),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import org.specs2.mutable.Specification
import org.joda.time.LocalDate
import com.github.mauricio.async.db.util.Log
import com.github.mauricio.async.db.exceptions.InsufficientParametersException
import java.util.Date
import java.util.UUID
import com.github.mauricio.async.db.postgresql.exceptions.GenericDatabaseException

class PreparedStatementSpec extends Specification with DatabaseTestHelper {
Expand Down Expand Up @@ -282,6 +282,61 @@ class PreparedStatementSpec extends Specification with DatabaseTestHelper {
}
}

"support UUID" in {
if ( System.getenv("TRAVIS") == null ) {
withHandler {
handler =>
val create = """create temp table uuids
|(
|id bigserial primary key,
|my_id uuid
|);""".stripMargin

val insert = "INSERT INTO uuids (my_id) VALUES (?) RETURNING id"
val select = "SELECT * FROM uuids"

val uuid = UUID.randomUUID()

executeDdl(handler, create)
executePreparedStatement(handler, insert, Array(uuid) )
val result = executePreparedStatement(handler, select).rows.get

result(0)("my_id").asInstanceOf[UUID] === uuid
}
success
} else {
pending
}
}

"support UUID array" in {
if ( System.getenv("TRAVIS") == null ) {
withHandler {
handler =>
val create = """create temp table uuids
|(
|id bigserial primary key,
|my_id uuid[]
|);""".stripMargin

val insert = "INSERT INTO uuids (my_id) VALUES (?) RETURNING id"
val select = "SELECT * FROM uuids"

val uuid1 = UUID.randomUUID()
val uuid2 = UUID.randomUUID()

executeDdl(handler, create)
executePreparedStatement(handler, insert, Array(Array(uuid1, uuid2)) )
val result = executePreparedStatement(handler, select).rows.get

result(0)("my_id").asInstanceOf[Seq[UUID]] === Seq(uuid1, uuid2)
}
success
} else {
pending
}
}

}

}