| 63788 |      1 | /*  Title:      Pure/General/sqlite.scala
 | 
| 63776 |      2 |     Author:     Makarius
 | 
|  |      3 | 
 | 
|  |      4 | Support for SQLite databases.
 | 
|  |      5 | */
 | 
|  |      6 | 
 | 
|  |      7 | package isabelle
 | 
|  |      8 | 
 | 
|  |      9 | 
 | 
| 63780 |     10 | import java.sql.{DriverManager, Connection, PreparedStatement}
 | 
| 63776 |     11 | 
 | 
|  |     12 | 
 | 
|  |     13 | object SQLite
 | 
|  |     14 | {
 | 
| 63780 |     15 |   /** database connection **/
 | 
| 63776 |     16 | 
 | 
| 63790 |     17 |   def open_database(path: Path): Database =
 | 
|  |     18 |   {
 | 
|  |     19 |     val path0 = path.expand
 | 
|  |     20 |     val s0 = File.platform_path(path0)
 | 
|  |     21 |     val s1 = if (Platform.is_windows) s0.replace('\\', '/') else s0
 | 
|  |     22 |     val connection = DriverManager.getConnection("jdbc:sqlite:" + s1)
 | 
|  |     23 |     new Database(path0, connection)
 | 
|  |     24 |   }
 | 
|  |     25 | 
 | 
| 63996 |     26 |   class Database private[SQLite](path: Path, val connection: Connection)
 | 
| 63776 |     27 |   {
 | 
| 63778 |     28 |     override def toString: String = path.toString
 | 
| 63776 |     29 | 
 | 
| 64142 |     30 |     def close() { connection.close }
 | 
| 63779 |     31 | 
 | 
| 63811 |     32 |     def rebuild { using(statement("VACUUM"))(_.execute()) }
 | 
|  |     33 | 
 | 
| 63779 |     34 |     def transaction[A](body: => A): A =
 | 
|  |     35 |     {
 | 
|  |     36 |       val auto_commit = connection.getAutoCommit
 | 
|  |     37 |       val savepoint = connection.setSavepoint
 | 
|  |     38 | 
 | 
|  |     39 |       try {
 | 
|  |     40 |         connection.setAutoCommit(false)
 | 
|  |     41 |         val result = body
 | 
|  |     42 |         connection.commit
 | 
|  |     43 |         result
 | 
|  |     44 |       }
 | 
|  |     45 |       catch { case exn: Throwable => connection.rollback(savepoint); throw exn }
 | 
|  |     46 |       finally { connection.setAutoCommit(auto_commit) }
 | 
|  |     47 |     }
 | 
| 63780 |     48 | 
 | 
| 63790 |     49 | 
 | 
| 63791 |     50 |     /* statements */
 | 
| 63790 |     51 | 
 | 
|  |     52 |     def statement(sql: String): PreparedStatement = connection.prepareStatement(sql)
 | 
|  |     53 | 
 | 
|  |     54 |     def insert_statement(table: SQL.Table): PreparedStatement = statement(table.sql_insert)
 | 
| 63780 |     55 | 
 | 
| 63791 |     56 |     def select_statement(table: SQL.Table, columns: List[SQL.Column[Any]],
 | 
|  |     57 |         sql: String = "", distinct: Boolean = false): PreparedStatement =
 | 
|  |     58 |       statement(table.sql_select(columns, distinct) + (if (sql == "") "" else " " + sql))
 | 
|  |     59 | 
 | 
| 63780 |     60 | 
 | 
|  |     61 |     /* tables */
 | 
|  |     62 | 
 | 
| 63790 |     63 |     def tables: List[String] =
 | 
|  |     64 |       SQL.iterator(connection.getMetaData.getTables(null, null, "%", null))(_.getString(3)).toList
 | 
|  |     65 | 
 | 
| 63780 |     66 |     def create_table(table: SQL.Table, strict: Boolean = true, rowid: Boolean = true): Unit =
 | 
| 63790 |     67 |       using(statement(table.sql_create(strict, rowid)))(_.execute())
 | 
| 63780 |     68 | 
 | 
|  |     69 |     def drop_table(table: SQL.Table, strict: Boolean = true): Unit =
 | 
| 63790 |     70 |       using(statement(table.sql_drop(strict)))(_.execute())
 | 
| 63791 |     71 | 
 | 
|  |     72 |     def create_index(table: SQL.Table, name: String, columns: List[SQL.Column[Any]],
 | 
|  |     73 |         strict: Boolean = true, unique: Boolean = false): Unit =
 | 
|  |     74 |       using(statement(table.sql_create_index(name, columns, strict, unique)))(_.execute())
 | 
|  |     75 | 
 | 
|  |     76 |     def drop_index(table: SQL.Table, name: String, strict: Boolean = true): Unit =
 | 
|  |     77 |       using(statement(table.sql_drop_index(name, strict)))(_.execute())
 | 
| 63778 |     78 |   }
 | 
| 63776 |     79 | }
 |