Advertisement
Guest User

Untitled

a guest
Mar 7th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. package com.example
  2.  
  3. import io.r2dbc.h2.H2ConnectionConfiguration
  4. import io.r2dbc.h2.H2ConnectionFactory
  5. import io.r2dbc.spi.Result
  6. import io.r2dbc.spi.Row
  7. import io.r2dbc.spi.RowMetadata
  8. import kotlinx.coroutines.GlobalScope
  9. import kotlinx.coroutines.reactive.awaitLast
  10. import kotlinx.coroutines.reactive.awaitSingle
  11. import kotlinx.coroutines.reactor.mono
  12. import kotlinx.coroutines.runBlocking
  13. import reactor.core.publisher.Mono
  14. import reactor.core.publisher.toFlux
  15. import reactor.core.publisher.toMono
  16. import kotlin.test.BeforeTest
  17. import kotlin.test.Test
  18.  
  19.  
  20. class R2Dbc {
  21.  
  22. var config: H2ConnectionConfiguration = H2ConnectionConfiguration.builder().inMemory("jdbc:h2:mem:r2dbc-test").build()
  23.  
  24. @BeforeTest
  25. fun createH2() {
  26. config = H2ConnectionConfiguration.builder().inMemory("jdbc:h2:mem:r2dbc-test").build()
  27. }
  28.  
  29. @Test
  30. fun firstTest() {
  31. val h2ConnectionFactory = H2ConnectionFactory(config)
  32. val connection = h2ConnectionFactory.create()
  33. val insert: Mono<MutableList<out Result>> = GlobalScope.mono {
  34. val con = connection.awaitSingle()
  35. con.createStatement(
  36. // language=SQL
  37. """create table messages(
  38. |id integer not null primary key ,
  39. |text varchar(10000) not null ,
  40. |user_id integer not null
  41. |)""".trimMargin())
  42. .execute()
  43. .log("create-table").awaitSingle()
  44. con.createStatement(
  45. //language=SQL
  46. """insert into messages(id, text, user_id) values (?1, ?2, ?3)""")
  47. .bind(0, 1000)
  48. .bind(1, "こんにちわ")
  49. .bind(2, 100)
  50. .execute()
  51. .toFlux()
  52. .collectList()
  53. .awaitSingle()
  54. }
  55. val mono: Mono<Triple<String, Int, Int>> = GlobalScope.mono {
  56. val a = insert.awaitLast()
  57. val rowUpdates = a.map { it.rowsUpdated.toMono().awaitSingle() }
  58. println(rowUpdates)
  59. val con = connection.awaitSingle()
  60. con.createStatement(
  61. //language=SQL
  62. """select text as text, id as id, user_id as user_id from messages""")
  63. .execute()
  64. .flatMap {
  65. it.map { row: Row, _: RowMetadata ->
  66. val text = row.get("text", String::class.java)
  67. val id = row.get("id") as Int
  68. val user = row.get("user_id") as Int
  69. Triple(text, id, user)
  70. }
  71. }
  72. .toMono().awaitSingle()
  73. }
  74. runBlocking {
  75. val result = mono.awaitSingle()
  76. println(result)
  77. }
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement