diff --git a/relate/src/main/scala/com/lucidchart/relate/Parameters.scala b/relate/src/main/scala/com/lucidchart/relate/Parameters.scala index cc45908..80f879d 100644 --- a/relate/src/main/scala/com/lucidchart/relate/Parameters.scala +++ b/relate/src/main/scala/com/lucidchart/relate/Parameters.scala @@ -632,7 +632,9 @@ trait MultipleParameter extends Parameter { } } -class TupleParameter(val params: Iterable[SingleParameter]) extends MultipleParameter { +class TupleParameter(_params: Iterable[SingleParameter]) extends MultipleParameter { + // get a `Seq` to make sure placeholder and parameterize get the same ordering + override protected val params = _params.toSeq def placeholder = params.iterator.map(_.placeholder).mkString(",") } @@ -640,6 +642,7 @@ object TupleParameter { def apply(params: SingleParameter*) = new TupleParameter(params) } -class TuplesParameter(val params: Iterable[TupleParameter]) extends MultipleParameter { +class TuplesParameter(_params: Iterable[TupleParameter]) extends MultipleParameter { + override protected val params = _params.toSeq def placeholder = if (params.isEmpty) "" else params.map(_.placeholder).mkString("(", "),(", ")") } diff --git a/relate/src/test/scala/ParameterizationTest.scala b/relate/src/test/scala/ParameterizationTest.scala index ada03db..11b2b27 100644 --- a/relate/src/test/scala/ParameterizationTest.scala +++ b/relate/src/test/scala/ParameterizationTest.scala @@ -24,6 +24,23 @@ class ParameterizationTest extends Specification { val querySql = sql"SELECT * FROM myTable WHERE id IN ($hashSet)" querySql.toString mustEqual "SELECT * FROM myTable WHERE id IN (?,?,?)" } + + "provide placeholders and parameters in the same order" in { + val setParams = scala.collection.mutable.Map.empty[Int, Int] + case class Param(int: Int) extends SingleParameter { + protected[this] def set(statement: PreparedStatement, i: Int) = setParams(i) = int + override def placeholder = int.toString + } + + val paramsSet: Parameter = Set(Param(1), Param(2), Param(3)) + paramsSet.parameterize(null, 1) + val query = sql"SELECT * FROM myTable WHERE id IN ($paramsSet)".toString + + setParams must haveSize(3) + val order = setParams.toSeq.sortBy(_._1).map(_._2).mkString(",") + // the order of the "placeholders" should match the order of the parameters + query mustEqual s"SELECT * FROM myTable WHERE id IN ($order)" + } } "tuple paramater" should {