@@ -2,19 +2,17 @@ package io.vertx.asyncsql.database
2
2
3
3
import scala .collection .JavaConverters .iterableAsScalaIterableConverter
4
4
import scala .concurrent .Future
5
- import org .vertx .scala .core .eventbus .Message
6
- import org .vertx .scala .core .json .{ JsonArray , JsonObject }
5
+ import org .vertx .scala .core .json .{JsonElement , JsonArray , JsonObject , Json }
7
6
import org .vertx .scala .core .logging .Logger
8
- import org .vertx .scala .platform .Verticle
9
7
import com .github .mauricio .async .db .{ Configuration , Connection , QueryResult , RowData }
10
8
import com .github .mauricio .async .db .postgresql .exceptions .GenericDatabaseException
11
9
import io .vertx .asyncsql .database .pool .AsyncConnectionPool
12
- import org .vertx .scala .core .json .Json
13
10
import org .vertx .scala .mods .ScalaBusMod
14
11
import org .vertx .scala .mods .replies ._
15
12
import org .vertx .scala .core .Vertx
16
13
import org .vertx .scala .platform .Container
17
14
import io .vertx .asyncsql .Starter
15
+ import org .vertx .scala .mods .ScalaBusMod .Receive
18
16
19
17
trait ConnectionHandler extends ScalaBusMod {
20
18
val verticle : Starter
@@ -32,23 +30,24 @@ trait ConnectionHandler extends ScalaBusMod {
32
30
def statementDelimiter : String = " ;"
33
31
34
32
import org .vertx .scala .core .eventbus ._
35
- override def receive (msg : Message [JsonObject ]) = {
33
+ override def receive : Receive = (msg : Message [JsonObject ]) => {
36
34
case " select" => select(msg.body)
37
35
case " insert" => insert(msg.body)
38
36
case " prepared" => AsyncReply (sendWithPool(prepared(msg.body)))
39
37
case " transaction" => transaction(msg.body)
40
38
case " raw" => AsyncReply (sendWithPool(rawCommand(msg.body.getString(" command" ))))
41
39
}
42
40
43
- def close () = pool.close
41
+ def close () = pool.close()
44
42
45
43
protected def escapeField (str : String ): String = " \" " + str.replace(" \" " , " \"\" " ) + " \" "
46
44
protected def escapeString (str : String ): String = " '" + str.replace(" '" , " ''" ) + " '"
47
45
48
46
protected def escapeValue (v : Any ): String = v match {
49
- case v : Int => v.toString
50
- case v : Boolean => v.toString
51
- case v => escapeString(v.toString)
47
+ case x : Int => x.toString
48
+ case x : Boolean => x.toString
49
+ case null => " NULL"
50
+ case x => escapeString(x.toString)
52
51
}
53
52
54
53
protected def selectCommand (json : JsonObject ): String = {
@@ -93,7 +92,7 @@ trait ConnectionHandler extends ScalaBusMod {
93
92
94
93
Option (json.getArray(" statements" )) match {
95
94
case Some (statements) => c.inTransaction { conn : Connection =>
96
- val futures = ( statements.asScala.map {
95
+ val futures = statements.asScala.map {
97
96
case js : JsonObject =>
98
97
js.getString(" action" ) match {
99
98
case " select" => Raw (selectCommand(js))
@@ -102,8 +101,8 @@ trait ConnectionHandler extends ScalaBusMod {
102
101
case " raw" => Raw (js.getString(" command" ))
103
102
}
104
103
case _ => throw new IllegalArgumentException (" 'statements' needs JsonObjects!" )
105
- })
106
- val f = ( futures.foldLeft(Future [Any ]()) { case (f , cmd) => f flatMap (_ => cmd.query(conn)) })
104
+ }
105
+ val f = futures.foldLeft(Future [Any ]()) { case (fut , cmd) => fut flatMap (_ => cmd.query(conn)) }
107
106
f map (_ => Ok (Json .obj()))
108
107
}
109
108
case None => throw new IllegalArgumentException (" No 'statements' field in request!" )
@@ -149,5 +148,15 @@ trait ConnectionHandler extends ScalaBusMod {
149
148
Ok (result)
150
149
}
151
150
152
- private def rowDataToJsonArray (rowData : RowData ): JsonArray = Json .arr(rowData.toList: _* )
151
+ private def dataToJson (data : Any ): Any = data match {
152
+ case null => null
153
+ case x : Boolean => x
154
+ case x : Number => x
155
+ case x : String => x
156
+ case x : Array [Byte ] => x
157
+ case x : JsonElement => x
158
+ case x => x.toString()
159
+ }
160
+
161
+ private def rowDataToJsonArray (rowData : RowData ): JsonArray = Json .arr(rowData.map(dataToJson).toList: _* )
153
162
}
0 commit comments