Guest User

Untitled

a guest
Feb 8th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. import org.jetbrains.exposed.sql.*
  2. import org.jetbrains.exposed.sql.transactions.transaction
  3. import org.jetbrains.exposed.sql.vendors.DatabaseDialect
  4. import kotlin.random.Random
  5.  
  6. // Unable to get the database dialect within Exposed...
  7. lateinit var databaseDialect: DatabaseDialect
  8.  
  9. fun main() {
  10. val db = "pg"
  11.  
  12. if (db == "pg") {
  13. Database.connect(
  14. "jdbc:postgresql://localhost:15432/boon", driver = "org.postgresql.Driver",
  15. user = "boon", password = "boon"
  16. ).apply { databaseDialect = dialect }
  17. } else {
  18. Database.connect(
  19. "jdbc:mysql://localhost:3306/boon", driver = "com.mysql.jdbc.Driver",
  20. user = "boon", password = "boon"
  21. ).apply { databaseDialect = dialect }
  22. }
  23.  
  24. transaction {
  25. val tx = this
  26. SchemaUtils.create(Customers)
  27.  
  28. val r = Random(System.currentTimeMillis())
  29.  
  30. if (Customers.selectAll().count() == 0) {
  31. for (i in 1..12_000)
  32. Customers.insert {
  33. it[name] = "Alice$i"
  34. it[misc] = mapOf(
  35. "age" to r.nextInt(99),
  36. "salary" to r.nextInt(9999) + 2000
  37. )
  38. if ((i + 1) % 10_000 == 0) {
  39. println("Added ${i + 1}.")
  40. tx.flushCache()
  41. }
  42. }
  43. }
  44.  
  45. transaction {
  46. val start = System.currentTimeMillis()
  47. val customers = Customers.select { Customers.misc.json<Int>(JsonKey("salary")) eq 5555 }
  48.  
  49. var count = 0
  50. for (customer in customers) {
  51. println(customer)
  52. count++
  53. }
  54.  
  55. val took = System.currentTimeMillis() - start
  56. println("Total customer count: $count (took $took ms)")
  57. }
  58. }
  59. }
Add Comment
Please, Sign In to add comment