-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApiExample.scala
53 lines (44 loc) · 1.67 KB
/
ApiExample.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
package com.example.scleradb.api
import com.scleradb.exec.Processor
object ApiExample {
def main(args: Array[String]): Unit = args match {
case Array("--init") => initialize() // initialize Sclera
case queries => runQueries(queries) // execute queries
}
// initialize Sclera
private def initialize(): Unit = {
// SQL processor
// we are initializing the schema, no need to check and validate
val processor: Processor = Processor(checkSchema = false)
try {
// initialize
processor.init()
// execute statement -- create schema
processor.handleStatement("create schema")
} finally processor.close()
}
// run queries on Sclera
private def runQueries(queries: Array[String]): Unit = {
// SQL processor
val processor: Processor = Processor()
try {
// initialize
processor.init()
// iterate over the input queries
queries.foreach { query =>
// execute query, and process the result
processor.handleStatement(query, { result =>
val colNames: Seq[String] =
result.columns.map { c => c.name }
println(colNames.mkString(", "))
result.rows.foreach { row =>
val rowVals: Seq[String] = colNames.map { cname =>
row.getStringOpt(cname) getOrElse "[not found]"
}
println(rowVals.mkString(", "))
}
})
}
} finally processor.close()
}
}