-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate update set on conflict for upserts (#145)
When upserting to tables with only key columns, typo would generate `do nothing` as a conflict resolution. This has the disadvantage of making the query not execute the `returning` part, which meant that in cases where an upsert was executed targeting a single row, no rows would return and the generated code would cause an error. As a workaround, we generate `update set k = excluded.k` for an arbitrary key column k instead. This causes the `returning` part of the query to run but shouldn't change the value of the row as typo sees it. - Add failing test case for upserting on an existing row - Make the test case work by generating `update set` instead of `do nothing`
- Loading branch information
Showing
100 changed files
with
1,292 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
.../generated-and-checked-in/adventureworks/public/only_pk_columns/OnlyPkColumnsFields.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* File has been automatically generated by `typo`. | ||
* | ||
* IF YOU CHANGE THIS FILE YOUR CHANGES WILL BE OVERWRITTEN. | ||
*/ | ||
package adventureworks | ||
package public | ||
package only_pk_columns | ||
|
||
import typo.dsl.Path | ||
import typo.dsl.Required | ||
import typo.dsl.SqlExpr | ||
import typo.dsl.SqlExpr.CompositeIn | ||
import typo.dsl.SqlExpr.CompositeIn.TuplePart | ||
import typo.dsl.SqlExpr.FieldLikeNoHkt | ||
import typo.dsl.SqlExpr.IdField | ||
import typo.dsl.Structure.Relation | ||
|
||
trait OnlyPkColumnsFields { | ||
def keyColumn1: IdField[String, OnlyPkColumnsRow] | ||
def keyColumn2: IdField[Int, OnlyPkColumnsRow] | ||
def compositeIdIs(compositeId: OnlyPkColumnsId): SqlExpr[Boolean, Required] = | ||
keyColumn1.isEqual(compositeId.keyColumn1).and(keyColumn2.isEqual(compositeId.keyColumn2)) | ||
def compositeIdIn(compositeIds: Array[OnlyPkColumnsId]): SqlExpr[Boolean, Required] = | ||
new CompositeIn(compositeIds)(TuplePart(keyColumn1)(_.keyColumn1), TuplePart(keyColumn2)(_.keyColumn2)) | ||
|
||
} | ||
|
||
object OnlyPkColumnsFields { | ||
lazy val structure: Relation[OnlyPkColumnsFields, OnlyPkColumnsRow] = | ||
new Impl(Nil) | ||
|
||
private final class Impl(val _path: List[Path]) | ||
extends Relation[OnlyPkColumnsFields, OnlyPkColumnsRow] { | ||
|
||
override lazy val fields: OnlyPkColumnsFields = new OnlyPkColumnsFields { | ||
override def keyColumn1 = IdField[String, OnlyPkColumnsRow](_path, "key_column_1", None, None, x => x.keyColumn1, (row, value) => row.copy(keyColumn1 = value)) | ||
override def keyColumn2 = IdField[Int, OnlyPkColumnsRow](_path, "key_column_2", None, Some("int4"), x => x.keyColumn2, (row, value) => row.copy(keyColumn2 = value)) | ||
} | ||
|
||
override lazy val columns: List[FieldLikeNoHkt[?, OnlyPkColumnsRow]] = | ||
List[FieldLikeNoHkt[?, OnlyPkColumnsRow]](fields.keyColumn1, fields.keyColumn2) | ||
|
||
override def copy(path: List[Path]): Impl = | ||
new Impl(path) | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
...norm/generated-and-checked-in/adventureworks/public/only_pk_columns/OnlyPkColumnsId.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* File has been automatically generated by `typo`. | ||
* | ||
* IF YOU CHANGE THIS FILE YOUR CHANGES WILL BE OVERWRITTEN. | ||
*/ | ||
package adventureworks | ||
package public | ||
package only_pk_columns | ||
|
||
import play.api.libs.json.JsObject | ||
import play.api.libs.json.JsResult | ||
import play.api.libs.json.JsValue | ||
import play.api.libs.json.OWrites | ||
import play.api.libs.json.Reads | ||
import play.api.libs.json.Writes | ||
import scala.collection.immutable.ListMap | ||
import scala.util.Try | ||
|
||
/** Type for the composite primary key of table `public.only_pk_columns` */ | ||
case class OnlyPkColumnsId( | ||
keyColumn1: String, | ||
keyColumn2: Int | ||
) | ||
object OnlyPkColumnsId { | ||
implicit lazy val ordering: Ordering[OnlyPkColumnsId] = Ordering.by(x => (x.keyColumn1, x.keyColumn2)) | ||
implicit lazy val reads: Reads[OnlyPkColumnsId] = Reads[OnlyPkColumnsId](json => JsResult.fromTry( | ||
Try( | ||
OnlyPkColumnsId( | ||
keyColumn1 = json.\("key_column_1").as(Reads.StringReads), | ||
keyColumn2 = json.\("key_column_2").as(Reads.IntReads) | ||
) | ||
) | ||
), | ||
) | ||
implicit lazy val writes: OWrites[OnlyPkColumnsId] = OWrites[OnlyPkColumnsId](o => | ||
new JsObject(ListMap[String, JsValue]( | ||
"key_column_1" -> Writes.StringWrites.writes(o.keyColumn1), | ||
"key_column_2" -> Writes.IntWrites.writes(o.keyColumn2) | ||
)) | ||
) | ||
} |
31 changes: 31 additions & 0 deletions
31
...rm/generated-and-checked-in/adventureworks/public/only_pk_columns/OnlyPkColumnsRepo.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* File has been automatically generated by `typo`. | ||
* | ||
* IF YOU CHANGE THIS FILE YOUR CHANGES WILL BE OVERWRITTEN. | ||
*/ | ||
package adventureworks | ||
package public | ||
package only_pk_columns | ||
|
||
import java.sql.Connection | ||
import typo.dsl.DeleteBuilder | ||
import typo.dsl.SelectBuilder | ||
import typo.dsl.UpdateBuilder | ||
|
||
trait OnlyPkColumnsRepo { | ||
def delete: DeleteBuilder[OnlyPkColumnsFields, OnlyPkColumnsRow] | ||
def deleteById(compositeId: OnlyPkColumnsId)(implicit c: Connection): Boolean | ||
def deleteByIds(compositeIds: Array[OnlyPkColumnsId])(implicit c: Connection): Int | ||
def insert(unsaved: OnlyPkColumnsRow)(implicit c: Connection): OnlyPkColumnsRow | ||
def insertStreaming(unsaved: Iterator[OnlyPkColumnsRow], batchSize: Int = 10000)(implicit c: Connection): Long | ||
def select: SelectBuilder[OnlyPkColumnsFields, OnlyPkColumnsRow] | ||
def selectAll(implicit c: Connection): List[OnlyPkColumnsRow] | ||
def selectById(compositeId: OnlyPkColumnsId)(implicit c: Connection): Option[OnlyPkColumnsRow] | ||
def selectByIds(compositeIds: Array[OnlyPkColumnsId])(implicit c: Connection): List[OnlyPkColumnsRow] | ||
def selectByIdsTracked(compositeIds: Array[OnlyPkColumnsId])(implicit c: Connection): Map[OnlyPkColumnsId, OnlyPkColumnsRow] | ||
def update: UpdateBuilder[OnlyPkColumnsFields, OnlyPkColumnsRow] | ||
def upsert(unsaved: OnlyPkColumnsRow)(implicit c: Connection): OnlyPkColumnsRow | ||
def upsertBatch(unsaved: Iterable[OnlyPkColumnsRow])(implicit c: Connection): List[OnlyPkColumnsRow] | ||
/* NOTE: this functionality is not safe if you use auto-commit mode! it runs 3 SQL statements */ | ||
def upsertStreaming(unsaved: Iterator[OnlyPkColumnsRow], batchSize: Int = 10000)(implicit c: Connection): Int | ||
} |
Oops, something went wrong.