src/Pure/General/sql.scala
author wenzelm
Thu Feb 09 22:40:15 2017 +0100 (2017-02-09)
changeset 65014 97a622d01609
parent 65013 86308845aa43
child 65018 8b36f225bbee
permissions -rw-r--r--
support for type Date;
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@63778
     9
wenzelm@65014
    10
import java.sql.{DriverManager, Connection, PreparedStatement, ResultSet, Timestamp}
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@65008
    55
  type Type_Name = Type.Value => String
wenzelm@65013
    56
wenzelm@65013
    57
  def type_name_default(t: Type.Value): String = t.toString
wenzelm@65013
    58
wenzelm@65013
    59
  def type_name_sqlite(t: Type.Value): String =
wenzelm@65013
    60
    if (t == Type.Boolean) "INTEGER"
wenzelm@65014
    61
    else if (t == Type.Date) "TEXT"
wenzelm@65013
    62
    else type_name_default(t)
wenzelm@65013
    63
wenzelm@65013
    64
  def type_name_postgresql(t: Type.Value): String =
wenzelm@65013
    65
    if (t == Type.Bytes) "BYTEA"
wenzelm@65013
    66
    else type_name_default(t)
wenzelm@65008
    67
wenzelm@65008
    68
wenzelm@63779
    69
  /* columns */
wenzelm@63779
    70
wenzelm@63779
    71
  object Column
wenzelm@63779
    72
  {
wenzelm@65011
    73
    def bool(name: String, strict: Boolean = true, primary_key: Boolean = false): Column[Boolean] =
wenzelm@65011
    74
      new Column_Boolean(name, strict, primary_key)
wenzelm@63781
    75
    def int(name: String, strict: Boolean = true, primary_key: Boolean = false): Column[Int] =
wenzelm@63781
    76
      new Column_Int(name, strict, primary_key)
wenzelm@63781
    77
    def long(name: String, strict: Boolean = true, primary_key: Boolean = false): Column[Long] =
wenzelm@63781
    78
      new Column_Long(name, strict, primary_key)
wenzelm@63781
    79
    def double(name: String, strict: Boolean = true, primary_key: Boolean = false): Column[Double] =
wenzelm@63781
    80
      new Column_Double(name, strict, primary_key)
wenzelm@63781
    81
    def string(name: String, strict: Boolean = true, primary_key: Boolean = false): Column[String] =
wenzelm@63781
    82
      new Column_String(name, strict, primary_key)
wenzelm@63781
    83
    def bytes(name: String, strict: Boolean = true, primary_key: Boolean = false): Column[Bytes] =
wenzelm@65008
    84
      new Column_Bytes(name, strict, primary_key)
wenzelm@65014
    85
    def date(name: String, strict: Boolean = true, primary_key: Boolean = false): Column[Date] =
wenzelm@65014
    86
      new Column_Date(name, strict, primary_key)
wenzelm@63779
    87
  }
wenzelm@63779
    88
wenzelm@63781
    89
  abstract class Column[+A] private[SQL](
wenzelm@65008
    90
      val name: String,
wenzelm@65008
    91
      val strict: Boolean,
wenzelm@65008
    92
      val primary_key: Boolean,
wenzelm@65008
    93
      val sql_type: Type.Value) extends Function[ResultSet, A]
wenzelm@63779
    94
  {
wenzelm@63779
    95
    def sql_name: String = quote_ident(name)
wenzelm@65008
    96
    def sql_decl(type_name: Type_Name): String =
wenzelm@65008
    97
      sql_name + " " + type_name(sql_type) +
wenzelm@63781
    98
      (if (strict) " NOT NULL" else "") +
wenzelm@63781
    99
      (if (primary_key) " PRIMARY KEY" else "")
wenzelm@63781
   100
wenzelm@63780
   101
    def string(rs: ResultSet): String =
wenzelm@63780
   102
    {
wenzelm@63780
   103
      val s = rs.getString(name)
wenzelm@63780
   104
      if (s == null) "" else s
wenzelm@63780
   105
    }
wenzelm@63780
   106
    def apply(rs: ResultSet): A
wenzelm@63780
   107
    def get(rs: ResultSet): Option[A] =
wenzelm@63780
   108
    {
wenzelm@63780
   109
      val x = apply(rs)
wenzelm@63780
   110
      if (rs.wasNull) None else Some(x)
wenzelm@63780
   111
    }
wenzelm@63779
   112
wenzelm@65013
   113
    override def toString: String = sql_decl(type_name_default)
wenzelm@63779
   114
  }
wenzelm@63779
   115
wenzelm@65011
   116
  class Column_Boolean private[SQL](name: String, strict: Boolean, primary_key: Boolean)
wenzelm@65011
   117
    extends Column[Boolean](name, strict, primary_key, Type.Boolean)
wenzelm@65011
   118
  {
wenzelm@65011
   119
    def apply(rs: ResultSet): Boolean = rs.getBoolean(name)
wenzelm@65011
   120
  }
wenzelm@65011
   121
wenzelm@63781
   122
  class Column_Int private[SQL](name: String, strict: Boolean, primary_key: Boolean)
wenzelm@65008
   123
    extends Column[Int](name, strict, primary_key, Type.Int)
wenzelm@63779
   124
  {
wenzelm@63780
   125
    def apply(rs: ResultSet): Int = rs.getInt(name)
wenzelm@63779
   126
  }
wenzelm@63779
   127
wenzelm@63781
   128
  class Column_Long private[SQL](name: String, strict: Boolean, primary_key: Boolean)
wenzelm@65008
   129
    extends Column[Long](name, strict, primary_key, Type.Long)
wenzelm@63779
   130
  {
wenzelm@63780
   131
    def apply(rs: ResultSet): Long = rs.getLong(name)
wenzelm@63779
   132
  }
wenzelm@63779
   133
wenzelm@63781
   134
  class Column_Double private[SQL](name: String, strict: Boolean, primary_key: Boolean)
wenzelm@65008
   135
    extends Column[Double](name, strict, primary_key, Type.Double)
wenzelm@63779
   136
  {
wenzelm@63780
   137
    def apply(rs: ResultSet): Double = rs.getDouble(name)
wenzelm@63779
   138
  }
wenzelm@63779
   139
wenzelm@63781
   140
  class Column_String private[SQL](name: String, strict: Boolean, primary_key: Boolean)
wenzelm@65008
   141
    extends Column[String](name, strict, primary_key, Type.String)
wenzelm@63779
   142
  {
wenzelm@63780
   143
    def apply(rs: ResultSet): String =
wenzelm@63780
   144
    {
wenzelm@63780
   145
      val s = rs.getString(name)
wenzelm@63780
   146
      if (s == null) "" else s
wenzelm@63780
   147
    }
wenzelm@63779
   148
  }
wenzelm@63779
   149
wenzelm@65008
   150
  class Column_Bytes private[SQL](name: String, strict: Boolean, primary_key: Boolean)
wenzelm@65008
   151
    extends Column[Bytes](name, strict, primary_key, Type.Bytes)
wenzelm@63779
   152
  {
wenzelm@63780
   153
    def apply(rs: ResultSet): Bytes =
wenzelm@63779
   154
    {
wenzelm@63779
   155
      val bs = rs.getBytes(name)
wenzelm@63780
   156
      if (bs == null) Bytes.empty else Bytes(bs)
wenzelm@63779
   157
    }
wenzelm@63779
   158
  }
wenzelm@63780
   159
wenzelm@65014
   160
  class Column_Date private[SQL](name: String, strict: Boolean, primary_key: Boolean)
wenzelm@65014
   161
    extends Column[Date](name, strict, primary_key, Type.Date)
wenzelm@65014
   162
  {
wenzelm@65014
   163
    def apply(rs: ResultSet): Date =
wenzelm@65014
   164
    {
wenzelm@65014
   165
      Date.instant(rs.getTimestamp(name).toInstant)
wenzelm@65014
   166
    }
wenzelm@65014
   167
  }
wenzelm@65014
   168
wenzelm@63780
   169
wenzelm@63780
   170
  /* tables */
wenzelm@63780
   171
wenzelm@63791
   172
  def table(name: String, columns: List[Column[Any]]): Table = new Table(name, columns)
wenzelm@63783
   173
wenzelm@63783
   174
  class Table private[SQL](name: String, columns: List[Column[Any]])
wenzelm@63780
   175
  {
wenzelm@63790
   176
    private val columns_index: Map[String, Int] =
wenzelm@63790
   177
      columns.iterator.map(_.name).zipWithIndex.toMap
wenzelm@63790
   178
wenzelm@63781
   179
    Library.duplicates(columns.map(_.name)) match {
wenzelm@63781
   180
      case Nil =>
wenzelm@63781
   181
      case bad => error("Duplicate column names " + commas_quote(bad) + " for table " + quote(name))
wenzelm@63781
   182
    }
wenzelm@63781
   183
wenzelm@63781
   184
    columns.filter(_.primary_key) match {
wenzelm@63781
   185
      case bad if bad.length > 1 =>
wenzelm@63781
   186
        error("Multiple primary keys " + commas_quote(bad.map(_.name)) + " for table " + quote(name))
wenzelm@63781
   187
      case _ =>
wenzelm@63781
   188
    }
wenzelm@63781
   189
wenzelm@65008
   190
    def sql_create(strict: Boolean, rowid: Boolean, type_name: Type_Name): String =
wenzelm@63784
   191
      "CREATE TABLE " + (if (strict) "" else "IF NOT EXISTS ") +
wenzelm@65008
   192
        quote_ident(name) + " " + enclosure(columns.map(_.sql_decl(type_name))) +
wenzelm@63780
   193
        (if (rowid) "" else " WITHOUT ROWID")
wenzelm@63780
   194
wenzelm@63780
   195
    def sql_drop(strict: Boolean): String =
wenzelm@63784
   196
      "DROP TABLE " + (if (strict) "" else "IF EXISTS ") + quote_ident(name)
wenzelm@63783
   197
wenzelm@63791
   198
    def sql_create_index(
wenzelm@63791
   199
        index_name: String, index_columns: List[Column[Any]],
wenzelm@63791
   200
        strict: Boolean, unique: Boolean): String =
wenzelm@63791
   201
      "CREATE " + (if (unique) "UNIQUE " else "") + "INDEX " +
wenzelm@63791
   202
        (if (strict) "" else "IF NOT EXISTS ") + quote_ident(index_name) + " ON " +
wenzelm@63791
   203
        quote_ident(name) + " " + enclosure(index_columns.map(_.name))
wenzelm@63791
   204
wenzelm@63791
   205
    def sql_drop_index(index_name: String, strict: Boolean): String =
wenzelm@63791
   206
      "DROP INDEX " + (if (strict) "" else "IF EXISTS ") + quote_ident(index_name)
wenzelm@63791
   207
wenzelm@63790
   208
    def sql_insert: String =
wenzelm@63791
   209
      "INSERT INTO " + quote_ident(name) + " VALUES " + enclosure(columns.map(_ => "?"))
wenzelm@63791
   210
wenzelm@63791
   211
    def sql_select(select_columns: List[Column[Any]], distinct: Boolean): String =
wenzelm@63791
   212
      "SELECT " + (if (distinct) "DISTINCT " else "") +
wenzelm@63791
   213
      commas(select_columns.map(_.sql_name)) + " FROM " + quote_ident(name)
wenzelm@63790
   214
wenzelm@63783
   215
    override def toString: String =
wenzelm@63791
   216
      "TABLE " + quote_ident(name) + " " + enclosure(columns.map(_.toString))
wenzelm@63780
   217
  }
wenzelm@63790
   218
wenzelm@63790
   219
wenzelm@65012
   220
wenzelm@65012
   221
  /** SQL database operations **/
wenzelm@65012
   222
wenzelm@63790
   223
  /* results */
wenzelm@63790
   224
wenzelm@63790
   225
  def iterator[A](rs: ResultSet)(get: ResultSet => A): Iterator[A] = new Iterator[A]
wenzelm@63790
   226
  {
wenzelm@63790
   227
    private var _next: Boolean = rs.next()
wenzelm@63790
   228
    def hasNext: Boolean = _next
wenzelm@63790
   229
    def next: A = { val x = get(rs); _next = rs.next(); x }
wenzelm@63790
   230
  }
wenzelm@65006
   231
wenzelm@65006
   232
  trait Database
wenzelm@65006
   233
  {
wenzelm@65008
   234
    /* types */
wenzelm@65008
   235
wenzelm@65011
   236
    def type_name(t: Type.Value): String
wenzelm@65008
   237
wenzelm@65008
   238
wenzelm@65006
   239
    /* connection */
wenzelm@65006
   240
wenzelm@65006
   241
    def connection: Connection
wenzelm@65006
   242
wenzelm@65006
   243
    def close() { connection.close }
wenzelm@65006
   244
wenzelm@65006
   245
    def transaction[A](body: => A): A =
wenzelm@65006
   246
    {
wenzelm@65006
   247
      val auto_commit = connection.getAutoCommit
wenzelm@65006
   248
      val savepoint = connection.setSavepoint
wenzelm@65006
   249
wenzelm@65006
   250
      try {
wenzelm@65006
   251
        connection.setAutoCommit(false)
wenzelm@65006
   252
        val result = body
wenzelm@65006
   253
        connection.commit
wenzelm@65006
   254
        result
wenzelm@65006
   255
      }
wenzelm@65006
   256
      catch { case exn: Throwable => connection.rollback(savepoint); throw exn }
wenzelm@65006
   257
      finally { connection.setAutoCommit(auto_commit) }
wenzelm@65006
   258
    }
wenzelm@65006
   259
wenzelm@65006
   260
wenzelm@65006
   261
    /* statements */
wenzelm@65006
   262
wenzelm@65006
   263
    def statement(sql: String): PreparedStatement = connection.prepareStatement(sql)
wenzelm@65006
   264
wenzelm@65006
   265
    def insert_statement(table: Table): PreparedStatement = statement(table.sql_insert)
wenzelm@65006
   266
wenzelm@65006
   267
    def select_statement(table: Table, columns: List[Column[Any]],
wenzelm@65006
   268
        sql: String = "", distinct: Boolean = false): PreparedStatement =
wenzelm@65006
   269
      statement(table.sql_select(columns, distinct) + (if (sql == "") "" else " " + sql))
wenzelm@65006
   270
wenzelm@65006
   271
wenzelm@65006
   272
    /* tables */
wenzelm@65006
   273
wenzelm@65006
   274
    def tables: List[String] =
wenzelm@65006
   275
      iterator(connection.getMetaData.getTables(null, null, "%", null))(_.getString(3)).toList
wenzelm@65006
   276
wenzelm@65006
   277
    def create_table(table: Table, strict: Boolean = true, rowid: Boolean = true): Unit =
wenzelm@65008
   278
      using(statement(table.sql_create(strict, rowid, type_name)))(_.execute())
wenzelm@65006
   279
wenzelm@65006
   280
    def drop_table(table: Table, strict: Boolean = true): Unit =
wenzelm@65006
   281
      using(statement(table.sql_drop(strict)))(_.execute())
wenzelm@65006
   282
wenzelm@65006
   283
    def create_index(table: Table, name: String, columns: List[Column[Any]],
wenzelm@65006
   284
        strict: Boolean = true, unique: Boolean = false): Unit =
wenzelm@65006
   285
      using(statement(table.sql_create_index(name, columns, strict, unique)))(_.execute())
wenzelm@65006
   286
wenzelm@65006
   287
    def drop_index(table: Table, name: String, strict: Boolean = true): Unit =
wenzelm@65006
   288
      using(statement(table.sql_drop_index(name, strict)))(_.execute())
wenzelm@65006
   289
  }
wenzelm@63778
   290
}
wenzelm@65006
   291
wenzelm@65006
   292
wenzelm@65006
   293
wenzelm@65006
   294
/** SQLite **/
wenzelm@65006
   295
wenzelm@65006
   296
object SQLite
wenzelm@65006
   297
{
wenzelm@65006
   298
  def open_database(path: Path): Database =
wenzelm@65006
   299
  {
wenzelm@65006
   300
    val path0 = path.expand
wenzelm@65006
   301
    val s0 = File.platform_path(path0)
wenzelm@65006
   302
    val s1 = if (Platform.is_windows) s0.replace('\\', '/') else s0
wenzelm@65006
   303
    val connection = DriverManager.getConnection("jdbc:sqlite:" + s1)
wenzelm@65007
   304
    new Database(path0.toString, connection)
wenzelm@65006
   305
  }
wenzelm@65006
   306
wenzelm@65007
   307
  class Database private[SQLite](name: String, val connection: Connection) extends SQL.Database
wenzelm@65006
   308
  {
wenzelm@65007
   309
    override def toString: String = name
wenzelm@65006
   310
wenzelm@65013
   311
    def type_name(t: SQL.Type.Value): String = SQL.type_name_sqlite(t)
wenzelm@65011
   312
wenzelm@65006
   313
    def rebuild { using(statement("VACUUM"))(_.execute()) }
wenzelm@65006
   314
  }
wenzelm@65006
   315
}
wenzelm@65006
   316
wenzelm@65006
   317
wenzelm@65006
   318
wenzelm@65006
   319
/** PostgreSQL **/
wenzelm@65006
   320
wenzelm@65006
   321
object PostgreSQL
wenzelm@65006
   322
{
wenzelm@65006
   323
  val default_port = 5432
wenzelm@65006
   324
wenzelm@65006
   325
  def open_database(
wenzelm@65006
   326
    user: String,
wenzelm@65006
   327
    password: String,
wenzelm@65006
   328
    database: String = "",
wenzelm@65006
   329
    host: String = "",
wenzelm@65009
   330
    port: Int = default_port,
wenzelm@65009
   331
    ssh: Option[SSH.Session] = None): Database =
wenzelm@65006
   332
  {
wenzelm@65006
   333
    require(user != "")
wenzelm@65009
   334
wenzelm@65009
   335
    val db_host = if (host != "") host else "localhost"
wenzelm@65009
   336
    val db_port = if (port != default_port) ":" + port else ""
wenzelm@65009
   337
    val db_name = "/" + (if (database != "") database else user)
wenzelm@65009
   338
wenzelm@65010
   339
    val (url, name, port_forwarding) =
wenzelm@65009
   340
      ssh match {
wenzelm@65010
   341
        case None =>
wenzelm@65010
   342
          val spec = db_host + db_port + db_name
wenzelm@65010
   343
          val url = "jdbc:postgresql://" + spec
wenzelm@65010
   344
          val name = user + "@" + spec
wenzelm@65010
   345
          (url, name, None)
wenzelm@65009
   346
        case Some(ssh) =>
wenzelm@65009
   347
          val fw = ssh.port_forwarding(remote_host = db_host, remote_port = port)
wenzelm@65010
   348
          val url = "jdbc:postgresql://localhost:" + fw.local_port + db_name
wenzelm@65010
   349
          val name = user + "@" + fw + db_name + " via ssh " + ssh
wenzelm@65010
   350
          (url, name, Some(fw))
wenzelm@65009
   351
      }
wenzelm@65009
   352
    try {
wenzelm@65010
   353
      val connection = DriverManager.getConnection(url, user, password)
wenzelm@65010
   354
      new Database(name, connection, port_forwarding)
wenzelm@65009
   355
    }
wenzelm@65009
   356
    catch { case exn: Throwable => port_forwarding.foreach(_.close); throw exn }
wenzelm@65006
   357
  }
wenzelm@65006
   358
wenzelm@65009
   359
  class Database private[PostgreSQL](
wenzelm@65009
   360
      name: String, val connection: Connection, port_forwarding: Option[SSH.Port_Forwarding])
wenzelm@65009
   361
    extends SQL.Database
wenzelm@65006
   362
  {
wenzelm@65010
   363
    override def toString: String = name
wenzelm@65008
   364
wenzelm@65013
   365
    def type_name(t: SQL.Type.Value): String = SQL.type_name_postgresql(t)
wenzelm@65009
   366
wenzelm@65009
   367
    override def close() { super.close; port_forwarding.foreach(_.close) }
wenzelm@65006
   368
  }
wenzelm@65006
   369
}