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