skoude
02/14/2017, 12:38 PMclass Dbutil {
companion object : KLogging()
/**
* DSL for extending the SQL2O for making a basic select Query and returning MutableList<T>
*/
public inline fun <reified T : Any> Connection.selectQuery(sqlString: String): MutableList<T>? {
try {
val connection: Connection = sql2o.open()
val results = connection.createQuery(sqlString).executeAndFetch(T::class.java)
return results
} catch (e: Exception) {
println(e.toString())
return null
}
}
}
skoude
02/14/2017, 12:39 PMskoude
02/14/2017, 12:40 PMfun selectQuerytest() {
var con: Connection = sql2o.open()
var vidaudList: MutableList<vidaud>? = con.selectQuery<vidaud>("select source, timestamp from video_audio limit 100")
}
kirillrakhman
02/14/2017, 12:40 PMskoude
02/14/2017, 12:40 PMkirillrakhman
02/14/2017, 12:41 PMkirillrakhman
02/14/2017, 12:41 PMDbUtil
kirillrakhman
02/14/2017, 12:41 PMskoude
02/14/2017, 12:41 PMkirillrakhman
02/14/2017, 12:42 PMskoude
02/14/2017, 12:42 PMkirillrakhman
02/14/2017, 12:42 PMskoude
02/14/2017, 12:42 PMkirillrakhman
02/14/2017, 12:42 PMwith (Dbutil()) {
con.selectQuery
}
kirillrakhman
02/14/2017, 12:42 PMskoude
02/14/2017, 12:43 PMskoude
02/14/2017, 12:43 PMkirillrakhman
02/14/2017, 12:44 PMkirillrakhman
02/14/2017, 12:44 PMthis@selectQuery
and this@Dbutil
kirillrakhman
02/14/2017, 12:46 PMcon
), you need to supply the other receiver implictly. this can be done when you're inside the class or when you're inside an extension function or lambda that has the class as receiver. that's what with(Dbutil()) {}
doesskoude
02/14/2017, 12:47 PMkirillrakhman
02/14/2017, 12:47 PMskoude
02/14/2017, 12:49 PMjanvladimirmostert
02/14/2017, 1:06 PMfun Channel.consume(
queue: String = "",
autoAck: Boolean = true,
onMessage: (consumerTag: String, envelope: Envelope, properties: BasicProperties, json: String) -> Unit
){
this.basicConsume(queue, autoAck, object : DefaultConsumer(this) {
override fun handleDelivery(consumerTag: String?, envelope: Envelope?, properties: AMQP.BasicProperties?, body: ByteArray?) {
onMessage(
consumerTag ?: "",
envelope ?: Envelope(0, false, "", ""),
properties ?: AMQP.BasicProperties(),
if (body != null) String(body) else "")
}
});
}
How do I make use of this extension function?
This gives me an error saying unexpected type specification
channel.consume(queue = "api", onMessage = (a: String, b: Envelope, c: BasicProperties, d: String){
})
If I remove the types in onMessage, IDE complains Unresolved Reference
miha-x64
02/14/2017, 1:08 PMchannel.consume(queue = "api", onMessage = { a: String, b: Envelope, c: BasicProperties, d: String ->
})
janvladimirmostert
02/14/2017, 1:09 PMmiha-x64
02/14/2017, 1:09 PM{ a, b, c, d ->
janvladimirmostert
02/14/2017, 1:10 PMmiha-x64
02/14/2017, 1:12 PMval func: (consumerTag: String, envelope: Any, properties: Any, json: String) -> Unit = { str, env: Any, prop, json: String -> }
janvladimirmostert
02/14/2017, 1:12 PM