src/Pure/Tools/sqlite.scala
changeset 63780 163244cefb4e
parent 63779 9da65bc75610
equal deleted inserted replaced
63779:9da65bc75610 63780:163244cefb4e
     5 */
     5 */
     6 
     6 
     7 package isabelle
     7 package isabelle
     8 
     8 
     9 
     9 
    10 import java.sql.{Connection, DriverManager}
    10 import java.sql.{DriverManager, Connection, PreparedStatement}
    11 
    11 
    12 
    12 
    13 object SQLite
    13 object SQLite
    14 {
    14 {
    15   /* database connection */
    15   /** database connection **/
    16 
    16 
    17   class Database private [SQLite](path: Path, val connection: Connection)
    17   class Database private [SQLite](path: Path, val connection: Connection)
    18   {
    18   {
    19     override def toString: String = path.toString
    19     override def toString: String = path.toString
    20 
    20 
    32         result
    32         result
    33       }
    33       }
    34       catch { case exn: Throwable => connection.rollback(savepoint); throw exn }
    34       catch { case exn: Throwable => connection.rollback(savepoint); throw exn }
    35       finally { connection.setAutoCommit(auto_commit) }
    35       finally { connection.setAutoCommit(auto_commit) }
    36     }
    36     }
       
    37 
       
    38     def with_statement[A](sql: String)(body: PreparedStatement => A): A =
       
    39     {
       
    40       val stmt = connection.prepareStatement(sql)
       
    41       try { body(stmt) } finally { stmt.close }
       
    42     }
       
    43 
       
    44 
       
    45     /* tables */
       
    46 
       
    47     def create_table(table: SQL.Table, strict: Boolean = true, rowid: Boolean = true): Unit =
       
    48       with_statement(table.sql_create(strict, rowid))(_.execute())
       
    49 
       
    50     def drop_table(table: SQL.Table, strict: Boolean = true): Unit =
       
    51       with_statement(table.sql_drop(strict))(_.execute())
    37   }
    52   }
       
    53 
       
    54 
       
    55   /* open database */
    38 
    56 
    39   def open_database(path: Path): Database =
    57   def open_database(path: Path): Database =
    40   {
    58   {
    41     val path0 = path.expand
    59     val path0 = path.expand
    42     val s0 = File.platform_path(path0)
    60     val s0 = File.platform_path(path0)