src/Pure/General/sql.scala
author wenzelm
Thu, 27 Apr 2017 23:36:59 +0200
changeset 65598 5deef985e38e
parent 65594 659305708959
child 65602 d9533e9615ad
permissions -rw-r--r--
allow null;
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
65321
2b1cd063e0b2 proper SQL syntax (according of PostgreSQL);
wenzelm
parents: 65319
diff changeset
    19
  def escape_char(c: Char): String =
63778
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 =
65321
2b1cd063e0b2 proper SQL syntax (according of PostgreSQL);
wenzelm
parents: 65319
diff changeset
    34
    "'" + s.map(escape_char(_)).mkString + "'"
63778
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 =
65325
981df08de0ab more general primary_key;
wenzelm
parents: 65323
diff changeset
    92
      sql_name + " " + sql_type(T) + (if (strict || primary_key) " NOT NULL" else "")
63781
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
    93
65593
607f7ad07a60 tuned signature;
wenzelm
parents: 65327
diff changeset
    94
    def sql_where_eq: String = "WHERE " + sql_name + " = "
607f7ad07a60 tuned signature;
wenzelm
parents: 65327
diff changeset
    95
    def sql_where_eq_string(s: String): String = sql_where_eq + quote_string(s)
607f7ad07a60 tuned signature;
wenzelm
parents: 65327
diff changeset
    96
65019
0ca91e5d52df tuned signature;
wenzelm
parents: 65018
diff changeset
    97
    override def toString: String = sql_decl(sql_type_default)
63779
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    98
  }
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    99
63780
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   100
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   101
  /* tables */
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   102
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   103
  sealed case class Table(name: String, columns: List[Column])
63780
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   104
  {
63790
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   105
    private val columns_index: Map[String, Int] =
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   106
      columns.iterator.map(_.name).zipWithIndex.toMap
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   107
63781
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   108
    Library.duplicates(columns.map(_.name)) match {
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   109
      case Nil =>
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   110
      case bad => error("Duplicate column names " + commas_quote(bad) + " for table " + quote(name))
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   111
    }
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   112
65325
981df08de0ab more general primary_key;
wenzelm
parents: 65323
diff changeset
   113
    def sql_columns(sql_type: Type.Value => String): String =
981df08de0ab more general primary_key;
wenzelm
parents: 65323
diff changeset
   114
    {
981df08de0ab more general primary_key;
wenzelm
parents: 65323
diff changeset
   115
      val primary_key =
981df08de0ab more general primary_key;
wenzelm
parents: 65323
diff changeset
   116
        columns.filter(_.primary_key).map(_.name) match {
981df08de0ab more general primary_key;
wenzelm
parents: 65323
diff changeset
   117
          case Nil => Nil
981df08de0ab more general primary_key;
wenzelm
parents: 65323
diff changeset
   118
          case keys => List("PRIMARY KEY " + enclosure(keys))
981df08de0ab more general primary_key;
wenzelm
parents: 65323
diff changeset
   119
        }
981df08de0ab more general primary_key;
wenzelm
parents: 65323
diff changeset
   120
      enclosure(columns.map(_.sql_decl(sql_type)) ::: primary_key)
63781
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   121
    }
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   122
65327
e886aed88b2c clarified signature: rowid is specific to SQLite;
wenzelm
parents: 65325
diff changeset
   123
    def sql_create(strict: Boolean, sql_type: Type.Value => String): String =
63784
wenzelm
parents: 63783
diff changeset
   124
      "CREATE TABLE " + (if (strict) "" else "IF NOT EXISTS ") +
65327
e886aed88b2c clarified signature: rowid is specific to SQLite;
wenzelm
parents: 65325
diff changeset
   125
        quote_ident(name) + " " + sql_columns(sql_type)
63780
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   126
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   127
    def sql_drop(strict: Boolean): String =
63784
wenzelm
parents: 63783
diff changeset
   128
      "DROP TABLE " + (if (strict) "" else "IF EXISTS ") + quote_ident(name)
63783
baa20f3b6cea tuned signature;
wenzelm
parents: 63781
diff changeset
   129
63791
c6cbdfaae19e more operations;
wenzelm
parents: 63790
diff changeset
   130
    def sql_create_index(
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   131
        index_name: String, index_columns: List[Column],
63791
c6cbdfaae19e more operations;
wenzelm
parents: 63790
diff changeset
   132
        strict: Boolean, unique: Boolean): String =
c6cbdfaae19e more operations;
wenzelm
parents: 63790
diff changeset
   133
      "CREATE " + (if (unique) "UNIQUE " else "") + "INDEX " +
c6cbdfaae19e more operations;
wenzelm
parents: 63790
diff changeset
   134
        (if (strict) "" else "IF NOT EXISTS ") + quote_ident(index_name) + " ON " +
c6cbdfaae19e more operations;
wenzelm
parents: 63790
diff changeset
   135
        quote_ident(name) + " " + enclosure(index_columns.map(_.name))
c6cbdfaae19e more operations;
wenzelm
parents: 63790
diff changeset
   136
c6cbdfaae19e more operations;
wenzelm
parents: 63790
diff changeset
   137
    def sql_drop_index(index_name: String, strict: Boolean): String =
c6cbdfaae19e more operations;
wenzelm
parents: 63790
diff changeset
   138
      "DROP INDEX " + (if (strict) "" else "IF EXISTS ") + quote_ident(index_name)
c6cbdfaae19e more operations;
wenzelm
parents: 63790
diff changeset
   139
63790
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   140
    def sql_insert: String =
63791
c6cbdfaae19e more operations;
wenzelm
parents: 63790
diff changeset
   141
      "INSERT INTO " + quote_ident(name) + " VALUES " + enclosure(columns.map(_ => "?"))
c6cbdfaae19e more operations;
wenzelm
parents: 63790
diff changeset
   142
65319
64da14387b2c more operations;
wenzelm
parents: 65292
diff changeset
   143
    def sql_delete: String =
64da14387b2c more operations;
wenzelm
parents: 65292
diff changeset
   144
      "DELETE FROM " + quote_ident(name)
64da14387b2c more operations;
wenzelm
parents: 65292
diff changeset
   145
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   146
    def sql_select(select_columns: List[Column], distinct: Boolean): String =
63791
c6cbdfaae19e more operations;
wenzelm
parents: 63790
diff changeset
   147
      "SELECT " + (if (distinct) "DISTINCT " else "") +
c6cbdfaae19e more operations;
wenzelm
parents: 63790
diff changeset
   148
      commas(select_columns.map(_.sql_name)) + " FROM " + quote_ident(name)
63790
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   149
63783
baa20f3b6cea tuned signature;
wenzelm
parents: 63781
diff changeset
   150
    override def toString: String =
65325
981df08de0ab more general primary_key;
wenzelm
parents: 65323
diff changeset
   151
      "TABLE " + quote_ident(name) + " " + sql_columns(sql_type_default)
63780
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   152
  }
63790
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   153
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   154
65012
wenzelm
parents: 65011
diff changeset
   155
wenzelm
parents: 65011
diff changeset
   156
  /** SQL database operations **/
wenzelm
parents: 65011
diff changeset
   157
63790
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   158
  /* results */
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   159
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   160
  def iterator[A](rs: ResultSet)(get: ResultSet => A): Iterator[A] = new Iterator[A]
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   161
  {
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   162
    private var _next: Boolean = rs.next()
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   163
    def hasNext: Boolean = _next
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   164
    def next: A = { val x = get(rs); _next = rs.next(); x }
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   165
  }
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   166
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   167
  trait Database
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   168
  {
65008
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
   169
    /* types */
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
   170
65019
0ca91e5d52df tuned signature;
wenzelm
parents: 65018
diff changeset
   171
    def sql_type(T: Type.Value): String
65008
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
   172
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
   173
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   174
    /* connection */
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   175
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   176
    def connection: Connection
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   177
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   178
    def close() { connection.close }
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   179
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   180
    def transaction[A](body: => A): A =
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   181
    {
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   182
      val auto_commit = connection.getAutoCommit
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   183
      try {
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   184
        connection.setAutoCommit(false)
65022
cda3d36aceb2 proper transaction for PostgreSQL;
wenzelm
parents: 65021
diff changeset
   185
        val savepoint = connection.setSavepoint
cda3d36aceb2 proper transaction for PostgreSQL;
wenzelm
parents: 65021
diff changeset
   186
        try {
cda3d36aceb2 proper transaction for PostgreSQL;
wenzelm
parents: 65021
diff changeset
   187
          val result = body
cda3d36aceb2 proper transaction for PostgreSQL;
wenzelm
parents: 65021
diff changeset
   188
          connection.commit
cda3d36aceb2 proper transaction for PostgreSQL;
wenzelm
parents: 65021
diff changeset
   189
          result
cda3d36aceb2 proper transaction for PostgreSQL;
wenzelm
parents: 65021
diff changeset
   190
        }
cda3d36aceb2 proper transaction for PostgreSQL;
wenzelm
parents: 65021
diff changeset
   191
        catch { case exn: Throwable => connection.rollback(savepoint); throw exn }
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   192
      }
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   193
      finally { connection.setAutoCommit(auto_commit) }
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   194
    }
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   195
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   196
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   197
    /* statements */
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   198
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   199
    def statement(sql: String): PreparedStatement = connection.prepareStatement(sql)
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   200
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   201
    def insert_statement(table: Table): PreparedStatement = statement(table.sql_insert)
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   202
65319
64da14387b2c more operations;
wenzelm
parents: 65292
diff changeset
   203
    def delete_statement(table: Table, sql: String = ""): PreparedStatement =
64da14387b2c more operations;
wenzelm
parents: 65292
diff changeset
   204
      statement(table.sql_delete + (if (sql == "") "" else " " + sql))
64da14387b2c more operations;
wenzelm
parents: 65292
diff changeset
   205
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   206
    def select_statement(table: Table, columns: List[Column],
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   207
        sql: String = "", distinct: Boolean = false): PreparedStatement =
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   208
      statement(table.sql_select(columns, distinct) + (if (sql == "") "" else " " + sql))
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   209
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   210
65020
a4fcaeadc825 more operations;
wenzelm
parents: 65019
diff changeset
   211
    /* input */
a4fcaeadc825 more operations;
wenzelm
parents: 65019
diff changeset
   212
a4fcaeadc825 more operations;
wenzelm
parents: 65019
diff changeset
   213
    def set_bool(stmt: PreparedStatement, i: Int, x: Boolean) { stmt.setBoolean(i, x) }
a4fcaeadc825 more operations;
wenzelm
parents: 65019
diff changeset
   214
    def set_int(stmt: PreparedStatement, i: Int, x: Int) { stmt.setInt(i, x) }
a4fcaeadc825 more operations;
wenzelm
parents: 65019
diff changeset
   215
    def set_long(stmt: PreparedStatement, i: Int, x: Long) { stmt.setLong(i, x) }
a4fcaeadc825 more operations;
wenzelm
parents: 65019
diff changeset
   216
    def set_double(stmt: PreparedStatement, i: Int, x: Double) { stmt.setDouble(i, x) }
a4fcaeadc825 more operations;
wenzelm
parents: 65019
diff changeset
   217
    def set_string(stmt: PreparedStatement, i: Int, x: String) { stmt.setString(i, x) }
a4fcaeadc825 more operations;
wenzelm
parents: 65019
diff changeset
   218
    def set_bytes(stmt: PreparedStatement, i: Int, bytes: Bytes)
a4fcaeadc825 more operations;
wenzelm
parents: 65019
diff changeset
   219
    { stmt.setBinaryStream(i, bytes.stream(), bytes.length) }
a4fcaeadc825 more operations;
wenzelm
parents: 65019
diff changeset
   220
    def set_date(stmt: PreparedStatement, i: Int, date: Date)
a4fcaeadc825 more operations;
wenzelm
parents: 65019
diff changeset
   221
65022
cda3d36aceb2 proper transaction for PostgreSQL;
wenzelm
parents: 65021
diff changeset
   222
65020
a4fcaeadc825 more operations;
wenzelm
parents: 65019
diff changeset
   223
    /* output */
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   224
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   225
    def bool(rs: ResultSet, name: String): Boolean = rs.getBoolean(name)
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   226
    def int(rs: ResultSet, name: String): Int = rs.getInt(name)
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   227
    def long(rs: ResultSet, name: String): Long = rs.getLong(name)
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   228
    def double(rs: ResultSet, name: String): Double = rs.getDouble(name)
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   229
    def string(rs: ResultSet, name: String): String =
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   230
    {
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   231
      val s = rs.getString(name)
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   232
      if (s == null) "" else s
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   233
    }
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   234
    def bytes(rs: ResultSet, name: String): Bytes =
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   235
    {
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   236
      val bs = rs.getBytes(name)
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   237
      if (bs == null) Bytes.empty else Bytes(bs)
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   238
    }
65021
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
   239
    def date(rs: ResultSet, name: String): Date
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   240
65323
7f6c738379f4 more operations;
wenzelm
parents: 65321
diff changeset
   241
    def bool(rs: ResultSet, column: Column): Boolean = bool(rs, column.name)
7f6c738379f4 more operations;
wenzelm
parents: 65321
diff changeset
   242
    def int(rs: ResultSet, column: Column): Int = int(rs, column.name)
7f6c738379f4 more operations;
wenzelm
parents: 65321
diff changeset
   243
    def long(rs: ResultSet, column: Column): Long = long(rs, column.name)
7f6c738379f4 more operations;
wenzelm
parents: 65321
diff changeset
   244
    def double(rs: ResultSet, column: Column): Double = double(rs, column.name)
7f6c738379f4 more operations;
wenzelm
parents: 65321
diff changeset
   245
    def string(rs: ResultSet, column: Column): String = string(rs, column.name)
7f6c738379f4 more operations;
wenzelm
parents: 65321
diff changeset
   246
    def bytes(rs: ResultSet, column: Column): Bytes = bytes(rs, column.name)
7f6c738379f4 more operations;
wenzelm
parents: 65321
diff changeset
   247
    def date(rs: ResultSet, column: Column): Date = date(rs, column.name)
7f6c738379f4 more operations;
wenzelm
parents: 65321
diff changeset
   248
7f6c738379f4 more operations;
wenzelm
parents: 65321
diff changeset
   249
    def get[A, B](rs: ResultSet, a: A, f: (ResultSet, A) => B): Option[B] =
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   250
    {
65323
7f6c738379f4 more operations;
wenzelm
parents: 65321
diff changeset
   251
      val x = f(rs, a)
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   252
      if (rs.wasNull) None else Some(x)
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   253
    }
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   254
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   255
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   256
    /* tables */
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   257
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   258
    def tables: List[String] =
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   259
      iterator(connection.getMetaData.getTables(null, null, "%", null))(_.getString(3)).toList
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   260
65327
e886aed88b2c clarified signature: rowid is specific to SQLite;
wenzelm
parents: 65325
diff changeset
   261
    def create_table(table: Table, strict: Boolean = false, sql: String = ""): Unit =
e886aed88b2c clarified signature: rowid is specific to SQLite;
wenzelm
parents: 65325
diff changeset
   262
      using(statement(table.sql_create(strict, sql_type) + (if (sql == "") "" else " " + sql)))(
e886aed88b2c clarified signature: rowid is specific to SQLite;
wenzelm
parents: 65325
diff changeset
   263
        _.execute())
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   264
65280
ef37f5236794 prefer non-strict default;
wenzelm
parents: 65022
diff changeset
   265
    def drop_table(table: Table, strict: Boolean = false): Unit =
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   266
      using(statement(table.sql_drop(strict)))(_.execute())
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   267
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   268
    def create_index(table: Table, name: String, columns: List[Column],
65280
ef37f5236794 prefer non-strict default;
wenzelm
parents: 65022
diff changeset
   269
        strict: Boolean = false, unique: Boolean = false): Unit =
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   270
      using(statement(table.sql_create_index(name, columns, strict, unique)))(_.execute())
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   271
65280
ef37f5236794 prefer non-strict default;
wenzelm
parents: 65022
diff changeset
   272
    def drop_index(table: Table, name: String, strict: Boolean = false): Unit =
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   273
      using(statement(table.sql_drop_index(name, strict)))(_.execute())
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   274
  }
63778
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
   275
}
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   276
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   277
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   278
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   279
/** SQLite **/
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   280
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   281
object SQLite
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   282
{
65021
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
   283
  // see https://www.sqlite.org/lang_datefunc.html
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
   284
  val date_format: Date.Format = Date.Format("uuuu-MM-dd HH:mm:ss.SSS x")
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
   285
65292
e3bd1e7ddd23 more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents: 65280
diff changeset
   286
  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
   287
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   288
  def open_database(path: Path): Database =
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   289
  {
65292
e3bd1e7ddd23 more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents: 65280
diff changeset
   290
    init_jdbc
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   291
    val path0 = path.expand
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   292
    val s0 = File.platform_path(path0)
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   293
    val s1 = if (Platform.is_windows) s0.replace('\\', '/') else s0
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   294
    val connection = DriverManager.getConnection("jdbc:sqlite:" + s1)
65007
b6a1a1d42f5d clarified toString;
wenzelm
parents: 65006
diff changeset
   295
    new Database(path0.toString, connection)
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   296
  }
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   297
65007
b6a1a1d42f5d clarified toString;
wenzelm
parents: 65006
diff changeset
   298
  class Database private[SQLite](name: String, val connection: Connection) extends SQL.Database
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   299
  {
65007
b6a1a1d42f5d clarified toString;
wenzelm
parents: 65006
diff changeset
   300
    override def toString: String = name
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   301
65019
0ca91e5d52df tuned signature;
wenzelm
parents: 65018
diff changeset
   302
    def sql_type(T: SQL.Type.Value): String = SQL.sql_type_sqlite(T)
65011
42bffd1637f0 support for type Boolean;
wenzelm
parents: 65010
diff changeset
   303
65021
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
   304
    def set_date(stmt: PreparedStatement, i: Int, date: Date): Unit =
65598
5deef985e38e allow null;
wenzelm
parents: 65594
diff changeset
   305
      if (date == null) set_string(stmt, i, null)
5deef985e38e allow null;
wenzelm
parents: 65594
diff changeset
   306
      else set_string(stmt, i, date_format(date))
5deef985e38e allow null;
wenzelm
parents: 65594
diff changeset
   307
65021
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
   308
    def date(rs: ResultSet, name: String): Date =
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
   309
      date_format.parse(string(rs, name))
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
   310
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   311
    def rebuild { using(statement("VACUUM"))(_.execute()) }
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   312
  }
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   313
}
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   314
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   315
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   316
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   317
/** PostgreSQL **/
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   318
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   319
object PostgreSQL
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   320
{
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   321
  val default_port = 5432
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   322
65292
e3bd1e7ddd23 more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents: 65280
diff changeset
   323
  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
   324
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   325
  def open_database(
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   326
    user: String,
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   327
    password: String,
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   328
    database: String = "",
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   329
    host: String = "",
65594
659305708959 clarified treatment of default port;
wenzelm
parents: 65593
diff changeset
   330
    port: Int = 0,
65009
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   331
    ssh: Option[SSH.Session] = None): Database =
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   332
  {
65292
e3bd1e7ddd23 more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents: 65280
diff changeset
   333
    init_jdbc
e3bd1e7ddd23 more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents: 65280
diff changeset
   334
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   335
    require(user != "")
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
    val db_host = if (host != "") host else "localhost"
65594
659305708959 clarified treatment of default port;
wenzelm
parents: 65593
diff changeset
   338
    val db_port = if (port > 0 && port != default_port) ":" + port else ""
65009
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   339
    val db_name = "/" + (if (database != "") database else user)
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   340
65010
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   341
    val (url, name, port_forwarding) =
65009
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   342
      ssh match {
65010
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   343
        case None =>
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   344
          val spec = db_host + db_port + db_name
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   345
          val url = "jdbc:postgresql://" + spec
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   346
          val name = user + "@" + spec
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   347
          (url, name, None)
65009
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   348
        case Some(ssh) =>
65594
659305708959 clarified treatment of default port;
wenzelm
parents: 65593
diff changeset
   349
          val fw =
659305708959 clarified treatment of default port;
wenzelm
parents: 65593
diff changeset
   350
            ssh.port_forwarding(remote_host = db_host,
659305708959 clarified treatment of default port;
wenzelm
parents: 65593
diff changeset
   351
              remote_port = if (port > 0) port else default_port)
65010
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   352
          val url = "jdbc:postgresql://localhost:" + fw.local_port + db_name
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   353
          val name = user + "@" + fw + db_name + " via ssh " + ssh
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   354
          (url, name, Some(fw))
65009
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   355
      }
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   356
    try {
65010
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   357
      val connection = DriverManager.getConnection(url, user, password)
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   358
      new Database(name, connection, port_forwarding)
65009
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   359
    }
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   360
    catch { case exn: Throwable => port_forwarding.foreach(_.close); throw exn }
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   361
  }
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   362
65009
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   363
  class Database private[PostgreSQL](
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   364
      name: String, val connection: Connection, port_forwarding: Option[SSH.Port_Forwarding])
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   365
    extends SQL.Database
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   366
  {
65010
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   367
    override def toString: String = name
65008
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
   368
65019
0ca91e5d52df tuned signature;
wenzelm
parents: 65018
diff changeset
   369
    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
   370
65021
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
   371
    // see https://jdbc.postgresql.org/documentation/head/8-date-time.html
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
   372
    def set_date(stmt: PreparedStatement, i: Int, date: Date): Unit =
65598
5deef985e38e allow null;
wenzelm
parents: 65594
diff changeset
   373
      if (date == null) stmt.setObject(i, null)
5deef985e38e allow null;
wenzelm
parents: 65594
diff changeset
   374
      else stmt.setObject(i, OffsetDateTime.from(date.to_utc.rep))
5deef985e38e allow null;
wenzelm
parents: 65594
diff changeset
   375
65021
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
   376
    def date(rs: ResultSet, name: String): Date =
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
   377
      Date.instant(rs.getObject(name, classOf[OffsetDateTime]).toInstant)
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
   378
65009
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   379
    override def close() { super.close; port_forwarding.foreach(_.close) }
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   380
  }
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   381
}