case class Student(firstName: String, lastName: String)derives sq.Reader
This is the function that reads the students:
def readStudents(cn: Connection, id: Option[Int]): List[Student] = {
sq.run(cn) {
var sql = if (id.isEmpty)
sql"""select last_name, first_name
from student"""
else
sql"select last_name, first_name from student where student_id =${id.get}"
sq.read[Student](sql)
}
}
Note that in both cases, the last_name field appears on the first position in the select statements. When the data is read, the last_name field populates the firstName member and the first_name populates the firstName member. This makes the code brittle ,imo.
I think it would be better to use a mapping between member names and column names, whether by attributes or convention (case class member names should match exactly the names of the columns, or add a configuration parameter that drives a name transformation).
I really like your library - it is the simplest one I could find among all the scala libraries, the next step would be just to write jdbc code by hand.
Note: the run(jsql.Connection) method doesn't exist in the code, I added it to be able to use the library with the Play framework.
I reworked this library a bit, mostly focusing on making the syntax lighter. However, I also added support for using columns by name, so this should be fixed in > 0.3.0
As an example, I have the following simple class:
This is the function that reads the students:
Note that in both cases, the last_name field appears on the first position in the select statements. When the data is read, the last_name field populates the firstName member and the first_name populates the firstName member. This makes the code brittle ,imo.
I think it would be better to use a mapping between member names and column names, whether by attributes or convention (case class member names should match exactly the names of the columns, or add a configuration parameter that drives a name transformation).
I really like your library - it is the simplest one I could find among all the scala libraries, the next step would be just to write jdbc code by hand.
Note: the run(jsql.Connection) method doesn't exist in the code, I added it to be able to use the library with the Play framework.