src/Pure/General/sql.scala
author wenzelm
Mon, 05 Sep 2016 11:51:25 +0200
changeset 63790 3d723062dc70
parent 63788 3160826b92f8
child 63791 c6cbdfaae19e
permissions -rw-r--r--
more operations; tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
63788
3160826b92f8 clarified modules;
wenzelm
parents: 63784
diff changeset
     1
/*  Title:      Pure/General/sql.scala
63778
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
     3
63779
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
     4
Generic support for SQL.
63778
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
     5
*/
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
     6
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
     7
package isabelle
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
     8
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
     9
63779
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    10
import java.sql.ResultSet
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    11
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    12
63778
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    13
object SQL
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    14
{
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    15
  /* concrete syntax */
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    16
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    17
  def quote_char(c: Char): String =
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    18
    c match {
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    19
      case '\u0000' => "\\0"
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    20
      case '\'' => "\\'"
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    21
      case '\"' => "\\\""
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    22
      case '\b' => "\\b"
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    23
      case '\n' => "\\n"
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    24
      case '\r' => "\\r"
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    25
      case '\t' => "\\t"
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    26
      case '\u001a' => "\\Z"
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    27
      case '\\' => "\\\\"
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    28
      case _ => c.toString
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    29
    }
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    30
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    31
  def quote_string(s: String): String =
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    32
    quote(s.map(quote_char(_)).mkString)
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
    33
63779
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    34
  def quote_ident(s: String): String =
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    35
  {
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    36
    require(!s.contains('`'))
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    37
    "`" + s + "`"
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    38
  }
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    39
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    40
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    41
  /* columns */
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    42
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    43
  object Column
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    44
  {
63781
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
    45
    def int(name: String, strict: Boolean = true, primary_key: Boolean = false): Column[Int] =
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
    46
      new Column_Int(name, strict, primary_key)
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
    47
    def long(name: String, strict: Boolean = true, primary_key: Boolean = false): Column[Long] =
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
    48
      new Column_Long(name, strict, primary_key)
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
    49
    def double(name: String, strict: Boolean = true, primary_key: Boolean = false): Column[Double] =
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
    50
      new Column_Double(name, strict, primary_key)
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
    51
    def string(name: String, strict: Boolean = true, primary_key: Boolean = false): Column[String] =
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
    52
      new Column_String(name, strict, primary_key)
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
    53
    def bytes(name: String, strict: Boolean = true, primary_key: Boolean = false): Column[Bytes] =
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
    54
      new Column_Bytes(name, strict, primary_key)
63779
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    55
  }
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    56
63781
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
    57
  abstract class Column[+A] private[SQL](
63790
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
    58
      val name: String, val strict: Boolean, val primary_key: Boolean)
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
    59
    extends Function[ResultSet, A]
63779
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    60
  {
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    61
    def sql_name: String = quote_ident(name)
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    62
    def sql_type: String
63781
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
    63
    def sql_decl: String =
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
    64
      sql_name + " " + sql_type +
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
    65
      (if (strict) " NOT NULL" else "") +
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
    66
      (if (primary_key) " PRIMARY KEY" else "")
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
    67
63780
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
    68
    def string(rs: ResultSet): String =
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
    69
    {
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
    70
      val s = rs.getString(name)
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
    71
      if (s == null) "" else s
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
    72
    }
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
    73
    def apply(rs: ResultSet): A
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
    74
    def get(rs: ResultSet): Option[A] =
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
    75
    {
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
    76
      val x = apply(rs)
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
    77
      if (rs.wasNull) None else Some(x)
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
    78
    }
63779
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    79
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    80
    override def toString: String = sql_decl
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    81
  }
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    82
63781
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
    83
  class Column_Int private[SQL](name: String, strict: Boolean, primary_key: Boolean)
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
    84
    extends Column[Int](name, strict, primary_key)
63779
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    85
  {
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    86
    def sql_type: String = "INTEGER"
63780
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
    87
    def apply(rs: ResultSet): Int = rs.getInt(name)
63779
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    88
  }
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    89
63781
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
    90
  class Column_Long private[SQL](name: String, strict: Boolean, primary_key: Boolean)
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
    91
    extends Column[Long](name, strict, primary_key)
63779
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    92
  {
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    93
    def sql_type: String = "INTEGER"
63780
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
    94
    def apply(rs: ResultSet): Long = rs.getLong(name)
63779
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    95
  }
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    96
63781
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
    97
  class Column_Double private[SQL](name: String, strict: Boolean, primary_key: Boolean)
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
    98
    extends Column[Double](name, strict, primary_key)
63779
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
    99
  {
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
   100
    def sql_type: String = "REAL"
63780
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   101
    def apply(rs: ResultSet): Double = rs.getDouble(name)
63779
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
   102
  }
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
   103
63781
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   104
  class Column_String private[SQL](name: String, strict: Boolean, primary_key: Boolean)
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   105
    extends Column[String](name, strict, primary_key)
63779
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
   106
  {
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
   107
    def sql_type: String = "TEXT"
63780
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   108
    def apply(rs: ResultSet): String =
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   109
    {
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   110
      val s = rs.getString(name)
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   111
      if (s == null) "" else s
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   112
    }
63779
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
   113
  }
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
   114
63781
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   115
  class Column_Bytes private[SQL](name: String, strict: Boolean, primary_key: Boolean)
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   116
    extends Column[Bytes](name, strict, primary_key)
63779
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
   117
  {
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
   118
    def sql_type: String = "BLOB"
63780
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   119
    def apply(rs: ResultSet): Bytes =
63779
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
   120
    {
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
   121
      val bs = rs.getBytes(name)
63780
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   122
      if (bs == null) Bytes.empty else Bytes(bs)
63779
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
   123
    }
9da65bc75610 more operations;
wenzelm
parents: 63778
diff changeset
   124
  }
63780
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   125
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   126
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   127
  /* tables */
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   128
63783
baa20f3b6cea tuned signature;
wenzelm
parents: 63781
diff changeset
   129
  def table(name: String, columns: Column[Any]*): Table = new Table(name, columns.toList)
baa20f3b6cea tuned signature;
wenzelm
parents: 63781
diff changeset
   130
baa20f3b6cea tuned signature;
wenzelm
parents: 63781
diff changeset
   131
  class Table private[SQL](name: String, columns: List[Column[Any]])
63780
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   132
  {
63790
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   133
    private val columns_index: Map[String, Int] =
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   134
      columns.iterator.map(_.name).zipWithIndex.toMap
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   135
63781
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   136
    Library.duplicates(columns.map(_.name)) match {
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   137
      case Nil =>
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   138
      case bad => error("Duplicate column names " + commas_quote(bad) + " for table " + quote(name))
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   139
    }
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   140
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   141
    columns.filter(_.primary_key) match {
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   142
      case bad if bad.length > 1 =>
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   143
        error("Multiple primary keys " + commas_quote(bad.map(_.name)) + " for table " + quote(name))
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   144
      case _ =>
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   145
    }
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63780
diff changeset
   146
63780
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   147
    def sql_create(strict: Boolean, rowid: Boolean): String =
63784
wenzelm
parents: 63783
diff changeset
   148
      "CREATE TABLE " + (if (strict) "" else "IF NOT EXISTS ") +
63780
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   149
        quote_ident(name) + " " + columns.map(_.sql_decl).mkString("(", ", ", ")") +
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   150
        (if (rowid) "" else " WITHOUT ROWID")
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   151
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   152
    def sql_drop(strict: Boolean): String =
63784
wenzelm
parents: 63783
diff changeset
   153
      "DROP TABLE " + (if (strict) "" else "IF EXISTS ") + quote_ident(name)
63783
baa20f3b6cea tuned signature;
wenzelm
parents: 63781
diff changeset
   154
63790
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   155
    def sql_insert: String =
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   156
      "INSERT INTO " + quote_ident(name) +
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   157
      " VALUES " + columns.map(_ => "?").mkString("(", ", ", ")")
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   158
63783
baa20f3b6cea tuned signature;
wenzelm
parents: 63781
diff changeset
   159
    override def toString: String =
baa20f3b6cea tuned signature;
wenzelm
parents: 63781
diff changeset
   160
      "TABLE " + quote_ident(name) + " " + columns.map(_.toString).mkString("(", ", ", ")")
63780
163244cefb4e more operations;
wenzelm
parents: 63779
diff changeset
   161
  }
63790
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   162
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   163
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   164
  /* results */
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   165
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   166
  def iterator[A](rs: ResultSet)(get: ResultSet => A): Iterator[A] = new Iterator[A]
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   167
  {
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   168
    private var _next: Boolean = rs.next()
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   169
    def hasNext: Boolean = _next
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   170
    def next: A = { val x = get(rs); _next = rs.next(); x }
3d723062dc70 more operations;
wenzelm
parents: 63788
diff changeset
   171
  }
63778
e06e899b78d0 clarified modules;
wenzelm
parents:
diff changeset
   172
}