author | wenzelm |
Wed, 08 Feb 2017 23:19:10 +0100 | |
changeset 65004 | fd4d1395fa17 |
parent 65003 | 4b4ccf86755c |
child 65006 | 632bdf7b8bab |
permissions | -rw-r--r-- |
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 = |
65003
4b4ccf86755c
more portable: SQL standard syntax instead of MySQL extension;
wenzelm
parents:
63791
diff
changeset
|
35 |
quote(s.replace("\"", "\"\"")) |
63779 | 36 |
|
63791 | 37 |
def enclosure(ss: Iterable[String]): String = ss.mkString("(", ", ", ")") |
38 |
||
63779 | 39 |
|
40 |
/* columns */ |
|
41 |
||
42 |
object Column |
|
43 |
{ |
|
63781 | 44 |
def int(name: String, strict: Boolean = true, primary_key: Boolean = false): Column[Int] = |
45 |
new Column_Int(name, strict, primary_key) |
|
46 |
def long(name: String, strict: Boolean = true, primary_key: Boolean = false): Column[Long] = |
|
47 |
new Column_Long(name, strict, primary_key) |
|
48 |
def double(name: String, strict: Boolean = true, primary_key: Boolean = false): Column[Double] = |
|
49 |
new Column_Double(name, strict, primary_key) |
|
50 |
def string(name: String, strict: Boolean = true, primary_key: Boolean = false): Column[String] = |
|
51 |
new Column_String(name, strict, primary_key) |
|
52 |
def bytes(name: String, strict: Boolean = true, primary_key: Boolean = false): Column[Bytes] = |
|
65004 | 53 |
new Column_Bytes(name, strict, primary_key, "BLOB") // SQL standard |
54 |
def bytea(name: String, strict: Boolean = true, primary_key: Boolean = false): Column[Bytes] = |
|
55 |
new Column_Bytes(name, strict, primary_key, "BYTEA") // PostgreSQL |
|
63779 | 56 |
} |
57 |
||
63781 | 58 |
abstract class Column[+A] private[SQL]( |
63790 | 59 |
val name: String, val strict: Boolean, val primary_key: Boolean) |
60 |
extends Function[ResultSet, A] |
|
63779 | 61 |
{ |
62 |
def sql_name: String = quote_ident(name) |
|
63 |
def sql_type: String |
|
63781 | 64 |
def sql_decl: String = |
65 |
sql_name + " " + sql_type + |
|
66 |
(if (strict) " NOT NULL" else "") + |
|
67 |
(if (primary_key) " PRIMARY KEY" else "") |
|
68 |
||
63780 | 69 |
def string(rs: ResultSet): String = |
70 |
{ |
|
71 |
val s = rs.getString(name) |
|
72 |
if (s == null) "" else s |
|
73 |
} |
|
74 |
def apply(rs: ResultSet): A |
|
75 |
def get(rs: ResultSet): Option[A] = |
|
76 |
{ |
|
77 |
val x = apply(rs) |
|
78 |
if (rs.wasNull) None else Some(x) |
|
79 |
} |
|
63779 | 80 |
|
81 |
override def toString: String = sql_decl |
|
82 |
} |
|
83 |
||
63781 | 84 |
class Column_Int private[SQL](name: String, strict: Boolean, primary_key: Boolean) |
85 |
extends Column[Int](name, strict, primary_key) |
|
63779 | 86 |
{ |
87 |
def sql_type: String = "INTEGER" |
|
63780 | 88 |
def apply(rs: ResultSet): Int = rs.getInt(name) |
63779 | 89 |
} |
90 |
||
63781 | 91 |
class Column_Long private[SQL](name: String, strict: Boolean, primary_key: Boolean) |
92 |
extends Column[Long](name, strict, primary_key) |
|
63779 | 93 |
{ |
65004 | 94 |
def sql_type: String = "BIGINT" |
63780 | 95 |
def apply(rs: ResultSet): Long = rs.getLong(name) |
63779 | 96 |
} |
97 |
||
63781 | 98 |
class Column_Double private[SQL](name: String, strict: Boolean, primary_key: Boolean) |
99 |
extends Column[Double](name, strict, primary_key) |
|
63779 | 100 |
{ |
65004 | 101 |
def sql_type: String = "DOUBLE PRECISION" |
63780 | 102 |
def apply(rs: ResultSet): Double = rs.getDouble(name) |
63779 | 103 |
} |
104 |
||
63781 | 105 |
class Column_String private[SQL](name: String, strict: Boolean, primary_key: Boolean) |
106 |
extends Column[String](name, strict, primary_key) |
|
63779 | 107 |
{ |
108 |
def sql_type: String = "TEXT" |
|
63780 | 109 |
def apply(rs: ResultSet): String = |
110 |
{ |
|
111 |
val s = rs.getString(name) |
|
112 |
if (s == null) "" else s |
|
113 |
} |
|
63779 | 114 |
} |
115 |
||
65004 | 116 |
class Column_Bytes private[SQL]( |
117 |
name: String, strict: Boolean, primary_key: Boolean, val sql_type: String) |
|
63781 | 118 |
extends Column[Bytes](name, strict, primary_key) |
63779 | 119 |
{ |
63780 | 120 |
def apply(rs: ResultSet): Bytes = |
63779 | 121 |
{ |
122 |
val bs = rs.getBytes(name) |
|
63780 | 123 |
if (bs == null) Bytes.empty else Bytes(bs) |
63779 | 124 |
} |
125 |
} |
|
63780 | 126 |
|
127 |
||
128 |
/* tables */ |
|
129 |
||
63791 | 130 |
def table(name: String, columns: List[Column[Any]]): Table = new Table(name, columns) |
63783 | 131 |
|
132 |
class Table private[SQL](name: String, columns: List[Column[Any]]) |
|
63780 | 133 |
{ |
63790 | 134 |
private val columns_index: Map[String, Int] = |
135 |
columns.iterator.map(_.name).zipWithIndex.toMap |
|
136 |
||
63781 | 137 |
Library.duplicates(columns.map(_.name)) match { |
138 |
case Nil => |
|
139 |
case bad => error("Duplicate column names " + commas_quote(bad) + " for table " + quote(name)) |
|
140 |
} |
|
141 |
||
142 |
columns.filter(_.primary_key) match { |
|
143 |
case bad if bad.length > 1 => |
|
144 |
error("Multiple primary keys " + commas_quote(bad.map(_.name)) + " for table " + quote(name)) |
|
145 |
case _ => |
|
146 |
} |
|
147 |
||
63780 | 148 |
def sql_create(strict: Boolean, rowid: Boolean): String = |
63784 | 149 |
"CREATE TABLE " + (if (strict) "" else "IF NOT EXISTS ") + |
63791 | 150 |
quote_ident(name) + " " + enclosure(columns.map(_.sql_decl)) + |
63780 | 151 |
(if (rowid) "" else " WITHOUT ROWID") |
152 |
||
153 |
def sql_drop(strict: Boolean): String = |
|
63784 | 154 |
"DROP TABLE " + (if (strict) "" else "IF EXISTS ") + quote_ident(name) |
63783 | 155 |
|
63791 | 156 |
def sql_create_index( |
157 |
index_name: String, index_columns: List[Column[Any]], |
|
158 |
strict: Boolean, unique: Boolean): String = |
|
159 |
"CREATE " + (if (unique) "UNIQUE " else "") + "INDEX " + |
|
160 |
(if (strict) "" else "IF NOT EXISTS ") + quote_ident(index_name) + " ON " + |
|
161 |
quote_ident(name) + " " + enclosure(index_columns.map(_.name)) |
|
162 |
||
163 |
def sql_drop_index(index_name: String, strict: Boolean): String = |
|
164 |
"DROP INDEX " + (if (strict) "" else "IF EXISTS ") + quote_ident(index_name) |
|
165 |
||
63790 | 166 |
def sql_insert: String = |
63791 | 167 |
"INSERT INTO " + quote_ident(name) + " VALUES " + enclosure(columns.map(_ => "?")) |
168 |
||
169 |
def sql_select(select_columns: List[Column[Any]], distinct: Boolean): String = |
|
170 |
"SELECT " + (if (distinct) "DISTINCT " else "") + |
|
171 |
commas(select_columns.map(_.sql_name)) + " FROM " + quote_ident(name) |
|
63790 | 172 |
|
63783 | 173 |
override def toString: String = |
63791 | 174 |
"TABLE " + quote_ident(name) + " " + enclosure(columns.map(_.toString)) |
63780 | 175 |
} |
63790 | 176 |
|
177 |
||
178 |
/* results */ |
|
179 |
||
180 |
def iterator[A](rs: ResultSet)(get: ResultSet => A): Iterator[A] = new Iterator[A] |
|
181 |
{ |
|
182 |
private var _next: Boolean = rs.next() |
|
183 |
def hasNext: Boolean = _next |
|
184 |
def next: A = { val x = get(rs); _next = rs.next(); x } |
|
185 |
} |
|
63778 | 186 |
} |