src/Pure/Tools/sqlite.scala
author wenzelm
Sun, 04 Sep 2016 15:44:20 +0200
changeset 63778 e06e899b78d0
parent 63777 fc030773ec90
child 63779 9da65bc75610
permissions -rw-r--r--
clarified modules; tuned signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
63776
f1968429e339 minimal support for SQLite databases;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Tools/sqlite.scala
f1968429e339 minimal support for SQLite databases;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
f1968429e339 minimal support for SQLite databases;
wenzelm
parents:
diff changeset
     3
f1968429e339 minimal support for SQLite databases;
wenzelm
parents:
diff changeset
     4
Support for SQLite databases.
f1968429e339 minimal support for SQLite databases;
wenzelm
parents:
diff changeset
     5
*/
f1968429e339 minimal support for SQLite databases;
wenzelm
parents:
diff changeset
     6
f1968429e339 minimal support for SQLite databases;
wenzelm
parents:
diff changeset
     7
package isabelle
f1968429e339 minimal support for SQLite databases;
wenzelm
parents:
diff changeset
     8
f1968429e339 minimal support for SQLite databases;
wenzelm
parents:
diff changeset
     9
f1968429e339 minimal support for SQLite databases;
wenzelm
parents:
diff changeset
    10
import java.sql.{Connection, DriverManager}
f1968429e339 minimal support for SQLite databases;
wenzelm
parents:
diff changeset
    11
f1968429e339 minimal support for SQLite databases;
wenzelm
parents:
diff changeset
    12
f1968429e339 minimal support for SQLite databases;
wenzelm
parents:
diff changeset
    13
object SQLite
f1968429e339 minimal support for SQLite databases;
wenzelm
parents:
diff changeset
    14
{
f1968429e339 minimal support for SQLite databases;
wenzelm
parents:
diff changeset
    15
  /* database connection */
f1968429e339 minimal support for SQLite databases;
wenzelm
parents:
diff changeset
    16
63778
e06e899b78d0 clarified modules;
wenzelm
parents: 63777
diff changeset
    17
  class Database private [SQLite](path: Path, val connection: Connection)
63776
f1968429e339 minimal support for SQLite databases;
wenzelm
parents:
diff changeset
    18
  {
63778
e06e899b78d0 clarified modules;
wenzelm
parents: 63777
diff changeset
    19
    override def toString: String = path.toString
63776
f1968429e339 minimal support for SQLite databases;
wenzelm
parents:
diff changeset
    20
63778
e06e899b78d0 clarified modules;
wenzelm
parents: 63777
diff changeset
    21
    def close { connection.close }
63776
f1968429e339 minimal support for SQLite databases;
wenzelm
parents:
diff changeset
    22
  }
f1968429e339 minimal support for SQLite databases;
wenzelm
parents:
diff changeset
    23
63778
e06e899b78d0 clarified modules;
wenzelm
parents: 63777
diff changeset
    24
  def open_database(path: Path): Database =
e06e899b78d0 clarified modules;
wenzelm
parents: 63777
diff changeset
    25
  {
e06e899b78d0 clarified modules;
wenzelm
parents: 63777
diff changeset
    26
    val path0 = path.expand
e06e899b78d0 clarified modules;
wenzelm
parents: 63777
diff changeset
    27
    val s0 = File.platform_path(path0)
e06e899b78d0 clarified modules;
wenzelm
parents: 63777
diff changeset
    28
    val s1 = if (Platform.is_windows) s0.replace('\\', '/') else s0
e06e899b78d0 clarified modules;
wenzelm
parents: 63777
diff changeset
    29
    val connection = DriverManager.getConnection("jdbc:sqlite:" + s1)
e06e899b78d0 clarified modules;
wenzelm
parents: 63777
diff changeset
    30
    new Database(path0, connection)
e06e899b78d0 clarified modules;
wenzelm
parents: 63777
diff changeset
    31
  }
63776
f1968429e339 minimal support for SQLite databases;
wenzelm
parents:
diff changeset
    32
63778
e06e899b78d0 clarified modules;
wenzelm
parents: 63777
diff changeset
    33
  def with_database[A](path: Path)(body: Database => A): A =
e06e899b78d0 clarified modules;
wenzelm
parents: 63777
diff changeset
    34
  {
e06e899b78d0 clarified modules;
wenzelm
parents: 63777
diff changeset
    35
    val db = open_database(path)
e06e899b78d0 clarified modules;
wenzelm
parents: 63777
diff changeset
    36
    try { body(db) } finally { db.close }
e06e899b78d0 clarified modules;
wenzelm
parents: 63777
diff changeset
    37
  }
63776
f1968429e339 minimal support for SQLite databases;
wenzelm
parents:
diff changeset
    38
}