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