src/Pure/General/sql.scala
author wenzelm
Sun Mar 19 12:57:29 2017 +0100 (2017-03-19)
changeset 65319 64da14387b2c
parent 65292 e3bd1e7ddd23
child 65321 2b1cd063e0b2
permissions -rw-r--r--
more operations;
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@63778
    19
  def quote_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@63778
    33
  def quote_string(s: String): String =
wenzelm@63778
    34
    quote(s.map(quote_char(_)).mkString)
wenzelm@63778
    35
wenzelm@63779
    36
  def quote_ident(s: String): String =
wenzelm@65003
    37
    quote(s.replace("\"", "\"\""))
wenzelm@63779
    38
wenzelm@63791
    39
  def enclosure(ss: Iterable[String]): String = ss.mkString("(", ", ", ")")
wenzelm@63791
    40
wenzelm@63779
    41
wenzelm@65008
    42
  /* types */
wenzelm@65008
    43
wenzelm@65008
    44
  object Type extends Enumeration
wenzelm@65008
    45
  {
wenzelm@65011
    46
    val Boolean = Value("BOOLEAN")
wenzelm@65008
    47
    val Int = Value("INTEGER")
wenzelm@65008
    48
    val Long = Value("BIGINT")
wenzelm@65008
    49
    val Double = Value("DOUBLE PRECISION")
wenzelm@65008
    50
    val String = Value("TEXT")
wenzelm@65008
    51
    val Bytes = Value("BLOB")
wenzelm@65014
    52
    val Date = Value("TIMESTAMP WITH TIME ZONE")
wenzelm@65008
    53
  }
wenzelm@65008
    54
wenzelm@65019
    55
  def sql_type_default(T: Type.Value): String = T.toString
wenzelm@65013
    56
wenzelm@65019
    57
  def sql_type_sqlite(T: Type.Value): String =
wenzelm@65019
    58
    if (T == Type.Boolean) "INTEGER"
wenzelm@65019
    59
    else if (T == Type.Date) "TEXT"
wenzelm@65019
    60
    else sql_type_default(T)
wenzelm@65013
    61
wenzelm@65019
    62
  def sql_type_postgresql(T: Type.Value): String =
wenzelm@65019
    63
    if (T == Type.Bytes) "BYTEA"
wenzelm@65019
    64
    else sql_type_default(T)
wenzelm@65008
    65
wenzelm@65008
    66
wenzelm@63779
    67
  /* columns */
wenzelm@63779
    68
wenzelm@63779
    69
  object Column
wenzelm@63779
    70
  {
wenzelm@65280
    71
    def bool(name: String, strict: Boolean = false, primary_key: Boolean = false): Column =
wenzelm@65018
    72
      Column(name, Type.Boolean, strict, primary_key)
wenzelm@65280
    73
    def int(name: String, strict: Boolean = false, primary_key: Boolean = false): Column =
wenzelm@65018
    74
      Column(name, Type.Int, strict, primary_key)
wenzelm@65280
    75
    def long(name: String, strict: Boolean = false, primary_key: Boolean = false): Column =
wenzelm@65018
    76
      Column(name, Type.Long, strict, primary_key)
wenzelm@65280
    77
    def double(name: String, strict: Boolean = false, primary_key: Boolean = false): Column =
wenzelm@65018
    78
      Column(name, Type.Double, strict, primary_key)
wenzelm@65280
    79
    def string(name: String, strict: Boolean = false, primary_key: Boolean = false): Column =
wenzelm@65018
    80
      Column(name, Type.String, strict, primary_key)
wenzelm@65280
    81
    def bytes(name: String, strict: Boolean = false, primary_key: Boolean = false): Column =
wenzelm@65018
    82
      Column(name, Type.Bytes, strict, primary_key)
wenzelm@65280
    83
    def date(name: String, strict: Boolean = false, primary_key: Boolean = false): Column =
wenzelm@65018
    84
      Column(name, Type.Date, strict, primary_key)
wenzelm@63779
    85
  }
wenzelm@63779
    86
wenzelm@65018
    87
  sealed case class Column(
wenzelm@65280
    88
    name: String, T: Type.Value, strict: Boolean = false, primary_key: Boolean = false)
wenzelm@63779
    89
  {
wenzelm@63779
    90
    def sql_name: String = quote_ident(name)
wenzelm@65019
    91
    def sql_decl(sql_type: Type.Value => String): String =
wenzelm@65019
    92
      sql_name + " " + sql_type(T) +
wenzelm@63781
    93
      (if (strict) " NOT NULL" else "") +
wenzelm@63781
    94
      (if (primary_key) " PRIMARY KEY" else "")
wenzelm@63781
    95
wenzelm@65019
    96
    override def toString: String = sql_decl(sql_type_default)
wenzelm@63779
    97
  }
wenzelm@63779
    98
wenzelm@63780
    99
wenzelm@63780
   100
  /* tables */
wenzelm@63780
   101
wenzelm@65018
   102
  sealed case class Table(name: String, columns: List[Column])
wenzelm@63780
   103
  {
wenzelm@63790
   104
    private val columns_index: Map[String, Int] =
wenzelm@63790
   105
      columns.iterator.map(_.name).zipWithIndex.toMap
wenzelm@63790
   106
wenzelm@63781
   107
    Library.duplicates(columns.map(_.name)) match {
wenzelm@63781
   108
      case Nil =>
wenzelm@63781
   109
      case bad => error("Duplicate column names " + commas_quote(bad) + " for table " + quote(name))
wenzelm@63781
   110
    }
wenzelm@63781
   111
wenzelm@63781
   112
    columns.filter(_.primary_key) match {
wenzelm@63781
   113
      case bad if bad.length > 1 =>
wenzelm@63781
   114
        error("Multiple primary keys " + commas_quote(bad.map(_.name)) + " for table " + quote(name))
wenzelm@63781
   115
      case _ =>
wenzelm@63781
   116
    }
wenzelm@63781
   117
wenzelm@65019
   118
    def sql_create(strict: Boolean, rowid: Boolean, sql_type: Type.Value => String): String =
wenzelm@63784
   119
      "CREATE TABLE " + (if (strict) "" else "IF NOT EXISTS ") +
wenzelm@65019
   120
        quote_ident(name) + " " + enclosure(columns.map(_.sql_decl(sql_type))) +
wenzelm@63780
   121
        (if (rowid) "" else " WITHOUT ROWID")
wenzelm@63780
   122
wenzelm@63780
   123
    def sql_drop(strict: Boolean): String =
wenzelm@63784
   124
      "DROP TABLE " + (if (strict) "" else "IF EXISTS ") + quote_ident(name)
wenzelm@63783
   125
wenzelm@63791
   126
    def sql_create_index(
wenzelm@65018
   127
        index_name: String, index_columns: List[Column],
wenzelm@63791
   128
        strict: Boolean, unique: Boolean): String =
wenzelm@63791
   129
      "CREATE " + (if (unique) "UNIQUE " else "") + "INDEX " +
wenzelm@63791
   130
        (if (strict) "" else "IF NOT EXISTS ") + quote_ident(index_name) + " ON " +
wenzelm@63791
   131
        quote_ident(name) + " " + enclosure(index_columns.map(_.name))
wenzelm@63791
   132
wenzelm@63791
   133
    def sql_drop_index(index_name: String, strict: Boolean): String =
wenzelm@63791
   134
      "DROP INDEX " + (if (strict) "" else "IF EXISTS ") + quote_ident(index_name)
wenzelm@63791
   135
wenzelm@63790
   136
    def sql_insert: String =
wenzelm@63791
   137
      "INSERT INTO " + quote_ident(name) + " VALUES " + enclosure(columns.map(_ => "?"))
wenzelm@63791
   138
wenzelm@65319
   139
    def sql_delete: String =
wenzelm@65319
   140
      "DELETE FROM " + quote_ident(name)
wenzelm@65319
   141
wenzelm@65018
   142
    def sql_select(select_columns: List[Column], distinct: Boolean): String =
wenzelm@63791
   143
      "SELECT " + (if (distinct) "DISTINCT " else "") +
wenzelm@63791
   144
      commas(select_columns.map(_.sql_name)) + " FROM " + quote_ident(name)
wenzelm@63790
   145
wenzelm@63783
   146
    override def toString: String =
wenzelm@63791
   147
      "TABLE " + quote_ident(name) + " " + enclosure(columns.map(_.toString))
wenzelm@63780
   148
  }
wenzelm@63790
   149
wenzelm@63790
   150
wenzelm@65012
   151
wenzelm@65012
   152
  /** SQL database operations **/
wenzelm@65012
   153
wenzelm@63790
   154
  /* results */
wenzelm@63790
   155
wenzelm@63790
   156
  def iterator[A](rs: ResultSet)(get: ResultSet => A): Iterator[A] = new Iterator[A]
wenzelm@63790
   157
  {
wenzelm@63790
   158
    private var _next: Boolean = rs.next()
wenzelm@63790
   159
    def hasNext: Boolean = _next
wenzelm@63790
   160
    def next: A = { val x = get(rs); _next = rs.next(); x }
wenzelm@63790
   161
  }
wenzelm@65006
   162
wenzelm@65006
   163
  trait Database
wenzelm@65006
   164
  {
wenzelm@65008
   165
    /* types */
wenzelm@65008
   166
wenzelm@65019
   167
    def sql_type(T: Type.Value): String
wenzelm@65008
   168
wenzelm@65008
   169
wenzelm@65006
   170
    /* connection */
wenzelm@65006
   171
wenzelm@65006
   172
    def connection: Connection
wenzelm@65006
   173
wenzelm@65006
   174
    def close() { connection.close }
wenzelm@65006
   175
wenzelm@65006
   176
    def transaction[A](body: => A): A =
wenzelm@65006
   177
    {
wenzelm@65006
   178
      val auto_commit = connection.getAutoCommit
wenzelm@65006
   179
      try {
wenzelm@65006
   180
        connection.setAutoCommit(false)
wenzelm@65022
   181
        val savepoint = connection.setSavepoint
wenzelm@65022
   182
        try {
wenzelm@65022
   183
          val result = body
wenzelm@65022
   184
          connection.commit
wenzelm@65022
   185
          result
wenzelm@65022
   186
        }
wenzelm@65022
   187
        catch { case exn: Throwable => connection.rollback(savepoint); throw exn }
wenzelm@65006
   188
      }
wenzelm@65006
   189
      finally { connection.setAutoCommit(auto_commit) }
wenzelm@65006
   190
    }
wenzelm@65006
   191
wenzelm@65006
   192
wenzelm@65006
   193
    /* statements */
wenzelm@65006
   194
wenzelm@65006
   195
    def statement(sql: String): PreparedStatement = connection.prepareStatement(sql)
wenzelm@65006
   196
wenzelm@65006
   197
    def insert_statement(table: Table): PreparedStatement = statement(table.sql_insert)
wenzelm@65006
   198
wenzelm@65319
   199
    def delete_statement(table: Table, sql: String = ""): PreparedStatement =
wenzelm@65319
   200
      statement(table.sql_delete + (if (sql == "") "" else " " + sql))
wenzelm@65319
   201
wenzelm@65018
   202
    def select_statement(table: Table, columns: List[Column],
wenzelm@65006
   203
        sql: String = "", distinct: Boolean = false): PreparedStatement =
wenzelm@65006
   204
      statement(table.sql_select(columns, distinct) + (if (sql == "") "" else " " + sql))
wenzelm@65006
   205
wenzelm@65006
   206
wenzelm@65020
   207
    /* input */
wenzelm@65020
   208
wenzelm@65020
   209
    def set_bool(stmt: PreparedStatement, i: Int, x: Boolean) { stmt.setBoolean(i, x) }
wenzelm@65020
   210
    def set_int(stmt: PreparedStatement, i: Int, x: Int) { stmt.setInt(i, x) }
wenzelm@65020
   211
    def set_long(stmt: PreparedStatement, i: Int, x: Long) { stmt.setLong(i, x) }
wenzelm@65020
   212
    def set_double(stmt: PreparedStatement, i: Int, x: Double) { stmt.setDouble(i, x) }
wenzelm@65020
   213
    def set_string(stmt: PreparedStatement, i: Int, x: String) { stmt.setString(i, x) }
wenzelm@65020
   214
    def set_bytes(stmt: PreparedStatement, i: Int, bytes: Bytes)
wenzelm@65020
   215
    { stmt.setBinaryStream(i, bytes.stream(), bytes.length) }
wenzelm@65020
   216
    def set_date(stmt: PreparedStatement, i: Int, date: Date)
wenzelm@65020
   217
wenzelm@65022
   218
wenzelm@65020
   219
    /* output */
wenzelm@65018
   220
wenzelm@65018
   221
    def bool(rs: ResultSet, name: String): Boolean = rs.getBoolean(name)
wenzelm@65018
   222
    def int(rs: ResultSet, name: String): Int = rs.getInt(name)
wenzelm@65018
   223
    def long(rs: ResultSet, name: String): Long = rs.getLong(name)
wenzelm@65018
   224
    def double(rs: ResultSet, name: String): Double = rs.getDouble(name)
wenzelm@65018
   225
    def string(rs: ResultSet, name: String): String =
wenzelm@65018
   226
    {
wenzelm@65018
   227
      val s = rs.getString(name)
wenzelm@65018
   228
      if (s == null) "" else s
wenzelm@65018
   229
    }
wenzelm@65018
   230
    def bytes(rs: ResultSet, name: String): Bytes =
wenzelm@65018
   231
    {
wenzelm@65018
   232
      val bs = rs.getBytes(name)
wenzelm@65018
   233
      if (bs == null) Bytes.empty else Bytes(bs)
wenzelm@65018
   234
    }
wenzelm@65021
   235
    def date(rs: ResultSet, name: String): Date
wenzelm@65018
   236
wenzelm@65018
   237
    def get[A](rs: ResultSet, name: String, f: (ResultSet, String) => A): Option[A] =
wenzelm@65018
   238
    {
wenzelm@65018
   239
      val x = f(rs, name)
wenzelm@65018
   240
      if (rs.wasNull) None else Some(x)
wenzelm@65018
   241
    }
wenzelm@65018
   242
wenzelm@65018
   243
wenzelm@65006
   244
    /* tables */
wenzelm@65006
   245
wenzelm@65006
   246
    def tables: List[String] =
wenzelm@65006
   247
      iterator(connection.getMetaData.getTables(null, null, "%", null))(_.getString(3)).toList
wenzelm@65006
   248
wenzelm@65280
   249
    def create_table(table: Table, strict: Boolean = false, rowid: Boolean = true): Unit =
wenzelm@65019
   250
      using(statement(table.sql_create(strict, rowid, sql_type)))(_.execute())
wenzelm@65006
   251
wenzelm@65280
   252
    def drop_table(table: Table, strict: Boolean = false): Unit =
wenzelm@65006
   253
      using(statement(table.sql_drop(strict)))(_.execute())
wenzelm@65006
   254
wenzelm@65018
   255
    def create_index(table: Table, name: String, columns: List[Column],
wenzelm@65280
   256
        strict: Boolean = false, unique: Boolean = false): Unit =
wenzelm@65006
   257
      using(statement(table.sql_create_index(name, columns, strict, unique)))(_.execute())
wenzelm@65006
   258
wenzelm@65280
   259
    def drop_index(table: Table, name: String, strict: Boolean = false): Unit =
wenzelm@65006
   260
      using(statement(table.sql_drop_index(name, strict)))(_.execute())
wenzelm@65006
   261
  }
wenzelm@63778
   262
}
wenzelm@65006
   263
wenzelm@65006
   264
wenzelm@65006
   265
wenzelm@65006
   266
/** SQLite **/
wenzelm@65006
   267
wenzelm@65006
   268
object SQLite
wenzelm@65006
   269
{
wenzelm@65021
   270
  // see https://www.sqlite.org/lang_datefunc.html
wenzelm@65021
   271
  val date_format: Date.Format = Date.Format("uuuu-MM-dd HH:mm:ss.SSS x")
wenzelm@65021
   272
wenzelm@65292
   273
  lazy val init_jdbc: Unit = Class.forName("org.sqlite.JDBC")
wenzelm@65292
   274
wenzelm@65006
   275
  def open_database(path: Path): Database =
wenzelm@65006
   276
  {
wenzelm@65292
   277
    init_jdbc
wenzelm@65006
   278
    val path0 = path.expand
wenzelm@65006
   279
    val s0 = File.platform_path(path0)
wenzelm@65006
   280
    val s1 = if (Platform.is_windows) s0.replace('\\', '/') else s0
wenzelm@65006
   281
    val connection = DriverManager.getConnection("jdbc:sqlite:" + s1)
wenzelm@65007
   282
    new Database(path0.toString, connection)
wenzelm@65006
   283
  }
wenzelm@65006
   284
wenzelm@65007
   285
  class Database private[SQLite](name: String, val connection: Connection) extends SQL.Database
wenzelm@65006
   286
  {
wenzelm@65007
   287
    override def toString: String = name
wenzelm@65006
   288
wenzelm@65019
   289
    def sql_type(T: SQL.Type.Value): String = SQL.sql_type_sqlite(T)
wenzelm@65011
   290
wenzelm@65021
   291
    def set_date(stmt: PreparedStatement, i: Int, date: Date): Unit =
wenzelm@65021
   292
      set_string(stmt, i, date_format(date))
wenzelm@65021
   293
    def date(rs: ResultSet, name: String): Date =
wenzelm@65021
   294
      date_format.parse(string(rs, name))
wenzelm@65021
   295
wenzelm@65006
   296
    def rebuild { using(statement("VACUUM"))(_.execute()) }
wenzelm@65006
   297
  }
wenzelm@65006
   298
}
wenzelm@65006
   299
wenzelm@65006
   300
wenzelm@65006
   301
wenzelm@65006
   302
/** PostgreSQL **/
wenzelm@65006
   303
wenzelm@65006
   304
object PostgreSQL
wenzelm@65006
   305
{
wenzelm@65006
   306
  val default_port = 5432
wenzelm@65006
   307
wenzelm@65292
   308
  lazy val init_jdbc: Unit = Class.forName("org.postgresql.Driver")
wenzelm@65292
   309
wenzelm@65006
   310
  def open_database(
wenzelm@65006
   311
    user: String,
wenzelm@65006
   312
    password: String,
wenzelm@65006
   313
    database: String = "",
wenzelm@65006
   314
    host: String = "",
wenzelm@65009
   315
    port: Int = default_port,
wenzelm@65009
   316
    ssh: Option[SSH.Session] = None): Database =
wenzelm@65006
   317
  {
wenzelm@65292
   318
    init_jdbc
wenzelm@65292
   319
wenzelm@65006
   320
    require(user != "")
wenzelm@65009
   321
wenzelm@65009
   322
    val db_host = if (host != "") host else "localhost"
wenzelm@65009
   323
    val db_port = if (port != default_port) ":" + port else ""
wenzelm@65009
   324
    val db_name = "/" + (if (database != "") database else user)
wenzelm@65009
   325
wenzelm@65010
   326
    val (url, name, port_forwarding) =
wenzelm@65009
   327
      ssh match {
wenzelm@65010
   328
        case None =>
wenzelm@65010
   329
          val spec = db_host + db_port + db_name
wenzelm@65010
   330
          val url = "jdbc:postgresql://" + spec
wenzelm@65010
   331
          val name = user + "@" + spec
wenzelm@65010
   332
          (url, name, None)
wenzelm@65009
   333
        case Some(ssh) =>
wenzelm@65009
   334
          val fw = ssh.port_forwarding(remote_host = db_host, remote_port = port)
wenzelm@65010
   335
          val url = "jdbc:postgresql://localhost:" + fw.local_port + db_name
wenzelm@65010
   336
          val name = user + "@" + fw + db_name + " via ssh " + ssh
wenzelm@65010
   337
          (url, name, Some(fw))
wenzelm@65009
   338
      }
wenzelm@65009
   339
    try {
wenzelm@65010
   340
      val connection = DriverManager.getConnection(url, user, password)
wenzelm@65010
   341
      new Database(name, connection, port_forwarding)
wenzelm@65009
   342
    }
wenzelm@65009
   343
    catch { case exn: Throwable => port_forwarding.foreach(_.close); throw exn }
wenzelm@65006
   344
  }
wenzelm@65006
   345
wenzelm@65009
   346
  class Database private[PostgreSQL](
wenzelm@65009
   347
      name: String, val connection: Connection, port_forwarding: Option[SSH.Port_Forwarding])
wenzelm@65009
   348
    extends SQL.Database
wenzelm@65006
   349
  {
wenzelm@65010
   350
    override def toString: String = name
wenzelm@65008
   351
wenzelm@65019
   352
    def sql_type(T: SQL.Type.Value): String = SQL.sql_type_postgresql(T)
wenzelm@65009
   353
wenzelm@65021
   354
    // see https://jdbc.postgresql.org/documentation/head/8-date-time.html
wenzelm@65021
   355
    def set_date(stmt: PreparedStatement, i: Int, date: Date): Unit =
wenzelm@65021
   356
      stmt.setObject(i, OffsetDateTime.from(date.to_utc.rep))
wenzelm@65021
   357
    def date(rs: ResultSet, name: String): Date =
wenzelm@65021
   358
      Date.instant(rs.getObject(name, classOf[OffsetDateTime]).toInstant)
wenzelm@65021
   359
wenzelm@65009
   360
    override def close() { super.close; port_forwarding.foreach(_.close) }
wenzelm@65006
   361
  }
wenzelm@65006
   362
}