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