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