63776
|
1 |
/* Title: Pure/Tools/sqlite.scala
|
|
2 |
Author: Makarius
|
|
3 |
Options: :folding=explicit:
|
|
4 |
|
|
5 |
Support for SQLite databases.
|
|
6 |
*/
|
|
7 |
|
|
8 |
package isabelle
|
|
9 |
|
|
10 |
|
|
11 |
import java.sql.{Connection, DriverManager}
|
|
12 |
|
|
13 |
|
|
14 |
object SQLite
|
|
15 |
{
|
|
16 |
/* database connection */
|
|
17 |
|
|
18 |
def open_connection(path: Path): Connection =
|
|
19 |
{
|
|
20 |
val s0 = File.platform_path(path.expand)
|
|
21 |
val s1 = if (Platform.is_windows) s0.replace('\\', '/') else s0
|
|
22 |
DriverManager.getConnection("jdbc:sqlite:" + s1)
|
|
23 |
}
|
|
24 |
|
|
25 |
def with_connection[A](path: Path)(body: Connection => A): A =
|
|
26 |
{
|
|
27 |
val connection = open_connection(path)
|
|
28 |
try { body(connection) } finally { connection.close }
|
|
29 |
}
|
|
30 |
|
|
31 |
|
|
32 |
/* SQL syntax */
|
|
33 |
|
|
34 |
def quote_char(c: Char): String =
|
|
35 |
c match {
|
|
36 |
case '\u0000' => "\\0"
|
|
37 |
case '\'' => "\\'"
|
|
38 |
case '\"' => "\\\""
|
|
39 |
case '\b' => "\\b"
|
|
40 |
case '\n' => "\\n"
|
|
41 |
case '\r' => "\\r"
|
|
42 |
case '\t' => "\\t"
|
|
43 |
case '\u001a' => "\\Z"
|
|
44 |
case '\\' => "\\\\"
|
|
45 |
case _ => c.toString
|
|
46 |
}
|
|
47 |
|
|
48 |
def quote_string(s: String): String =
|
|
49 |
quote(s.map(quote_char(_)).mkString)
|
|
50 |
|
|
51 |
def quote_ident(s: String): String = "`" + s + "`"
|
|
52 |
}
|