author | wenzelm |
Thu, 09 Mar 2023 12:54:19 +0100 | |
changeset 77598 | 6370d9e5ab50 |
parent 77596 | dd8b08729458 |
child 77664 | f5d3ade80d15 |
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 |
||
69393
ed0824ef337e
static type for Library.using: avoid Java 11 warnings on "Illegal reflective access";
wenzelm
parents:
69327
diff
changeset
|
9 |
|
65021 | 10 |
import java.time.OffsetDateTime |
11 |
import java.sql.{DriverManager, Connection, PreparedStatement, ResultSet} |
|
63779 | 12 |
|
77037
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
13 |
import org.sqlite.jdbc4.JDBC4Connection |
77039 | 14 |
import org.postgresql.{PGConnection, PGNotification} |
77037
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
15 |
|
65740 | 16 |
import scala.collection.mutable |
17 |
||
63779 | 18 |
|
75393 | 19 |
object SQL { |
65006 | 20 |
/** SQL language **/ |
21 |
||
65730 | 22 |
type Source = String |
23 |
||
24 |
||
63778 | 25 |
/* concrete syntax */ |
26 |
||
65321 | 27 |
def escape_char(c: Char): String = |
63778 | 28 |
c match { |
29 |
case '\u0000' => "\\0" |
|
30 |
case '\'' => "\\'" |
|
31 |
case '\"' => "\\\"" |
|
32 |
case '\b' => "\\b" |
|
33 |
case '\n' => "\\n" |
|
34 |
case '\r' => "\\r" |
|
35 |
case '\t' => "\\t" |
|
36 |
case '\u001a' => "\\Z" |
|
37 |
case '\\' => "\\\\" |
|
38 |
case _ => c.toString |
|
39 |
} |
|
40 |
||
65730 | 41 |
def string(s: String): Source = |
71601 | 42 |
s.iterator.map(escape_char).mkString("'", "", "'") |
63778 | 43 |
|
65730 | 44 |
def ident(s: String): Source = |
65677
7d25b8dbdbfa
proper quote (amending 546020a98a91): e.g. relevant for "ISABELLE_BUILD_OPTIONS";
wenzelm
parents:
65673
diff
changeset
|
45 |
Long_Name.implode(Long_Name.explode(s).map(a => quote(a.replace("\"", "\"\"")))) |
63779 | 46 |
|
65730 | 47 |
def enclose(s: Source): Source = "(" + s + ")" |
48 |
def enclosure(ss: Iterable[Source]): Source = ss.mkString("(", ", ", ")") |
|
63791 | 49 |
|
77381
a86e346b20d8
misc tuning and clarification: more uniform use of optional "sql" in SQL.Table.delete/select;
wenzelm
parents:
77377
diff
changeset
|
50 |
def separate(sql: Source): Source = |
a86e346b20d8
misc tuning and clarification: more uniform use of optional "sql" in SQL.Table.delete/select;
wenzelm
parents:
77377
diff
changeset
|
51 |
(if (sql.isEmpty || sql.startsWith(" ")) "" else " ") + sql |
a86e346b20d8
misc tuning and clarification: more uniform use of optional "sql" in SQL.Table.delete/select;
wenzelm
parents:
77377
diff
changeset
|
52 |
|
77403
be8e14c7da80
clarified signature, although "sql" argument is de-facto mandatory;
wenzelm
parents:
77381
diff
changeset
|
53 |
def select(columns: List[Column] = Nil, distinct: Boolean = false, sql: Source = ""): Source = |
65774 | 54 |
"SELECT " + (if (distinct) "DISTINCT " else "") + |
77403
be8e14c7da80
clarified signature, although "sql" argument is de-facto mandatory;
wenzelm
parents:
77381
diff
changeset
|
55 |
(if (columns.isEmpty) "*" else commas(columns.map(_.ident))) + " FROM " + sql |
65644
7ef438495a02
support for qualified names, which are not quoted (e.g. for SQLite);
wenzelm
parents:
65641
diff
changeset
|
56 |
|
65775 | 57 |
val join_outer: Source = " LEFT OUTER JOIN " |
58 |
val join_inner: Source = " INNER JOIN " |
|
65776 | 59 |
def join(outer: Boolean): Source = if (outer) join_outer else join_inner |
65775 | 60 |
|
77370 | 61 |
def infix(op: Source, args: Iterable[Source]): Source = { |
62 |
val body = args.iterator.filter(_.nonEmpty).mkString(" " + op + " ") |
|
63 |
if_proper(body, enclose(body)) |
|
64 |
} |
|
65 |
||
66 |
def AND(args: Iterable[Source]): Source = infix("AND", args) |
|
67 |
def OR(args: Iterable[Source]): Source = infix("OR", args) |
|
68 |
||
69 |
def and(args: Source*): Source = AND(args) |
|
70 |
def or(args: Source*): Source = OR(args) |
|
71 |
||
72 |
val TRUE: Source = "TRUE" |
|
73 |
val FALSE: Source = "FALSE" |
|
74 |
||
77375 | 75 |
def equal(sql: Source, s: String): Source = sql + " = " + string(s) |
76 |
||
77 |
def member(sql: Source, set: Iterable[String]): Source = |
|
77370 | 78 |
if (set.isEmpty) FALSE |
77375 | 79 |
else OR(set.iterator.map(equal(sql, _)).toList) |
65804 | 80 |
|
77370 | 81 |
def where(sql: Source): Source = if_proper(sql, " WHERE " + sql) |
82 |
||
83 |
||
65008 | 84 |
/* types */ |
85 |
||
75393 | 86 |
object Type extends Enumeration { |
65011 | 87 |
val Boolean = Value("BOOLEAN") |
65008 | 88 |
val Int = Value("INTEGER") |
89 |
val Long = Value("BIGINT") |
|
90 |
val Double = Value("DOUBLE PRECISION") |
|
91 |
val String = Value("TEXT") |
|
92 |
val Bytes = Value("BLOB") |
|
65014 | 93 |
val Date = Value("TIMESTAMP WITH TIME ZONE") |
65008 | 94 |
} |
95 |
||
65730 | 96 |
def sql_type_default(T: Type.Value): Source = T.toString |
65013 | 97 |
|
65730 | 98 |
def sql_type_sqlite(T: Type.Value): Source = |
65019 | 99 |
if (T == Type.Boolean) "INTEGER" |
100 |
else if (T == Type.Date) "TEXT" |
|
101 |
else sql_type_default(T) |
|
65013 | 102 |
|
65730 | 103 |
def sql_type_postgresql(T: Type.Value): Source = |
65019 | 104 |
if (T == Type.Bytes) "BYTEA" |
105 |
else sql_type_default(T) |
|
65008 | 106 |
|
107 |
||
63779 | 108 |
/* columns */ |
109 |
||
75393 | 110 |
object Column { |
65280 | 111 |
def bool(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 112 |
Column(name, Type.Boolean, strict, primary_key) |
65280 | 113 |
def int(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 114 |
Column(name, Type.Int, strict, primary_key) |
65280 | 115 |
def long(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 116 |
Column(name, Type.Long, strict, primary_key) |
65280 | 117 |
def double(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 118 |
Column(name, Type.Double, strict, primary_key) |
65280 | 119 |
def string(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 120 |
Column(name, Type.String, strict, primary_key) |
65280 | 121 |
def bytes(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 122 |
Column(name, Type.Bytes, strict, primary_key) |
65280 | 123 |
def date(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 124 |
Column(name, Type.Date, strict, primary_key) |
63779 | 125 |
} |
126 |
||
65018 | 127 |
sealed case class Column( |
75393 | 128 |
name: String, |
129 |
T: Type.Value, |
|
130 |
strict: Boolean = false, |
|
131 |
primary_key: Boolean = false, |
|
132 |
expr: SQL.Source = "" |
|
133 |
) { |
|
66857 | 134 |
def make_primary_key: Column = copy(primary_key = true) |
135 |
||
65691 | 136 |
def apply(table: Table): Column = |
137 |
Column(Long_Name.qualify(table.name, name), T, strict = strict, primary_key = primary_key) |
|
65644
7ef438495a02
support for qualified names, which are not quoted (e.g. for SQLite);
wenzelm
parents:
65641
diff
changeset
|
138 |
|
65780 | 139 |
def ident: Source = |
140 |
if (expr == "") SQL.ident(name) |
|
141 |
else enclose(expr) + " AS " + SQL.ident(name) |
|
65695 | 142 |
|
65730 | 143 |
def decl(sql_type: Type.Value => Source): Source = |
65695 | 144 |
ident + " " + sql_type(T) + (if (strict || primary_key) " NOT NULL" else "") |
63781 | 145 |
|
66856 | 146 |
def defined: String = ident + " IS NOT NULL" |
147 |
def undefined: String = ident + " IS NULL" |
|
148 |
||
77375 | 149 |
def equal(s: String): Source = SQL.equal(ident, s) |
150 |
def member(set: Iterable[String]): Source = SQL.member(ident, set) |
|
151 |
||
152 |
def where_equal(s: String): Source = SQL.where(equal(s)) |
|
153 |
def where_member(set: Iterable[String]): Source = SQL.where(member(set)) |
|
65593 | 154 |
|
65730 | 155 |
override def toString: Source = ident |
63779 | 156 |
} |
157 |
||
76870 | 158 |
def order_by(columns: List[Column], descending: Boolean = false): Source = |
159 |
" ORDER BY " + columns.mkString(", ") + (if (descending) " DESC" else "") |
|
160 |
||
63780 | 161 |
|
162 |
/* tables */ |
|
163 |
||
75393 | 164 |
sealed case class Table(name: String, columns: List[Column], body: Source = "") { |
63781 | 165 |
Library.duplicates(columns.map(_.name)) match { |
166 |
case Nil => |
|
167 |
case bad => error("Duplicate column names " + commas_quote(bad) + " for table " + quote(name)) |
|
168 |
} |
|
169 |
||
65730 | 170 |
def ident: Source = SQL.ident(name) |
65691 | 171 |
|
65730 | 172 |
def query: Source = |
65696 | 173 |
if (body == "") error("Missing SQL body for table " + quote(name)) |
174 |
else SQL.enclose(body) |
|
65649 | 175 |
|
65778 | 176 |
def query_named: Source = query + " AS " + SQL.ident(name) |
65702 | 177 |
|
75966 | 178 |
def create(strict: Boolean, sql_type: Type.Value => Source): Source = { |
65325 | 179 |
val primary_key = |
180 |
columns.filter(_.primary_key).map(_.name) match { |
|
181 |
case Nil => Nil |
|
182 |
case keys => List("PRIMARY KEY " + enclosure(keys)) |
|
183 |
} |
|
65701 | 184 |
"CREATE TABLE " + (if (strict) "" else "IF NOT EXISTS ") + |
185 |
ident + " " + enclosure(columns.map(_.decl(sql_type)) ::: primary_key) |
|
63781 | 186 |
} |
187 |
||
65698 | 188 |
def create_index(index_name: String, index_columns: List[Column], |
65730 | 189 |
strict: Boolean = false, unique: Boolean = false): Source = |
63791 | 190 |
"CREATE " + (if (unique) "UNIQUE " else "") + "INDEX " + |
65695 | 191 |
(if (strict) "" else "IF NOT EXISTS ") + SQL.ident(index_name) + " ON " + |
192 |
ident + " " + enclosure(index_columns.map(_.name)) |
|
63791 | 193 |
|
77365 | 194 |
def insert_cmd(cmd: Source = "INSERT", sql: Source = ""): Source = |
77377 | 195 |
cmd + " INTO " + ident + " VALUES " + enclosure(columns.map(_ => "?")) + SQL.separate(sql) |
63791 | 196 |
|
77365 | 197 |
def insert(sql: Source = ""): Source = insert_cmd(sql = sql) |
65703
cead65c19f2e
more direct insert_permissive statement, which avoids somewhat fragile nested transactions;
wenzelm
parents:
65702
diff
changeset
|
198 |
|
65730 | 199 |
def delete(sql: Source = ""): Source = |
77377 | 200 |
"DELETE FROM " + ident + SQL.separate(sql) |
65319 | 201 |
|
77537 | 202 |
def update(update_columns: List[Column] = Nil, sql: Source = ""): Source = |
203 |
"UPDATE " + ident + " SET " + commas(update_columns.map(c => c.ident + " = ?")) + |
|
204 |
SQL.separate(sql) |
|
205 |
||
65774 | 206 |
def select( |
77381
a86e346b20d8
misc tuning and clarification: more uniform use of optional "sql" in SQL.Table.delete/select;
wenzelm
parents:
77377
diff
changeset
|
207 |
select_columns: List[Column] = Nil, |
a86e346b20d8
misc tuning and clarification: more uniform use of optional "sql" in SQL.Table.delete/select;
wenzelm
parents:
77377
diff
changeset
|
208 |
distinct: Boolean = false, |
a86e346b20d8
misc tuning and clarification: more uniform use of optional "sql" in SQL.Table.delete/select;
wenzelm
parents:
77377
diff
changeset
|
209 |
sql: Source = "" |
77403
be8e14c7da80
clarified signature, although "sql" argument is de-facto mandatory;
wenzelm
parents:
77381
diff
changeset
|
210 |
): Source = SQL.select(select_columns, distinct = distinct, sql = ident + SQL.separate(sql)) |
63790 | 211 |
|
65730 | 212 |
override def toString: Source = ident |
63780 | 213 |
} |
63790 | 214 |
|
215 |
||
77596 | 216 |
/* table groups */ |
217 |
||
218 |
object Tables { |
|
219 |
def list(list: List[Table]): Tables = new Tables(list) |
|
220 |
def apply(args: Table*): Tables = list(args.toList) |
|
221 |
} |
|
222 |
||
223 |
final class Tables private(val list: List[Table]) extends Iterable[Table] { |
|
224 |
override def toString: String = list.mkString("SQL.Tables(", ", ", ")") |
|
225 |
||
226 |
def iterator: Iterator[Table] = list.iterator |
|
227 |
||
228 |
// requires transaction |
|
229 |
def create_lock(db: Database): Unit = { |
|
230 |
foreach(db.create_table(_)) |
|
231 |
lock(db) |
|
232 |
} |
|
233 |
||
234 |
// requires transaction |
|
235 |
def lock(db: Database): Unit = { |
|
236 |
val sql = db.lock_tables(list) |
|
237 |
if (sql.nonEmpty) db.execute_statement(sql) |
|
238 |
} |
|
239 |
} |
|
240 |
||
241 |
||
65012 | 242 |
|
243 |
/** SQL database operations **/ |
|
244 |
||
65740 | 245 |
/* statements */ |
246 |
||
75393 | 247 |
class Statement private[SQL](val db: Database, val rep: PreparedStatement) extends AutoCloseable { |
65740 | 248 |
stmt => |
249 |
||
75393 | 250 |
object bool { |
73340 | 251 |
def update(i: Int, x: Boolean): Unit = rep.setBoolean(i, x) |
75393 | 252 |
def update(i: Int, x: Option[Boolean]): Unit = { |
65748 | 253 |
if (x.isDefined) update(i, x.get) |
254 |
else rep.setNull(i, java.sql.Types.BOOLEAN) |
|
255 |
} |
|
65740 | 256 |
} |
75393 | 257 |
object int { |
73340 | 258 |
def update(i: Int, x: Int): Unit = rep.setInt(i, x) |
75393 | 259 |
def update(i: Int, x: Option[Int]): Unit = { |
65748 | 260 |
if (x.isDefined) update(i, x.get) |
261 |
else rep.setNull(i, java.sql.Types.INTEGER) |
|
262 |
} |
|
65740 | 263 |
} |
75393 | 264 |
object long { |
73340 | 265 |
def update(i: Int, x: Long): Unit = rep.setLong(i, x) |
75393 | 266 |
def update(i: Int, x: Option[Long]): Unit = { |
65748 | 267 |
if (x.isDefined) update(i, x.get) |
268 |
else rep.setNull(i, java.sql.Types.BIGINT) |
|
269 |
} |
|
65740 | 270 |
} |
75393 | 271 |
object double { |
73340 | 272 |
def update(i: Int, x: Double): Unit = rep.setDouble(i, x) |
75393 | 273 |
def update(i: Int, x: Option[Double]): Unit = { |
65748 | 274 |
if (x.isDefined) update(i, x.get) |
275 |
else rep.setNull(i, java.sql.Types.DOUBLE) |
|
276 |
} |
|
277 |
} |
|
75393 | 278 |
object string { |
73340 | 279 |
def update(i: Int, x: String): Unit = rep.setString(i, x) |
65748 | 280 |
def update(i: Int, x: Option[String]): Unit = update(i, x.orNull) |
65740 | 281 |
} |
75393 | 282 |
object bytes { |
283 |
def update(i: Int, bytes: Bytes): Unit = { |
|
65748 | 284 |
if (bytes == null) rep.setBytes(i, null) |
285 |
else rep.setBinaryStream(i, bytes.stream(), bytes.length) |
|
286 |
} |
|
287 |
def update(i: Int, bytes: Option[Bytes]): Unit = update(i, bytes.orNull) |
|
65740 | 288 |
} |
75393 | 289 |
object date { |
65748 | 290 |
def update(i: Int, date: Date): Unit = db.update_date(stmt, i, date) |
291 |
def update(i: Int, date: Option[Date]): Unit = update(i, date.orNull) |
|
292 |
} |
|
65740 | 293 |
|
294 |
def execute(): Boolean = rep.execute() |
|
295 |
def execute_query(): Result = new Result(this, rep.executeQuery()) |
|
296 |
||
73367 | 297 |
def close(): Unit = rep.close() |
65740 | 298 |
} |
299 |
||
300 |
||
63790 | 301 |
/* results */ |
302 |
||
75393 | 303 |
class Result private[SQL](val stmt: Statement, val rep: ResultSet) { |
65740 | 304 |
res => |
305 |
||
306 |
def next(): Boolean = rep.next() |
|
307 |
||
75393 | 308 |
def iterator[A](get: Result => A): Iterator[A] = new Iterator[A] { |
65740 | 309 |
private var _next: Boolean = res.next() |
310 |
def hasNext: Boolean = _next |
|
73337 | 311 |
def next(): A = { val x = get(res); _next = res.next(); x } |
65740 | 312 |
} |
313 |
||
314 |
def bool(column: Column): Boolean = rep.getBoolean(column.name) |
|
315 |
def int(column: Column): Int = rep.getInt(column.name) |
|
316 |
def long(column: Column): Long = rep.getLong(column.name) |
|
317 |
def double(column: Column): Double = rep.getDouble(column.name) |
|
75393 | 318 |
def string(column: Column): String = { |
65740 | 319 |
val s = rep.getString(column.name) |
320 |
if (s == null) "" else s |
|
321 |
} |
|
75393 | 322 |
def bytes(column: Column): Bytes = { |
65740 | 323 |
val bs = rep.getBytes(column.name) |
324 |
if (bs == null) Bytes.empty else Bytes(bs) |
|
325 |
} |
|
326 |
def date(column: Column): Date = stmt.db.date(res, column) |
|
327 |
||
71601 | 328 |
def timing(c1: Column, c2: Column, c3: Column): Timing = |
65741 | 329 |
Timing(Time.ms(long(c1)), Time.ms(long(c2)), Time.ms(long(c3))) |
330 |
||
75393 | 331 |
def get[A](column: Column, f: Column => A): Option[A] = { |
65740 | 332 |
val x = f(column) |
77598 | 333 |
if (rep.wasNull || x == null) None else Some(x) |
65740 | 334 |
} |
71601 | 335 |
def get_bool(column: Column): Option[Boolean] = get(column, bool) |
336 |
def get_int(column: Column): Option[Int] = get(column, int) |
|
337 |
def get_long(column: Column): Option[Long] = get(column, long) |
|
338 |
def get_double(column: Column): Option[Double] = get(column, double) |
|
339 |
def get_string(column: Column): Option[String] = get(column, string) |
|
340 |
def get_bytes(column: Column): Option[Bytes] = get(column, bytes) |
|
341 |
def get_date(column: Column): Option[Date] = get(column, date) |
|
63790 | 342 |
} |
65006 | 343 |
|
65740 | 344 |
|
345 |
/* database */ |
|
346 |
||
75393 | 347 |
trait Database extends AutoCloseable { |
65740 | 348 |
db => |
349 |
||
77542 | 350 |
def is_sqlite: Boolean = isInstanceOf[SQLite.Database] |
351 |
def is_postgresql: Boolean = isInstanceOf[PostgreSQL.Database] |
|
76363
f7174238b5e3
no compression for database server: let PostgreSQL/TOAST do the job;
wenzelm
parents:
76122
diff
changeset
|
352 |
|
77348 | 353 |
def rebuild(): Unit = () |
354 |
||
77527
790085b1002f
more detailed table "isabelle_build_serial": allow to monitor activity of build_process instances;
wenzelm
parents:
77403
diff
changeset
|
355 |
def now(): Date |
790085b1002f
more detailed table "isabelle_build_serial": allow to monitor activity of build_process instances;
wenzelm
parents:
77403
diff
changeset
|
356 |
|
65740 | 357 |
|
65008 | 358 |
/* types */ |
359 |
||
65730 | 360 |
def sql_type(T: Type.Value): Source |
65008 | 361 |
|
362 |
||
65006 | 363 |
/* connection */ |
364 |
||
365 |
def connection: Connection |
|
366 |
||
77037
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
367 |
def sqlite_connection: Option[JDBC4Connection] = |
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
368 |
connection match { case conn: JDBC4Connection => Some(conn) case _ => None } |
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
369 |
|
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
370 |
def postgresql_connection: Option[PGConnection] = |
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
371 |
connection match { case conn: PGConnection => Some(conn) case _ => None } |
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
372 |
|
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
373 |
def the_sqlite_connection: JDBC4Connection = |
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
374 |
sqlite_connection getOrElse |
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
375 |
error("SQLite connection expected, but found " + connection.getClass.getName) |
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
376 |
|
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
377 |
def the_postgresql_connection: PGConnection = |
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
378 |
postgresql_connection getOrElse |
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
379 |
error("PostgreSQL connection expected, but found " + connection.getClass.getName) |
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
380 |
|
73367 | 381 |
def close(): Unit = connection.close() |
65006 | 382 |
|
75393 | 383 |
def transaction[A](body: => A): A = { |
75776
72e77c8307ec
tuned signature, following hints by IntelliJ IDEA;
wenzelm
parents:
75393
diff
changeset
|
384 |
val auto_commit = connection.getAutoCommit() |
65006 | 385 |
try { |
386 |
connection.setAutoCommit(false) |
|
75776
72e77c8307ec
tuned signature, following hints by IntelliJ IDEA;
wenzelm
parents:
75393
diff
changeset
|
387 |
val savepoint = connection.setSavepoint() |
65022 | 388 |
try { |
389 |
val result = body |
|
75776
72e77c8307ec
tuned signature, following hints by IntelliJ IDEA;
wenzelm
parents:
75393
diff
changeset
|
390 |
connection.commit() |
65022 | 391 |
result |
392 |
} |
|
393 |
catch { case exn: Throwable => connection.rollback(savepoint); throw exn } |
|
65006 | 394 |
} |
395 |
finally { connection.setAutoCommit(auto_commit) } |
|
396 |
} |
|
397 |
||
77596 | 398 |
def transaction_lock[A](tables: Tables)(body: => A): A = |
399 |
transaction { tables.lock(db); body } |
|
400 |
||
401 |
def lock_tables(tables: List[Table]): Source = "" // PostgreSQL only |
|
77590
edc96be6b939
explicit locking for PostgreSQL --- neither available nor required for SQLite;
wenzelm
parents:
77552
diff
changeset
|
402 |
|
65006 | 403 |
|
65740 | 404 |
/* statements and results */ |
405 |
||
406 |
def statement(sql: Source): Statement = |
|
407 |
new Statement(db, connection.prepareStatement(sql)) |
|
65006 | 408 |
|
65740 | 409 |
def using_statement[A](sql: Source)(f: Statement => A): A = |
410 |
using(statement(sql))(f) |
|
65006 | 411 |
|
77541 | 412 |
def execute_statement(sql: Source, body: Statement => Unit = _ => ()): Unit = |
413 |
using_statement(sql) { stmt => body(stmt); stmt.execute() } |
|
77540 | 414 |
|
77552 | 415 |
def execute_query_statement[A, B]( |
416 |
sql: Source, |
|
417 |
make_result: Iterator[A] => B, |
|
418 |
get: Result => A |
|
419 |
): B = using_statement(sql)(stmt => make_result(stmt.execute_query().iterator(get))) |
|
420 |
||
421 |
def execute_query_statementO[A](sql: Source, get: Result => A): Option[A] = |
|
422 |
execute_query_statement[A, Option[A]](sql, _.nextOption, get) |
|
423 |
||
424 |
def execute_query_statementB(sql: Source): Boolean = |
|
425 |
using_statement(sql)(stmt => stmt.execute_query().next()) |
|
426 |
||
65748 | 427 |
def update_date(stmt: Statement, i: Int, date: Date): Unit |
65740 | 428 |
def date(res: Result, column: Column): Date |
65006 | 429 |
|
65730 | 430 |
def insert_permissive(table: Table, sql: Source = ""): Source |
65703
cead65c19f2e
more direct insert_permissive statement, which avoids somewhat fragile nested transactions;
wenzelm
parents:
65702
diff
changeset
|
431 |
|
65006 | 432 |
|
65669 | 433 |
/* tables and views */ |
65006 | 434 |
|
75393 | 435 |
def tables: List[String] = { |
65740 | 436 |
val result = new mutable.ListBuffer[String] |
437 |
val rs = connection.getMetaData.getTables(null, null, "%", null) |
|
438 |
while (rs.next) { result += rs.getString(3) } |
|
439 |
result.toList |
|
440 |
} |
|
65006 | 441 |
|
65730 | 442 |
def create_table(table: Table, strict: Boolean = false, sql: Source = ""): Unit = |
77540 | 443 |
execute_statement(table.create(strict, sql_type) + SQL.separate(sql)) |
65006 | 444 |
|
65018 | 445 |
def create_index(table: Table, name: String, columns: List[Column], |
65280 | 446 |
strict: Boolean = false, unique: Boolean = false): Unit = |
77540 | 447 |
execute_statement(table.create_index(name, columns, strict, unique)) |
65006 | 448 |
|
75393 | 449 |
def create_view(table: Table, strict: Boolean = false): Unit = { |
65691 | 450 |
if (strict || !tables.contains(table.name)) { |
77540 | 451 |
execute_statement("CREATE VIEW " + table + " AS " + { table.query; table.body }) |
65690 | 452 |
} |
65691 | 453 |
} |
65006 | 454 |
} |
63778 | 455 |
} |
65006 | 456 |
|
457 |
||
458 |
||
459 |
/** SQLite **/ |
|
460 |
||
75393 | 461 |
object SQLite { |
65021 | 462 |
// see https://www.sqlite.org/lang_datefunc.html |
463 |
val date_format: Date.Format = Date.Format("uuuu-MM-dd HH:mm:ss.SSS x") |
|
464 |
||
75393 | 465 |
lazy val init_jdbc: Unit = { |
65890
1b004f5974af
refer to already extracted library files, to avoid tmp files produced by SQLiteJDBCLoader, which tend to remain after JVM crash;
wenzelm
parents:
65804
diff
changeset
|
466 |
val lib_path = Path.explode("$ISABELLE_SQLITE_HOME/" + Platform.jvm_platform) |
76529 | 467 |
val lib_name = File.get_file(lib_path).file_name |
468 |
||
65890
1b004f5974af
refer to already extracted library files, to avoid tmp files produced by SQLiteJDBCLoader, which tend to remain after JVM crash;
wenzelm
parents:
65804
diff
changeset
|
469 |
System.setProperty("org.sqlite.lib.path", File.platform_path(lib_path)) |
1b004f5974af
refer to already extracted library files, to avoid tmp files produced by SQLiteJDBCLoader, which tend to remain after JVM crash;
wenzelm
parents:
65804
diff
changeset
|
470 |
System.setProperty("org.sqlite.lib.name", lib_name) |
1b004f5974af
refer to already extracted library files, to avoid tmp files produced by SQLiteJDBCLoader, which tend to remain after JVM crash;
wenzelm
parents:
65804
diff
changeset
|
471 |
|
1b004f5974af
refer to already extracted library files, to avoid tmp files produced by SQLiteJDBCLoader, which tend to remain after JVM crash;
wenzelm
parents:
65804
diff
changeset
|
472 |
Class.forName("org.sqlite.JDBC") |
1b004f5974af
refer to already extracted library files, to avoid tmp files produced by SQLiteJDBCLoader, which tend to remain after JVM crash;
wenzelm
parents:
65804
diff
changeset
|
473 |
} |
65292
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
474 |
|
75393 | 475 |
def open_database(path: Path): Database = { |
65292
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
476 |
init_jdbc |
65006 | 477 |
val path0 = path.expand |
478 |
val s0 = File.platform_path(path0) |
|
479 |
val s1 = if (Platform.is_windows) s0.replace('\\', '/') else s0 |
|
480 |
val connection = DriverManager.getConnection("jdbc:sqlite:" + s1) |
|
65007 | 481 |
new Database(path0.toString, connection) |
65006 | 482 |
} |
483 |
||
75393 | 484 |
class Database private[SQLite](name: String, val connection: Connection) extends SQL.Database { |
65007 | 485 |
override def toString: String = name |
77540 | 486 |
override def rebuild(): Unit = execute_statement("VACUUM") |
65006 | 487 |
|
77527
790085b1002f
more detailed table "isabelle_build_serial": allow to monitor activity of build_process instances;
wenzelm
parents:
77403
diff
changeset
|
488 |
override def now(): Date = Date.now() |
790085b1002f
more detailed table "isabelle_build_serial": allow to monitor activity of build_process instances;
wenzelm
parents:
77403
diff
changeset
|
489 |
|
65730 | 490 |
def sql_type(T: SQL.Type.Value): SQL.Source = SQL.sql_type_sqlite(T) |
65011 | 491 |
|
65748 | 492 |
def update_date(stmt: SQL.Statement, i: Int, date: Date): Unit = |
493 |
if (date == null) stmt.string(i) = (null: String) |
|
494 |
else stmt.string(i) = date_format(date) |
|
65598 | 495 |
|
65740 | 496 |
def date(res: SQL.Result, column: SQL.Column): Date = |
77598 | 497 |
proper_string(res.string(column)) match { |
498 |
case None => null |
|
499 |
case Some(s) => date_format.parse(s) |
|
500 |
} |
|
65021 | 501 |
|
65730 | 502 |
def insert_permissive(table: SQL.Table, sql: SQL.Source = ""): SQL.Source = |
77365 | 503 |
table.insert_cmd(cmd = "INSERT OR IGNORE", sql = sql) |
65006 | 504 |
} |
505 |
} |
|
506 |
||
507 |
||
508 |
||
509 |
/** PostgreSQL **/ |
|
510 |
||
75393 | 511 |
object PostgreSQL { |
75967
ff164add75cd
maintain "uuid" column in session build database, to identity the original build process uniquely;
wenzelm
parents:
75966
diff
changeset
|
512 |
type Source = SQL.Source |
ff164add75cd
maintain "uuid" column in session build database, to identity the original build process uniquely;
wenzelm
parents:
75966
diff
changeset
|
513 |
|
65006 | 514 |
val default_port = 5432 |
515 |
||
65292
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
516 |
lazy val init_jdbc: Unit = Class.forName("org.postgresql.Driver") |
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
517 |
|
65006 | 518 |
def open_database( |
519 |
user: String, |
|
520 |
password: String, |
|
521 |
database: String = "", |
|
522 |
host: String = "", |
|
65594 | 523 |
port: Int = 0, |
65636
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65620
diff
changeset
|
524 |
ssh: Option[SSH.Session] = None, |
77591 | 525 |
ssh_close: Boolean = false, |
526 |
// see https://www.postgresql.org/docs/current/transaction-iso.html |
|
527 |
transaction_isolation: Int = Connection.TRANSACTION_SERIALIZABLE |
|
75393 | 528 |
): Database = { |
65292
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
529 |
init_jdbc |
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
530 |
|
65641 | 531 |
if (user == "") error("Undefined database user") |
65009 | 532 |
|
65717 | 533 |
val db_host = proper_string(host) getOrElse "localhost" |
65594 | 534 |
val db_port = if (port > 0 && port != default_port) ":" + port else "" |
65717 | 535 |
val db_name = "/" + (proper_string(database) getOrElse user) |
65009 | 536 |
|
65010 | 537 |
val (url, name, port_forwarding) = |
65009 | 538 |
ssh match { |
65010 | 539 |
case None => |
540 |
val spec = db_host + db_port + db_name |
|
541 |
val url = "jdbc:postgresql://" + spec |
|
542 |
val name = user + "@" + spec |
|
543 |
(url, name, None) |
|
65009 | 544 |
case Some(ssh) => |
65594 | 545 |
val fw = |
546 |
ssh.port_forwarding(remote_host = db_host, |
|
65636
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65620
diff
changeset
|
547 |
remote_port = if (port > 0) port else default_port, |
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65620
diff
changeset
|
548 |
ssh_close = ssh_close) |
76122
b8f26c20d3b1
ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents:
75967
diff
changeset
|
549 |
val url = "jdbc:postgresql://localhost:" + fw.port + db_name |
65010 | 550 |
val name = user + "@" + fw + db_name + " via ssh " + ssh |
551 |
(url, name, Some(fw)) |
|
65009 | 552 |
} |
553 |
try { |
|
65010 | 554 |
val connection = DriverManager.getConnection(url, user, password) |
77591 | 555 |
connection.setTransactionIsolation(transaction_isolation) |
65010 | 556 |
new Database(name, connection, port_forwarding) |
65009 | 557 |
} |
73367 | 558 |
catch { case exn: Throwable => port_forwarding.foreach(_.close()); throw exn } |
65006 | 559 |
} |
560 |
||
65009 | 561 |
class Database private[PostgreSQL]( |
75393 | 562 |
name: String, |
563 |
val connection: Connection, |
|
564 |
port_forwarding: Option[SSH.Port_Forwarding] |
|
565 |
) extends SQL.Database { |
|
65010 | 566 |
override def toString: String = name |
65008 | 567 |
|
77527
790085b1002f
more detailed table "isabelle_build_serial": allow to monitor activity of build_process instances;
wenzelm
parents:
77403
diff
changeset
|
568 |
override def now(): Date = { |
790085b1002f
more detailed table "isabelle_build_serial": allow to monitor activity of build_process instances;
wenzelm
parents:
77403
diff
changeset
|
569 |
val now = SQL.Column.date("now") |
77552 | 570 |
execute_query_statementO[Date]("SELECT NOW() as " + now.ident, res => res.date(now)) |
571 |
.getOrElse(error("Failed to get current date/time from database server " + toString)) |
|
77527
790085b1002f
more detailed table "isabelle_build_serial": allow to monitor activity of build_process instances;
wenzelm
parents:
77403
diff
changeset
|
572 |
} |
790085b1002f
more detailed table "isabelle_build_serial": allow to monitor activity of build_process instances;
wenzelm
parents:
77403
diff
changeset
|
573 |
|
65730 | 574 |
def sql_type(T: SQL.Type.Value): SQL.Source = SQL.sql_type_postgresql(T) |
65009 | 575 |
|
65021 | 576 |
// see https://jdbc.postgresql.org/documentation/head/8-date-time.html |
65748 | 577 |
def update_date(stmt: SQL.Statement, i: Int, date: Date): Unit = |
65740 | 578 |
if (date == null) stmt.rep.setObject(i, null) |
69980 | 579 |
else stmt.rep.setObject(i, OffsetDateTime.from(date.to(Date.timezone_utc).rep)) |
65598 | 580 |
|
75393 | 581 |
def date(res: SQL.Result, column: SQL.Column): Date = { |
65740 | 582 |
val obj = res.rep.getObject(column.name, classOf[OffsetDateTime]) |
65704 | 583 |
if (obj == null) null else Date.instant(obj.toInstant) |
584 |
} |
|
65021 | 585 |
|
65730 | 586 |
def insert_permissive(table: SQL.Table, sql: SQL.Source = ""): SQL.Source = |
77377 | 587 |
table.insert_cmd(sql = if_proper(sql, sql + " ") + "ON CONFLICT DO NOTHING") |
65703
cead65c19f2e
more direct insert_permissive statement, which avoids somewhat fragile nested transactions;
wenzelm
parents:
65702
diff
changeset
|
588 |
|
77039 | 589 |
|
77590
edc96be6b939
explicit locking for PostgreSQL --- neither available nor required for SQLite;
wenzelm
parents:
77552
diff
changeset
|
590 |
/* explicit locking: only applicable to PostgreSQL within transaction context */ |
edc96be6b939
explicit locking for PostgreSQL --- neither available nor required for SQLite;
wenzelm
parents:
77552
diff
changeset
|
591 |
// see https://www.postgresql.org/docs/current/sql-lock.html |
edc96be6b939
explicit locking for PostgreSQL --- neither available nor required for SQLite;
wenzelm
parents:
77552
diff
changeset
|
592 |
// see https://www.postgresql.org/docs/current/explicit-locking.html |
edc96be6b939
explicit locking for PostgreSQL --- neither available nor required for SQLite;
wenzelm
parents:
77552
diff
changeset
|
593 |
|
77596 | 594 |
override def lock_tables(tables: List[SQL.Table]): PostgreSQL.Source = |
595 |
"LOCK TABLE " + tables.mkString(", ") + " IN ACCESS EXCLUSIVE MODE" |
|
77590
edc96be6b939
explicit locking for PostgreSQL --- neither available nor required for SQLite;
wenzelm
parents:
77552
diff
changeset
|
596 |
|
edc96be6b939
explicit locking for PostgreSQL --- neither available nor required for SQLite;
wenzelm
parents:
77552
diff
changeset
|
597 |
|
77039 | 598 |
/* notifications: IPC via database server */ |
599 |
// see https://www.postgresql.org/docs/current/sql-notify.html |
|
600 |
||
601 |
def listen(name: String): Unit = |
|
77541 | 602 |
execute_statement("LISTEN " + SQL.ident(name)) |
77039 | 603 |
|
604 |
def unlisten(name: String = "*"): Unit = |
|
77541 | 605 |
execute_statement("UNLISTEN " + (if (name == "*") name else SQL.ident(name))) |
77039 | 606 |
|
607 |
def notify(name: String, payload: String = ""): Unit = |
|
77543 | 608 |
execute_statement("NOTIFY " + SQL.ident(name) + if_proper(payload, ", " + SQL.string(payload))) |
77039 | 609 |
|
610 |
def get_notifications(): List[PGNotification] = |
|
611 |
the_postgresql_connection.getNotifications() match { |
|
612 |
case null => Nil |
|
613 |
case array => array.toList |
|
614 |
} |
|
615 |
||
616 |
||
73367 | 617 |
override def close(): Unit = { super.close(); port_forwarding.foreach(_.close()) } |
65006 | 618 |
} |
619 |
} |