src/Pure/General/sql.scala
author wenzelm
Fri, 17 Mar 2017 20:33:27 +0100
changeset 65292 e3bd1e7ddd23
parent 65280 ef37f5236794
child 65319 64da14387b2c
permissions -rw-r--r--
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
63788
3160826b92f8 clarified modules;
wenzelm
parents: 63784
diff changeset
     1
/*  Title:      Pure/General/sql.scala
63778
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
     3
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
     4
Support for SQL databases: SQLite and PostgreSQL.
63778
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
     5
*/
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
     6
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
     7
package isabelle
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
     8
65021
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
     9
import java.time.OffsetDateTime
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
    10
import java.sql.{DriverManager, Connection, PreparedStatement, ResultSet}
63779
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    11
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    12
63778
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    13
object SQL
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    14
{
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
    15
  /** SQL language **/
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
    16
63778
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    17
  /* concrete syntax */
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    18
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    19
  def quote_char(c: Char): String =
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    20
    c match {
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    21
      case '\u0000' => "\\0"
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    22
      case '\'' => "\\'"
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    23
      case '\"' => "\\\""
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    24
      case '\b' => "\\b"
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    25
      case '\n' => "\\n"
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    26
      case '\r' => "\\r"
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    27
      case '\t' => "\\t"
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    28
      case '\u001a' => "\\Z"
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    29
      case '\\' => "\\\\"
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    30
      case _ => c.toString
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    31
    }
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    32
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    33
  def quote_string(s: String): String =
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    34
    quote(s.map(quote_char(_)).mkString)
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    35
63779
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    36
  def quote_ident(s: String): String =
65003
4b4ccf86755c more portable: SQL standard syntax instead of MySQL extension;
wenzelm
parents: 63791
diff changeset
    37
    quote(s.replace("\"", "\"\""))
63779
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    38
63791
c6cbdfaae19e more operations;
wenzelm
parents: 63790
diff changeset
    39
  def enclosure(ss: Iterable[String]): String = ss.mkString("(", ", ", ")")
c6cbdfaae19e more operations;
wenzelm
parents: 63790
diff changeset
    40
63779
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    41
65008
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
    42
  /* types */
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
    43
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
    44
  object Type extends Enumeration
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
    45
  {
65011
42bffd1637f0 support for type Boolean;
wenzelm
parents: 65010
diff changeset
    46
    val Boolean = Value("BOOLEAN")
65008
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
    47
    val Int = Value("INTEGER")
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
    48
    val Long = Value("BIGINT")
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
    49
    val Double = Value("DOUBLE PRECISION")
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
    50
    val String = Value("TEXT")
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
    51
    val Bytes = Value("BLOB")
65014
97a622d01609 support for type Date;
wenzelm
parents: 65013
diff changeset
    52
    val Date = Value("TIMESTAMP WITH TIME ZONE")
65008
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
    53
  }
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
    54
65019
0ca91e5d52df tuned signature;
wenzelm
parents: 65018
diff changeset
    55
  def sql_type_default(T: Type.Value): String = T.toString
65013
wenzelm
parents: 65012
diff changeset
    56
65019
0ca91e5d52df tuned signature;
wenzelm
parents: 65018
diff changeset
    57
  def sql_type_sqlite(T: Type.Value): String =
0ca91e5d52df tuned signature;
wenzelm
parents: 65018
diff changeset
    58
    if (T == Type.Boolean) "INTEGER"
0ca91e5d52df tuned signature;
wenzelm
parents: 65018
diff changeset
    59
    else if (T == Type.Date) "TEXT"
0ca91e5d52df tuned signature;
wenzelm
parents: 65018
diff changeset
    60
    else sql_type_default(T)
65013
wenzelm
parents: 65012
diff changeset
    61
65019
0ca91e5d52df tuned signature;
wenzelm
parents: 65018
diff changeset
    62
  def sql_type_postgresql(T: Type.Value): String =
0ca91e5d52df tuned signature;
wenzelm
parents: 65018
diff changeset
    63
    if (T == Type.Bytes) "BYTEA"
0ca91e5d52df tuned signature;
wenzelm
parents: 65018
diff changeset
    64
    else sql_type_default(T)
65008
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
    65
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
    66
63779
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    67
  /* columns */
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    68
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    69
  object Column
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    70
  {
65280
ef37f5236794 prefer non-strict default;
wenzelm
parents: 65022
diff changeset
    71
    def bool(name: String, strict: Boolean = false, primary_key: Boolean = false): Column =
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
    72
      Column(name, Type.Boolean, strict, primary_key)
65280
ef37f5236794 prefer non-strict default;
wenzelm
parents: 65022
diff changeset
    73
    def int(name: String, strict: Boolean = false, primary_key: Boolean = false): Column =
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
    74
      Column(name, Type.Int, strict, primary_key)
65280
ef37f5236794 prefer non-strict default;
wenzelm
parents: 65022
diff changeset
    75
    def long(name: String, strict: Boolean = false, primary_key: Boolean = false): Column =
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
    76
      Column(name, Type.Long, strict, primary_key)
65280
ef37f5236794 prefer non-strict default;
wenzelm
parents: 65022
diff changeset
    77
    def double(name: String, strict: Boolean = false, primary_key: Boolean = false): Column =
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
    78
      Column(name, Type.Double, strict, primary_key)
65280
ef37f5236794 prefer non-strict default;
wenzelm
parents: 65022
diff changeset
    79
    def string(name: String, strict: Boolean = false, primary_key: Boolean = false): Column =
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
    80
      Column(name, Type.String, strict, primary_key)
65280
ef37f5236794 prefer non-strict default;
wenzelm
parents: 65022
diff changeset
    81
    def bytes(name: String, strict: Boolean = false, primary_key: Boolean = false): Column =
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
    82
      Column(name, Type.Bytes, strict, primary_key)
65280
ef37f5236794 prefer non-strict default;
wenzelm
parents: 65022
diff changeset
    83
    def date(name: String, strict: Boolean = false, primary_key: Boolean = false): Column =
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
    84
      Column(name, Type.Date, strict, primary_key)
63779
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    85
  }
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    86
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
    87
  sealed case class Column(
65280
ef37f5236794 prefer non-strict default;
wenzelm
parents: 65022
diff changeset
    88
    name: String, T: Type.Value, strict: Boolean = false, primary_key: Boolean = false)
63779
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    89
  {
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    90
    def sql_name: String = quote_ident(name)
65019
0ca91e5d52df tuned signature;
wenzelm
parents: 65018
diff changeset
    91
    def sql_decl(sql_type: Type.Value => String): String =
0ca91e5d52df tuned signature;
wenzelm
parents: 65018
diff changeset
    92
      sql_name + " " + sql_type(T) +
63781
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
    93
      (if (strict) " NOT NULL" else "") +
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
    94
      (if (primary_key) " PRIMARY KEY" else "")
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
    95
65019
0ca91e5d52df tuned signature;
wenzelm
parents: 65018
diff changeset
    96
    override def toString: String = sql_decl(sql_type_default)
63779
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    97
  }
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    98
63780
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
    99
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   100
  /* tables */
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   101
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   102
  sealed case class Table(name: String, columns: List[Column])
63780
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   103
  {
63790
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   104
    private val columns_index: Map[String, Int] =
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   105
      columns.iterator.map(_.name).zipWithIndex.toMap
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   106
63781
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   107
    Library.duplicates(columns.map(_.name)) match {
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   108
      case Nil =>
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   109
      case bad => error("Duplicate column names " + commas_quote(bad) + " for table " + quote(name))
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   110
    }
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   111
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   112
    columns.filter(_.primary_key) match {
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   113
      case bad if bad.length > 1 =>
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   114
        error("Multiple primary keys " + commas_quote(bad.map(_.name)) + " for table " + quote(name))
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   115
      case _ =>
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   116
    }
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   117
65019
0ca91e5d52df tuned signature;
wenzelm
parents: 65018
diff changeset
   118
    def sql_create(strict: Boolean, rowid: Boolean, sql_type: Type.Value => String): String =
63784
wenzelm
parents: 63783
diff changeset
   119
      "CREATE TABLE " + (if (strict) "" else "IF NOT EXISTS ") +
65019
0ca91e5d52df tuned signature;
wenzelm
parents: 65018
diff changeset
   120
        quote_ident(name) + " " + enclosure(columns.map(_.sql_decl(sql_type))) +
63780
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   121
        (if (rowid) "" else " WITHOUT ROWID")
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   122
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   123
    def sql_drop(strict: Boolean): String =
63784
wenzelm
parents: 63783
diff changeset
   124
      "DROP TABLE " + (if (strict) "" else "IF EXISTS ") + quote_ident(name)
63783
baa20f3b6cea tuned signature;
wenzelm
parents: 63781
diff changeset
   125
63791
c6cbdfaae19e more operations;
wenzelm
parents: 63790
diff changeset
   126
    def sql_create_index(
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   127
        index_name: String, index_columns: List[Column],
63791
c6cbdfaae19e more operations;
wenzelm
parents: 63790
diff changeset
   128
        strict: Boolean, unique: Boolean): String =
c6cbdfaae19e more operations;
wenzelm
parents: 63790
diff changeset
   129
      "CREATE " + (if (unique) "UNIQUE " else "") + "INDEX " +
c6cbdfaae19e more operations;
wenzelm
parents: 63790
diff changeset
   130
        (if (strict) "" else "IF NOT EXISTS ") + quote_ident(index_name) + " ON " +
c6cbdfaae19e more operations;
wenzelm
parents: 63790
diff changeset
   131
        quote_ident(name) + " " + enclosure(index_columns.map(_.name))
c6cbdfaae19e more operations;
wenzelm
parents: 63790
diff changeset
   132
c6cbdfaae19e more operations;
wenzelm
parents: 63790
diff changeset
   133
    def sql_drop_index(index_name: String, strict: Boolean): String =
c6cbdfaae19e more operations;
wenzelm
parents: 63790
diff changeset
   134
      "DROP INDEX " + (if (strict) "" else "IF EXISTS ") + quote_ident(index_name)
c6cbdfaae19e more operations;
wenzelm
parents: 63790
diff changeset
   135
63790
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   136
    def sql_insert: String =
63791
c6cbdfaae19e more operations;
wenzelm
parents: 63790
diff changeset
   137
      "INSERT INTO " + quote_ident(name) + " VALUES " + enclosure(columns.map(_ => "?"))
c6cbdfaae19e more operations;
wenzelm
parents: 63790
diff changeset
   138
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   139
    def sql_select(select_columns: List[Column], distinct: Boolean): String =
63791
c6cbdfaae19e more operations;
wenzelm
parents: 63790
diff changeset
   140
      "SELECT " + (if (distinct) "DISTINCT " else "") +
c6cbdfaae19e more operations;
wenzelm
parents: 63790
diff changeset
   141
      commas(select_columns.map(_.sql_name)) + " FROM " + quote_ident(name)
63790
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   142
63783
baa20f3b6cea tuned signature;
wenzelm
parents: 63781
diff changeset
   143
    override def toString: String =
63791
c6cbdfaae19e more operations;
wenzelm
parents: 63790
diff changeset
   144
      "TABLE " + quote_ident(name) + " " + enclosure(columns.map(_.toString))
63780
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   145
  }
63790
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   146
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   147
65012
wenzelm
parents: 65011
diff changeset
   148
wenzelm
parents: 65011
diff changeset
   149
  /** SQL database operations **/
wenzelm
parents: 65011
diff changeset
   150
63790
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   151
  /* results */
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   152
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   153
  def iterator[A](rs: ResultSet)(get: ResultSet => A): Iterator[A] = new Iterator[A]
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   154
  {
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   155
    private var _next: Boolean = rs.next()
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   156
    def hasNext: Boolean = _next
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   157
    def next: A = { val x = get(rs); _next = rs.next(); x }
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   158
  }
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   159
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   160
  trait Database
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   161
  {
65008
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
   162
    /* types */
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
   163
65019
0ca91e5d52df tuned signature;
wenzelm
parents: 65018
diff changeset
   164
    def sql_type(T: Type.Value): String
65008
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
   165
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
   166
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   167
    /* connection */
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   168
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   169
    def connection: Connection
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   170
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   171
    def close() { connection.close }
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   172
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   173
    def transaction[A](body: => A): A =
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   174
    {
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   175
      val auto_commit = connection.getAutoCommit
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   176
      try {
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   177
        connection.setAutoCommit(false)
65022
cda3d36aceb2 proper transaction for PostgreSQL;
wenzelm
parents: 65021
diff changeset
   178
        val savepoint = connection.setSavepoint
cda3d36aceb2 proper transaction for PostgreSQL;
wenzelm
parents: 65021
diff changeset
   179
        try {
cda3d36aceb2 proper transaction for PostgreSQL;
wenzelm
parents: 65021
diff changeset
   180
          val result = body
cda3d36aceb2 proper transaction for PostgreSQL;
wenzelm
parents: 65021
diff changeset
   181
          connection.commit
cda3d36aceb2 proper transaction for PostgreSQL;
wenzelm
parents: 65021
diff changeset
   182
          result
cda3d36aceb2 proper transaction for PostgreSQL;
wenzelm
parents: 65021
diff changeset
   183
        }
cda3d36aceb2 proper transaction for PostgreSQL;
wenzelm
parents: 65021
diff changeset
   184
        catch { case exn: Throwable => connection.rollback(savepoint); throw exn }
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   185
      }
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   186
      finally { connection.setAutoCommit(auto_commit) }
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   187
    }
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   188
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   189
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   190
    /* statements */
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   191
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   192
    def statement(sql: String): PreparedStatement = connection.prepareStatement(sql)
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   193
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   194
    def insert_statement(table: Table): PreparedStatement = statement(table.sql_insert)
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   195
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   196
    def select_statement(table: Table, columns: List[Column],
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   197
        sql: String = "", distinct: Boolean = false): PreparedStatement =
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   198
      statement(table.sql_select(columns, distinct) + (if (sql == "") "" else " " + sql))
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   199
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   200
65020
a4fcaeadc825 more operations;
wenzelm
parents: 65019
diff changeset
   201
    /* input */
a4fcaeadc825 more operations;
wenzelm
parents: 65019
diff changeset
   202
a4fcaeadc825 more operations;
wenzelm
parents: 65019
diff changeset
   203
    def set_bool(stmt: PreparedStatement, i: Int, x: Boolean) { stmt.setBoolean(i, x) }
a4fcaeadc825 more operations;
wenzelm
parents: 65019
diff changeset
   204
    def set_int(stmt: PreparedStatement, i: Int, x: Int) { stmt.setInt(i, x) }
a4fcaeadc825 more operations;
wenzelm
parents: 65019
diff changeset
   205
    def set_long(stmt: PreparedStatement, i: Int, x: Long) { stmt.setLong(i, x) }
a4fcaeadc825 more operations;
wenzelm
parents: 65019
diff changeset
   206
    def set_double(stmt: PreparedStatement, i: Int, x: Double) { stmt.setDouble(i, x) }
a4fcaeadc825 more operations;
wenzelm
parents: 65019
diff changeset
   207
    def set_string(stmt: PreparedStatement, i: Int, x: String) { stmt.setString(i, x) }
a4fcaeadc825 more operations;
wenzelm
parents: 65019
diff changeset
   208
    def set_bytes(stmt: PreparedStatement, i: Int, bytes: Bytes)
a4fcaeadc825 more operations;
wenzelm
parents: 65019
diff changeset
   209
    { stmt.setBinaryStream(i, bytes.stream(), bytes.length) }
a4fcaeadc825 more operations;
wenzelm
parents: 65019
diff changeset
   210
    def set_date(stmt: PreparedStatement, i: Int, date: Date)
a4fcaeadc825 more operations;
wenzelm
parents: 65019
diff changeset
   211
65022
cda3d36aceb2 proper transaction for PostgreSQL;
wenzelm
parents: 65021
diff changeset
   212
65020
a4fcaeadc825 more operations;
wenzelm
parents: 65019
diff changeset
   213
    /* output */
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   214
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   215
    def bool(rs: ResultSet, name: String): Boolean = rs.getBoolean(name)
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   216
    def int(rs: ResultSet, name: String): Int = rs.getInt(name)
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   217
    def long(rs: ResultSet, name: String): Long = rs.getLong(name)
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   218
    def double(rs: ResultSet, name: String): Double = rs.getDouble(name)
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   219
    def string(rs: ResultSet, name: String): String =
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   220
    {
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   221
      val s = rs.getString(name)
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   222
      if (s == null) "" else s
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   223
    }
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   224
    def bytes(rs: ResultSet, name: String): Bytes =
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   225
    {
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   226
      val bs = rs.getBytes(name)
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   227
      if (bs == null) Bytes.empty else Bytes(bs)
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   228
    }
65021
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
   229
    def date(rs: ResultSet, name: String): Date
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   230
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   231
    def get[A](rs: ResultSet, name: String, f: (ResultSet, String) => A): Option[A] =
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   232
    {
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   233
      val x = f(rs, name)
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   234
      if (rs.wasNull) None else Some(x)
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   235
    }
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   236
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   237
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   238
    /* tables */
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   239
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   240
    def tables: List[String] =
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   241
      iterator(connection.getMetaData.getTables(null, null, "%", null))(_.getString(3)).toList
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   242
65280
ef37f5236794 prefer non-strict default;
wenzelm
parents: 65022
diff changeset
   243
    def create_table(table: Table, strict: Boolean = false, rowid: Boolean = true): Unit =
65019
0ca91e5d52df tuned signature;
wenzelm
parents: 65018
diff changeset
   244
      using(statement(table.sql_create(strict, rowid, sql_type)))(_.execute())
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   245
65280
ef37f5236794 prefer non-strict default;
wenzelm
parents: 65022
diff changeset
   246
    def drop_table(table: Table, strict: Boolean = false): Unit =
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   247
      using(statement(table.sql_drop(strict)))(_.execute())
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   248
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   249
    def create_index(table: Table, name: String, columns: List[Column],
65280
ef37f5236794 prefer non-strict default;
wenzelm
parents: 65022
diff changeset
   250
        strict: Boolean = false, unique: Boolean = false): Unit =
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   251
      using(statement(table.sql_create_index(name, columns, strict, unique)))(_.execute())
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   252
65280
ef37f5236794 prefer non-strict default;
wenzelm
parents: 65022
diff changeset
   253
    def drop_index(table: Table, name: String, strict: Boolean = false): Unit =
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   254
      using(statement(table.sql_drop_index(name, strict)))(_.execute())
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   255
  }
63778
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
   256
}
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   257
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   258
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   259
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   260
/** SQLite **/
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   261
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   262
object SQLite
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   263
{
65021
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
   264
  // see https://www.sqlite.org/lang_datefunc.html
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
   265
  val date_format: Date.Format = Date.Format("uuuu-MM-dd HH:mm:ss.SSS x")
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
   266
65292
e3bd1e7ddd23 more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents: 65280
diff changeset
   267
  lazy val init_jdbc: Unit = Class.forName("org.sqlite.JDBC")
e3bd1e7ddd23 more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents: 65280
diff changeset
   268
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   269
  def open_database(path: Path): Database =
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   270
  {
65292
e3bd1e7ddd23 more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents: 65280
diff changeset
   271
    init_jdbc
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   272
    val path0 = path.expand
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   273
    val s0 = File.platform_path(path0)
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   274
    val s1 = if (Platform.is_windows) s0.replace('\\', '/') else s0
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   275
    val connection = DriverManager.getConnection("jdbc:sqlite:" + s1)
65007
b6a1a1d42f5d clarified toString;
wenzelm
parents: 65006
diff changeset
   276
    new Database(path0.toString, connection)
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   277
  }
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   278
65007
b6a1a1d42f5d clarified toString;
wenzelm
parents: 65006
diff changeset
   279
  class Database private[SQLite](name: String, val connection: Connection) extends SQL.Database
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   280
  {
65007
b6a1a1d42f5d clarified toString;
wenzelm
parents: 65006
diff changeset
   281
    override def toString: String = name
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   282
65019
0ca91e5d52df tuned signature;
wenzelm
parents: 65018
diff changeset
   283
    def sql_type(T: SQL.Type.Value): String = SQL.sql_type_sqlite(T)
65011
42bffd1637f0 support for type Boolean;
wenzelm
parents: 65010
diff changeset
   284
65021
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
   285
    def set_date(stmt: PreparedStatement, i: Int, date: Date): Unit =
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
   286
      set_string(stmt, i, date_format(date))
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
   287
    def date(rs: ResultSet, name: String): Date =
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
   288
      date_format.parse(string(rs, name))
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
   289
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   290
    def rebuild { using(statement("VACUUM"))(_.execute()) }
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   291
  }
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   292
}
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   293
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   294
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   295
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   296
/** PostgreSQL **/
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   297
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   298
object PostgreSQL
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   299
{
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   300
  val default_port = 5432
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   301
65292
e3bd1e7ddd23 more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents: 65280
diff changeset
   302
  lazy val init_jdbc: Unit = Class.forName("org.postgresql.Driver")
e3bd1e7ddd23 more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents: 65280
diff changeset
   303
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   304
  def open_database(
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   305
    user: String,
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   306
    password: String,
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   307
    database: String = "",
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   308
    host: String = "",
65009
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   309
    port: Int = default_port,
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   310
    ssh: Option[SSH.Session] = None): Database =
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   311
  {
65292
e3bd1e7ddd23 more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents: 65280
diff changeset
   312
    init_jdbc
e3bd1e7ddd23 more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents: 65280
diff changeset
   313
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   314
    require(user != "")
65009
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   315
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   316
    val db_host = if (host != "") host else "localhost"
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   317
    val db_port = if (port != default_port) ":" + port else ""
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   318
    val db_name = "/" + (if (database != "") database else user)
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   319
65010
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   320
    val (url, name, port_forwarding) =
65009
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   321
      ssh match {
65010
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   322
        case None =>
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   323
          val spec = db_host + db_port + db_name
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   324
          val url = "jdbc:postgresql://" + spec
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   325
          val name = user + "@" + spec
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   326
          (url, name, None)
65009
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   327
        case Some(ssh) =>
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   328
          val fw = ssh.port_forwarding(remote_host = db_host, remote_port = port)
65010
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   329
          val url = "jdbc:postgresql://localhost:" + fw.local_port + db_name
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   330
          val name = user + "@" + fw + db_name + " via ssh " + ssh
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   331
          (url, name, Some(fw))
65009
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   332
      }
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   333
    try {
65010
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   334
      val connection = DriverManager.getConnection(url, user, password)
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   335
      new Database(name, connection, port_forwarding)
65009
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   336
    }
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   337
    catch { case exn: Throwable => port_forwarding.foreach(_.close); throw exn }
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   338
  }
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   339
65009
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   340
  class Database private[PostgreSQL](
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   341
      name: String, val connection: Connection, port_forwarding: Option[SSH.Port_Forwarding])
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   342
    extends SQL.Database
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   343
  {
65010
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   344
    override def toString: String = name
65008
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
   345
65019
0ca91e5d52df tuned signature;
wenzelm
parents: 65018
diff changeset
   346
    def sql_type(T: SQL.Type.Value): String = SQL.sql_type_postgresql(T)
65009
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   347
65021
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
   348
    // see https://jdbc.postgresql.org/documentation/head/8-date-time.html
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
   349
    def set_date(stmt: PreparedStatement, i: Int, date: Date): Unit =
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
   350
      stmt.setObject(i, OffsetDateTime.from(date.to_utc.rep))
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
   351
    def date(rs: ResultSet, name: String): Date =
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
   352
      Date.instant(rs.getObject(name, classOf[OffsetDateTime]).toInstant)
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
   353
65009
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   354
    override def close() { super.close; port_forwarding.foreach(_.close) }
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   355
  }
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   356
}