63776
|
1 |
/* Title: Pure/Tools/sqlite.scala
|
|
2 |
Author: Makarius
|
|
3 |
|
|
4 |
Support for SQLite databases.
|
|
5 |
*/
|
|
6 |
|
|
7 |
package isabelle
|
|
8 |
|
|
9 |
|
|
10 |
import java.sql.{Connection, DriverManager}
|
|
11 |
|
|
12 |
|
|
13 |
object SQLite
|
|
14 |
{
|
|
15 |
/* database connection */
|
|
16 |
|
63778
|
17 |
class Database private [SQLite](path: Path, val connection: Connection)
|
63776
|
18 |
{
|
63778
|
19 |
override def toString: String = path.toString
|
63776
|
20 |
|
63778
|
21 |
def close { connection.close }
|
63776
|
22 |
}
|
|
23 |
|
63778
|
24 |
def open_database(path: Path): Database =
|
|
25 |
{
|
|
26 |
val path0 = path.expand
|
|
27 |
val s0 = File.platform_path(path0)
|
|
28 |
val s1 = if (Platform.is_windows) s0.replace('\\', '/') else s0
|
|
29 |
val connection = DriverManager.getConnection("jdbc:sqlite:" + s1)
|
|
30 |
new Database(path0, connection)
|
|
31 |
}
|
63776
|
32 |
|
63778
|
33 |
def with_database[A](path: Path)(body: Database => A): A =
|
|
34 |
{
|
|
35 |
val db = open_database(path)
|
|
36 |
try { body(db) } finally { db.close }
|
|
37 |
}
|
63776
|
38 |
}
|