Skip to content

Commit 4a9a9fd

Browse files
committed
Add support for UUID
1 parent a6aabd1 commit 4a9a9fd

File tree

5 files changed

+87
-2
lines changed

5 files changed

+87
-2
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2013 Maurício Linhares
3+
*
4+
* Maurício Linhares licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
package com.github.mauricio.async.db.column
18+
19+
import java.util.UUID
20+
21+
object UUIDEncoderDecoder extends ColumnEncoderDecoder {
22+
23+
override def decode(value: String): UUID = UUID.fromString(value)
24+
25+
}

postgresql-async/src/main/scala/com/github/mauricio/async/db/postgresql/column/ColumnTypes.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ object ColumnTypes {
6363

6464
final val MoneyArray = 791
6565
final val NameArray = 1003
66+
final val UUID = 2950
6667
final val UUIDArray = 2951
6768
final val XMLArray = 143
6869

postgresql-async/src/main/scala/com/github/mauricio/async/db/postgresql/column/PostgreSQLColumnDecoderRegistry.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class PostgreSQLColumnDecoderRegistry( charset : Charset = CharsetUtil.UTF_8 ) e
4545
private final val timeArrayDecoder = new ArrayDecoder(TimeEncoderDecoder.Instance)
4646
private final val timeWithTimestampArrayDecoder = new ArrayDecoder(TimeWithTimezoneEncoderDecoder)
4747
private final val intervalArrayDecoder = new ArrayDecoder(PostgreSQLIntervalEncoderDecoder)
48+
private final val uuidArrayDecoder = new ArrayDecoder(UUIDEncoderDecoder)
4849

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

109110
case MoneyArray => this.stringArrayDecoder
110111
case NameArray => this.stringArrayDecoder
111-
case UUIDArray => this.stringArrayDecoder
112+
case UUID => UUIDEncoderDecoder
113+
case UUIDArray => this.uuidArrayDecoder
112114
case XMLArray => this.stringArrayDecoder
113115
case ByteA => ByteArrayEncoderDecoder
114116

postgresql-async/src/main/scala/com/github/mauricio/async/db/postgresql/column/PostgreSQLColumnEncoderRegistry.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class PostgreSQLColumnEncoderRegistry extends ColumnEncoderRegistry {
5252
classOf[BigDecimal] -> (BigDecimalEncoderDecoder -> ColumnTypes.Numeric),
5353
classOf[java.math.BigDecimal] -> (BigDecimalEncoderDecoder -> ColumnTypes.Numeric),
5454

55+
classOf[java.util.UUID] -> (UUIDEncoderDecoder -> ColumnTypes.UUID),
56+
5557
classOf[LocalDate] -> ( DateEncoderDecoder -> ColumnTypes.Date ),
5658
classOf[LocalDateTime] -> (TimestampEncoderDecoder.Instance -> ColumnTypes.Timestamp),
5759
classOf[DateTime] -> (TimestampWithTimezoneEncoderDecoder -> ColumnTypes.TimestampWithTimezone),

postgresql-async/src/test/scala/com/github/mauricio/async/db/postgresql/PreparedStatementSpec.scala

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import org.specs2.mutable.Specification
2020
import org.joda.time.LocalDate
2121
import com.github.mauricio.async.db.util.Log
2222
import com.github.mauricio.async.db.exceptions.InsufficientParametersException
23-
import java.util.Date
23+
import java.util.UUID
2424
import com.github.mauricio.async.db.postgresql.exceptions.GenericDatabaseException
2525

2626
class PreparedStatementSpec extends Specification with DatabaseTestHelper {
@@ -282,6 +282,61 @@ class PreparedStatementSpec extends Specification with DatabaseTestHelper {
282282
}
283283
}
284284

285+
"support UUID" in {
286+
if ( System.getenv("TRAVIS") == null ) {
287+
withHandler {
288+
handler =>
289+
val create = """create temp table uuids
290+
|(
291+
|id bigserial primary key,
292+
|my_id uuid
293+
|);""".stripMargin
294+
295+
val insert = "INSERT INTO uuids (my_id) VALUES (?) RETURNING id"
296+
val select = "SELECT * FROM uuids"
297+
298+
val uuid = UUID.randomUUID()
299+
300+
executeDdl(handler, create)
301+
executePreparedStatement(handler, insert, Array(uuid) )
302+
val result = executePreparedStatement(handler, select).rows.get
303+
304+
result(0)("my_id").asInstanceOf[UUID] === uuid
305+
}
306+
success
307+
} else {
308+
pending
309+
}
310+
}
311+
312+
"support UUID array" in {
313+
if ( System.getenv("TRAVIS") == null ) {
314+
withHandler {
315+
handler =>
316+
val create = """create temp table uuids
317+
|(
318+
|id bigserial primary key,
319+
|my_id uuid[]
320+
|);""".stripMargin
321+
322+
val insert = "INSERT INTO uuids (my_id) VALUES (?) RETURNING id"
323+
val select = "SELECT * FROM uuids"
324+
325+
val uuid1 = UUID.randomUUID()
326+
val uuid2 = UUID.randomUUID()
327+
328+
executeDdl(handler, create)
329+
executePreparedStatement(handler, insert, Array(Array(uuid1, uuid2)) )
330+
val result = executePreparedStatement(handler, select).rows.get
331+
332+
result(0)("my_id").asInstanceOf[Seq[UUID]] === Seq(uuid1, uuid2)
333+
}
334+
success
335+
} else {
336+
pending
337+
}
338+
}
339+
285340
}
286341

287342
}

0 commit comments

Comments
 (0)