Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[jdbc-scala] Regex extractor for executed statement, usable with rich…
… pattern: ```scala e/* : QueryExecution */ match { case ~(ExecutedStatement("^SELECT"), (sql, parameters)) => // ... } ```
- Loading branch information
cchantep
committed
Mar 1, 2014
1 parent
7bc9b54
commit 65a7e33
Showing
10 changed files
with
193 additions
and
106 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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,53 @@ | |||
package acolyte | |||
|
|||
import acolyte.ParameterMetaData.ParameterDef | |||
|
|||
/** Execution information */ | |||
sealed trait Execution { | |||
/** Executed SQL */ | |||
def sql: String | |||
|
|||
/** Execution parameters */ | |||
def parameters: List[ExecutedParameter] | |||
} | |||
|
|||
case class QueryExecution( | |||
sql: String, parameters: List[ExecutedParameter] = Nil) extends Execution | |||
|
|||
case class UpdateExecution( | |||
sql: String, parameters: List[ExecutedParameter] = Nil) extends Execution | |||
|
|||
/** Statement extractor */ | |||
case class ExecutedStatement( | |||
/** Statement pattern */ | |||
val p: String) { | |||
|
|||
val re = p.r | |||
|
|||
def unapply(x: Execution): Option[(String, List[ExecutedParameter])] = | |||
re.findFirstIn(x.sql).map(_ ⇒ (x.sql -> x.parameters)) | |||
} | |||
|
|||
sealed trait ExecutedParameter { def value: Any } | |||
|
|||
/** Executed parameter companion */ | |||
object ExecutedParameter { | |||
def apply(v: Any): ExecutedParameter = | |||
new ExecutedParameter { val value = v } | |||
|
|||
def unapply(p: ExecutedParameter): Option[Any] = Some(p.value) | |||
} | |||
|
|||
/** Parameter along with its definition. */ | |||
case class DefinedParameter( | |||
value: Any, definition: ParameterDef) extends ExecutedParameter { | |||
|
|||
override lazy val toString = s"Param($value, ${definition.sqlTypeName})" | |||
} | |||
|
|||
/** Value extractor for execution parameter. */ | |||
@deprecated( | |||
message = "Use [[acolyte.ExecutedParameter]] extractor", since = "1.0.15") | |||
object ParameterVal { | |||
def unapply(p: ExecutedParameter): Option[Any] = ExecutedParameter.unapply(p) | |||
} |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,81 @@ | |||
package acolyte | |||
|
|||
import acolyte.{ ExecutedParameter ⇒ XP } | |||
|
|||
object ExecutionSpec extends org.specs2.mutable.Specification { | |||
"Execution" title | |||
|
|||
"Query execution" >> { | |||
val (q1, q2) = ( | |||
QueryExecution("SELECT * FROM Test WHERE id = ?", XP("x") :: Nil), | |||
QueryExecution("EXEC reindex")) | |||
|
|||
"Query #1" should { | |||
"match case class pattern" in { | |||
q1 aka "q1" must beLike { | |||
case QueryExecution("SELECT * FROM Test WHERE id = ?", | |||
XP("x") :: Nil) ⇒ ok | |||
} | |||
} | |||
|
|||
"not match case class pattern" in { | |||
q1 aka "q1" must not(beLike { | |||
case QueryExecution("EXEC reindex", Nil) ⇒ ok | |||
}) | |||
} | |||
|
|||
"match statement extractor #1" in { | |||
q1 aka "q1" must beLike { | |||
case ~(ExecutedStatement("FROM Test"), (sql, XP("x") :: Nil)) ⇒ | |||
sql aka "matching SQL" must_== "SELECT * FROM Test WHERE id = ?" | |||
} | |||
} | |||
|
|||
"match statement extractor #2" in { | |||
q1 aka "q1" must beLike { | |||
case ~(ExecutedStatement("^SELECT"), (sql, _)) ⇒ ok | |||
} | |||
} | |||
|
|||
"not match statement extractor" in { | |||
q1 aka "q1" must not(beLike { | |||
case ~(ExecutedStatement(" reindex$"), (_, XP("x") :: Nil)) ⇒ ok | |||
}) | |||
} | |||
} | |||
|
|||
"Query #2" should { | |||
"match case class pattern" in { | |||
q2 aka "q2" must beLike { | |||
case QueryExecution("EXEC reindex", Nil) ⇒ ok | |||
} | |||
} | |||
|
|||
"not match case class pattern" in { | |||
q2 aka "q2" must not(beLike { | |||
case QueryExecution("SELECT * FROM Test WHERE id = ?", | |||
XP("x") :: Nil) ⇒ ok | |||
}) | |||
} | |||
|
|||
"match statement extractor #1" in { | |||
q2 aka "q2" must beLike { | |||
case ~(ExecutedStatement("EXEC"), (sql, Nil)) ⇒ | |||
sql aka "matching SQL" must_== "EXEC reindex" | |||
} | |||
} | |||
|
|||
"match statement extractor #2" in { | |||
q2 aka "q2" must beLike { | |||
case ~(ExecutedStatement(" reindex$"), (sql, _)) ⇒ ok | |||
} | |||
} | |||
|
|||
"not match statement extractor" in { | |||
q2 aka "q2" must not(beLike { | |||
case ~(ExecutedStatement("^SELECT"), (_, Nil)) ⇒ ok | |||
}) | |||
} | |||
} | |||
} | |||
} |
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
Oops, something went wrong.