Skip to content

Commit

Permalink
Merge pull request #87 from pauldraper/pauldraper/tuple
Browse files Browse the repository at this point in the history
Fix TupleParameter using sub-parameter placeholders
  • Loading branch information
tmccombs authored Sep 29, 2023
2 parents 1efb986 + 5eac690 commit 3b1d297
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
path: '*/target/**/*.jar'
retention-days: 1
publish:
if: ${{ github.event_name != 'pull_request' }} && (github.repository == 'lucidsoftware/java-thread-context') }}
if: github.event_name != 'pull_request' && (github.repository == 'lucidsoftware/java-thread-context')
needs: build
runs-on: ubuntu-22.04
steps:
Expand All @@ -60,7 +60,7 @@ jobs:
SONATYPE_USERNAME: ${{ secrets.SONATYPE_USERNAME }}
SONATYPE_PASSWORD: ${{ secrets.SONATYPE_PASSWORD }}
release:
if: ${{ startsWith(github.ref, 'refs/tags') && (github.repository == 'lucidsoftware/relate') }}
if: startsWith(github.ref, 'refs/tags') && (github.repository == 'lucidsoftware/relate')
runs-on: ubuntu-latest
needs: build
steps:
Expand Down
1 change: 1 addition & 0 deletions generate_sbt_opts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ if [[ $GITHUB_REF_TYPE = tag ]]; then
else
version="${GITHUB_HEAD_REF:-$GITHUB_REF_NAME}-SNAPSHOT"
fi
version="${version//\//_}"
echo "SBT_OPTS=-Dbuild.version=$version" >> $GITHUB_ENV
14 changes: 5 additions & 9 deletions relate/src/main/scala/com/lucidchart/relate/Parameters.scala
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,13 @@ trait MultipleParameter extends Parameter {
}

class TupleParameter(val params: Iterable[SingleParameter]) extends MultipleParameter {
def appendPlaceholders(stringBuilder: StringBuilder) = {
val length = params.size
if(length > 0) {
stringBuilder.append("?")
var i = 1
while (i < length) {
stringBuilder.append(",?")
i += 1
def appendPlaceholders(stringBuilder: StringBuilder) =
params.zipWithIndex.foreach { case (param, index) =>
if (0 < index) {
stringBuilder.append(",")
}
param.appendPlaceholders(stringBuilder)
}
}
}

object TupleParameter {
Expand Down
12 changes: 12 additions & 0 deletions relate/src/test/scala/ParameterizationTest.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.lucidchart.relate

import java.sql.PreparedStatement
import org.specs2.mutable._

class ParameterizationTest extends Specification {
Expand All @@ -17,4 +18,15 @@ class ParameterizationTest extends Specification {
querySql.toString mustEqual("INSERT INTO myTable (foo) VALUES (?,?,?)")
}
}

"tuple paramater" should {
"use sub-parameter placeholders" in {
class CustomParameter(value: Int) extends SingleParameter {
protected[this] def set(statement: PreparedStatement, i: Int) = implicitly[Parameterizable[Int]].set(statement, i, value)
override def appendPlaceholders(stringBuilder: StringBuilder) = stringBuilder.append("?::smallint")
}
val querySql = sql"INSERT INTO myTable (foo, bar) VALUES (${(1, new CustomParameter(1))})"
querySql.toString mustEqual("INSERT INTO myTable (foo, bar) VALUES (?,?::smallint)")
}
}
}

0 comments on commit 3b1d297

Please sign in to comment.