src/Pure/General/sql.scala
author wenzelm
Mon Dec 03 14:59:42 2018 +0100 (7 months ago)
changeset 69393 ed0824ef337e
parent 69327 264b44dce6be
child 69980 f2e3adfd916f
permissions -rw-r--r--
static type for Library.using: avoid Java 11 warnings on "Illegal reflective access";
more uses of "using";
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@69393
     9
wenzelm@65021
    10
import java.time.OffsetDateTime
wenzelm@65021
    11
import java.sql.{DriverManager, Connection, PreparedStatement, ResultSet}
wenzelm@63779
    12
wenzelm@65740
    13
import scala.collection.mutable
wenzelm@65740
    14
wenzelm@63779
    15
wenzelm@63778
    16
object SQL
wenzelm@63778
    17
{
wenzelm@65006
    18
  /** SQL language **/
wenzelm@65006
    19
wenzelm@65730
    20
  type Source = String
wenzelm@65730
    21
wenzelm@65730
    22
wenzelm@63778
    23
  /* concrete syntax */
wenzelm@63778
    24
wenzelm@65321
    25
  def escape_char(c: Char): String =
wenzelm@63778
    26
    c match {
wenzelm@63778
    27
      case '\u0000' => "\\0"
wenzelm@63778
    28
      case '\'' => "\\'"
wenzelm@63778
    29
      case '\"' => "\\\""
wenzelm@63778
    30
      case '\b' => "\\b"
wenzelm@63778
    31
      case '\n' => "\\n"
wenzelm@63778
    32
      case '\r' => "\\r"
wenzelm@63778
    33
      case '\t' => "\\t"
wenzelm@63778
    34
      case '\u001a' => "\\Z"
wenzelm@63778
    35
      case '\\' => "\\\\"
wenzelm@63778
    36
      case _ => c.toString
wenzelm@63778
    37
    }
wenzelm@63778
    38
wenzelm@65730
    39
  def string(s: String): Source =
wenzelm@69327
    40
    s.iterator.map(escape_char(_)).mkString("'", "", "'")
wenzelm@63778
    41
wenzelm@65730
    42
  def ident(s: String): Source =
wenzelm@65677
    43
    Long_Name.implode(Long_Name.explode(s).map(a => quote(a.replace("\"", "\"\""))))
wenzelm@63779
    44
wenzelm@65730
    45
  def enclose(s: Source): Source = "(" + s + ")"
wenzelm@65730
    46
  def enclosure(ss: Iterable[Source]): Source = ss.mkString("(", ", ", ")")
wenzelm@63791
    47
wenzelm@65774
    48
  def select(columns: List[Column] = Nil, distinct: Boolean = false): Source =
wenzelm@65774
    49
    "SELECT " + (if (distinct) "DISTINCT " else "") +
wenzelm@65774
    50
    (if (columns.isEmpty) "*" else commas(columns.map(_.ident))) + " FROM "
wenzelm@65644
    51
wenzelm@65775
    52
  val join_outer: Source = " LEFT OUTER JOIN "
wenzelm@65775
    53
  val join_inner: Source = " INNER JOIN "
wenzelm@65776
    54
  def join(outer: Boolean): Source = if (outer) join_outer else join_inner
wenzelm@65775
    55
wenzelm@65804
    56
  def member(x: Source, set: Iterable[String]): Source =
wenzelm@65804
    57
    set.iterator.map(a => x + " = " + SQL.string(a)).mkString("(", " OR ", ")")
wenzelm@65804
    58
wenzelm@63779
    59
wenzelm@65008
    60
  /* types */
wenzelm@65008
    61
wenzelm@65008
    62
  object Type extends Enumeration
wenzelm@65008
    63
  {
wenzelm@65011
    64
    val Boolean = Value("BOOLEAN")
wenzelm@65008
    65
    val Int = Value("INTEGER")
wenzelm@65008
    66
    val Long = Value("BIGINT")
wenzelm@65008
    67
    val Double = Value("DOUBLE PRECISION")
wenzelm@65008
    68
    val String = Value("TEXT")
wenzelm@65008
    69
    val Bytes = Value("BLOB")
wenzelm@65014
    70
    val Date = Value("TIMESTAMP WITH TIME ZONE")
wenzelm@65008
    71
  }
wenzelm@65008
    72
wenzelm@65730
    73
  def sql_type_default(T: Type.Value): Source = T.toString
wenzelm@65013
    74
wenzelm@65730
    75
  def sql_type_sqlite(T: Type.Value): Source =
wenzelm@65019
    76
    if (T == Type.Boolean) "INTEGER"
wenzelm@65019
    77
    else if (T == Type.Date) "TEXT"
wenzelm@65019
    78
    else sql_type_default(T)
wenzelm@65013
    79
wenzelm@65730
    80
  def sql_type_postgresql(T: Type.Value): Source =
wenzelm@65019
    81
    if (T == Type.Bytes) "BYTEA"
wenzelm@65019
    82
    else sql_type_default(T)
wenzelm@65008
    83
wenzelm@65008
    84
wenzelm@63779
    85
  /* columns */
wenzelm@63779
    86
wenzelm@63779
    87
  object Column
wenzelm@63779
    88
  {
wenzelm@65280
    89
    def bool(name: String, strict: Boolean = false, primary_key: Boolean = false): Column =
wenzelm@65018
    90
      Column(name, Type.Boolean, strict, primary_key)
wenzelm@65280
    91
    def int(name: String, strict: Boolean = false, primary_key: Boolean = false): Column =
wenzelm@65018
    92
      Column(name, Type.Int, strict, primary_key)
wenzelm@65280
    93
    def long(name: String, strict: Boolean = false, primary_key: Boolean = false): Column =
wenzelm@65018
    94
      Column(name, Type.Long, strict, primary_key)
wenzelm@65280
    95
    def double(name: String, strict: Boolean = false, primary_key: Boolean = false): Column =
wenzelm@65018
    96
      Column(name, Type.Double, strict, primary_key)
wenzelm@65280
    97
    def string(name: String, strict: Boolean = false, primary_key: Boolean = false): Column =
wenzelm@65018
    98
      Column(name, Type.String, strict, primary_key)
wenzelm@65280
    99
    def bytes(name: String, strict: Boolean = false, primary_key: Boolean = false): Column =
wenzelm@65018
   100
      Column(name, Type.Bytes, strict, primary_key)
wenzelm@65280
   101
    def date(name: String, strict: Boolean = false, primary_key: Boolean = false): Column =
wenzelm@65018
   102
      Column(name, Type.Date, strict, primary_key)
wenzelm@63779
   103
  }
wenzelm@63779
   104
wenzelm@65018
   105
  sealed case class Column(
wenzelm@65780
   106
    name: String, T: Type.Value, strict: Boolean = false, primary_key: Boolean = false,
wenzelm@65780
   107
    expr: SQL.Source = "")
wenzelm@63779
   108
  {
wenzelm@66857
   109
    def make_primary_key: Column = copy(primary_key = true)
wenzelm@66857
   110
wenzelm@65691
   111
    def apply(table: Table): Column =
wenzelm@65691
   112
      Column(Long_Name.qualify(table.name, name), T, strict = strict, primary_key = primary_key)
wenzelm@65644
   113
wenzelm@65780
   114
    def ident: Source =
wenzelm@65780
   115
      if (expr == "") SQL.ident(name)
wenzelm@65780
   116
      else enclose(expr) + " AS " + SQL.ident(name)
wenzelm@65695
   117
wenzelm@65730
   118
    def decl(sql_type: Type.Value => Source): Source =
wenzelm@65695
   119
      ident + " " + sql_type(T) + (if (strict || primary_key) " NOT NULL" else "")
wenzelm@63781
   120
wenzelm@66856
   121
    def defined: String = ident + " IS NOT NULL"
wenzelm@66856
   122
    def undefined: String = ident + " IS NULL"
wenzelm@66856
   123
wenzelm@68091
   124
    def equal(s: String): Source = ident + " = " + string(s)
wenzelm@68091
   125
    def where_equal(s: String): Source = "WHERE " + equal(s)
wenzelm@65593
   126
wenzelm@65730
   127
    override def toString: Source = ident
wenzelm@63779
   128
  }
wenzelm@63779
   129
wenzelm@63780
   130
wenzelm@63780
   131
  /* tables */
wenzelm@63780
   132
wenzelm@65730
   133
  sealed case class Table(name: String, columns: List[Column], body: Source = "")
wenzelm@63780
   134
  {
wenzelm@63790
   135
    private val columns_index: Map[String, Int] =
wenzelm@63790
   136
      columns.iterator.map(_.name).zipWithIndex.toMap
wenzelm@63790
   137
wenzelm@63781
   138
    Library.duplicates(columns.map(_.name)) match {
wenzelm@63781
   139
      case Nil =>
wenzelm@63781
   140
      case bad => error("Duplicate column names " + commas_quote(bad) + " for table " + quote(name))
wenzelm@63781
   141
    }
wenzelm@63781
   142
wenzelm@65730
   143
    def ident: Source = SQL.ident(name)
wenzelm@65691
   144
wenzelm@65730
   145
    def query: Source =
wenzelm@65696
   146
      if (body == "") error("Missing SQL body for table " + quote(name))
wenzelm@65696
   147
      else SQL.enclose(body)
wenzelm@65649
   148
wenzelm@65778
   149
    def query_named: Source = query + " AS " + SQL.ident(name)
wenzelm@65702
   150
wenzelm@65730
   151
    def create(strict: Boolean = false, sql_type: Type.Value => Source): Source =
wenzelm@65325
   152
    {
wenzelm@65325
   153
      val primary_key =
wenzelm@65325
   154
        columns.filter(_.primary_key).map(_.name) match {
wenzelm@65325
   155
          case Nil => Nil
wenzelm@65325
   156
          case keys => List("PRIMARY KEY " + enclosure(keys))
wenzelm@65325
   157
        }
wenzelm@65701
   158
      "CREATE TABLE " + (if (strict) "" else "IF NOT EXISTS ") +
wenzelm@65701
   159
        ident + " " + enclosure(columns.map(_.decl(sql_type)) ::: primary_key)
wenzelm@63781
   160
    }
wenzelm@63781
   161
wenzelm@65698
   162
    def create_index(index_name: String, index_columns: List[Column],
wenzelm@65730
   163
        strict: Boolean = false, unique: Boolean = false): Source =
wenzelm@63791
   164
      "CREATE " + (if (unique) "UNIQUE " else "") + "INDEX " +
wenzelm@65695
   165
        (if (strict) "" else "IF NOT EXISTS ") + SQL.ident(index_name) + " ON " +
wenzelm@65695
   166
        ident + " " + enclosure(index_columns.map(_.name))
wenzelm@63791
   167
wenzelm@65730
   168
    def insert_cmd(cmd: Source, sql: Source = ""): Source =
wenzelm@65703
   169
      cmd + " INTO " + ident + " VALUES " + enclosure(columns.map(_ => "?")) +
wenzelm@65698
   170
        (if (sql == "") "" else " " + sql)
wenzelm@63791
   171
wenzelm@65730
   172
    def insert(sql: Source = ""): Source = insert_cmd("INSERT", sql)
wenzelm@65703
   173
wenzelm@65730
   174
    def delete(sql: Source = ""): Source =
wenzelm@65698
   175
      "DELETE FROM " + ident +
wenzelm@65698
   176
        (if (sql == "") "" else " " + sql)
wenzelm@65319
   177
wenzelm@65774
   178
    def select(
wenzelm@65774
   179
        select_columns: List[Column] = Nil, sql: Source = "", distinct: Boolean = false): Source =
wenzelm@65698
   180
      SQL.select(select_columns, distinct = distinct) + ident +
wenzelm@65698
   181
        (if (sql == "") "" else " " + sql)
wenzelm@63790
   182
wenzelm@65730
   183
    override def toString: Source = ident
wenzelm@63780
   184
  }
wenzelm@63790
   185
wenzelm@63790
   186
wenzelm@65012
   187
wenzelm@65012
   188
  /** SQL database operations **/
wenzelm@65012
   189
wenzelm@65740
   190
  /* statements */
wenzelm@65740
   191
wenzelm@65740
   192
  class Statement private[SQL](val db: Database, val rep: PreparedStatement)
wenzelm@69393
   193
    extends AutoCloseable
wenzelm@65740
   194
  {
wenzelm@65740
   195
    stmt =>
wenzelm@65740
   196
wenzelm@65748
   197
    object bool
wenzelm@65740
   198
    {
wenzelm@65748
   199
      def update(i: Int, x: Boolean) { rep.setBoolean(i, x) }
wenzelm@65748
   200
      def update(i: Int, x: Option[Boolean])
wenzelm@65748
   201
      {
wenzelm@65748
   202
        if (x.isDefined) update(i, x.get)
wenzelm@65748
   203
        else rep.setNull(i, java.sql.Types.BOOLEAN)
wenzelm@65748
   204
      }
wenzelm@65740
   205
    }
wenzelm@65748
   206
    object int
wenzelm@65740
   207
    {
wenzelm@65748
   208
      def update(i: Int, x: Int) { rep.setInt(i, x) }
wenzelm@65748
   209
      def update(i: Int, x: Option[Int])
wenzelm@65748
   210
      {
wenzelm@65748
   211
        if (x.isDefined) update(i, x.get)
wenzelm@65748
   212
        else rep.setNull(i, java.sql.Types.INTEGER)
wenzelm@65748
   213
      }
wenzelm@65740
   214
    }
wenzelm@65748
   215
    object long
wenzelm@65740
   216
    {
wenzelm@65748
   217
      def update(i: Int, x: Long) { rep.setLong(i, x) }
wenzelm@65748
   218
      def update(i: Int, x: Option[Long])
wenzelm@65748
   219
      {
wenzelm@65748
   220
        if (x.isDefined) update(i, x.get)
wenzelm@65748
   221
        else rep.setNull(i, java.sql.Types.BIGINT)
wenzelm@65748
   222
      }
wenzelm@65740
   223
    }
wenzelm@65748
   224
    object double
wenzelm@65740
   225
    {
wenzelm@65748
   226
      def update(i: Int, x: Double) { rep.setDouble(i, x) }
wenzelm@65748
   227
      def update(i: Int, x: Option[Double])
wenzelm@65748
   228
      {
wenzelm@65748
   229
        if (x.isDefined) update(i, x.get)
wenzelm@65748
   230
        else rep.setNull(i, java.sql.Types.DOUBLE)
wenzelm@65748
   231
      }
wenzelm@65748
   232
    }
wenzelm@65748
   233
    object string
wenzelm@65748
   234
    {
wenzelm@65748
   235
      def update(i: Int, x: String) { rep.setString(i, x) }
wenzelm@65748
   236
      def update(i: Int, x: Option[String]): Unit = update(i, x.orNull)
wenzelm@65740
   237
    }
wenzelm@65748
   238
    object bytes
wenzelm@65740
   239
    {
wenzelm@65748
   240
      def update(i: Int, bytes: Bytes)
wenzelm@65748
   241
      {
wenzelm@65748
   242
        if (bytes == null) rep.setBytes(i, null)
wenzelm@65748
   243
        else rep.setBinaryStream(i, bytes.stream(), bytes.length)
wenzelm@65748
   244
      }
wenzelm@65748
   245
      def update(i: Int, bytes: Option[Bytes]): Unit = update(i, bytes.orNull)
wenzelm@65740
   246
    }
wenzelm@65748
   247
    object date
wenzelm@65748
   248
    {
wenzelm@65748
   249
      def update(i: Int, date: Date): Unit = db.update_date(stmt, i, date)
wenzelm@65748
   250
      def update(i: Int, date: Option[Date]): Unit = update(i, date.orNull)
wenzelm@65748
   251
    }
wenzelm@65740
   252
wenzelm@65740
   253
    def execute(): Boolean = rep.execute()
wenzelm@65740
   254
    def execute_query(): Result = new Result(this, rep.executeQuery())
wenzelm@65740
   255
wenzelm@65740
   256
    def close(): Unit = rep.close
wenzelm@65740
   257
  }
wenzelm@65740
   258
wenzelm@65740
   259
wenzelm@63790
   260
  /* results */
wenzelm@63790
   261
wenzelm@65740
   262
  class Result private[SQL](val stmt: Statement, val rep: ResultSet)
wenzelm@63790
   263
  {
wenzelm@65740
   264
    res =>
wenzelm@65740
   265
wenzelm@65740
   266
    def next(): Boolean = rep.next()
wenzelm@65740
   267
wenzelm@65740
   268
    def iterator[A](get: Result => A): Iterator[A] = new Iterator[A]
wenzelm@65740
   269
    {
wenzelm@65740
   270
      private var _next: Boolean = res.next()
wenzelm@65740
   271
      def hasNext: Boolean = _next
wenzelm@65740
   272
      def next: A = { val x = get(res); _next = res.next(); x }
wenzelm@65740
   273
    }
wenzelm@65740
   274
wenzelm@65740
   275
    def bool(column: Column): Boolean = rep.getBoolean(column.name)
wenzelm@65740
   276
    def int(column: Column): Int = rep.getInt(column.name)
wenzelm@65740
   277
    def long(column: Column): Long = rep.getLong(column.name)
wenzelm@65740
   278
    def double(column: Column): Double = rep.getDouble(column.name)
wenzelm@65740
   279
    def string(column: Column): String =
wenzelm@65740
   280
    {
wenzelm@65740
   281
      val s = rep.getString(column.name)
wenzelm@65740
   282
      if (s == null) "" else s
wenzelm@65740
   283
    }
wenzelm@65740
   284
    def bytes(column: Column): Bytes =
wenzelm@65740
   285
    {
wenzelm@65740
   286
      val bs = rep.getBytes(column.name)
wenzelm@65740
   287
      if (bs == null) Bytes.empty else Bytes(bs)
wenzelm@65740
   288
    }
wenzelm@65740
   289
    def date(column: Column): Date = stmt.db.date(res, column)
wenzelm@65740
   290
wenzelm@65741
   291
    def timing(c1: Column, c2: Column, c3: Column) =
wenzelm@65741
   292
      Timing(Time.ms(long(c1)), Time.ms(long(c2)), Time.ms(long(c3)))
wenzelm@65741
   293
wenzelm@65740
   294
    def get[A](column: Column, f: Column => A): Option[A] =
wenzelm@65740
   295
    {
wenzelm@65740
   296
      val x = f(column)
wenzelm@65740
   297
      if (rep.wasNull) None else Some(x)
wenzelm@65740
   298
    }
wenzelm@65740
   299
    def get_bool(column: Column): Option[Boolean] = get(column, bool _)
wenzelm@65740
   300
    def get_int(column: Column): Option[Int] = get(column, int _)
wenzelm@65740
   301
    def get_long(column: Column): Option[Long] = get(column, long _)
wenzelm@65740
   302
    def get_double(column: Column): Option[Double] = get(column, double _)
wenzelm@65740
   303
    def get_string(column: Column): Option[String] = get(column, string _)
wenzelm@65740
   304
    def get_bytes(column: Column): Option[Bytes] = get(column, bytes _)
wenzelm@65740
   305
    def get_date(column: Column): Option[Date] = get(column, date _)
wenzelm@63790
   306
  }
wenzelm@65006
   307
wenzelm@65740
   308
wenzelm@65740
   309
  /* database */
wenzelm@65740
   310
wenzelm@69393
   311
  trait Database extends AutoCloseable
wenzelm@65006
   312
  {
wenzelm@65740
   313
    db =>
wenzelm@65740
   314
wenzelm@65740
   315
wenzelm@65008
   316
    /* types */
wenzelm@65008
   317
wenzelm@65730
   318
    def sql_type(T: Type.Value): Source
wenzelm@65008
   319
wenzelm@65008
   320
wenzelm@65006
   321
    /* connection */
wenzelm@65006
   322
wenzelm@65006
   323
    def connection: Connection
wenzelm@65006
   324
wenzelm@65006
   325
    def close() { connection.close }
wenzelm@65006
   326
wenzelm@65006
   327
    def transaction[A](body: => A): A =
wenzelm@65006
   328
    {
wenzelm@65006
   329
      val auto_commit = connection.getAutoCommit
wenzelm@65006
   330
      try {
wenzelm@65006
   331
        connection.setAutoCommit(false)
wenzelm@65022
   332
        val savepoint = connection.setSavepoint
wenzelm@65022
   333
        try {
wenzelm@65022
   334
          val result = body
wenzelm@65022
   335
          connection.commit
wenzelm@65022
   336
          result
wenzelm@65022
   337
        }
wenzelm@65022
   338
        catch { case exn: Throwable => connection.rollback(savepoint); throw exn }
wenzelm@65006
   339
      }
wenzelm@65006
   340
      finally { connection.setAutoCommit(auto_commit) }
wenzelm@65006
   341
    }
wenzelm@65006
   342
wenzelm@65006
   343
wenzelm@65740
   344
    /* statements and results */
wenzelm@65740
   345
wenzelm@65740
   346
    def statement(sql: Source): Statement =
wenzelm@65740
   347
      new Statement(db, connection.prepareStatement(sql))
wenzelm@65006
   348
wenzelm@65740
   349
    def using_statement[A](sql: Source)(f: Statement => A): A =
wenzelm@65740
   350
      using(statement(sql))(f)
wenzelm@65006
   351
wenzelm@65748
   352
    def update_date(stmt: Statement, i: Int, date: Date): Unit
wenzelm@65740
   353
    def date(res: Result, column: Column): Date
wenzelm@65006
   354
wenzelm@65730
   355
    def insert_permissive(table: Table, sql: Source = ""): Source
wenzelm@65703
   356
wenzelm@65006
   357
wenzelm@65669
   358
    /* tables and views */
wenzelm@65006
   359
wenzelm@65006
   360
    def tables: List[String] =
wenzelm@65740
   361
    {
wenzelm@65740
   362
      val result = new mutable.ListBuffer[String]
wenzelm@65740
   363
      val rs = connection.getMetaData.getTables(null, null, "%", null)
wenzelm@65740
   364
      while (rs.next) { result += rs.getString(3) }
wenzelm@65740
   365
      result.toList
wenzelm@65740
   366
    }
wenzelm@65006
   367
wenzelm@65730
   368
    def create_table(table: Table, strict: Boolean = false, sql: Source = ""): Unit =
wenzelm@65698
   369
      using_statement(
wenzelm@65698
   370
        table.create(strict, sql_type) + (if (sql == "") "" else " " + sql))(_.execute())
wenzelm@65006
   371
wenzelm@65018
   372
    def create_index(table: Table, name: String, columns: List[Column],
wenzelm@65280
   373
        strict: Boolean = false, unique: Boolean = false): Unit =
wenzelm@65698
   374
      using_statement(table.create_index(name, columns, strict, unique))(_.execute())
wenzelm@65006
   375
wenzelm@65691
   376
    def create_view(table: Table, strict: Boolean = false): Unit =
wenzelm@65691
   377
    {
wenzelm@65691
   378
      if (strict || !tables.contains(table.name)) {
wenzelm@65731
   379
        val sql = "CREATE VIEW " + table + " AS " + { table.query; table.body }
wenzelm@65698
   380
        using_statement(sql)(_.execute())
wenzelm@65690
   381
      }
wenzelm@65691
   382
    }
wenzelm@65006
   383
  }
wenzelm@63778
   384
}
wenzelm@65006
   385
wenzelm@65006
   386
wenzelm@65006
   387
wenzelm@65006
   388
/** SQLite **/
wenzelm@65006
   389
wenzelm@65006
   390
object SQLite
wenzelm@65006
   391
{
wenzelm@65021
   392
  // see https://www.sqlite.org/lang_datefunc.html
wenzelm@65021
   393
  val date_format: Date.Format = Date.Format("uuuu-MM-dd HH:mm:ss.SSS x")
wenzelm@65021
   394
wenzelm@65890
   395
  lazy val init_jdbc: Unit =
wenzelm@65890
   396
  {
wenzelm@65890
   397
    val lib_path = Path.explode("$ISABELLE_SQLITE_HOME/" + Platform.jvm_platform)
wenzelm@65890
   398
    val lib_name =
wenzelm@65890
   399
      File.find_files(lib_path.file) match {
wenzelm@65890
   400
        case List(file) => file.getName
wenzelm@65918
   401
        case _ => error("Exactly one file expected in directory " + lib_path.expand)
wenzelm@65890
   402
      }
wenzelm@65890
   403
    System.setProperty("org.sqlite.lib.path", File.platform_path(lib_path))
wenzelm@65890
   404
    System.setProperty("org.sqlite.lib.name", lib_name)
wenzelm@65890
   405
wenzelm@65890
   406
    Class.forName("org.sqlite.JDBC")
wenzelm@65890
   407
  }
wenzelm@65292
   408
wenzelm@65006
   409
  def open_database(path: Path): Database =
wenzelm@65006
   410
  {
wenzelm@65292
   411
    init_jdbc
wenzelm@65006
   412
    val path0 = path.expand
wenzelm@65006
   413
    val s0 = File.platform_path(path0)
wenzelm@65006
   414
    val s1 = if (Platform.is_windows) s0.replace('\\', '/') else s0
wenzelm@65006
   415
    val connection = DriverManager.getConnection("jdbc:sqlite:" + s1)
wenzelm@65007
   416
    new Database(path0.toString, connection)
wenzelm@65006
   417
  }
wenzelm@65006
   418
wenzelm@65007
   419
  class Database private[SQLite](name: String, val connection: Connection) extends SQL.Database
wenzelm@65006
   420
  {
wenzelm@65007
   421
    override def toString: String = name
wenzelm@65006
   422
wenzelm@65730
   423
    def sql_type(T: SQL.Type.Value): SQL.Source = SQL.sql_type_sqlite(T)
wenzelm@65011
   424
wenzelm@65748
   425
    def update_date(stmt: SQL.Statement, i: Int, date: Date): Unit =
wenzelm@65748
   426
      if (date == null) stmt.string(i) = (null: String)
wenzelm@65748
   427
      else stmt.string(i) = date_format(date)
wenzelm@65598
   428
wenzelm@65740
   429
    def date(res: SQL.Result, column: SQL.Column): Date =
wenzelm@65740
   430
      date_format.parse(res.string(column))
wenzelm@65021
   431
wenzelm@65730
   432
    def insert_permissive(table: SQL.Table, sql: SQL.Source = ""): SQL.Source =
wenzelm@65703
   433
      table.insert_cmd("INSERT OR IGNORE", sql = sql)
wenzelm@65703
   434
wenzelm@65698
   435
    def rebuild { using_statement("VACUUM")(_.execute()) }
wenzelm@65006
   436
  }
wenzelm@65006
   437
}
wenzelm@65006
   438
wenzelm@65006
   439
wenzelm@65006
   440
wenzelm@65006
   441
/** PostgreSQL **/
wenzelm@65006
   442
wenzelm@65006
   443
object PostgreSQL
wenzelm@65006
   444
{
wenzelm@65006
   445
  val default_port = 5432
wenzelm@65006
   446
wenzelm@65292
   447
  lazy val init_jdbc: Unit = Class.forName("org.postgresql.Driver")
wenzelm@65292
   448
wenzelm@65006
   449
  def open_database(
wenzelm@65006
   450
    user: String,
wenzelm@65006
   451
    password: String,
wenzelm@65006
   452
    database: String = "",
wenzelm@65006
   453
    host: String = "",
wenzelm@65594
   454
    port: Int = 0,
wenzelm@65636
   455
    ssh: Option[SSH.Session] = None,
wenzelm@65636
   456
    ssh_close: Boolean = false): Database =
wenzelm@65006
   457
  {
wenzelm@65292
   458
    init_jdbc
wenzelm@65292
   459
wenzelm@65641
   460
    if (user == "") error("Undefined database user")
wenzelm@65009
   461
wenzelm@65717
   462
    val db_host = proper_string(host) getOrElse "localhost"
wenzelm@65594
   463
    val db_port = if (port > 0 && port != default_port) ":" + port else ""
wenzelm@65717
   464
    val db_name = "/" + (proper_string(database) getOrElse user)
wenzelm@65009
   465
wenzelm@65010
   466
    val (url, name, port_forwarding) =
wenzelm@65009
   467
      ssh match {
wenzelm@65010
   468
        case None =>
wenzelm@65010
   469
          val spec = db_host + db_port + db_name
wenzelm@65010
   470
          val url = "jdbc:postgresql://" + spec
wenzelm@65010
   471
          val name = user + "@" + spec
wenzelm@65010
   472
          (url, name, None)
wenzelm@65009
   473
        case Some(ssh) =>
wenzelm@65594
   474
          val fw =
wenzelm@65594
   475
            ssh.port_forwarding(remote_host = db_host,
wenzelm@65636
   476
              remote_port = if (port > 0) port else default_port,
wenzelm@65636
   477
              ssh_close = ssh_close)
wenzelm@65010
   478
          val url = "jdbc:postgresql://localhost:" + fw.local_port + db_name
wenzelm@65010
   479
          val name = user + "@" + fw + db_name + " via ssh " + ssh
wenzelm@65010
   480
          (url, name, Some(fw))
wenzelm@65009
   481
      }
wenzelm@65009
   482
    try {
wenzelm@65010
   483
      val connection = DriverManager.getConnection(url, user, password)
wenzelm@65010
   484
      new Database(name, connection, port_forwarding)
wenzelm@65009
   485
    }
wenzelm@65009
   486
    catch { case exn: Throwable => port_forwarding.foreach(_.close); throw exn }
wenzelm@65006
   487
  }
wenzelm@65006
   488
wenzelm@65009
   489
  class Database private[PostgreSQL](
wenzelm@65009
   490
      name: String, val connection: Connection, port_forwarding: Option[SSH.Port_Forwarding])
wenzelm@65009
   491
    extends SQL.Database
wenzelm@65006
   492
  {
wenzelm@65010
   493
    override def toString: String = name
wenzelm@65008
   494
wenzelm@65730
   495
    def sql_type(T: SQL.Type.Value): SQL.Source = SQL.sql_type_postgresql(T)
wenzelm@65009
   496
wenzelm@65021
   497
    // see https://jdbc.postgresql.org/documentation/head/8-date-time.html
wenzelm@65748
   498
    def update_date(stmt: SQL.Statement, i: Int, date: Date): Unit =
wenzelm@65740
   499
      if (date == null) stmt.rep.setObject(i, null)
wenzelm@65740
   500
      else stmt.rep.setObject(i, OffsetDateTime.from(date.to_utc.rep))
wenzelm@65598
   501
wenzelm@65740
   502
    def date(res: SQL.Result, column: SQL.Column): Date =
wenzelm@65704
   503
    {
wenzelm@65740
   504
      val obj = res.rep.getObject(column.name, classOf[OffsetDateTime])
wenzelm@65704
   505
      if (obj == null) null else Date.instant(obj.toInstant)
wenzelm@65704
   506
    }
wenzelm@65021
   507
wenzelm@65730
   508
    def insert_permissive(table: SQL.Table, sql: SQL.Source = ""): SQL.Source =
wenzelm@65703
   509
      table.insert_cmd("INSERT",
wenzelm@65703
   510
        sql = sql + (if (sql == "") "" else " ") + "ON CONFLICT DO NOTHING")
wenzelm@65703
   511
wenzelm@65009
   512
    override def close() { super.close; port_forwarding.foreach(_.close) }
wenzelm@65006
   513
  }
wenzelm@65006
   514
}