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