src/Pure/General/sql.scala
author wenzelm
Thu Feb 09 10:59:19 2017 +0100 (2017-02-09)
changeset 65006 632bdf7b8bab
parent 65004 fd4d1395fa17
child 65007 b6a1a1d42f5d
permissions -rw-r--r--
clarified modules;
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@65006
    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@63779
    42
  /* columns */
wenzelm@63779
    43
wenzelm@63779
    44
  object Column
wenzelm@63779
    45
  {
wenzelm@63781
    46
    def int(name: String, strict: Boolean = true, primary_key: Boolean = false): Column[Int] =
wenzelm@63781
    47
      new Column_Int(name, strict, primary_key)
wenzelm@63781
    48
    def long(name: String, strict: Boolean = true, primary_key: Boolean = false): Column[Long] =
wenzelm@63781
    49
      new Column_Long(name, strict, primary_key)
wenzelm@63781
    50
    def double(name: String, strict: Boolean = true, primary_key: Boolean = false): Column[Double] =
wenzelm@63781
    51
      new Column_Double(name, strict, primary_key)
wenzelm@63781
    52
    def string(name: String, strict: Boolean = true, primary_key: Boolean = false): Column[String] =
wenzelm@63781
    53
      new Column_String(name, strict, primary_key)
wenzelm@63781
    54
    def bytes(name: String, strict: Boolean = true, primary_key: Boolean = false): Column[Bytes] =
wenzelm@65004
    55
      new Column_Bytes(name, strict, primary_key, "BLOB")  // SQL standard
wenzelm@65004
    56
    def bytea(name: String, strict: Boolean = true, primary_key: Boolean = false): Column[Bytes] =
wenzelm@65004
    57
      new Column_Bytes(name, strict, primary_key, "BYTEA")  // PostgreSQL
wenzelm@63779
    58
  }
wenzelm@63779
    59
wenzelm@63781
    60
  abstract class Column[+A] private[SQL](
wenzelm@63790
    61
      val name: String, val strict: Boolean, val primary_key: Boolean)
wenzelm@63790
    62
    extends Function[ResultSet, A]
wenzelm@63779
    63
  {
wenzelm@63779
    64
    def sql_name: String = quote_ident(name)
wenzelm@63779
    65
    def sql_type: String
wenzelm@63781
    66
    def sql_decl: String =
wenzelm@63781
    67
      sql_name + " " + sql_type +
wenzelm@63781
    68
      (if (strict) " NOT NULL" else "") +
wenzelm@63781
    69
      (if (primary_key) " PRIMARY KEY" else "")
wenzelm@63781
    70
wenzelm@63780
    71
    def string(rs: ResultSet): String =
wenzelm@63780
    72
    {
wenzelm@63780
    73
      val s = rs.getString(name)
wenzelm@63780
    74
      if (s == null) "" else s
wenzelm@63780
    75
    }
wenzelm@63780
    76
    def apply(rs: ResultSet): A
wenzelm@63780
    77
    def get(rs: ResultSet): Option[A] =
wenzelm@63780
    78
    {
wenzelm@63780
    79
      val x = apply(rs)
wenzelm@63780
    80
      if (rs.wasNull) None else Some(x)
wenzelm@63780
    81
    }
wenzelm@63779
    82
wenzelm@63779
    83
    override def toString: String = sql_decl
wenzelm@63779
    84
  }
wenzelm@63779
    85
wenzelm@63781
    86
  class Column_Int private[SQL](name: String, strict: Boolean, primary_key: Boolean)
wenzelm@63781
    87
    extends Column[Int](name, strict, primary_key)
wenzelm@63779
    88
  {
wenzelm@63779
    89
    def sql_type: String = "INTEGER"
wenzelm@63780
    90
    def apply(rs: ResultSet): Int = rs.getInt(name)
wenzelm@63779
    91
  }
wenzelm@63779
    92
wenzelm@63781
    93
  class Column_Long private[SQL](name: String, strict: Boolean, primary_key: Boolean)
wenzelm@63781
    94
    extends Column[Long](name, strict, primary_key)
wenzelm@63779
    95
  {
wenzelm@65004
    96
    def sql_type: String = "BIGINT"
wenzelm@63780
    97
    def apply(rs: ResultSet): Long = rs.getLong(name)
wenzelm@63779
    98
  }
wenzelm@63779
    99
wenzelm@63781
   100
  class Column_Double private[SQL](name: String, strict: Boolean, primary_key: Boolean)
wenzelm@63781
   101
    extends Column[Double](name, strict, primary_key)
wenzelm@63779
   102
  {
wenzelm@65004
   103
    def sql_type: String = "DOUBLE PRECISION"
wenzelm@63780
   104
    def apply(rs: ResultSet): Double = rs.getDouble(name)
wenzelm@63779
   105
  }
wenzelm@63779
   106
wenzelm@63781
   107
  class Column_String private[SQL](name: String, strict: Boolean, primary_key: Boolean)
wenzelm@63781
   108
    extends Column[String](name, strict, primary_key)
wenzelm@63779
   109
  {
wenzelm@63779
   110
    def sql_type: String = "TEXT"
wenzelm@63780
   111
    def apply(rs: ResultSet): String =
wenzelm@63780
   112
    {
wenzelm@63780
   113
      val s = rs.getString(name)
wenzelm@63780
   114
      if (s == null) "" else s
wenzelm@63780
   115
    }
wenzelm@63779
   116
  }
wenzelm@63779
   117
wenzelm@65004
   118
  class Column_Bytes private[SQL](
wenzelm@65004
   119
    name: String, strict: Boolean, primary_key: Boolean, val sql_type: String)
wenzelm@63781
   120
    extends Column[Bytes](name, strict, primary_key)
wenzelm@63779
   121
  {
wenzelm@63780
   122
    def apply(rs: ResultSet): Bytes =
wenzelm@63779
   123
    {
wenzelm@63779
   124
      val bs = rs.getBytes(name)
wenzelm@63780
   125
      if (bs == null) Bytes.empty else Bytes(bs)
wenzelm@63779
   126
    }
wenzelm@63779
   127
  }
wenzelm@63780
   128
wenzelm@63780
   129
wenzelm@63780
   130
  /* tables */
wenzelm@63780
   131
wenzelm@63791
   132
  def table(name: String, columns: List[Column[Any]]): Table = new Table(name, columns)
wenzelm@63783
   133
wenzelm@63783
   134
  class Table private[SQL](name: String, columns: List[Column[Any]])
wenzelm@63780
   135
  {
wenzelm@63790
   136
    private val columns_index: Map[String, Int] =
wenzelm@63790
   137
      columns.iterator.map(_.name).zipWithIndex.toMap
wenzelm@63790
   138
wenzelm@63781
   139
    Library.duplicates(columns.map(_.name)) match {
wenzelm@63781
   140
      case Nil =>
wenzelm@63781
   141
      case bad => error("Duplicate column names " + commas_quote(bad) + " for table " + quote(name))
wenzelm@63781
   142
    }
wenzelm@63781
   143
wenzelm@63781
   144
    columns.filter(_.primary_key) match {
wenzelm@63781
   145
      case bad if bad.length > 1 =>
wenzelm@63781
   146
        error("Multiple primary keys " + commas_quote(bad.map(_.name)) + " for table " + quote(name))
wenzelm@63781
   147
      case _ =>
wenzelm@63781
   148
    }
wenzelm@63781
   149
wenzelm@63780
   150
    def sql_create(strict: Boolean, rowid: Boolean): String =
wenzelm@63784
   151
      "CREATE TABLE " + (if (strict) "" else "IF NOT EXISTS ") +
wenzelm@63791
   152
        quote_ident(name) + " " + enclosure(columns.map(_.sql_decl)) +
wenzelm@63780
   153
        (if (rowid) "" else " WITHOUT ROWID")
wenzelm@63780
   154
wenzelm@63780
   155
    def sql_drop(strict: Boolean): String =
wenzelm@63784
   156
      "DROP TABLE " + (if (strict) "" else "IF EXISTS ") + quote_ident(name)
wenzelm@63783
   157
wenzelm@63791
   158
    def sql_create_index(
wenzelm@63791
   159
        index_name: String, index_columns: List[Column[Any]],
wenzelm@63791
   160
        strict: Boolean, unique: Boolean): String =
wenzelm@63791
   161
      "CREATE " + (if (unique) "UNIQUE " else "") + "INDEX " +
wenzelm@63791
   162
        (if (strict) "" else "IF NOT EXISTS ") + quote_ident(index_name) + " ON " +
wenzelm@63791
   163
        quote_ident(name) + " " + enclosure(index_columns.map(_.name))
wenzelm@63791
   164
wenzelm@63791
   165
    def sql_drop_index(index_name: String, strict: Boolean): String =
wenzelm@63791
   166
      "DROP INDEX " + (if (strict) "" else "IF EXISTS ") + quote_ident(index_name)
wenzelm@63791
   167
wenzelm@63790
   168
    def sql_insert: String =
wenzelm@63791
   169
      "INSERT INTO " + quote_ident(name) + " VALUES " + enclosure(columns.map(_ => "?"))
wenzelm@63791
   170
wenzelm@63791
   171
    def sql_select(select_columns: List[Column[Any]], distinct: Boolean): String =
wenzelm@63791
   172
      "SELECT " + (if (distinct) "DISTINCT " else "") +
wenzelm@63791
   173
      commas(select_columns.map(_.sql_name)) + " FROM " + quote_ident(name)
wenzelm@63790
   174
wenzelm@63783
   175
    override def toString: String =
wenzelm@63791
   176
      "TABLE " + quote_ident(name) + " " + enclosure(columns.map(_.toString))
wenzelm@63780
   177
  }
wenzelm@63790
   178
wenzelm@63790
   179
wenzelm@63790
   180
  /* results */
wenzelm@63790
   181
wenzelm@63790
   182
  def iterator[A](rs: ResultSet)(get: ResultSet => A): Iterator[A] = new Iterator[A]
wenzelm@63790
   183
  {
wenzelm@63790
   184
    private var _next: Boolean = rs.next()
wenzelm@63790
   185
    def hasNext: Boolean = _next
wenzelm@63790
   186
    def next: A = { val x = get(rs); _next = rs.next(); x }
wenzelm@63790
   187
  }
wenzelm@65006
   188
wenzelm@65006
   189
wenzelm@65006
   190
wenzelm@65006
   191
  /** SQL database operations **/
wenzelm@65006
   192
wenzelm@65006
   193
  trait Database
wenzelm@65006
   194
  {
wenzelm@65006
   195
    /* connection */
wenzelm@65006
   196
wenzelm@65006
   197
    def connection: Connection
wenzelm@65006
   198
wenzelm@65006
   199
    def close() { connection.close }
wenzelm@65006
   200
wenzelm@65006
   201
    def transaction[A](body: => A): A =
wenzelm@65006
   202
    {
wenzelm@65006
   203
      val auto_commit = connection.getAutoCommit
wenzelm@65006
   204
      val savepoint = connection.setSavepoint
wenzelm@65006
   205
wenzelm@65006
   206
      try {
wenzelm@65006
   207
        connection.setAutoCommit(false)
wenzelm@65006
   208
        val result = body
wenzelm@65006
   209
        connection.commit
wenzelm@65006
   210
        result
wenzelm@65006
   211
      }
wenzelm@65006
   212
      catch { case exn: Throwable => connection.rollback(savepoint); throw exn }
wenzelm@65006
   213
      finally { connection.setAutoCommit(auto_commit) }
wenzelm@65006
   214
    }
wenzelm@65006
   215
wenzelm@65006
   216
wenzelm@65006
   217
    /* statements */
wenzelm@65006
   218
wenzelm@65006
   219
    def statement(sql: String): PreparedStatement = connection.prepareStatement(sql)
wenzelm@65006
   220
wenzelm@65006
   221
    def insert_statement(table: Table): PreparedStatement = statement(table.sql_insert)
wenzelm@65006
   222
wenzelm@65006
   223
    def select_statement(table: Table, columns: List[Column[Any]],
wenzelm@65006
   224
        sql: String = "", distinct: Boolean = false): PreparedStatement =
wenzelm@65006
   225
      statement(table.sql_select(columns, distinct) + (if (sql == "") "" else " " + sql))
wenzelm@65006
   226
wenzelm@65006
   227
wenzelm@65006
   228
    /* tables */
wenzelm@65006
   229
wenzelm@65006
   230
    def tables: List[String] =
wenzelm@65006
   231
      iterator(connection.getMetaData.getTables(null, null, "%", null))(_.getString(3)).toList
wenzelm@65006
   232
wenzelm@65006
   233
    def create_table(table: Table, strict: Boolean = true, rowid: Boolean = true): Unit =
wenzelm@65006
   234
      using(statement(table.sql_create(strict, rowid)))(_.execute())
wenzelm@65006
   235
wenzelm@65006
   236
    def drop_table(table: Table, strict: Boolean = true): Unit =
wenzelm@65006
   237
      using(statement(table.sql_drop(strict)))(_.execute())
wenzelm@65006
   238
wenzelm@65006
   239
    def create_index(table: Table, name: String, columns: List[Column[Any]],
wenzelm@65006
   240
        strict: Boolean = true, unique: Boolean = false): Unit =
wenzelm@65006
   241
      using(statement(table.sql_create_index(name, columns, strict, unique)))(_.execute())
wenzelm@65006
   242
wenzelm@65006
   243
    def drop_index(table: Table, name: String, strict: Boolean = true): Unit =
wenzelm@65006
   244
      using(statement(table.sql_drop_index(name, strict)))(_.execute())
wenzelm@65006
   245
  }
wenzelm@63778
   246
}
wenzelm@65006
   247
wenzelm@65006
   248
wenzelm@65006
   249
wenzelm@65006
   250
/** SQLite **/
wenzelm@65006
   251
wenzelm@65006
   252
object SQLite
wenzelm@65006
   253
{
wenzelm@65006
   254
  def open_database(path: Path): Database =
wenzelm@65006
   255
  {
wenzelm@65006
   256
    val path0 = path.expand
wenzelm@65006
   257
    val s0 = File.platform_path(path0)
wenzelm@65006
   258
    val s1 = if (Platform.is_windows) s0.replace('\\', '/') else s0
wenzelm@65006
   259
    val connection = DriverManager.getConnection("jdbc:sqlite:" + s1)
wenzelm@65006
   260
    new Database(path0, connection)
wenzelm@65006
   261
  }
wenzelm@65006
   262
wenzelm@65006
   263
  class Database private[SQLite](path: Path, val connection: Connection) extends SQL.Database
wenzelm@65006
   264
  {
wenzelm@65006
   265
    override def toString: String = path.toString
wenzelm@65006
   266
wenzelm@65006
   267
    def rebuild { using(statement("VACUUM"))(_.execute()) }
wenzelm@65006
   268
  }
wenzelm@65006
   269
}
wenzelm@65006
   270
wenzelm@65006
   271
wenzelm@65006
   272
wenzelm@65006
   273
/** PostgreSQL **/
wenzelm@65006
   274
wenzelm@65006
   275
object PostgreSQL
wenzelm@65006
   276
{
wenzelm@65006
   277
  val default_port = 5432
wenzelm@65006
   278
wenzelm@65006
   279
  def open_database(
wenzelm@65006
   280
    user: String,
wenzelm@65006
   281
    password: String,
wenzelm@65006
   282
    database: String = "",
wenzelm@65006
   283
    host: String = "",
wenzelm@65006
   284
    port: Int = default_port): Database =
wenzelm@65006
   285
  {
wenzelm@65006
   286
    require(user != "")
wenzelm@65006
   287
    val spec =
wenzelm@65006
   288
      (if (host != "") host else "localhost") +
wenzelm@65006
   289
      (if (port != default_port) ":" + port else "") + "/" +
wenzelm@65006
   290
      (if (database != "") database else user)
wenzelm@65006
   291
    val connection = DriverManager.getConnection("jdbc:postgresql://" + spec, user, password)
wenzelm@65006
   292
    new Database(spec, connection)
wenzelm@65006
   293
  }
wenzelm@65006
   294
wenzelm@65006
   295
  class Database private[PostgreSQL](spec: String, val connection: Connection) extends SQL.Database
wenzelm@65006
   296
  {
wenzelm@65006
   297
    override def toString: String = spec
wenzelm@65006
   298
  }
wenzelm@65006
   299
}