/
ScalaUseCases.scala
124 lines (103 loc) · 3.97 KB
/
ScalaUseCases.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package acolyte
import java.sql.{ Connection ⇒ SqlConnection, Date, DriverManager }
import acolyte.{ Driver ⇒ AcolyteDriver }
import acolyte.RowLists.{ rowList1, rowList3, stringList }
import acolyte.Rows.{ row1, row3 }
import acolyte.Acolyte.{ connection, handleQuery, handleStatement } // DSL
import acolyte.Implicits._
/**
* Use cases for testing.
*
* @author Cedric Chantepie
*/
object ScalaUseCases {
/**
* Use case #1
*/
def useCase1: SqlConnection = {
// Configure in anyway JDBC with following url,
// declaring handler registered with 'handler1' will be used.
val jdbcUrl =
"jdbc:acolyte:anything-you-want?handler=handler1";
// Prepare handler
val handler: ScalaCompositeHandler = handleStatement.
withQueryDetection(
"^SELECT ", // regex test from beginning
"EXEC that_proc"). // second detection regex
withUpdateHandler { e: UpdateExecution ⇒
if (e.sql.startsWith("DELETE ")) {
// Process deletion ...
/* deleted = */ 2;
} else {
// ... Process ...
/* count = */ 1;
}
} withQueryHandler { e: QueryExecution ⇒
if (e.sql.startsWith("SELECT ")) {
RowLists.rowList1(classOf[String]).asResult
} else {
// ... EXEC that_proc
// (see previous withQueryDetection)
// Prepare list of 2 rows
// with 3 columns of types String, Float, Date
val rows: RowList3[String, Float, Date] =
rowList3(classOf[String], classOf[Float], classOf[Date]).
withLabels( // Optional: set labels
1 -> "String",
3 -> "Date") :+
("str", 1.2f, new Date(1l)) :+
row3[String, Float, Date]("val", 2.34f, null)
rows.asResult
}
}
// Register prepared handler with expected ID 'handler1'
AcolyteDriver.register("handler1", handler)
// ... then connection is managed through |handler|
DriverManager.getConnection(jdbcUrl)
} // end of useCase1
/**
* Use case #2 - Columns definition
*/
def useCase2: SqlConnection = {
val jdbcUrl = "jdbc:acolyte:anything-you-want?handler=handler2";
val handler: ScalaCompositeHandler = handleStatement.
withQueryDetection("^SELECT ") withQueryHandler { execution ⇒
rowList3(classOf[String] -> "str",
classOf[Float] -> "f",
classOf[Date] -> "date").
append(row3("text", 2.3f, new Date(3l))).
append(row3("label", 4.56f, new Date(4l)))
}
// Register prepared handler with expected ID 'handler2'
acolyte.Driver.register("handler2", handler)
// ... then connection is managed through |handler|
return DriverManager.getConnection(jdbcUrl);
} // end of useCase2
/**
* Use case #3 - Pattern matching
*/
def useCase3: SqlConnection = {
val jdbcUrl = "jdbc:acolyte:anything-you-want?handler=handler3";
val handler: ScalaCompositeHandler = handleStatement.
withQueryDetection("^SELECT ") withQueryHandler {
_ match { // QueryExecution
case QueryExecution(s, ParameterVal("id") :: Nil) ⇒
(stringList :+ "useCase_3a").asResult
case QueryExecution(s,
DefinedParameter("id", _) :: DefinedParameter(3, _) :: Nil) ⇒
(rowList3(classOf[String], classOf[Int], classOf[Long]) :+ row3(
"useCase_3str", 2, 3l)).asResult
case q ⇒ QueryResult.Nil withWarning "Now you're warned"
}
}
// Register prepared handler with expected ID 'handler3'
acolyte.Driver.register("handler3", handler)
// ... then connection is managed through |handler|
return DriverManager.getConnection(jdbcUrl);
} // end of useCase3
/**
* Use case #4 - Row list convinience constructor
* and query handler convertion.
*/
def useCase4: SqlConnection = connection { handleQuery { e ⇒ true } }
} // end of class ScalaUseCases