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