Skip to content

Commit

Permalink
Merge pull request #293 from lewchuk/upsertQuoting
Browse files Browse the repository at this point in the history
Add support for upsert with tables containing keyword column names
  • Loading branch information
tminglei authored Jul 27, 2016
2 parents 183608d + cfb69bf commit 7c69400
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 14 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,52 +190,52 @@ Install
-------
To use `slick-pg` in [sbt](http://www.scala-sbt.org/ "slick-sbt") project, add the following to your project file:
```scala
libraryDependencies += "com.github.tminglei" %% "slick-pg" % "0.14.2"
libraryDependencies += "com.github.tminglei" %% "slick-pg" % "0.14.3"
```

> If you need `joda-time` support, pls append dependency:
```scala
libraryDependencies += "com.github.tminglei" %% "slick-pg_joda-time" % "0.14.2"
libraryDependencies += "com.github.tminglei" %% "slick-pg_joda-time" % "0.14.3"
```

> If you need `jts` geom support, pls append dependency:
```scala
libraryDependencies += "com.github.tminglei" %% "slick-pg_jts" % "0.14.2"
libraryDependencies += "com.github.tminglei" %% "slick-pg_jts" % "0.14.3"
```

> If you need `jdk8 date` support, pls append dependency:
```scala
libraryDependencies += "com.github.tminglei" %% "slick-pg_date2" % "0.14.2"
libraryDependencies += "com.github.tminglei" %% "slick-pg_date2" % "0.14.3"
```

> If you need `threeten-bp` support, pls append dependency:
```scala
libraryDependencies += "com.github.tminglei" %% "slick-pg_threeten" % "0.14.2"
libraryDependencies += "com.github.tminglei" %% "slick-pg_threeten" % "0.14.3"
```

> If you need `json4s` support, pls append dependency:
```scala
libraryDependencies += "com.github.tminglei" %% "slick-pg_json4s" % "0.14.2"
libraryDependencies += "com.github.tminglei" %% "slick-pg_json4s" % "0.14.3"
```

> If you need `play-json` support, pls append dependency:
```scala
libraryDependencies += "com.github.tminglei" %% "slick-pg_play-json" % "0.14.2"
libraryDependencies += "com.github.tminglei" %% "slick-pg_play-json" % "0.14.3"
```

> If you need `spray-json` support, pls append dependency:
```scala
libraryDependencies += "com.github.tminglei" %% "slick-pg_spray-json" % "0.14.2"
libraryDependencies += "com.github.tminglei" %% "slick-pg_spray-json" % "0.14.3"
```

> If you need `argonaut json` support, pls append dependency:
```scala
libraryDependencies += "com.github.tminglei" %% "slick-pg_argonaut" % "0.14.2"
libraryDependencies += "com.github.tminglei" %% "slick-pg_argonaut" % "0.14.3"
```

> If you need `circe json` support, pls append dependency:
```scala
libraryDependencies += "com.github.tminglei" %% "slick-pg_circe-json" % "0.14.2"
libraryDependencies += "com.github.tminglei" %% "slick-pg_circe-json" % "0.14.3"
```


Expand All @@ -244,7 +244,7 @@ Or, in [maven](http://maven.apache.org/ "maven") project, you can add `slick-pg`
<dependency>
<groupId>com.github.tminglei</groupId>
<artifactId>slick-pg_2.11</artifactId>
<version>0.14.2</version>
<version>0.14.3</version>
</dependency>
<!-- other addons if necessary -->
...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,12 @@ trait ExPostgresDriver extends JdbcDriver with PostgresDriver with Logging { dri
sym.options.contains(ColumnOption.PrimaryKey) || funcDefinedPKs.exists(pk => pk.columns.collect {
case Select(_, f: FieldSymbol) => f
}.exists(_.name == sym.name)) }
private lazy val insertNames = insertingSyms.map { fs => quoteIdentifier(fs.name) }
private lazy val pkNames = pkSyms.map { fs => quoteIdentifier(fs.name) }
private lazy val softNames = softSyms.map { fs => quoteIdentifier(fs.name) }

override def buildInsert: InsertBuilderResult = {
val insert = s"insert into $tableName (${insertingSyms.mkString(",")}) values (${insertingSyms.map(_ => "?").mkString(",")})"
val insert = s"insert into $tableName (${insertNames.mkString(",")}) values (${insertNames.map(_ => "?").mkString(",")})"
val onConflict = "on conflict (" + pkNames.mkString(", ") + ")"
val doSomething = if (softNames.isEmpty) "do nothing" else "do update set " + softNames.map(n => s"$n=EXCLUDED.$n").mkString(",")
val padding = if (nonPkAutoIncSyms.isEmpty) "" else "where ? is null or ?=?"
Expand Down Expand Up @@ -183,4 +184,4 @@ trait ExPostgresDriver extends JdbcDriver with PostgresDriver with Logging { dri
}
}

object ExPostgresDriver extends ExPostgresDriver
object ExPostgresDriver extends ExPostgresDriver
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,54 @@ class PgUpsertSuite extends FunSuite {
).transactionally
), Duration.Inf)
}

///---
case class Bean2(id: Long, start: Int, end: Int)

class UpsertTestTable2(tag: Tag) extends Table[Bean2](tag, "test_tab_upsert2") {
def id = column[Long]("id", O.AutoInc, O.PrimaryKey)
def start = column[Int]("start")
def end = column[Int]("end")

def * = (id, start, end) <> (Bean2.tupled, Bean2.unapply)
}
val UpsertTests2 = TableQuery[UpsertTestTable2]

//------------------------------------------------------------------------------

test("native upsert support with keyword columns") {
import MyPostgresDriver.api._

val upsertSql = MyPostgresDriver.compileInsert(UpsertTests2.toNode).upsert.sql
println(s"upsert sql: $upsertSql")

assert(upsertSql.contains("on conflict"))

Await.result(db.run(
DBIO.seq(
(UpsertTests2.schema) create,
///
UpsertTests2 forceInsertAll Seq(
Bean2(101, 1, 2),
Bean2(102, 3, 4),
Bean2(103, 5, 6)
),
UpsertTests2.insertOrUpdate(Bean2(101, 1, 7)),
UpsertTests2.insertOrUpdate(Bean2(107, 8, 9))
).andThen(
DBIO.seq(
UpsertTests2.sortBy(_.id).to[List].result.map(
r => assert(Seq(
Bean2(101, 1, 7),
Bean2(102, 3, 4),
Bean2(103, 5, 6),
Bean2(107, 8, 9)
) === r)
)
)
).andFinally(
(UpsertTests2.schema) drop
).transactionally
), Duration.Inf)
}
}
2 changes: 1 addition & 1 deletion project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ object SlickPgBuild extends Build {
organizationName := "slick-pg",
organization := "com.github.tminglei",
name := "slick-pg",
version := "0.14.2",
version := "0.14.3",

scalaVersion := "2.11.8",
crossScalaVersions := Seq("2.11.8", "2.10.6"),
Expand Down

0 comments on commit 7c69400

Please sign in to comment.