Skip to content

Commit

Permalink
anorm: update returns maybe row
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindberg committed Aug 30, 2024
1 parent 92d7e1e commit fddba55
Show file tree
Hide file tree
Showing 231 changed files with 645 additions and 799 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ trait PersonRepo {
def selectByFieldValues(fieldValues: List[PersonFieldOrIdValue[?]])(implicit c: Connection): List[PersonRow]
def selectById(compositeId: PersonId)(implicit c: Connection): Option[PersonRow]
def update: UpdateBuilder[PersonFields, PersonRow]
def update(row: PersonRow)(implicit c: Connection): Boolean
def update(row: PersonRow)(implicit c: Connection): Option[PersonRow]
def updateFieldValues(compositeId: PersonId, fieldValues: List[PersonFieldValue[?]])(implicit c: Connection): Boolean
def upsert(unsaved: PersonRow)(implicit c: Connection): PersonRow
def upsertBatch(unsaved: Iterable[PersonRow])(implicit c: Connection): List[PersonRow]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,13 @@ class PersonRepoImpl extends PersonRepo {
override def update: UpdateBuilder[PersonFields, PersonRow] = {
UpdateBuilder(""""compositepk"."person"""", PersonFields.structure, PersonRow.rowParser)
}
override def update(row: PersonRow)(implicit c: Connection): Boolean = {
override def update(row: PersonRow)(implicit c: Connection): Option[PersonRow] = {
val compositeId = row.compositeId
SQL"""update "compositepk"."person"
set "name" = ${ParameterValue(row.name, null, ToStatement.optionToStatement(ToStatement.stringToStatement, ParameterMetaData.StringParameterMetaData))}
where "one" = ${ParameterValue(compositeId.one, null, ToStatement.longToStatement)} AND "two" = ${ParameterValue(compositeId.two, null, ToStatement.optionToStatement(ToStatement.stringToStatement, ParameterMetaData.StringParameterMetaData))}
""".executeUpdate() > 0
returning "one", "two", "name"
""".executeInsert(PersonRow.rowParser(1).singleOpt)
}
override def updateFieldValues(compositeId: PersonId, fieldValues: List[PersonFieldValue[?]])(implicit c: Connection): Boolean = {
fieldValues match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,10 @@ class PersonRepoMock(toRow: Function1[PersonRowUnsaved, PersonRow],
override def update: UpdateBuilder[PersonFields, PersonRow] = {
UpdateBuilderMock(UpdateParams.empty, PersonFields.structure, map)
}
override def update(row: PersonRow)(implicit c: Connection): Boolean = {
map.get(row.compositeId) match {
case Some(`row`) => false
case Some(_) =>
map.put(row.compositeId, row): @nowarn
true
case None => false
override def update(row: PersonRow)(implicit c: Connection): Option[PersonRow] = {
map.get(row.compositeId).map { _ =>
map.put(row.compositeId, row): @nowarn
row
}
}
override def updateFieldValues(compositeId: PersonId, fieldValues: List[PersonFieldValue[?]])(implicit c: Connection): Boolean = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ trait FootballClubRepo {
def selectByIds(ids: Array[FootballClubId])(implicit c: Connection): List[FootballClubRow]
def selectByIdsTracked(ids: Array[FootballClubId])(implicit c: Connection): Map[FootballClubId, FootballClubRow]
def update: UpdateBuilder[FootballClubFields, FootballClubRow]
def update(row: FootballClubRow)(implicit c: Connection): Boolean
def update(row: FootballClubRow)(implicit c: Connection): Option[FootballClubRow]
def updateFieldValues(id: FootballClubId, fieldValues: List[FootballClubFieldValue[?]])(implicit c: Connection): Boolean
def upsert(unsaved: FootballClubRow)(implicit c: Connection): FootballClubRow
def upsertBatch(unsaved: Iterable[FootballClubRow])(implicit c: Connection): List[FootballClubRow]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,13 @@ class FootballClubRepoImpl extends FootballClubRepo {
override def update: UpdateBuilder[FootballClubFields, FootballClubRow] = {
UpdateBuilder(""""myschema"."football_club"""", FootballClubFields.structure, FootballClubRow.rowParser)
}
override def update(row: FootballClubRow)(implicit c: Connection): Boolean = {
override def update(row: FootballClubRow)(implicit c: Connection): Option[FootballClubRow] = {
val id = row.id
SQL"""update "myschema"."football_club"
set "name" = ${ParameterValue(row.name, null, ToStatement.stringToStatement)}
where "id" = ${ParameterValue(id, null, FootballClubId.toStatement)}
""".executeUpdate() > 0
returning "id", "name"
""".executeInsert(FootballClubRow.rowParser(1).singleOpt)
}
override def updateFieldValues(id: FootballClubId, fieldValues: List[FootballClubFieldValue[?]])(implicit c: Connection): Boolean = {
fieldValues match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,10 @@ class FootballClubRepoMock(map: scala.collection.mutable.Map[FootballClubId, Foo
override def update: UpdateBuilder[FootballClubFields, FootballClubRow] = {
UpdateBuilderMock(UpdateParams.empty, FootballClubFields.structure, map)
}
override def update(row: FootballClubRow)(implicit c: Connection): Boolean = {
map.get(row.id) match {
case Some(`row`) => false
case Some(_) =>
map.put(row.id, row): @nowarn
true
case None => false
override def update(row: FootballClubRow)(implicit c: Connection): Option[FootballClubRow] = {
map.get(row.id).map { _ =>
map.put(row.id, row): @nowarn
row
}
}
override def updateFieldValues(id: FootballClubId, fieldValues: List[FootballClubFieldValue[?]])(implicit c: Connection): Boolean = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ trait PersonRepo {
def selectByIds(ids: Array[PersonId])(implicit c: Connection): List[PersonRow]
def selectByIdsTracked(ids: Array[PersonId])(implicit c: Connection): Map[PersonId, PersonRow]
def update: UpdateBuilder[PersonFields, PersonRow]
def update(row: PersonRow)(implicit c: Connection): Boolean
def update(row: PersonRow)(implicit c: Connection): Option[PersonRow]
def updateFieldValues(id: PersonId, fieldValues: List[PersonFieldValue[?]])(implicit c: Connection): Boolean
def upsert(unsaved: PersonRow)(implicit c: Connection): PersonRow
def upsertBatch(unsaved: Iterable[PersonRow])(implicit c: Connection): List[PersonRow]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class PersonRepoImpl extends PersonRepo {
override def update: UpdateBuilder[PersonFields, PersonRow] = {
UpdateBuilder(""""myschema"."person"""", PersonFields.structure, PersonRow.rowParser)
}
override def update(row: PersonRow)(implicit c: Connection): Boolean = {
override def update(row: PersonRow)(implicit c: Connection): Option[PersonRow] = {
val id = row.id
SQL"""update "myschema"."person"
set "favourite_football_club_id" = ${ParameterValue(row.favouriteFootballClubId, null, FootballClubId.toStatement)},
Expand All @@ -165,7 +165,8 @@ class PersonRepoImpl extends PersonRepo {
"work_email" = ${ParameterValue(row.workEmail, null, ToStatement.optionToStatement(ToStatement.stringToStatement, ParameterMetaData.StringParameterMetaData))},
"favorite_number" = ${ParameterValue(row.favoriteNumber, null, Number.toStatement)}::myschema.number
where "id" = ${ParameterValue(id, null, PersonId.toStatement)}
""".executeUpdate() > 0
returning "id", "favourite_football_club_id", "name", "nick_name", "blog_url", "email", "phone", "likes_pizza", "marital_status_id", "work_email", "sector", "favorite_number"
""".executeInsert(PersonRow.rowParser(1).singleOpt)
}
override def updateFieldValues(id: PersonId, fieldValues: List[PersonFieldValue[?]])(implicit c: Connection): Boolean = {
fieldValues match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,10 @@ class PersonRepoMock(toRow: Function1[PersonRowUnsaved, PersonRow],
override def update: UpdateBuilder[PersonFields, PersonRow] = {
UpdateBuilderMock(UpdateParams.empty, PersonFields.structure, map)
}
override def update(row: PersonRow)(implicit c: Connection): Boolean = {
map.get(row.id) match {
case Some(`row`) => false
case Some(_) =>
map.put(row.id, row): @nowarn
true
case None => false
override def update(row: PersonRow)(implicit c: Connection): Option[PersonRow] = {
map.get(row.id).map { _ =>
map.put(row.id, row): @nowarn
row
}
}
override def updateFieldValues(id: PersonId, fieldValues: List[PersonFieldValue[?]])(implicit c: Connection): Boolean = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ trait PersonRepo {
def selectByFieldValues(fieldValues: List[PersonFieldOrIdValue[?]])(implicit c: Connection): List[PersonRow]
def selectById(compositeId: PersonId)(implicit c: Connection): Option[PersonRow]
def update: UpdateBuilder[PersonFields, PersonRow]
def update(row: PersonRow)(implicit c: Connection): Boolean
def update(row: PersonRow)(implicit c: Connection): Option[PersonRow]
def updateFieldValues(compositeId: PersonId, fieldValues: List[PersonFieldValue[?]])(implicit c: Connection): Boolean
def upsert(unsaved: PersonRow)(implicit c: Connection): PersonRow
def upsertBatch(unsaved: Iterable[PersonRow])(implicit c: Connection): List[PersonRow]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,13 @@ class PersonRepoImpl extends PersonRepo {
override def update: UpdateBuilder[PersonFields, PersonRow] = {
UpdateBuilder(""""compositepk"."person"""", PersonFields.structure, PersonRow.rowParser)
}
override def update(row: PersonRow)(implicit c: Connection): Boolean = {
override def update(row: PersonRow)(implicit c: Connection): Option[PersonRow] = {
val compositeId = row.compositeId
SQL"""update "compositepk"."person"
set "name" = ${ParameterValue(row.name, null, ToStatement.optionToStatement(ToStatement.stringToStatement, ParameterMetaData.StringParameterMetaData))}
where "one" = ${ParameterValue(compositeId.one, null, ToStatement.longToStatement)} AND "two" = ${ParameterValue(compositeId.two, null, ToStatement.optionToStatement(ToStatement.stringToStatement, ParameterMetaData.StringParameterMetaData))}
""".executeUpdate() > 0
returning "one", "two", "name"
""".executeInsert(PersonRow.rowParser(1).singleOpt)
}
override def updateFieldValues(compositeId: PersonId, fieldValues: List[PersonFieldValue[?]])(implicit c: Connection): Boolean = {
fieldValues match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,10 @@ class PersonRepoMock(toRow: Function1[PersonRowUnsaved, PersonRow],
override def update: UpdateBuilder[PersonFields, PersonRow] = {
UpdateBuilderMock(UpdateParams.empty, PersonFields.structure, map)
}
override def update(row: PersonRow)(implicit c: Connection): Boolean = {
map.get(row.compositeId) match {
case Some(`row`) => false
case Some(_) =>
map.put(row.compositeId, row): @nowarn
true
case None => false
override def update(row: PersonRow)(implicit c: Connection): Option[PersonRow] = {
map.get(row.compositeId).map { _ =>
map.put(row.compositeId, row): @nowarn
row
}
}
override def updateFieldValues(compositeId: PersonId, fieldValues: List[PersonFieldValue[?]])(implicit c: Connection): Boolean = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ trait FootballClubRepo {
def selectByIds(ids: Array[FootballClubId])(implicit c: Connection): List[FootballClubRow]
def selectByIdsTracked(ids: Array[FootballClubId])(implicit c: Connection): Map[FootballClubId, FootballClubRow]
def update: UpdateBuilder[FootballClubFields, FootballClubRow]
def update(row: FootballClubRow)(implicit c: Connection): Boolean
def update(row: FootballClubRow)(implicit c: Connection): Option[FootballClubRow]
def updateFieldValues(id: FootballClubId, fieldValues: List[FootballClubFieldValue[?]])(implicit c: Connection): Boolean
def upsert(unsaved: FootballClubRow)(implicit c: Connection): FootballClubRow
def upsertBatch(unsaved: Iterable[FootballClubRow])(implicit c: Connection): List[FootballClubRow]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,13 @@ class FootballClubRepoImpl extends FootballClubRepo {
override def update: UpdateBuilder[FootballClubFields, FootballClubRow] = {
UpdateBuilder(""""myschema"."football_club"""", FootballClubFields.structure, FootballClubRow.rowParser)
}
override def update(row: FootballClubRow)(implicit c: Connection): Boolean = {
override def update(row: FootballClubRow)(implicit c: Connection): Option[FootballClubRow] = {
val id = row.id
SQL"""update "myschema"."football_club"
set "name" = ${ParameterValue(row.name, null, ToStatement.stringToStatement)}
where "id" = ${ParameterValue(id, null, FootballClubId.toStatement)}
""".executeUpdate() > 0
returning "id", "name"
""".executeInsert(FootballClubRow.rowParser(1).singleOpt)
}
override def updateFieldValues(id: FootballClubId, fieldValues: List[FootballClubFieldValue[?]])(implicit c: Connection): Boolean = {
fieldValues match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,10 @@ class FootballClubRepoMock(map: scala.collection.mutable.Map[FootballClubId, Foo
override def update: UpdateBuilder[FootballClubFields, FootballClubRow] = {
UpdateBuilderMock(UpdateParams.empty, FootballClubFields.structure, map)
}
override def update(row: FootballClubRow)(implicit c: Connection): Boolean = {
map.get(row.id) match {
case Some(`row`) => false
case Some(_) =>
map.put(row.id, row): @nowarn
true
case None => false
override def update(row: FootballClubRow)(implicit c: Connection): Option[FootballClubRow] = {
map.get(row.id).map { _ =>
map.put(row.id, row): @nowarn
row
}
}
override def updateFieldValues(id: FootballClubId, fieldValues: List[FootballClubFieldValue[?]])(implicit c: Connection): Boolean = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ trait PersonRepo {
def selectByIds(ids: Array[PersonId])(implicit c: Connection): List[PersonRow]
def selectByIdsTracked(ids: Array[PersonId])(implicit c: Connection): Map[PersonId, PersonRow]
def update: UpdateBuilder[PersonFields, PersonRow]
def update(row: PersonRow)(implicit c: Connection): Boolean
def update(row: PersonRow)(implicit c: Connection): Option[PersonRow]
def updateFieldValues(id: PersonId, fieldValues: List[PersonFieldValue[?]])(implicit c: Connection): Boolean
def upsert(unsaved: PersonRow)(implicit c: Connection): PersonRow
def upsertBatch(unsaved: Iterable[PersonRow])(implicit c: Connection): List[PersonRow]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class PersonRepoImpl extends PersonRepo {
override def update: UpdateBuilder[PersonFields, PersonRow] = {
UpdateBuilder(""""myschema"."person"""", PersonFields.structure, PersonRow.rowParser)
}
override def update(row: PersonRow)(implicit c: Connection): Boolean = {
override def update(row: PersonRow)(implicit c: Connection): Option[PersonRow] = {
val id = row.id
SQL"""update "myschema"."person"
set "favourite_football_club_id" = ${ParameterValue(row.favouriteFootballClubId, null, FootballClubId.toStatement)},
Expand All @@ -165,7 +165,8 @@ class PersonRepoImpl extends PersonRepo {
"work_email" = ${ParameterValue(row.workEmail, null, ToStatement.optionToStatement(ToStatement.stringToStatement, ParameterMetaData.StringParameterMetaData))},
"favorite_number" = ${ParameterValue(row.favoriteNumber, null, Number.toStatement)}::myschema.number
where "id" = ${ParameterValue(id, null, PersonId.toStatement)}
""".executeUpdate() > 0
returning "id", "favourite_football_club_id", "name", "nick_name", "blog_url", "email", "phone", "likes_pizza", "marital_status_id", "work_email", "sector", "favorite_number"
""".executeInsert(PersonRow.rowParser(1).singleOpt)
}
override def updateFieldValues(id: PersonId, fieldValues: List[PersonFieldValue[?]])(implicit c: Connection): Boolean = {
fieldValues match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,10 @@ class PersonRepoMock(toRow: Function1[PersonRowUnsaved, PersonRow],
override def update: UpdateBuilder[PersonFields, PersonRow] = {
UpdateBuilderMock(UpdateParams.empty, PersonFields.structure, map)
}
override def update(row: PersonRow)(implicit c: Connection): Boolean = {
map.get(row.id) match {
case Some(`row`) => false
case Some(_) =>
map.put(row.id, row): @nowarn
true
case None => false
override def update(row: PersonRow)(implicit c: Connection): Option[PersonRow] = {
map.get(row.id).map { _ =>
map.put(row.id, row): @nowarn
row
}
}
override def updateFieldValues(id: PersonId, fieldValues: List[PersonFieldValue[?]])(implicit c: Connection): Boolean = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ trait DepartmentRepo {
def selectByIds(departmentids: Array[DepartmentId])(implicit c: Connection): List[DepartmentRow]
def selectByIdsTracked(departmentids: Array[DepartmentId])(implicit c: Connection): Map[DepartmentId, DepartmentRow]
def update: UpdateBuilder[DepartmentFields, DepartmentRow]
def update(row: DepartmentRow)(implicit c: Connection): Boolean
def update(row: DepartmentRow)(implicit c: Connection): Option[DepartmentRow]
def upsert(unsaved: DepartmentRow)(implicit c: Connection): DepartmentRow
def upsertBatch(unsaved: Iterable[DepartmentRow])(implicit c: Connection): List[DepartmentRow]
/* NOTE: this functionality is not safe if you use auto-commit mode! it runs 3 SQL statements */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,15 @@ class DepartmentRepoImpl extends DepartmentRepo {
override def update: UpdateBuilder[DepartmentFields, DepartmentRow] = {
UpdateBuilder(""""humanresources"."department"""", DepartmentFields.structure, DepartmentRow.rowParser)
}
override def update(row: DepartmentRow)(implicit c: Connection): Boolean = {
override def update(row: DepartmentRow)(implicit c: Connection): Option[DepartmentRow] = {
val departmentid = row.departmentid
SQL"""update "humanresources"."department"
set "name" = ${ParameterValue(row.name, null, Name.toStatement)}::varchar,
"groupname" = ${ParameterValue(row.groupname, null, Name.toStatement)}::varchar,
"modifieddate" = ${ParameterValue(row.modifieddate, null, TypoLocalDateTime.toStatement)}::timestamp
where "departmentid" = ${ParameterValue(departmentid, null, DepartmentId.toStatement)}
""".executeUpdate() > 0
returning "departmentid", "name", "groupname", "modifieddate"::text
""".executeInsert(DepartmentRow.rowParser(1).singleOpt)
}
override def upsert(unsaved: DepartmentRow)(implicit c: Connection): DepartmentRow = {
SQL"""insert into "humanresources"."department"("departmentid", "name", "groupname", "modifieddate")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,10 @@ class DepartmentRepoMock(toRow: Function1[DepartmentRowUnsaved, DepartmentRow],
override def update: UpdateBuilder[DepartmentFields, DepartmentRow] = {
UpdateBuilderMock(UpdateParams.empty, DepartmentFields.structure, map)
}
override def update(row: DepartmentRow)(implicit c: Connection): Boolean = {
map.get(row.departmentid) match {
case Some(`row`) => false
case Some(_) =>
map.put(row.departmentid, row): @nowarn
true
case None => false
override def update(row: DepartmentRow)(implicit c: Connection): Option[DepartmentRow] = {
map.get(row.departmentid).map { _ =>
map.put(row.departmentid, row): @nowarn
row
}
}
override def upsert(unsaved: DepartmentRow)(implicit c: Connection): DepartmentRow = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ trait EmployeeRepo {
def selectByIds(businessentityids: Array[BusinessentityId])(implicit c: Connection): List[EmployeeRow]
def selectByIdsTracked(businessentityids: Array[BusinessentityId])(implicit c: Connection): Map[BusinessentityId, EmployeeRow]
def update: UpdateBuilder[EmployeeFields, EmployeeRow]
def update(row: EmployeeRow)(implicit c: Connection): Boolean
def update(row: EmployeeRow)(implicit c: Connection): Option[EmployeeRow]
def upsert(unsaved: EmployeeRow)(implicit c: Connection): EmployeeRow
def upsertBatch(unsaved: Iterable[EmployeeRow])(implicit c: Connection): List[EmployeeRow]
/* NOTE: this functionality is not safe if you use auto-commit mode! it runs 3 SQL statements */
Expand Down
Loading

0 comments on commit fddba55

Please sign in to comment.