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