src/Pure/General/sql.scala
author wenzelm
Wed, 03 May 2017 17:00:50 +0200
changeset 65702 7c6a91deb212
parent 65701 d788c11176e5
child 65703 cead65c19f2e
permissions -rw-r--r--
tuned signature;
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
65648
69dfec14b0df tuned signature;
wenzelm
parents: 65644
diff changeset
    33
  def 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
65695
4edac706bc5e tuned signature;
wenzelm
parents: 65691
diff changeset
    36
  def ident(s: String): String =
65677
7d25b8dbdbfa proper quote (amending 546020a98a91): e.g. relevant for "ISABELLE_BUILD_OPTIONS";
wenzelm
parents: 65673
diff changeset
    37
    Long_Name.implode(Long_Name.explode(s).map(a => quote(a.replace("\"", "\"\""))))
63779
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    38
65668
366bc4e6a238 more operations;
wenzelm
parents: 65649
diff changeset
    39
  def enclose(s: String): String = "(" + s + ")"
63791
c6cbdfaae19e more operations;
wenzelm
parents: 63790
diff changeset
    40
  def enclosure(ss: Iterable[String]): String = ss.mkString("(", ", ", ")")
c6cbdfaae19e more operations;
wenzelm
parents: 63790
diff changeset
    41
65644
7ef438495a02 support for qualified names, which are not quoted (e.g. for SQLite);
wenzelm
parents: 65641
diff changeset
    42
  def select(columns: List[Column], distinct: Boolean = false): String =
65695
4edac706bc5e tuned signature;
wenzelm
parents: 65691
diff changeset
    43
    "SELECT " + (if (distinct) "DISTINCT " else "") + commas(columns.map(_.ident)) + " FROM "
65644
7ef438495a02 support for qualified names, which are not quoted (e.g. for SQLite);
wenzelm
parents: 65641
diff changeset
    44
65668
366bc4e6a238 more operations;
wenzelm
parents: 65649
diff changeset
    45
  def join(table1: Table, table2: Table, sql: String = "", outer: Boolean = false): String =
65695
4edac706bc5e tuned signature;
wenzelm
parents: 65691
diff changeset
    46
    table1.ident + (if (outer) " LEFT OUTER JOIN " else " INNER JOIN ") + table2.ident +
65668
366bc4e6a238 more operations;
wenzelm
parents: 65649
diff changeset
    47
      (if (sql == "") "" else " ON " + sql)
366bc4e6a238 more operations;
wenzelm
parents: 65649
diff changeset
    48
366bc4e6a238 more operations;
wenzelm
parents: 65649
diff changeset
    49
  def join_outer(table1: Table, table2: Table, sql: String = ""): String =
366bc4e6a238 more operations;
wenzelm
parents: 65649
diff changeset
    50
    join(table1, table2, sql, outer = true)
366bc4e6a238 more operations;
wenzelm
parents: 65649
diff changeset
    51
63779
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    52
65008
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
    53
  /* types */
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
    54
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
    55
  object Type extends Enumeration
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
    56
  {
65011
42bffd1637f0 support for type Boolean;
wenzelm
parents: 65010
diff changeset
    57
    val Boolean = Value("BOOLEAN")
65008
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
    58
    val Int = Value("INTEGER")
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
    59
    val Long = Value("BIGINT")
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
    60
    val Double = Value("DOUBLE PRECISION")
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
    61
    val String = Value("TEXT")
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
    62
    val Bytes = Value("BLOB")
65014
97a622d01609 support for type Date;
wenzelm
parents: 65013
diff changeset
    63
    val Date = Value("TIMESTAMP WITH TIME ZONE")
65008
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
    64
  }
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
    65
65019
0ca91e5d52df tuned signature;
wenzelm
parents: 65018
diff changeset
    66
  def sql_type_default(T: Type.Value): String = T.toString
65013
wenzelm
parents: 65012
diff changeset
    67
65019
0ca91e5d52df tuned signature;
wenzelm
parents: 65018
diff changeset
    68
  def sql_type_sqlite(T: Type.Value): String =
0ca91e5d52df tuned signature;
wenzelm
parents: 65018
diff changeset
    69
    if (T == Type.Boolean) "INTEGER"
0ca91e5d52df tuned signature;
wenzelm
parents: 65018
diff changeset
    70
    else if (T == Type.Date) "TEXT"
0ca91e5d52df tuned signature;
wenzelm
parents: 65018
diff changeset
    71
    else sql_type_default(T)
65013
wenzelm
parents: 65012
diff changeset
    72
65019
0ca91e5d52df tuned signature;
wenzelm
parents: 65018
diff changeset
    73
  def sql_type_postgresql(T: Type.Value): String =
0ca91e5d52df tuned signature;
wenzelm
parents: 65018
diff changeset
    74
    if (T == Type.Bytes) "BYTEA"
0ca91e5d52df tuned signature;
wenzelm
parents: 65018
diff changeset
    75
    else sql_type_default(T)
65008
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
    76
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
    77
63779
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    78
  /* columns */
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    79
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    80
  object Column
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    81
  {
65280
ef37f5236794 prefer non-strict default;
wenzelm
parents: 65022
diff changeset
    82
    def bool(name: String, strict: Boolean = false, primary_key: Boolean = false): Column =
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
    83
      Column(name, Type.Boolean, strict, primary_key)
65280
ef37f5236794 prefer non-strict default;
wenzelm
parents: 65022
diff changeset
    84
    def int(name: String, strict: Boolean = false, primary_key: Boolean = false): Column =
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
    85
      Column(name, Type.Int, strict, primary_key)
65280
ef37f5236794 prefer non-strict default;
wenzelm
parents: 65022
diff changeset
    86
    def long(name: String, strict: Boolean = false, primary_key: Boolean = false): Column =
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
    87
      Column(name, Type.Long, strict, primary_key)
65280
ef37f5236794 prefer non-strict default;
wenzelm
parents: 65022
diff changeset
    88
    def double(name: String, strict: Boolean = false, primary_key: Boolean = false): Column =
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
    89
      Column(name, Type.Double, strict, primary_key)
65280
ef37f5236794 prefer non-strict default;
wenzelm
parents: 65022
diff changeset
    90
    def string(name: String, strict: Boolean = false, primary_key: Boolean = false): Column =
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
    91
      Column(name, Type.String, strict, primary_key)
65280
ef37f5236794 prefer non-strict default;
wenzelm
parents: 65022
diff changeset
    92
    def bytes(name: String, strict: Boolean = false, primary_key: Boolean = false): Column =
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
    93
      Column(name, Type.Bytes, strict, primary_key)
65280
ef37f5236794 prefer non-strict default;
wenzelm
parents: 65022
diff changeset
    94
    def date(name: String, strict: Boolean = false, primary_key: Boolean = false): Column =
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
    95
      Column(name, Type.Date, strict, primary_key)
63779
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    96
  }
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    97
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
    98
  sealed case class Column(
65280
ef37f5236794 prefer non-strict default;
wenzelm
parents: 65022
diff changeset
    99
    name: String, T: Type.Value, strict: Boolean = false, primary_key: Boolean = false)
63779
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
   100
  {
65691
2229276a1f99 eliminated redundant type SQL.View;
wenzelm
parents: 65690
diff changeset
   101
    def apply(table: Table): Column =
2229276a1f99 eliminated redundant type SQL.View;
wenzelm
parents: 65690
diff changeset
   102
      Column(Long_Name.qualify(table.name, name), T, strict = strict, primary_key = primary_key)
65644
7ef438495a02 support for qualified names, which are not quoted (e.g. for SQLite);
wenzelm
parents: 65641
diff changeset
   103
65695
4edac706bc5e tuned signature;
wenzelm
parents: 65691
diff changeset
   104
    def ident: String = SQL.ident(name)
4edac706bc5e tuned signature;
wenzelm
parents: 65691
diff changeset
   105
65701
d788c11176e5 simplified: standard toString is SQL.ident;
wenzelm
parents: 65699
diff changeset
   106
    def decl(sql_type: Type.Value => String): String =
65695
4edac706bc5e tuned signature;
wenzelm
parents: 65691
diff changeset
   107
      ident + " " + sql_type(T) + (if (strict || primary_key) " NOT NULL" else "")
63781
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   108
65699
9f74d9aa0bdf tuned signature;
wenzelm
parents: 65698
diff changeset
   109
    def where_equal(s: String): String = "WHERE " + ident + " = " + string(s)
65593
607f7ad07a60 tuned signature;
wenzelm
parents: 65327
diff changeset
   110
65701
d788c11176e5 simplified: standard toString is SQL.ident;
wenzelm
parents: 65699
diff changeset
   111
    override def toString: String = ident
63779
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
   112
  }
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
   113
63780
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   114
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   115
  /* tables */
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   116
65696
3f53a05c1266 tuned signature;
wenzelm
parents: 65695
diff changeset
   117
  sealed case class Table(name: String, columns: List[Column], body: String = "")
63780
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   118
  {
63790
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   119
    private val columns_index: Map[String, Int] =
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   120
      columns.iterator.map(_.name).zipWithIndex.toMap
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   121
63781
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   122
    Library.duplicates(columns.map(_.name)) match {
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   123
      case Nil =>
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   124
      case bad => error("Duplicate column names " + commas_quote(bad) + " for table " + quote(name))
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   125
    }
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   126
65696
3f53a05c1266 tuned signature;
wenzelm
parents: 65695
diff changeset
   127
    def ident: String = SQL.ident(name)
65691
2229276a1f99 eliminated redundant type SQL.View;
wenzelm
parents: 65690
diff changeset
   128
65702
7c6a91deb212 tuned signature;
wenzelm
parents: 65701
diff changeset
   129
    def query: String =
65696
3f53a05c1266 tuned signature;
wenzelm
parents: 65695
diff changeset
   130
      if (body == "") error("Missing SQL body for table " + quote(name))
3f53a05c1266 tuned signature;
wenzelm
parents: 65695
diff changeset
   131
      else SQL.enclose(body)
65649
0818da4f67bb tuned signature;
wenzelm
parents: 65648
diff changeset
   132
65702
7c6a91deb212 tuned signature;
wenzelm
parents: 65701
diff changeset
   133
    def query_alias(alias: String = name): String =
7c6a91deb212 tuned signature;
wenzelm
parents: 65701
diff changeset
   134
      query + " AS " + SQL.ident(alias)
7c6a91deb212 tuned signature;
wenzelm
parents: 65701
diff changeset
   135
65701
d788c11176e5 simplified: standard toString is SQL.ident;
wenzelm
parents: 65699
diff changeset
   136
    def create(strict: Boolean = false, sql_type: Type.Value => String): String =
65325
981df08de0ab more general primary_key;
wenzelm
parents: 65323
diff changeset
   137
    {
981df08de0ab more general primary_key;
wenzelm
parents: 65323
diff changeset
   138
      val primary_key =
981df08de0ab more general primary_key;
wenzelm
parents: 65323
diff changeset
   139
        columns.filter(_.primary_key).map(_.name) match {
981df08de0ab more general primary_key;
wenzelm
parents: 65323
diff changeset
   140
          case Nil => Nil
981df08de0ab more general primary_key;
wenzelm
parents: 65323
diff changeset
   141
          case keys => List("PRIMARY KEY " + enclosure(keys))
981df08de0ab more general primary_key;
wenzelm
parents: 65323
diff changeset
   142
        }
65701
d788c11176e5 simplified: standard toString is SQL.ident;
wenzelm
parents: 65699
diff changeset
   143
      "CREATE TABLE " + (if (strict) "" else "IF NOT EXISTS ") +
d788c11176e5 simplified: standard toString is SQL.ident;
wenzelm
parents: 65699
diff changeset
   144
        ident + " " + enclosure(columns.map(_.decl(sql_type)) ::: primary_key)
63781
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   145
    }
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   146
65698
38139b2067cf clarified signature;
wenzelm
parents: 65697
diff changeset
   147
    def create_index(index_name: String, index_columns: List[Column],
38139b2067cf clarified signature;
wenzelm
parents: 65697
diff changeset
   148
        strict: Boolean = false, unique: Boolean = false): String =
63791
c6cbdfaae19e more operations;
wenzelm
parents: 63790
diff changeset
   149
      "CREATE " + (if (unique) "UNIQUE " else "") + "INDEX " +
65695
4edac706bc5e tuned signature;
wenzelm
parents: 65691
diff changeset
   150
        (if (strict) "" else "IF NOT EXISTS ") + SQL.ident(index_name) + " ON " +
4edac706bc5e tuned signature;
wenzelm
parents: 65691
diff changeset
   151
        ident + " " + enclosure(index_columns.map(_.name))
63791
c6cbdfaae19e more operations;
wenzelm
parents: 63790
diff changeset
   152
65698
38139b2067cf clarified signature;
wenzelm
parents: 65697
diff changeset
   153
    def insert(sql: String = ""): String =
38139b2067cf clarified signature;
wenzelm
parents: 65697
diff changeset
   154
      "INSERT INTO " + ident + " VALUES " + enclosure(columns.map(_ => "?")) +
38139b2067cf clarified signature;
wenzelm
parents: 65697
diff changeset
   155
        (if (sql == "") "" else " " + sql)
63791
c6cbdfaae19e more operations;
wenzelm
parents: 63790
diff changeset
   156
65698
38139b2067cf clarified signature;
wenzelm
parents: 65697
diff changeset
   157
    def delete(sql: String = ""): String =
38139b2067cf clarified signature;
wenzelm
parents: 65697
diff changeset
   158
      "DELETE FROM " + ident +
38139b2067cf clarified signature;
wenzelm
parents: 65697
diff changeset
   159
        (if (sql == "") "" else " " + sql)
65319
64da14387b2c more operations;
wenzelm
parents: 65292
diff changeset
   160
65698
38139b2067cf clarified signature;
wenzelm
parents: 65697
diff changeset
   161
    def select(select_columns: List[Column], sql: String = "", distinct: Boolean = false): String =
38139b2067cf clarified signature;
wenzelm
parents: 65697
diff changeset
   162
      SQL.select(select_columns, distinct = distinct) + ident +
38139b2067cf clarified signature;
wenzelm
parents: 65697
diff changeset
   163
        (if (sql == "") "" else " " + sql)
63790
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   164
65701
d788c11176e5 simplified: standard toString is SQL.ident;
wenzelm
parents: 65699
diff changeset
   165
    override def toString: String = ident
63780
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   166
  }
63790
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   167
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   168
65012
wenzelm
parents: 65011
diff changeset
   169
wenzelm
parents: 65011
diff changeset
   170
  /** SQL database operations **/
wenzelm
parents: 65011
diff changeset
   171
63790
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   172
  /* results */
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   173
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   174
  def iterator[A](rs: ResultSet)(get: ResultSet => A): Iterator[A] = new Iterator[A]
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   175
  {
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   176
    private var _next: Boolean = rs.next()
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   177
    def hasNext: Boolean = _next
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   178
    def next: A = { val x = get(rs); _next = rs.next(); x }
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   179
  }
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   180
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   181
  trait Database
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   182
  {
65008
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
   183
    /* types */
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
   184
65019
0ca91e5d52df tuned signature;
wenzelm
parents: 65018
diff changeset
   185
    def sql_type(T: Type.Value): String
65008
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
   186
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
   187
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   188
    /* connection */
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   189
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   190
    def connection: Connection
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   191
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   192
    def close() { connection.close }
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   193
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   194
    def transaction[A](body: => A): A =
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   195
    {
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   196
      val auto_commit = connection.getAutoCommit
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   197
      try {
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   198
        connection.setAutoCommit(false)
65022
cda3d36aceb2 proper transaction for PostgreSQL;
wenzelm
parents: 65021
diff changeset
   199
        val savepoint = connection.setSavepoint
cda3d36aceb2 proper transaction for PostgreSQL;
wenzelm
parents: 65021
diff changeset
   200
        try {
cda3d36aceb2 proper transaction for PostgreSQL;
wenzelm
parents: 65021
diff changeset
   201
          val result = body
cda3d36aceb2 proper transaction for PostgreSQL;
wenzelm
parents: 65021
diff changeset
   202
          connection.commit
cda3d36aceb2 proper transaction for PostgreSQL;
wenzelm
parents: 65021
diff changeset
   203
          result
cda3d36aceb2 proper transaction for PostgreSQL;
wenzelm
parents: 65021
diff changeset
   204
        }
cda3d36aceb2 proper transaction for PostgreSQL;
wenzelm
parents: 65021
diff changeset
   205
        catch { case exn: Throwable => connection.rollback(savepoint); throw exn }
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   206
      }
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   207
      finally { connection.setAutoCommit(auto_commit) }
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   208
    }
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   209
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   210
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   211
    /* statements */
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   212
65698
38139b2067cf clarified signature;
wenzelm
parents: 65697
diff changeset
   213
    def statement(sql: String): PreparedStatement =
38139b2067cf clarified signature;
wenzelm
parents: 65697
diff changeset
   214
      connection.prepareStatement(sql)
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   215
65698
38139b2067cf clarified signature;
wenzelm
parents: 65697
diff changeset
   216
    def using_statement[A](sql: String)(f: PreparedStatement => A): A =
38139b2067cf clarified signature;
wenzelm
parents: 65697
diff changeset
   217
      using(statement(sql))(f)
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   218
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   219
65020
a4fcaeadc825 more operations;
wenzelm
parents: 65019
diff changeset
   220
    /* input */
a4fcaeadc825 more operations;
wenzelm
parents: 65019
diff changeset
   221
a4fcaeadc825 more operations;
wenzelm
parents: 65019
diff changeset
   222
    def set_bool(stmt: PreparedStatement, i: Int, x: Boolean) { stmt.setBoolean(i, x) }
65612
f70e918105da allow null for primitive types;
wenzelm
parents: 65610
diff changeset
   223
    def set_bool(stmt: PreparedStatement, i: Int, x: Option[Boolean])
f70e918105da allow null for primitive types;
wenzelm
parents: 65610
diff changeset
   224
    {
f70e918105da allow null for primitive types;
wenzelm
parents: 65610
diff changeset
   225
      if (x.isDefined) set_bool(stmt, i, x.get)
f70e918105da allow null for primitive types;
wenzelm
parents: 65610
diff changeset
   226
      else stmt.setNull(i, java.sql.Types.BOOLEAN)
f70e918105da allow null for primitive types;
wenzelm
parents: 65610
diff changeset
   227
    }
65615
67974c59ba93 tuned signature: avoid null in regular Scala code;
wenzelm
parents: 65612
diff changeset
   228
65020
a4fcaeadc825 more operations;
wenzelm
parents: 65019
diff changeset
   229
    def set_int(stmt: PreparedStatement, i: Int, x: Int) { stmt.setInt(i, x) }
65612
f70e918105da allow null for primitive types;
wenzelm
parents: 65610
diff changeset
   230
    def set_int(stmt: PreparedStatement, i: Int, x: Option[Int])
f70e918105da allow null for primitive types;
wenzelm
parents: 65610
diff changeset
   231
    {
f70e918105da allow null for primitive types;
wenzelm
parents: 65610
diff changeset
   232
      if (x.isDefined) set_int(stmt, i, x.get)
f70e918105da allow null for primitive types;
wenzelm
parents: 65610
diff changeset
   233
      else stmt.setNull(i, java.sql.Types.INTEGER)
f70e918105da allow null for primitive types;
wenzelm
parents: 65610
diff changeset
   234
    }
65615
67974c59ba93 tuned signature: avoid null in regular Scala code;
wenzelm
parents: 65612
diff changeset
   235
65020
a4fcaeadc825 more operations;
wenzelm
parents: 65019
diff changeset
   236
    def set_long(stmt: PreparedStatement, i: Int, x: Long) { stmt.setLong(i, x) }
65612
f70e918105da allow null for primitive types;
wenzelm
parents: 65610
diff changeset
   237
    def set_long(stmt: PreparedStatement, i: Int, x: Option[Long])
f70e918105da allow null for primitive types;
wenzelm
parents: 65610
diff changeset
   238
    {
f70e918105da allow null for primitive types;
wenzelm
parents: 65610
diff changeset
   239
      if (x.isDefined) set_long(stmt, i, x.get)
f70e918105da allow null for primitive types;
wenzelm
parents: 65610
diff changeset
   240
      else stmt.setNull(i, java.sql.Types.BIGINT)
f70e918105da allow null for primitive types;
wenzelm
parents: 65610
diff changeset
   241
    }
65615
67974c59ba93 tuned signature: avoid null in regular Scala code;
wenzelm
parents: 65612
diff changeset
   242
65020
a4fcaeadc825 more operations;
wenzelm
parents: 65019
diff changeset
   243
    def set_double(stmt: PreparedStatement, i: Int, x: Double) { stmt.setDouble(i, x) }
65612
f70e918105da allow null for primitive types;
wenzelm
parents: 65610
diff changeset
   244
    def set_double(stmt: PreparedStatement, i: Int, x: Option[Double])
f70e918105da allow null for primitive types;
wenzelm
parents: 65610
diff changeset
   245
    {
f70e918105da allow null for primitive types;
wenzelm
parents: 65610
diff changeset
   246
      if (x.isDefined) set_double(stmt, i, x.get)
f70e918105da allow null for primitive types;
wenzelm
parents: 65610
diff changeset
   247
      else stmt.setNull(i, java.sql.Types.DOUBLE)
f70e918105da allow null for primitive types;
wenzelm
parents: 65610
diff changeset
   248
    }
65615
67974c59ba93 tuned signature: avoid null in regular Scala code;
wenzelm
parents: 65612
diff changeset
   249
65020
a4fcaeadc825 more operations;
wenzelm
parents: 65019
diff changeset
   250
    def set_string(stmt: PreparedStatement, i: Int, x: String) { stmt.setString(i, x) }
65615
67974c59ba93 tuned signature: avoid null in regular Scala code;
wenzelm
parents: 65612
diff changeset
   251
    def set_string(stmt: PreparedStatement, i: Int, x: Option[String]): Unit =
67974c59ba93 tuned signature: avoid null in regular Scala code;
wenzelm
parents: 65612
diff changeset
   252
      set_string(stmt, i, x.orNull)
67974c59ba93 tuned signature: avoid null in regular Scala code;
wenzelm
parents: 65612
diff changeset
   253
65020
a4fcaeadc825 more operations;
wenzelm
parents: 65019
diff changeset
   254
    def set_bytes(stmt: PreparedStatement, i: Int, bytes: Bytes)
65610
e6e3fed86519 allow null;
wenzelm
parents: 65602
diff changeset
   255
    {
e6e3fed86519 allow null;
wenzelm
parents: 65602
diff changeset
   256
      if (bytes == null) stmt.setBytes(i, null)
e6e3fed86519 allow null;
wenzelm
parents: 65602
diff changeset
   257
      else stmt.setBinaryStream(i, bytes.stream(), bytes.length)
e6e3fed86519 allow null;
wenzelm
parents: 65602
diff changeset
   258
    }
65615
67974c59ba93 tuned signature: avoid null in regular Scala code;
wenzelm
parents: 65612
diff changeset
   259
    def set_bytes(stmt: PreparedStatement, i: Int, bytes: Option[Bytes]): Unit =
67974c59ba93 tuned signature: avoid null in regular Scala code;
wenzelm
parents: 65612
diff changeset
   260
      set_bytes(stmt, i, bytes.orNull)
67974c59ba93 tuned signature: avoid null in regular Scala code;
wenzelm
parents: 65612
diff changeset
   261
67974c59ba93 tuned signature: avoid null in regular Scala code;
wenzelm
parents: 65612
diff changeset
   262
    def set_date(stmt: PreparedStatement, i: Int, date: Date): Unit
67974c59ba93 tuned signature: avoid null in regular Scala code;
wenzelm
parents: 65612
diff changeset
   263
    def set_date(stmt: PreparedStatement, i: Int, date: Option[Date]): Unit =
67974c59ba93 tuned signature: avoid null in regular Scala code;
wenzelm
parents: 65612
diff changeset
   264
      set_date(stmt, i, date.orNull)
65020
a4fcaeadc825 more operations;
wenzelm
parents: 65019
diff changeset
   265
65022
cda3d36aceb2 proper transaction for PostgreSQL;
wenzelm
parents: 65021
diff changeset
   266
65020
a4fcaeadc825 more operations;
wenzelm
parents: 65019
diff changeset
   267
    /* output */
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   268
65620
9b99d61be5af tuned signature: less overloading;
wenzelm
parents: 65619
diff changeset
   269
    def bool(rs: ResultSet, column: Column): Boolean = rs.getBoolean(column.name)
9b99d61be5af tuned signature: less overloading;
wenzelm
parents: 65619
diff changeset
   270
    def int(rs: ResultSet, column: Column): Int = rs.getInt(column.name)
9b99d61be5af tuned signature: less overloading;
wenzelm
parents: 65619
diff changeset
   271
    def long(rs: ResultSet, column: Column): Long = rs.getLong(column.name)
9b99d61be5af tuned signature: less overloading;
wenzelm
parents: 65619
diff changeset
   272
    def double(rs: ResultSet, column: Column): Double = rs.getDouble(column.name)
9b99d61be5af tuned signature: less overloading;
wenzelm
parents: 65619
diff changeset
   273
    def string(rs: ResultSet, column: Column): String =
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   274
    {
65620
9b99d61be5af tuned signature: less overloading;
wenzelm
parents: 65619
diff changeset
   275
      val s = rs.getString(column.name)
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   276
      if (s == null) "" else s
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   277
    }
65620
9b99d61be5af tuned signature: less overloading;
wenzelm
parents: 65619
diff changeset
   278
    def bytes(rs: ResultSet, column: Column): Bytes =
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   279
    {
65620
9b99d61be5af tuned signature: less overloading;
wenzelm
parents: 65619
diff changeset
   280
      val bs = rs.getBytes(column.name)
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   281
      if (bs == null) Bytes.empty else Bytes(bs)
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   282
    }
65620
9b99d61be5af tuned signature: less overloading;
wenzelm
parents: 65619
diff changeset
   283
    def date(rs: ResultSet, column: Column): Date
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   284
65620
9b99d61be5af tuned signature: less overloading;
wenzelm
parents: 65619
diff changeset
   285
    def get[A](rs: ResultSet, column: Column, f: (ResultSet, Column) => A): Option[A] =
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   286
    {
65620
9b99d61be5af tuned signature: less overloading;
wenzelm
parents: 65619
diff changeset
   287
      val x = f(rs, column)
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   288
      if (rs.wasNull) None else Some(x)
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   289
    }
65697
60f4fb867d70 tuned signature;
wenzelm
parents: 65696
diff changeset
   290
    def get_bool(rs: ResultSet, column: Column): Option[Boolean] = get(rs, column, bool _)
60f4fb867d70 tuned signature;
wenzelm
parents: 65696
diff changeset
   291
    def get_int(rs: ResultSet, column: Column): Option[Int] = get(rs, column, int _)
60f4fb867d70 tuned signature;
wenzelm
parents: 65696
diff changeset
   292
    def get_long(rs: ResultSet, column: Column): Option[Long] = get(rs, column, long _)
60f4fb867d70 tuned signature;
wenzelm
parents: 65696
diff changeset
   293
    def get_double(rs: ResultSet, column: Column): Option[Double] = get(rs, column, double _)
60f4fb867d70 tuned signature;
wenzelm
parents: 65696
diff changeset
   294
    def get_string(rs: ResultSet, column: Column): Option[String] = get(rs, column, string _)
60f4fb867d70 tuned signature;
wenzelm
parents: 65696
diff changeset
   295
    def get_bytes(rs: ResultSet, column: Column): Option[Bytes] = get(rs, column, bytes _)
60f4fb867d70 tuned signature;
wenzelm
parents: 65696
diff changeset
   296
    def get_date(rs: ResultSet, column: Column): Option[Date] = get(rs, column, date _)
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   297
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   298
65669
d2f19b4a16ae support for views;
wenzelm
parents: 65668
diff changeset
   299
    /* tables and views */
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   300
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   301
    def tables: List[String] =
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   302
      iterator(connection.getMetaData.getTables(null, null, "%", null))(_.getString(3)).toList
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   303
65327
e886aed88b2c clarified signature: rowid is specific to SQLite;
wenzelm
parents: 65325
diff changeset
   304
    def create_table(table: Table, strict: Boolean = false, sql: String = ""): Unit =
65698
38139b2067cf clarified signature;
wenzelm
parents: 65697
diff changeset
   305
      using_statement(
38139b2067cf clarified signature;
wenzelm
parents: 65697
diff changeset
   306
        table.create(strict, sql_type) + (if (sql == "") "" else " " + sql))(_.execute())
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   307
65018
8b36f225bbee clarified signature;
wenzelm
parents: 65014
diff changeset
   308
    def create_index(table: Table, name: String, columns: List[Column],
65280
ef37f5236794 prefer non-strict default;
wenzelm
parents: 65022
diff changeset
   309
        strict: Boolean = false, unique: Boolean = false): Unit =
65698
38139b2067cf clarified signature;
wenzelm
parents: 65697
diff changeset
   310
      using_statement(table.create_index(name, columns, strict, unique))(_.execute())
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   311
65691
2229276a1f99 eliminated redundant type SQL.View;
wenzelm
parents: 65690
diff changeset
   312
    def create_view(table: Table, strict: Boolean = false): Unit =
2229276a1f99 eliminated redundant type SQL.View;
wenzelm
parents: 65690
diff changeset
   313
    {
2229276a1f99 eliminated redundant type SQL.View;
wenzelm
parents: 65690
diff changeset
   314
      if (strict || !tables.contains(table.name)) {
65702
7c6a91deb212 tuned signature;
wenzelm
parents: 65701
diff changeset
   315
        val sql = "CREATE VIEW " + table.ident + " AS " + table.query
65698
38139b2067cf clarified signature;
wenzelm
parents: 65697
diff changeset
   316
        using_statement(sql)(_.execute())
65690
wenzelm
parents: 65677
diff changeset
   317
      }
65691
2229276a1f99 eliminated redundant type SQL.View;
wenzelm
parents: 65690
diff changeset
   318
    }
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   319
  }
63778
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
   320
}
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   321
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   322
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   323
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   324
/** SQLite **/
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   325
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   326
object SQLite
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   327
{
65021
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
   328
  // see https://www.sqlite.org/lang_datefunc.html
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
   329
  val date_format: Date.Format = Date.Format("uuuu-MM-dd HH:mm:ss.SSS x")
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
   330
65292
e3bd1e7ddd23 more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents: 65280
diff changeset
   331
  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
   332
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   333
  def open_database(path: Path): Database =
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   334
  {
65292
e3bd1e7ddd23 more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents: 65280
diff changeset
   335
    init_jdbc
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   336
    val path0 = path.expand
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   337
    val s0 = File.platform_path(path0)
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   338
    val s1 = if (Platform.is_windows) s0.replace('\\', '/') else s0
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   339
    val connection = DriverManager.getConnection("jdbc:sqlite:" + s1)
65007
b6a1a1d42f5d clarified toString;
wenzelm
parents: 65006
diff changeset
   340
    new Database(path0.toString, connection)
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   341
  }
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   342
65007
b6a1a1d42f5d clarified toString;
wenzelm
parents: 65006
diff changeset
   343
  class Database private[SQLite](name: String, val connection: Connection) extends SQL.Database
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   344
  {
65007
b6a1a1d42f5d clarified toString;
wenzelm
parents: 65006
diff changeset
   345
    override def toString: String = name
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   346
65019
0ca91e5d52df tuned signature;
wenzelm
parents: 65018
diff changeset
   347
    def sql_type(T: SQL.Type.Value): String = SQL.sql_type_sqlite(T)
65011
42bffd1637f0 support for type Boolean;
wenzelm
parents: 65010
diff changeset
   348
65021
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
   349
    def set_date(stmt: PreparedStatement, i: Int, date: Date): Unit =
65615
67974c59ba93 tuned signature: avoid null in regular Scala code;
wenzelm
parents: 65612
diff changeset
   350
      if (date == null) set_string(stmt, i, null: String)
65598
5deef985e38e allow null;
wenzelm
parents: 65594
diff changeset
   351
      else set_string(stmt, i, date_format(date))
5deef985e38e allow null;
wenzelm
parents: 65594
diff changeset
   352
65620
9b99d61be5af tuned signature: less overloading;
wenzelm
parents: 65619
diff changeset
   353
    def date(rs: ResultSet, column: SQL.Column): Date =
9b99d61be5af tuned signature: less overloading;
wenzelm
parents: 65619
diff changeset
   354
      date_format.parse(string(rs, column))
65021
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
   355
65698
38139b2067cf clarified signature;
wenzelm
parents: 65697
diff changeset
   356
    def rebuild { using_statement("VACUUM")(_.execute()) }
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   357
  }
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   358
}
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   359
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   360
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   361
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   362
/** PostgreSQL **/
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   363
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   364
object PostgreSQL
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   365
{
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   366
  val default_port = 5432
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   367
65292
e3bd1e7ddd23 more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents: 65280
diff changeset
   368
  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
   369
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   370
  def open_database(
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   371
    user: String,
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   372
    password: String,
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   373
    database: String = "",
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   374
    host: String = "",
65594
659305708959 clarified treatment of default port;
wenzelm
parents: 65593
diff changeset
   375
    port: Int = 0,
65636
df804cdba5f9 ssh_close for proper termination after use of database;
wenzelm
parents: 65620
diff changeset
   376
    ssh: Option[SSH.Session] = None,
df804cdba5f9 ssh_close for proper termination after use of database;
wenzelm
parents: 65620
diff changeset
   377
    ssh_close: Boolean = false): Database =
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   378
  {
65292
e3bd1e7ddd23 more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents: 65280
diff changeset
   379
    init_jdbc
e3bd1e7ddd23 more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents: 65280
diff changeset
   380
65641
3b0110e25745 tuned message;
wenzelm
parents: 65636
diff changeset
   381
    if (user == "") error("Undefined database user")
65009
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   382
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   383
    val db_host = if (host != "") host else "localhost"
65594
659305708959 clarified treatment of default port;
wenzelm
parents: 65593
diff changeset
   384
    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
   385
    val db_name = "/" + (if (database != "") database else user)
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   386
65010
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   387
    val (url, name, port_forwarding) =
65009
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   388
      ssh match {
65010
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   389
        case None =>
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   390
          val spec = db_host + db_port + db_name
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   391
          val url = "jdbc:postgresql://" + spec
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   392
          val name = user + "@" + spec
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   393
          (url, name, None)
65009
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   394
        case Some(ssh) =>
65594
659305708959 clarified treatment of default port;
wenzelm
parents: 65593
diff changeset
   395
          val fw =
659305708959 clarified treatment of default port;
wenzelm
parents: 65593
diff changeset
   396
            ssh.port_forwarding(remote_host = db_host,
65636
df804cdba5f9 ssh_close for proper termination after use of database;
wenzelm
parents: 65620
diff changeset
   397
              remote_port = if (port > 0) port else default_port,
df804cdba5f9 ssh_close for proper termination after use of database;
wenzelm
parents: 65620
diff changeset
   398
              ssh_close = ssh_close)
65010
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   399
          val url = "jdbc:postgresql://localhost:" + fw.local_port + db_name
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   400
          val name = user + "@" + fw + db_name + " via ssh " + ssh
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   401
          (url, name, Some(fw))
65009
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   402
      }
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   403
    try {
65010
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   404
      val connection = DriverManager.getConnection(url, user, password)
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   405
      new Database(name, connection, port_forwarding)
65009
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   406
    }
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   407
    catch { case exn: Throwable => port_forwarding.foreach(_.close); throw exn }
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   408
  }
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   409
65009
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   410
  class Database private[PostgreSQL](
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   411
      name: String, val connection: Connection, port_forwarding: Option[SSH.Port_Forwarding])
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   412
    extends SQL.Database
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   413
  {
65010
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   414
    override def toString: String = name
65008
ed2eedf786f3 clarified handling of SQL.Type;
wenzelm
parents: 65007
diff changeset
   415
65019
0ca91e5d52df tuned signature;
wenzelm
parents: 65018
diff changeset
   416
    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
   417
65021
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
   418
    // see https://jdbc.postgresql.org/documentation/head/8-date-time.html
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
   419
    def set_date(stmt: PreparedStatement, i: Int, date: Date): Unit =
65598
5deef985e38e allow null;
wenzelm
parents: 65594
diff changeset
   420
      if (date == null) stmt.setObject(i, null)
5deef985e38e allow null;
wenzelm
parents: 65594
diff changeset
   421
      else stmt.setObject(i, OffsetDateTime.from(date.to_utc.rep))
5deef985e38e allow null;
wenzelm
parents: 65594
diff changeset
   422
65620
9b99d61be5af tuned signature: less overloading;
wenzelm
parents: 65619
diff changeset
   423
    def date(rs: ResultSet, column: SQL.Column): Date =
9b99d61be5af tuned signature: less overloading;
wenzelm
parents: 65619
diff changeset
   424
      Date.instant(rs.getObject(column.name, classOf[OffsetDateTime]).toInstant)
65021
da8ae577d171 clarified Date storage;
wenzelm
parents: 65020
diff changeset
   425
65009
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 65008
diff changeset
   426
    override def close() { super.close; port_forwarding.foreach(_.close) }
65006
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   427
  }
632bdf7b8bab clarified modules;
wenzelm
parents: 65004
diff changeset
   428
}