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