Guest User

Untitled

a guest
Jun 24th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. package demo
  2.  
  3. import java.sql.{Connection, ResultSet, Statement}
  4.  
  5. import scala.io.Source
  6.  
  7. object Main {
  8.  
  9. def main(args: Array[String]): Unit = {
  10. implicit val connection:Connection = MyFactory.connection
  11. val employees = readAllEmployeesFromDb
  12. println(employees)
  13. //Now read data from csv file and create domain objects and insert into DB
  14. //remember, they are relational. So, one's generated ID would be used in another.
  15. val csvDataSeq = readDataFromCsvFile()
  16. println(csvDataSeq)
  17. //Let's use currying and apply it using partially applied functions
  18. val empSeq = csvDataSeq.map(d => Employee(firstName = d.fName, lastName = d.lName))
  19. val addressSeq = csvDataSeq.map(d => Address(line1 = d.line1, line2 = d.line2, postcode = d.postcode))
  20. //here we know mapping is 1:1, so we cam zip both of them
  21. //if mapping is 1:MANY, then group them as Map[Employee, Set[Address]]
  22. val zippedEmpAndAddress = empSeq.zip(addressSeq)
  23. zippedEmpAndAddress foreach { empAndAdd =>
  24. val updatedEmployee = insertEmployee(empAndAdd._1)(connection)
  25. println(updatedEmployee)
  26. //this is not exactly partially applied function.
  27. val updatedAddress = insertAddress(empAndAdd._2, connection)(updatedEmployee)
  28. println(updatedAddress)
  29. }
  30. connection.commit()
  31.  
  32. val insertedEmployees = readAllEmployeesFromDb
  33. println(insertedEmployees)
  34.  
  35. }
  36.  
  37. def readAllEmployeesFromDb(implicit connection: Connection): Seq[Employee] = {
  38. val ps = connection.prepareStatement("SELECT * FROM EMP_DB.EMPLOYEE")
  39. val rs = ps.executeQuery()
  40. lazy val rsToEmp: ResultSet => Employee =
  41. resultSet => Employee(
  42. Some(resultSet.getLong("id")),
  43. resultSet.getString("first_name"),
  44. resultSet.getString("last_name"))
  45. resultSetToRows(rs, rsToEmp)
  46. }
  47.  
  48. def resultSetToRows[T](rs: ResultSet, f: ResultSet => T): Seq[T] = {
  49. def loop(accumulator: Seq[T]): Seq[T] = {
  50. if (rs.next()) loop(f(rs) +: accumulator)
  51. else accumulator
  52. }
  53.  
  54. loop(Nil)
  55. }
  56.  
  57. private def readDataFromCsvFile(): Seq[CsvModel] = {
  58. val data = Source.fromResource("emp_data.csv").getLines().drop(1).toList
  59. data.map(_.split(",")).map(row => CsvModel(row.head, row(1), row(2), row(3), row(4)))
  60. }
  61.  
  62. def insertEmployee(e: Employee)(implicit connection: Connection): Employee = {
  63. val sql = "INSERT INTO EMP_DB.EMPLOYEE(first_name,last_name) values(?,?)"
  64. val ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)
  65. ps.setString(1, e.firstName)
  66. ps.setString(2, e.lastName)
  67. ps.executeUpdate()
  68. val rs = ps.getGeneratedKeys
  69. println("insert-employee-resultSet-get-next = " + rs.next())
  70. val empId = rs.getLong(1)
  71. e.copy(id = Some(empId))
  72. }
  73.  
  74. def insertAddress(a: Address, connection: Connection)(e: Employee): Address = {
  75. val sql = "INSERT INTO EMP_DB.ADDRESS(line_1,line_2,postcode,emp_id) values(?,?,?,?)"
  76. val ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)
  77. ps.setString(1, a.line1)
  78. ps.setString(2, a.line2)
  79. ps.setString(3, a.postcode)
  80. ps.setLong(4, e.id.getOrElse(throw new RuntimeException("emp-id not available for insert")))
  81. ps.executeUpdate()
  82. val rs = ps.getGeneratedKeys
  83. println("insert-address-resultSet-get-next = " + rs.next())
  84. val addressId = rs.getLong(1)
  85. a.copy(id = Some(addressId), empId = e.id)
  86. }
  87.  
  88. //is there any use of this val??
  89. val partiallyAppliedInsertAddress: (Address, Connection) => Employee => Address = {
  90. (a: Address, connection: Connection) => insertAddress(a, connection)
  91. }
  92.  
  93. }
Add Comment
Please, Sign In to add comment