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