author | wenzelm |
Sat, 09 Mar 2024 20:52:06 +0100 | |
changeset 79839 | f425bbc4b2eb |
parent 79775 | 752806151432 |
child 79846 | 3d83a2554a71 |
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. |
78351 | 5 |
|
79724 | 6 |
See https://docs.oracle.com/en/java/javase/21/docs/api/java.sql/java/sql/Connection.html |
63778 | 7 |
*/ |
8 |
||
9 |
package isabelle |
|
10 |
||
69393
ed0824ef337e
static type for Library.using: avoid Java 11 warnings on "Illegal reflective access";
wenzelm
parents:
69327
diff
changeset
|
11 |
|
65021 | 12 |
import java.time.OffsetDateTime |
78538
56e8458ba262
support for execute_batch: multiple statements in one round-trip;
wenzelm
parents:
78422
diff
changeset
|
13 |
import java.sql.{DriverManager, Connection, PreparedStatement, ResultSet, SQLException} |
63779 | 14 |
|
78891
76d1382d6077
proper SQL.string syntax, following actual SQL standard instead of historic variations before PostgreSQL 9.1;
wenzelm
parents:
78863
diff
changeset
|
15 |
import org.sqlite.SQLiteConfig |
77037
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
16 |
import org.sqlite.jdbc4.JDBC4Connection |
79721
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
17 |
import org.postgresql.PGConnection |
77037
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
18 |
|
65740 | 19 |
import scala.collection.mutable |
20 |
||
63779 | 21 |
|
75393 | 22 |
object SQL { |
78547 | 23 |
lazy val time_start = Time.now() |
24 |
||
65006 | 25 |
/** SQL language **/ |
26 |
||
65730 | 27 |
type Source = String |
28 |
||
29 |
||
63778 | 30 |
/* concrete syntax */ |
31 |
||
78891
76d1382d6077
proper SQL.string syntax, following actual SQL standard instead of historic variations before PostgreSQL 9.1;
wenzelm
parents:
78863
diff
changeset
|
32 |
def string(s: String): Source = { |
76d1382d6077
proper SQL.string syntax, following actual SQL standard instead of historic variations before PostgreSQL 9.1;
wenzelm
parents:
78863
diff
changeset
|
33 |
val q = '\'' |
76d1382d6077
proper SQL.string syntax, following actual SQL standard instead of historic variations before PostgreSQL 9.1;
wenzelm
parents:
78863
diff
changeset
|
34 |
val result = new StringBuilder(s.length + 10) |
76d1382d6077
proper SQL.string syntax, following actual SQL standard instead of historic variations before PostgreSQL 9.1;
wenzelm
parents:
78863
diff
changeset
|
35 |
result += q |
76d1382d6077
proper SQL.string syntax, following actual SQL standard instead of historic variations before PostgreSQL 9.1;
wenzelm
parents:
78863
diff
changeset
|
36 |
for (c <- s.iterator) { |
76d1382d6077
proper SQL.string syntax, following actual SQL standard instead of historic variations before PostgreSQL 9.1;
wenzelm
parents:
78863
diff
changeset
|
37 |
if (c == '\u0000') error("Illegal NUL character in SQL string literal") |
76d1382d6077
proper SQL.string syntax, following actual SQL standard instead of historic variations before PostgreSQL 9.1;
wenzelm
parents:
78863
diff
changeset
|
38 |
if (c == q) result += q |
76d1382d6077
proper SQL.string syntax, following actual SQL standard instead of historic variations before PostgreSQL 9.1;
wenzelm
parents:
78863
diff
changeset
|
39 |
result += c |
63778 | 40 |
} |
78891
76d1382d6077
proper SQL.string syntax, following actual SQL standard instead of historic variations before PostgreSQL 9.1;
wenzelm
parents:
78863
diff
changeset
|
41 |
result += q |
76d1382d6077
proper SQL.string syntax, following actual SQL standard instead of historic variations before PostgreSQL 9.1;
wenzelm
parents:
78863
diff
changeset
|
42 |
result.toString |
76d1382d6077
proper SQL.string syntax, following actual SQL standard instead of historic variations before PostgreSQL 9.1;
wenzelm
parents:
78863
diff
changeset
|
43 |
} |
63778 | 44 |
|
65730 | 45 |
def ident(s: String): Source = |
65677
7d25b8dbdbfa
proper quote (amending 546020a98a91): e.g. relevant for "ISABELLE_BUILD_OPTIONS";
wenzelm
parents:
65673
diff
changeset
|
46 |
Long_Name.implode(Long_Name.explode(s).map(a => quote(a.replace("\"", "\"\"")))) |
63779 | 47 |
|
65730 | 48 |
def enclose(s: Source): Source = "(" + s + ")" |
49 |
def enclosure(ss: Iterable[Source]): Source = ss.mkString("(", ", ", ")") |
|
63791 | 50 |
|
77381
a86e346b20d8
misc tuning and clarification: more uniform use of optional "sql" in SQL.Table.delete/select;
wenzelm
parents:
77377
diff
changeset
|
51 |
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
|
52 |
(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
|
53 |
|
77403
be8e14c7da80
clarified signature, although "sql" argument is de-facto mandatory;
wenzelm
parents:
77381
diff
changeset
|
54 |
def select(columns: List[Column] = Nil, distinct: Boolean = false, sql: Source = ""): Source = |
65774 | 55 |
"SELECT " + (if (distinct) "DISTINCT " else "") + |
77403
be8e14c7da80
clarified signature, although "sql" argument is de-facto mandatory;
wenzelm
parents:
77381
diff
changeset
|
56 |
(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
|
57 |
|
65775 | 58 |
val join_outer: Source = " LEFT OUTER JOIN " |
59 |
val join_inner: Source = " INNER JOIN " |
|
65776 | 60 |
def join(outer: Boolean): Source = if (outer) join_outer else join_inner |
65775 | 61 |
|
78544 | 62 |
def MULTI(args: Iterable[Source]): Source = |
63 |
args.iterator.filter(_.nonEmpty).mkString(";\n") |
|
64 |
def multi(args: Source*): Source = MULTI(args) |
|
65 |
||
77370 | 66 |
def infix(op: Source, args: Iterable[Source]): Source = { |
67 |
val body = args.iterator.filter(_.nonEmpty).mkString(" " + op + " ") |
|
68 |
if_proper(body, enclose(body)) |
|
69 |
} |
|
70 |
||
71 |
def AND(args: Iterable[Source]): Source = infix("AND", args) |
|
72 |
def OR(args: Iterable[Source]): Source = infix("OR", args) |
|
73 |
||
74 |
def and(args: Source*): Source = AND(args) |
|
75 |
def or(args: Source*): Source = OR(args) |
|
76 |
||
77 |
val TRUE: Source = "TRUE" |
|
78 |
val FALSE: Source = "FALSE" |
|
79 |
||
78152 | 80 |
def equal(sql: Source, x: Int): Source = sql + " = " + x |
81 |
def equal(sql: Source, x: Long): Source = sql + " = " + x |
|
82 |
def equal(sql: Source, x: String): Source = sql + " = " + string(x) |
|
77375 | 83 |
|
84 |
def member(sql: Source, set: Iterable[String]): Source = |
|
77370 | 85 |
if (set.isEmpty) FALSE |
77375 | 86 |
else OR(set.iterator.map(equal(sql, _)).toList) |
65804 | 87 |
|
77370 | 88 |
def where(sql: Source): Source = if_proper(sql, " WHERE " + sql) |
78153 | 89 |
def where_and(args: Source*): Source = where(and(args:_*)) |
90 |
def where_or(args: Source*): Source = where(or(args:_*)) |
|
77370 | 91 |
|
92 |
||
65008 | 93 |
/* types */ |
94 |
||
78598 | 95 |
enum Type { case Boolean, Int, Long, Double, String, Bytes, Date } |
96 |
||
78607 | 97 |
val sql_type_postgresql: Type => Source = |
98 |
{ |
|
99 |
case Type.Boolean => "BOOLEAN" |
|
100 |
case Type.Int => "INTEGER" |
|
101 |
case Type.Long => "BIGINT" |
|
102 |
case Type.Double => "DOUBLE PRECISION" |
|
103 |
case Type.String => "TEXT" |
|
104 |
case Type.Bytes => "BYTEA" |
|
105 |
case Type.Date => "TIMESTAMP WITH TIME ZONE" |
|
106 |
} |
|
65008 | 107 |
|
78607 | 108 |
val sql_type_sqlite: Type => Source = |
109 |
{ |
|
110 |
case Type.Boolean => "INTEGER" |
|
111 |
case Type.Bytes => "BLOB" |
|
112 |
case Type.Date => "TEXT" |
|
113 |
case t => sql_type_postgresql(t) |
|
114 |
} |
|
65008 | 115 |
|
116 |
||
63779 | 117 |
/* columns */ |
118 |
||
75393 | 119 |
object Column { |
65280 | 120 |
def bool(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 121 |
Column(name, Type.Boolean, strict, primary_key) |
65280 | 122 |
def int(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 123 |
Column(name, Type.Int, strict, primary_key) |
65280 | 124 |
def long(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 125 |
Column(name, Type.Long, strict, primary_key) |
65280 | 126 |
def double(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 127 |
Column(name, Type.Double, strict, primary_key) |
65280 | 128 |
def string(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 129 |
Column(name, Type.String, strict, primary_key) |
65280 | 130 |
def bytes(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 131 |
Column(name, Type.Bytes, strict, primary_key) |
65280 | 132 |
def date(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 133 |
Column(name, Type.Date, strict, primary_key) |
63779 | 134 |
} |
135 |
||
65018 | 136 |
sealed case class Column( |
75393 | 137 |
name: String, |
78598 | 138 |
T: Type, |
75393 | 139 |
strict: Boolean = false, |
140 |
primary_key: Boolean = false, |
|
141 |
expr: SQL.Source = "" |
|
142 |
) { |
|
66857 | 143 |
def make_primary_key: Column = copy(primary_key = true) |
144 |
||
65691 | 145 |
def apply(table: Table): Column = |
146 |
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
|
147 |
|
65780 | 148 |
def ident: Source = |
149 |
if (expr == "") SQL.ident(name) |
|
150 |
else enclose(expr) + " AS " + SQL.ident(name) |
|
65695 | 151 |
|
78598 | 152 |
def decl(sql_type: Type => Source): Source = |
65695 | 153 |
ident + " " + sql_type(T) + (if (strict || primary_key) " NOT NULL" else "") |
63781 | 154 |
|
66856 | 155 |
def defined: String = ident + " IS NOT NULL" |
156 |
def undefined: String = ident + " IS NULL" |
|
157 |
||
78152 | 158 |
def equal(x: Int): Source = SQL.equal(ident, x) |
159 |
def equal(x: Long): Source = SQL.equal(ident, x) |
|
160 |
def equal(x: String): Source = SQL.equal(ident, x) |
|
161 |
||
162 |
def where_equal(x: Int): Source = SQL.where(equal(x)) |
|
163 |
def where_equal(x: Long): Source = SQL.where(equal(x)) |
|
164 |
def where_equal(x: String): Source = SQL.where(equal(x)) |
|
165 |
||
77375 | 166 |
def member(set: Iterable[String]): Source = SQL.member(ident, set) |
167 |
def where_member(set: Iterable[String]): Source = SQL.where(member(set)) |
|
65593 | 168 |
|
78151 | 169 |
def max: Column = copy(expr = "MAX(" + ident + ")") |
170 |
||
65730 | 171 |
override def toString: Source = ident |
63779 | 172 |
} |
173 |
||
76870 | 174 |
def order_by(columns: List[Column], descending: Boolean = false): Source = |
175 |
" ORDER BY " + columns.mkString(", ") + (if (descending) " DESC" else "") |
|
176 |
||
63780 | 177 |
|
178 |
/* tables */ |
|
179 |
||
75393 | 180 |
sealed case class Table(name: String, columns: List[Column], body: Source = "") { |
63781 | 181 |
Library.duplicates(columns.map(_.name)) match { |
182 |
case Nil => |
|
183 |
case bad => error("Duplicate column names " + commas_quote(bad) + " for table " + quote(name)) |
|
184 |
} |
|
185 |
||
65730 | 186 |
def ident: Source = SQL.ident(name) |
65691 | 187 |
|
65730 | 188 |
def query: Source = |
65696 | 189 |
if (body == "") error("Missing SQL body for table " + quote(name)) |
190 |
else SQL.enclose(body) |
|
65649 | 191 |
|
65778 | 192 |
def query_named: Source = query + " AS " + SQL.ident(name) |
65702 | 193 |
|
78598 | 194 |
def create(sql_type: Type => Source): Source = { |
65325 | 195 |
val primary_key = |
196 |
columns.filter(_.primary_key).map(_.name) match { |
|
197 |
case Nil => Nil |
|
198 |
case keys => List("PRIMARY KEY " + enclosure(keys)) |
|
199 |
} |
|
78392
27c2fa1db6ed
more uniform guard (!exists_table(table)): avoid "ALTER TABLE" on already existing table, which could lead to deadlocks if this is presently locked;
wenzelm
parents:
78391
diff
changeset
|
200 |
"CREATE TABLE " + ident + " " + enclosure(columns.map(_.decl(sql_type)) ::: primary_key) |
63781 | 201 |
} |
202 |
||
77365 | 203 |
def insert_cmd(cmd: Source = "INSERT", sql: Source = ""): Source = |
77377 | 204 |
cmd + " INTO " + ident + " VALUES " + enclosure(columns.map(_ => "?")) + SQL.separate(sql) |
63791 | 205 |
|
77365 | 206 |
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
|
207 |
|
65730 | 208 |
def delete(sql: Source = ""): Source = |
77377 | 209 |
"DELETE FROM " + ident + SQL.separate(sql) |
65319 | 210 |
|
77537 | 211 |
def update(update_columns: List[Column] = Nil, sql: Source = ""): Source = |
212 |
"UPDATE " + ident + " SET " + commas(update_columns.map(c => c.ident + " = ?")) + |
|
213 |
SQL.separate(sql) |
|
214 |
||
65774 | 215 |
def select( |
77381
a86e346b20d8
misc tuning and clarification: more uniform use of optional "sql" in SQL.Table.delete/select;
wenzelm
parents:
77377
diff
changeset
|
216 |
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
|
217 |
distinct: Boolean = false, |
a86e346b20d8
misc tuning and clarification: more uniform use of optional "sql" in SQL.Table.delete/select;
wenzelm
parents:
77377
diff
changeset
|
218 |
sql: Source = "" |
77403
be8e14c7da80
clarified signature, although "sql" argument is de-facto mandatory;
wenzelm
parents:
77381
diff
changeset
|
219 |
): Source = SQL.select(select_columns, distinct = distinct, sql = ident + SQL.separate(sql)) |
63790 | 220 |
|
65730 | 221 |
override def toString: Source = ident |
63780 | 222 |
} |
63790 | 223 |
|
224 |
||
77596 | 225 |
/* table groups */ |
226 |
||
227 |
object Tables { |
|
228 |
def list(list: List[Table]): Tables = new Tables(list) |
|
229 |
def apply(args: Table*): Tables = list(args.toList) |
|
230 |
} |
|
231 |
||
232 |
final class Tables private(val list: List[Table]) extends Iterable[Table] { |
|
233 |
override def toString: String = list.mkString("SQL.Tables(", ", ", ")") |
|
234 |
||
235 |
def iterator: Iterator[Table] = list.iterator |
|
236 |
||
79839 | 237 |
def index(table: Table): Int = |
238 |
iterator.zipWithIndex |
|
239 |
.collectFirst({ case (t, i) if t.name == table.name => i }) |
|
240 |
.getOrElse(error("No table " + quote(table.name))) |
|
241 |
||
77596 | 242 |
// requires transaction |
78355
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
243 |
def lock(db: Database, create: Boolean = false): Boolean = { |
78154 | 244 |
if (create) foreach(db.create_table(_)) |
77596 | 245 |
val sql = db.lock_tables(list) |
78355
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
246 |
if (sql.nonEmpty) { db.execute_statement(sql); true } |
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
247 |
else false |
77596 | 248 |
} |
249 |
} |
|
250 |
||
79775
752806151432
clarified signature: incorporate guard into Logger;
wenzelm
parents:
79728
diff
changeset
|
251 |
|
752806151432
clarified signature: incorporate guard into Logger;
wenzelm
parents:
79728
diff
changeset
|
252 |
/* access data */ |
752806151432
clarified signature: incorporate guard into Logger;
wenzelm
parents:
79728
diff
changeset
|
253 |
|
752806151432
clarified signature: incorporate guard into Logger;
wenzelm
parents:
79728
diff
changeset
|
254 |
def transaction_logger(): Logger = |
752806151432
clarified signature: incorporate guard into Logger;
wenzelm
parents:
79728
diff
changeset
|
255 |
new System_Logger(guard_time = Time.guard_property("isabelle.transaction_trace")) |
752806151432
clarified signature: incorporate guard into Logger;
wenzelm
parents:
79728
diff
changeset
|
256 |
|
78187
2df0f3604a67
clarified signature: more explicit class SQL.Data;
wenzelm
parents:
78167
diff
changeset
|
257 |
abstract class Data(table_prefix: String = "") { |
78389
41e8ae87184d
clarified signature: eliminate SQL.Tables.empty to avoid confusion (see also 0bd366fad888);
wenzelm
parents:
78386
diff
changeset
|
258 |
def tables: Tables |
78187
2df0f3604a67
clarified signature: more explicit class SQL.Data;
wenzelm
parents:
78167
diff
changeset
|
259 |
|
2df0f3604a67
clarified signature: more explicit class SQL.Data;
wenzelm
parents:
78167
diff
changeset
|
260 |
def transaction_lock[A]( |
2df0f3604a67
clarified signature: more explicit class SQL.Data;
wenzelm
parents:
78167
diff
changeset
|
261 |
db: Database, |
78207 | 262 |
create: Boolean = false, |
78356 | 263 |
label: String = "", |
79775
752806151432
clarified signature: incorporate guard into Logger;
wenzelm
parents:
79728
diff
changeset
|
264 |
log: Logger = transaction_logger() |
78207 | 265 |
)(body: => A): A = { |
78369
ba71ea02d965
more robust Java/Scala multithreading: transaction is always connection.synchronized;
wenzelm
parents:
78366
diff
changeset
|
266 |
db.transaction_lock(tables, create = create, label = label, log = log)(body) |
78207 | 267 |
} |
78187
2df0f3604a67
clarified signature: more explicit class SQL.Data;
wenzelm
parents:
78167
diff
changeset
|
268 |
|
78266 | 269 |
def make_table(columns: List[Column], body: String = "", name: String = ""): Table = { |
78187
2df0f3604a67
clarified signature: more explicit class SQL.Data;
wenzelm
parents:
78167
diff
changeset
|
270 |
val table_name = |
2df0f3604a67
clarified signature: more explicit class SQL.Data;
wenzelm
parents:
78167
diff
changeset
|
271 |
List(proper_string(table_prefix), proper_string(name)).flatten.mkString("_") |
2df0f3604a67
clarified signature: more explicit class SQL.Data;
wenzelm
parents:
78167
diff
changeset
|
272 |
require(table_name.nonEmpty, "Undefined database table name") |
2df0f3604a67
clarified signature: more explicit class SQL.Data;
wenzelm
parents:
78167
diff
changeset
|
273 |
Table(table_name, columns, body = body) |
2df0f3604a67
clarified signature: more explicit class SQL.Data;
wenzelm
parents:
78167
diff
changeset
|
274 |
} |
2df0f3604a67
clarified signature: more explicit class SQL.Data;
wenzelm
parents:
78167
diff
changeset
|
275 |
} |
2df0f3604a67
clarified signature: more explicit class SQL.Data;
wenzelm
parents:
78167
diff
changeset
|
276 |
|
77596 | 277 |
|
65012 | 278 |
|
279 |
/** SQL database operations **/ |
|
280 |
||
65740 | 281 |
/* statements */ |
282 |
||
78538
56e8458ba262
support for execute_batch: multiple statements in one round-trip;
wenzelm
parents:
78422
diff
changeset
|
283 |
class Batch_Error(val results: List[Int]) extends SQLException |
56e8458ba262
support for execute_batch: multiple statements in one round-trip;
wenzelm
parents:
78422
diff
changeset
|
284 |
|
75393 | 285 |
class Statement private[SQL](val db: Database, val rep: PreparedStatement) extends AutoCloseable { |
65740 | 286 |
stmt => |
287 |
||
75393 | 288 |
object bool { |
73340 | 289 |
def update(i: Int, x: Boolean): Unit = rep.setBoolean(i, x) |
75393 | 290 |
def update(i: Int, x: Option[Boolean]): Unit = { |
65748 | 291 |
if (x.isDefined) update(i, x.get) |
292 |
else rep.setNull(i, java.sql.Types.BOOLEAN) |
|
293 |
} |
|
65740 | 294 |
} |
75393 | 295 |
object int { |
73340 | 296 |
def update(i: Int, x: Int): Unit = rep.setInt(i, x) |
75393 | 297 |
def update(i: Int, x: Option[Int]): Unit = { |
65748 | 298 |
if (x.isDefined) update(i, x.get) |
299 |
else rep.setNull(i, java.sql.Types.INTEGER) |
|
300 |
} |
|
65740 | 301 |
} |
75393 | 302 |
object long { |
73340 | 303 |
def update(i: Int, x: Long): Unit = rep.setLong(i, x) |
75393 | 304 |
def update(i: Int, x: Option[Long]): Unit = { |
65748 | 305 |
if (x.isDefined) update(i, x.get) |
306 |
else rep.setNull(i, java.sql.Types.BIGINT) |
|
307 |
} |
|
65740 | 308 |
} |
75393 | 309 |
object double { |
73340 | 310 |
def update(i: Int, x: Double): Unit = rep.setDouble(i, x) |
75393 | 311 |
def update(i: Int, x: Option[Double]): Unit = { |
65748 | 312 |
if (x.isDefined) update(i, x.get) |
313 |
else rep.setNull(i, java.sql.Types.DOUBLE) |
|
314 |
} |
|
315 |
} |
|
75393 | 316 |
object string { |
73340 | 317 |
def update(i: Int, x: String): Unit = rep.setString(i, x) |
65748 | 318 |
def update(i: Int, x: Option[String]): Unit = update(i, x.orNull) |
65740 | 319 |
} |
75393 | 320 |
object bytes { |
321 |
def update(i: Int, bytes: Bytes): Unit = { |
|
65748 | 322 |
if (bytes == null) rep.setBytes(i, null) |
323 |
else rep.setBinaryStream(i, bytes.stream(), bytes.length) |
|
324 |
} |
|
325 |
def update(i: Int, bytes: Option[Bytes]): Unit = update(i, bytes.orNull) |
|
65740 | 326 |
} |
75393 | 327 |
object date { |
65748 | 328 |
def update(i: Int, date: Date): Unit = db.update_date(stmt, i, date) |
329 |
def update(i: Int, date: Option[Date]): Unit = update(i, date.orNull) |
|
330 |
} |
|
65740 | 331 |
|
332 |
def execute(): Boolean = rep.execute() |
|
78538
56e8458ba262
support for execute_batch: multiple statements in one round-trip;
wenzelm
parents:
78422
diff
changeset
|
333 |
|
78554
54991440905e
clarified signature: proper treatment of implicit state (amending d0c9d277620e);
wenzelm
parents:
78551
diff
changeset
|
334 |
def execute_batch(batch: IterableOnce[Statement => Unit]): Unit = { |
54991440905e
clarified signature: proper treatment of implicit state (amending d0c9d277620e);
wenzelm
parents:
78551
diff
changeset
|
335 |
val it = batch.iterator |
54991440905e
clarified signature: proper treatment of implicit state (amending d0c9d277620e);
wenzelm
parents:
78551
diff
changeset
|
336 |
if (it.nonEmpty) { |
54991440905e
clarified signature: proper treatment of implicit state (amending d0c9d277620e);
wenzelm
parents:
78551
diff
changeset
|
337 |
for (body <- it) { body(this); rep.addBatch() } |
78540 | 338 |
val res = rep.executeBatch() |
339 |
if (!res.forall(i => i >= 0 || i == java.sql.Statement.SUCCESS_NO_INFO)) { |
|
340 |
throw new Batch_Error(res.toList) |
|
341 |
} |
|
78538
56e8458ba262
support for execute_batch: multiple statements in one round-trip;
wenzelm
parents:
78422
diff
changeset
|
342 |
} |
56e8458ba262
support for execute_batch: multiple statements in one round-trip;
wenzelm
parents:
78422
diff
changeset
|
343 |
} |
56e8458ba262
support for execute_batch: multiple statements in one round-trip;
wenzelm
parents:
78422
diff
changeset
|
344 |
|
65740 | 345 |
def execute_query(): Result = new Result(this, rep.executeQuery()) |
346 |
||
78353 | 347 |
override def close(): Unit = rep.close() |
65740 | 348 |
} |
349 |
||
350 |
||
63790 | 351 |
/* results */ |
352 |
||
78352 | 353 |
class Result private[SQL](val stmt: Statement, val rep: ResultSet) extends AutoCloseable { |
65740 | 354 |
res => |
355 |
||
356 |
def next(): Boolean = rep.next() |
|
357 |
||
75393 | 358 |
def iterator[A](get: Result => A): Iterator[A] = new Iterator[A] { |
65740 | 359 |
private var _next: Boolean = res.next() |
360 |
def hasNext: Boolean = _next |
|
73337 | 361 |
def next(): A = { val x = get(res); _next = res.next(); x } |
65740 | 362 |
} |
363 |
||
364 |
def bool(column: Column): Boolean = rep.getBoolean(column.name) |
|
365 |
def int(column: Column): Int = rep.getInt(column.name) |
|
366 |
def long(column: Column): Long = rep.getLong(column.name) |
|
367 |
def double(column: Column): Double = rep.getDouble(column.name) |
|
75393 | 368 |
def string(column: Column): String = { |
65740 | 369 |
val s = rep.getString(column.name) |
370 |
if (s == null) "" else s |
|
371 |
} |
|
75393 | 372 |
def bytes(column: Column): Bytes = { |
65740 | 373 |
val bs = rep.getBytes(column.name) |
374 |
if (bs == null) Bytes.empty else Bytes(bs) |
|
375 |
} |
|
376 |
def date(column: Column): Date = stmt.db.date(res, column) |
|
377 |
||
71601 | 378 |
def timing(c1: Column, c2: Column, c3: Column): Timing = |
65741 | 379 |
Timing(Time.ms(long(c1)), Time.ms(long(c2)), Time.ms(long(c3))) |
380 |
||
75393 | 381 |
def get[A](column: Column, f: Column => A): Option[A] = { |
65740 | 382 |
val x = f(column) |
77598 | 383 |
if (rep.wasNull || x == null) None else Some(x) |
65740 | 384 |
} |
71601 | 385 |
def get_bool(column: Column): Option[Boolean] = get(column, bool) |
386 |
def get_int(column: Column): Option[Int] = get(column, int) |
|
387 |
def get_long(column: Column): Option[Long] = get(column, long) |
|
388 |
def get_double(column: Column): Option[Double] = get(column, double) |
|
389 |
def get_string(column: Column): Option[String] = get(column, string) |
|
390 |
def get_bytes(column: Column): Option[Bytes] = get(column, bytes) |
|
391 |
def get_date(column: Column): Option[Date] = get(column, date) |
|
78352 | 392 |
|
393 |
override def close(): Unit = rep.close() |
|
63790 | 394 |
} |
65006 | 395 |
|
65740 | 396 |
|
79721
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
397 |
/* notifications: IPC via database server */ |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
398 |
|
79728 | 399 |
sealed case class Notification(channel: String, payload: String = "") { |
400 |
override def toString = |
|
401 |
"Notification(" + channel + if_proper(payload, "," + payload) + ")" |
|
402 |
} |
|
79721
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
403 |
|
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
404 |
|
65740 | 405 |
/* database */ |
406 |
||
75393 | 407 |
trait Database extends AutoCloseable { |
65740 | 408 |
db => |
409 |
||
77542 | 410 |
def is_sqlite: Boolean = isInstanceOf[SQLite.Database] |
411 |
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
|
412 |
|
78389
41e8ae87184d
clarified signature: eliminate SQL.Tables.empty to avoid confusion (see also 0bd366fad888);
wenzelm
parents:
78386
diff
changeset
|
413 |
def vacuum(tables: List[SQL.Table] = Nil): Unit = |
78390
a84f8fca0833
clarified "vacuum" (again, reverting 0bd366fad888);
wenzelm
parents:
78389
diff
changeset
|
414 |
if (is_sqlite) execute_statement("VACUUM") // always FULL |
a84f8fca0833
clarified "vacuum" (again, reverting 0bd366fad888);
wenzelm
parents:
78389
diff
changeset
|
415 |
else if (tables.isEmpty) execute_statement("VACUUM FULL") |
a84f8fca0833
clarified "vacuum" (again, reverting 0bd366fad888);
wenzelm
parents:
78389
diff
changeset
|
416 |
else if (postgresql_major_version.get <= 10) { |
a84f8fca0833
clarified "vacuum" (again, reverting 0bd366fad888);
wenzelm
parents:
78389
diff
changeset
|
417 |
for (t <- tables) execute_statement("VACUUM " + t.ident) |
78273
95a3bb4d7e38
clarified "vacuum" operation for various database versions (PostgreSQL <= 10 is strictly speaking obsolete, but still used on some test machines);
wenzelm
parents:
78271
diff
changeset
|
418 |
} |
78390
a84f8fca0833
clarified "vacuum" (again, reverting 0bd366fad888);
wenzelm
parents:
78389
diff
changeset
|
419 |
else execute_statement("VACUUM" + commas(tables.map(_.ident))) |
77348 | 420 |
|
77527
790085b1002f
more detailed table "isabelle_build_serial": allow to monitor activity of build_process instances;
wenzelm
parents:
77403
diff
changeset
|
421 |
def now(): Date |
790085b1002f
more detailed table "isabelle_build_serial": allow to monitor activity of build_process instances;
wenzelm
parents:
77403
diff
changeset
|
422 |
|
65740 | 423 |
|
65008 | 424 |
/* types */ |
425 |
||
78598 | 426 |
def sql_type(T: Type): Source |
65008 | 427 |
|
428 |
||
65006 | 429 |
/* connection */ |
430 |
||
431 |
def connection: Connection |
|
432 |
||
77037
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
433 |
def sqlite_connection: Option[JDBC4Connection] = |
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
434 |
connection match { case conn: JDBC4Connection => Some(conn) case _ => None } |
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
435 |
|
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
436 |
def postgresql_connection: Option[PGConnection] = |
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
437 |
connection match { case conn: PGConnection => Some(conn) case _ => None } |
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
438 |
|
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
439 |
def the_sqlite_connection: JDBC4Connection = |
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
440 |
sqlite_connection getOrElse |
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
441 |
error("SQLite connection expected, but found " + connection.getClass.getName) |
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
442 |
|
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
443 |
def the_postgresql_connection: PGConnection = |
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
444 |
postgresql_connection getOrElse |
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
445 |
error("PostgreSQL connection expected, but found " + connection.getClass.getName) |
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
446 |
|
78273
95a3bb4d7e38
clarified "vacuum" operation for various database versions (PostgreSQL <= 10 is strictly speaking obsolete, but still used on some test machines);
wenzelm
parents:
78271
diff
changeset
|
447 |
def postgresql_major_version: Option[Int] = |
95a3bb4d7e38
clarified "vacuum" operation for various database versions (PostgreSQL <= 10 is strictly speaking obsolete, but still used on some test machines);
wenzelm
parents:
78271
diff
changeset
|
448 |
if (is_postgresql) { |
95a3bb4d7e38
clarified "vacuum" operation for various database versions (PostgreSQL <= 10 is strictly speaking obsolete, but still used on some test machines);
wenzelm
parents:
78271
diff
changeset
|
449 |
def err(s: String): Nothing = error("Bad PostgreSQL version " + s) |
95a3bb4d7e38
clarified "vacuum" operation for various database versions (PostgreSQL <= 10 is strictly speaking obsolete, but still used on some test machines);
wenzelm
parents:
78271
diff
changeset
|
450 |
|
95a3bb4d7e38
clarified "vacuum" operation for various database versions (PostgreSQL <= 10 is strictly speaking obsolete, but still used on some test machines);
wenzelm
parents:
78271
diff
changeset
|
451 |
the_postgresql_connection.getParameterStatus("server_version") match { |
95a3bb4d7e38
clarified "vacuum" operation for various database versions (PostgreSQL <= 10 is strictly speaking obsolete, but still used on some test machines);
wenzelm
parents:
78271
diff
changeset
|
452 |
case null => err("null") |
95a3bb4d7e38
clarified "vacuum" operation for various database versions (PostgreSQL <= 10 is strictly speaking obsolete, but still used on some test machines);
wenzelm
parents:
78271
diff
changeset
|
453 |
case str => |
95a3bb4d7e38
clarified "vacuum" operation for various database versions (PostgreSQL <= 10 is strictly speaking obsolete, but still used on some test machines);
wenzelm
parents:
78271
diff
changeset
|
454 |
str.iterator.takeWhile(Symbol.is_ascii_digit).mkString match { |
95a3bb4d7e38
clarified "vacuum" operation for various database versions (PostgreSQL <= 10 is strictly speaking obsolete, but still used on some test machines);
wenzelm
parents:
78271
diff
changeset
|
455 |
case Value.Int(m) => Some(m) |
95a3bb4d7e38
clarified "vacuum" operation for various database versions (PostgreSQL <= 10 is strictly speaking obsolete, but still used on some test machines);
wenzelm
parents:
78271
diff
changeset
|
456 |
case _ => err(quote(str)) |
95a3bb4d7e38
clarified "vacuum" operation for various database versions (PostgreSQL <= 10 is strictly speaking obsolete, but still used on some test machines);
wenzelm
parents:
78271
diff
changeset
|
457 |
} |
95a3bb4d7e38
clarified "vacuum" operation for various database versions (PostgreSQL <= 10 is strictly speaking obsolete, but still used on some test machines);
wenzelm
parents:
78271
diff
changeset
|
458 |
} |
95a3bb4d7e38
clarified "vacuum" operation for various database versions (PostgreSQL <= 10 is strictly speaking obsolete, but still used on some test machines);
wenzelm
parents:
78271
diff
changeset
|
459 |
} |
95a3bb4d7e38
clarified "vacuum" operation for various database versions (PostgreSQL <= 10 is strictly speaking obsolete, but still used on some test machines);
wenzelm
parents:
78271
diff
changeset
|
460 |
else None |
95a3bb4d7e38
clarified "vacuum" operation for various database versions (PostgreSQL <= 10 is strictly speaking obsolete, but still used on some test machines);
wenzelm
parents:
78271
diff
changeset
|
461 |
|
78353 | 462 |
override def close(): Unit = connection.close() |
65006 | 463 |
|
78369
ba71ea02d965
more robust Java/Scala multithreading: transaction is always connection.synchronized;
wenzelm
parents:
78366
diff
changeset
|
464 |
def transaction[A](body: => A): A = connection.synchronized { |
78383
d032bf604e93
more robust: exclude accidental nesting (synchronized block is re-entrant);
wenzelm
parents:
78381
diff
changeset
|
465 |
require(connection.getAutoCommit(), "transaction already active") |
65006 | 466 |
try { |
467 |
connection.setAutoCommit(false) |
|
65022 | 468 |
try { |
469 |
val result = body |
|
75776
72e77c8307ec
tuned signature, following hints by IntelliJ IDEA;
wenzelm
parents:
75393
diff
changeset
|
470 |
connection.commit() |
65022 | 471 |
result |
472 |
} |
|
78386
ee588c4b5557
more elementary transaction implementation (despite fda3f7a158b9 and 9da65bc75610);
wenzelm
parents:
78384
diff
changeset
|
473 |
catch { case exn: Throwable => connection.rollback(); throw exn } |
65006 | 474 |
} |
78383
d032bf604e93
more robust: exclude accidental nesting (synchronized block is re-entrant);
wenzelm
parents:
78381
diff
changeset
|
475 |
finally { connection.setAutoCommit(true) } |
65006 | 476 |
} |
477 |
||
78355
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
478 |
def transaction_lock[A]( |
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
479 |
tables: Tables, |
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
480 |
create: Boolean = false, |
78356 | 481 |
label: String = "", |
79775
752806151432
clarified signature: incorporate guard into Logger;
wenzelm
parents:
79728
diff
changeset
|
482 |
log: Logger = transaction_logger() |
78355
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
483 |
)(body: => A): A = { |
78362 | 484 |
val trace_count = - SQL.transaction_count() |
78355
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
485 |
val trace_start = Time.now() |
78361 | 486 |
var trace_nl = false |
78355
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
487 |
|
78380 | 488 |
def trace(msg: String): Unit = { |
78358
f5cf8e500dee
clarified isabelle.transaction_log: support time_min (in ms);
wenzelm
parents:
78356
diff
changeset
|
489 |
val trace_time = Time.now() - trace_start |
79775
752806151432
clarified signature: incorporate guard into Logger;
wenzelm
parents:
79728
diff
changeset
|
490 |
if (log.guard(trace_time)) { |
78557 | 491 |
time_start |
492 |
val nl = |
|
493 |
if (trace_nl) "" |
|
494 |
else { trace_nl = true; "\nnow = " + (Time.now() - time_start).toString + "\n" } |
|
78361 | 495 |
log(nl + trace_time + " transaction " + trace_count + |
496 |
if_proper(label, " " + label) + ": " + msg) |
|
78355
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
497 |
} |
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
498 |
} |
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
499 |
|
78373 | 500 |
try { |
501 |
val res = |
|
502 |
transaction { |
|
503 |
trace("begin") |
|
504 |
if (tables.lock(db, create = create)) { |
|
78380 | 505 |
trace("locked " + commas_quote(tables.list.map(_.name))) |
78373 | 506 |
} |
507 |
val res = Exn.capture { body } |
|
508 |
trace("end") |
|
509 |
res |
|
78371 | 510 |
} |
78373 | 511 |
trace("commit") |
512 |
Exn.release(res) |
|
513 |
} |
|
514 |
catch { case exn: Throwable => trace("crash"); throw exn } |
|
78355
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
515 |
} |
77596 | 516 |
|
517 |
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
|
518 |
|
65006 | 519 |
|
65740 | 520 |
/* statements and results */ |
521 |
||
522 |
def statement(sql: Source): Statement = |
|
523 |
new Statement(db, connection.prepareStatement(sql)) |
|
65006 | 524 |
|
65740 | 525 |
def using_statement[A](sql: Source)(f: Statement => A): A = |
526 |
using(statement(sql))(f) |
|
65006 | 527 |
|
77541 | 528 |
def execute_statement(sql: Source, body: Statement => Unit = _ => ()): Unit = |
529 |
using_statement(sql) { stmt => body(stmt); stmt.execute() } |
|
77540 | 530 |
|
78538
56e8458ba262
support for execute_batch: multiple statements in one round-trip;
wenzelm
parents:
78422
diff
changeset
|
531 |
def execute_batch_statement( |
56e8458ba262
support for execute_batch: multiple statements in one round-trip;
wenzelm
parents:
78422
diff
changeset
|
532 |
sql: Source, |
78554
54991440905e
clarified signature: proper treatment of implicit state (amending d0c9d277620e);
wenzelm
parents:
78551
diff
changeset
|
533 |
batch: IterableOnce[Statement => Unit] = Nil |
78538
56e8458ba262
support for execute_batch: multiple statements in one round-trip;
wenzelm
parents:
78422
diff
changeset
|
534 |
): Unit = using_statement(sql) { stmt => stmt.execute_batch(batch) } |
56e8458ba262
support for execute_batch: multiple statements in one round-trip;
wenzelm
parents:
78422
diff
changeset
|
535 |
|
77552 | 536 |
def execute_query_statement[A, B]( |
537 |
sql: Source, |
|
538 |
make_result: Iterator[A] => B, |
|
539 |
get: Result => A |
|
78352 | 540 |
): B = { |
541 |
using_statement(sql) { stmt => |
|
542 |
using(stmt.execute_query()) { res => make_result(res.iterator(get)) } |
|
543 |
} |
|
544 |
} |
|
77552 | 545 |
|
546 |
def execute_query_statementO[A](sql: Source, get: Result => A): Option[A] = |
|
547 |
execute_query_statement[A, Option[A]](sql, _.nextOption, get) |
|
548 |
||
549 |
def execute_query_statementB(sql: Source): Boolean = |
|
78352 | 550 |
using_statement(sql)(stmt => using(stmt.execute_query())(_.next())) |
77552 | 551 |
|
65748 | 552 |
def update_date(stmt: Statement, i: Int, date: Date): Unit |
65740 | 553 |
def date(res: Result, column: Column): Date |
65006 | 554 |
|
65730 | 555 |
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
|
556 |
|
65006 | 557 |
|
65669 | 558 |
/* tables and views */ |
65006 | 559 |
|
78375
234f2ff9afe6
clarified signature: more specific exists_table --- avoid retrieving full list beforehand;
wenzelm
parents:
78373
diff
changeset
|
560 |
def get_tables(pattern: String = "%"): List[String] = { |
65740 | 561 |
val result = new mutable.ListBuffer[String] |
78375
234f2ff9afe6
clarified signature: more specific exists_table --- avoid retrieving full list beforehand;
wenzelm
parents:
78373
diff
changeset
|
562 |
val rs = connection.getMetaData.getTables(null, null, pattern, null) |
65740 | 563 |
while (rs.next) { result += rs.getString(3) } |
564 |
result.toList |
|
565 |
} |
|
65006 | 566 |
|
78375
234f2ff9afe6
clarified signature: more specific exists_table --- avoid retrieving full list beforehand;
wenzelm
parents:
78373
diff
changeset
|
567 |
def exists_table(name: String): Boolean = { |
234f2ff9afe6
clarified signature: more specific exists_table --- avoid retrieving full list beforehand;
wenzelm
parents:
78373
diff
changeset
|
568 |
val escape = connection.getMetaData.getSearchStringEscape |
234f2ff9afe6
clarified signature: more specific exists_table --- avoid retrieving full list beforehand;
wenzelm
parents:
78373
diff
changeset
|
569 |
val pattern = |
234f2ff9afe6
clarified signature: more specific exists_table --- avoid retrieving full list beforehand;
wenzelm
parents:
78373
diff
changeset
|
570 |
name.iterator.map(c => |
234f2ff9afe6
clarified signature: more specific exists_table --- avoid retrieving full list beforehand;
wenzelm
parents:
78373
diff
changeset
|
571 |
(if (c == '_' || c == '%' || c == escape(0)) escape else "") + c).mkString |
234f2ff9afe6
clarified signature: more specific exists_table --- avoid retrieving full list beforehand;
wenzelm
parents:
78373
diff
changeset
|
572 |
get_tables(pattern = pattern).nonEmpty |
234f2ff9afe6
clarified signature: more specific exists_table --- avoid retrieving full list beforehand;
wenzelm
parents:
78373
diff
changeset
|
573 |
} |
234f2ff9afe6
clarified signature: more specific exists_table --- avoid retrieving full list beforehand;
wenzelm
parents:
78373
diff
changeset
|
574 |
|
234f2ff9afe6
clarified signature: more specific exists_table --- avoid retrieving full list beforehand;
wenzelm
parents:
78373
diff
changeset
|
575 |
def exists_table(table: Table): Boolean = exists_table(table.name) |
234f2ff9afe6
clarified signature: more specific exists_table --- avoid retrieving full list beforehand;
wenzelm
parents:
78373
diff
changeset
|
576 |
|
78392
27c2fa1db6ed
more uniform guard (!exists_table(table)): avoid "ALTER TABLE" on already existing table, which could lead to deadlocks if this is presently locked;
wenzelm
parents:
78391
diff
changeset
|
577 |
def create_table(table: Table, sql: Source = ""): Unit = { |
27c2fa1db6ed
more uniform guard (!exists_table(table)): avoid "ALTER TABLE" on already existing table, which could lead to deadlocks if this is presently locked;
wenzelm
parents:
78391
diff
changeset
|
578 |
if (!exists_table(table)) { |
27c2fa1db6ed
more uniform guard (!exists_table(table)): avoid "ALTER TABLE" on already existing table, which could lead to deadlocks if this is presently locked;
wenzelm
parents:
78391
diff
changeset
|
579 |
execute_statement(table.create(sql_type) + SQL.separate(sql)) |
27c2fa1db6ed
more uniform guard (!exists_table(table)): avoid "ALTER TABLE" on already existing table, which could lead to deadlocks if this is presently locked;
wenzelm
parents:
78391
diff
changeset
|
580 |
if (is_postgresql) { |
27c2fa1db6ed
more uniform guard (!exists_table(table)): avoid "ALTER TABLE" on already existing table, which could lead to deadlocks if this is presently locked;
wenzelm
parents:
78391
diff
changeset
|
581 |
for (column <- table.columns if column.T == SQL.Type.Bytes) { |
27c2fa1db6ed
more uniform guard (!exists_table(table)): avoid "ALTER TABLE" on already existing table, which could lead to deadlocks if this is presently locked;
wenzelm
parents:
78391
diff
changeset
|
582 |
execute_statement( |
27c2fa1db6ed
more uniform guard (!exists_table(table)): avoid "ALTER TABLE" on already existing table, which could lead to deadlocks if this is presently locked;
wenzelm
parents:
78391
diff
changeset
|
583 |
"ALTER TABLE " + table + " ALTER COLUMN " + column + " SET STORAGE EXTERNAL") |
27c2fa1db6ed
more uniform guard (!exists_table(table)): avoid "ALTER TABLE" on already existing table, which could lead to deadlocks if this is presently locked;
wenzelm
parents:
78391
diff
changeset
|
584 |
} |
77681
1db732e6c3d2
back to compression in Isabelle/Scala (in contrast to f7174238b5e3), e.g. relevant for old_command_timings_blob, but also for prospective heaps;
wenzelm
parents:
77665
diff
changeset
|
585 |
} |
1db732e6c3d2
back to compression in Isabelle/Scala (in contrast to f7174238b5e3), e.g. relevant for old_command_timings_blob, but also for prospective heaps;
wenzelm
parents:
77665
diff
changeset
|
586 |
} |
1db732e6c3d2
back to compression in Isabelle/Scala (in contrast to f7174238b5e3), e.g. relevant for old_command_timings_blob, but also for prospective heaps;
wenzelm
parents:
77665
diff
changeset
|
587 |
} |
65006 | 588 |
|
78392
27c2fa1db6ed
more uniform guard (!exists_table(table)): avoid "ALTER TABLE" on already existing table, which could lead to deadlocks if this is presently locked;
wenzelm
parents:
78391
diff
changeset
|
589 |
def create_view(table: Table): Unit = { |
27c2fa1db6ed
more uniform guard (!exists_table(table)): avoid "ALTER TABLE" on already existing table, which could lead to deadlocks if this is presently locked;
wenzelm
parents:
78391
diff
changeset
|
590 |
if (!exists_table(table)) { |
77540 | 591 |
execute_statement("CREATE VIEW " + table + " AS " + { table.query; table.body }) |
65690 | 592 |
} |
65691 | 593 |
} |
79721
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
594 |
|
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
595 |
|
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
596 |
/* notifications (PostgreSQL only) */ |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
597 |
|
79727
529a6e35aaa9
tuned signature: follow PostgreSQL syntax instead of JDBC API;
wenzelm
parents:
79726
diff
changeset
|
598 |
def listen(channel: String): Unit = () |
529a6e35aaa9
tuned signature: follow PostgreSQL syntax instead of JDBC API;
wenzelm
parents:
79726
diff
changeset
|
599 |
def unlisten(channel: String = "*"): Unit = () |
529a6e35aaa9
tuned signature: follow PostgreSQL syntax instead of JDBC API;
wenzelm
parents:
79726
diff
changeset
|
600 |
def send(channel: String, payload: String): Unit = () |
529a6e35aaa9
tuned signature: follow PostgreSQL syntax instead of JDBC API;
wenzelm
parents:
79726
diff
changeset
|
601 |
final def send(channel: String): Unit = send(channel, "") |
79725
abb5eedfeecf
clarified signature: more convenient send/receive operations;
wenzelm
parents:
79724
diff
changeset
|
602 |
final def send(notification: Notification): Unit = |
79727
529a6e35aaa9
tuned signature: follow PostgreSQL syntax instead of JDBC API;
wenzelm
parents:
79726
diff
changeset
|
603 |
send(notification.channel, notification.payload) |
79725
abb5eedfeecf
clarified signature: more convenient send/receive operations;
wenzelm
parents:
79724
diff
changeset
|
604 |
def receive(filter: Notification => Boolean): Option[List[Notification]] = None |
65006 | 605 |
} |
78362 | 606 |
|
607 |
||
608 |
private val transaction_count = Counter.make() |
|
63778 | 609 |
} |
65006 | 610 |
|
611 |
||
612 |
||
613 |
/** SQLite **/ |
|
614 |
||
75393 | 615 |
object SQLite { |
65021 | 616 |
// see https://www.sqlite.org/lang_datefunc.html |
617 |
val date_format: Date.Format = Date.Format("uuuu-MM-dd HH:mm:ss.SSS x") |
|
618 |
||
75393 | 619 |
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
|
620 |
val lib_path = Path.explode("$ISABELLE_SQLITE_HOME/" + Platform.jvm_platform) |
76529 | 621 |
val lib_name = File.get_file(lib_path).file_name |
622 |
||
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
|
623 |
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
|
624 |
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
|
625 |
|
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
|
626 |
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
|
627 |
} |
65292
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
628 |
|
78163 | 629 |
def open_database(path: Path, restrict: Boolean = false): Database = { |
65292
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
630 |
init_jdbc |
65006 | 631 |
val path0 = path.expand |
632 |
val s0 = File.platform_path(path0) |
|
633 |
val s1 = if (Platform.is_windows) s0.replace('\\', '/') else s0 |
|
78891
76d1382d6077
proper SQL.string syntax, following actual SQL standard instead of historic variations before PostgreSQL 9.1;
wenzelm
parents:
78863
diff
changeset
|
634 |
|
76d1382d6077
proper SQL.string syntax, following actual SQL standard instead of historic variations before PostgreSQL 9.1;
wenzelm
parents:
78863
diff
changeset
|
635 |
val config = new SQLiteConfig() |
76d1382d6077
proper SQL.string syntax, following actual SQL standard instead of historic variations before PostgreSQL 9.1;
wenzelm
parents:
78863
diff
changeset
|
636 |
config.setEncoding(SQLiteConfig.Encoding.UTF8) |
76d1382d6077
proper SQL.string syntax, following actual SQL standard instead of historic variations before PostgreSQL 9.1;
wenzelm
parents:
78863
diff
changeset
|
637 |
val connection = config.createConnection("jdbc:sqlite:" + s1) |
76d1382d6077
proper SQL.string syntax, following actual SQL standard instead of historic variations before PostgreSQL 9.1;
wenzelm
parents:
78863
diff
changeset
|
638 |
|
78163 | 639 |
val db = new Database(path0.toString, connection) |
640 |
||
641 |
try { if (restrict) File.restrict(path0) } |
|
642 |
catch { case exn: Throwable => db.close(); throw exn } |
|
643 |
||
644 |
db |
|
65006 | 645 |
} |
646 |
||
75393 | 647 |
class Database private[SQLite](name: String, val connection: Connection) extends SQL.Database { |
65007 | 648 |
override def toString: String = name |
77664
f5d3ade80d15
more specific vacuum operation, which is also relevant to PostgreSQL;
wenzelm
parents:
77598
diff
changeset
|
649 |
|
77527
790085b1002f
more detailed table "isabelle_build_serial": allow to monitor activity of build_process instances;
wenzelm
parents:
77403
diff
changeset
|
650 |
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
|
651 |
|
78598 | 652 |
def sql_type(T: SQL.Type): SQL.Source = SQL.sql_type_sqlite(T) |
65011 | 653 |
|
65748 | 654 |
def update_date(stmt: SQL.Statement, i: Int, date: Date): Unit = |
655 |
if (date == null) stmt.string(i) = (null: String) |
|
656 |
else stmt.string(i) = date_format(date) |
|
65598 | 657 |
|
65740 | 658 |
def date(res: SQL.Result, column: SQL.Column): Date = |
77598 | 659 |
proper_string(res.string(column)) match { |
660 |
case None => null |
|
661 |
case Some(s) => date_format.parse(s) |
|
662 |
} |
|
65021 | 663 |
|
65730 | 664 |
def insert_permissive(table: SQL.Table, sql: SQL.Source = ""): SQL.Source = |
77365 | 665 |
table.insert_cmd(cmd = "INSERT OR IGNORE", sql = sql) |
65006 | 666 |
} |
667 |
} |
|
668 |
||
669 |
||
670 |
||
671 |
/** PostgreSQL **/ |
|
672 |
||
79724 | 673 |
// see https://www.postgresql.org/docs/14/index.html |
78351 | 674 |
// see https://jdbc.postgresql.org/documentation |
675 |
||
75393 | 676 |
object PostgreSQL { |
75967
ff164add75cd
maintain "uuid" column in session build database, to identity the original build process uniquely;
wenzelm
parents:
75966
diff
changeset
|
677 |
type Source = SQL.Source |
ff164add75cd
maintain "uuid" column in session build database, to identity the original build process uniquely;
wenzelm
parents:
75966
diff
changeset
|
678 |
|
78347 | 679 |
lazy val init_jdbc: Unit = Class.forName("org.postgresql.Driver") |
680 |
||
78345 | 681 |
val default_server: SSH.Server = SSH.local_server(port = 5432) |
65006 | 682 |
|
78378 | 683 |
def open_database( |
684 |
user: String, |
|
685 |
password: String, |
|
686 |
database: String = "", |
|
687 |
server: SSH.Server = default_server, |
|
79722 | 688 |
server_close: Boolean = false, |
689 |
receiver_delay: Time = Time.seconds(0.5) |
|
78378 | 690 |
): Database = { |
691 |
init_jdbc |
|
692 |
||
693 |
if (user.isEmpty) error("Undefined database user") |
|
694 |
if (server.host.isEmpty) error("Undefined database server host") |
|
695 |
if (server.port <= 0) error("Undefined database server port") |
|
696 |
||
697 |
val name = proper_string(database) getOrElse user |
|
698 |
val url = "jdbc:postgresql://" + server.host + ":" + server.port + "/" + name |
|
699 |
val ssh = server.ssh_system.ssh_session |
|
78422 | 700 |
val print = |
78561 | 701 |
"server " + quote(user + "@" + server + "/" + name) + |
702 |
if_proper(ssh, " via ssh " + quote(ssh.get.toString)) |
|
78378 | 703 |
|
704 |
val connection = DriverManager.getConnection(url, user, password) |
|
79722 | 705 |
val db = new Database(connection, print, server, server_close, receiver_delay) |
78891
76d1382d6077
proper SQL.string syntax, following actual SQL standard instead of historic variations before PostgreSQL 9.1;
wenzelm
parents:
78863
diff
changeset
|
706 |
|
76d1382d6077
proper SQL.string syntax, following actual SQL standard instead of historic variations before PostgreSQL 9.1;
wenzelm
parents:
78863
diff
changeset
|
707 |
try { db.execute_statement("SET standard_conforming_strings = on") } |
76d1382d6077
proper SQL.string syntax, following actual SQL standard instead of historic variations before PostgreSQL 9.1;
wenzelm
parents:
78863
diff
changeset
|
708 |
catch { case exn: Throwable => db.close(); throw exn } |
76d1382d6077
proper SQL.string syntax, following actual SQL standard instead of historic variations before PostgreSQL 9.1;
wenzelm
parents:
78863
diff
changeset
|
709 |
|
76d1382d6077
proper SQL.string syntax, following actual SQL standard instead of historic variations before PostgreSQL 9.1;
wenzelm
parents:
78863
diff
changeset
|
710 |
db |
78378 | 711 |
} |
712 |
||
78366 | 713 |
def open_server( |
714 |
options: Options, |
|
715 |
host: String = "", |
|
716 |
port: Int = 0, |
|
717 |
ssh_host: String = "", |
|
718 |
ssh_port: Int = 0, |
|
719 |
ssh_user: String = "" |
|
720 |
): SSH.Server = { |
|
721 |
val server_host = proper_string(host).getOrElse(default_server.host) |
|
722 |
val server_port = if (port > 0) port else default_server.port |
|
723 |
||
724 |
if (ssh_host.isEmpty) SSH.local_server(host = server_host, port = server_port) |
|
725 |
else { |
|
726 |
SSH.open_server(options, host = ssh_host, port = ssh_port, user = ssh_user, |
|
727 |
remote_host = server_host, remote_port = server_port) |
|
728 |
} |
|
729 |
} |
|
730 |
||
78347 | 731 |
def open_database_server( |
732 |
options: Options, |
|
733 |
user: String = "", |
|
734 |
password: String = "", |
|
735 |
database: String = "", |
|
78366 | 736 |
server: SSH.Server = SSH.no_server, |
78347 | 737 |
host: String = "", |
738 |
port: Int = 0, |
|
739 |
ssh_host: String = "", |
|
740 |
ssh_port: Int = 0, |
|
78863
f627ab8c276c
discontinued pointless option (reverting 63d55ba90a9f): performance tuning works better via SQL.Database.execute_batch_statement;
wenzelm
parents:
78607
diff
changeset
|
741 |
ssh_user: String = "" |
78347 | 742 |
): PostgreSQL.Database = { |
78366 | 743 |
val db_server = |
744 |
if (server.defined) server |
|
78347 | 745 |
else { |
78366 | 746 |
open_server(options, host = host, port = port, ssh_host = ssh_host, |
747 |
ssh_port = ssh_port, ssh_user = ssh_user) |
|
78347 | 748 |
} |
78366 | 749 |
val server_close = !server.defined |
78347 | 750 |
try { |
751 |
open_database(user = user, password = password, database = database, |
|
78863
f627ab8c276c
discontinued pointless option (reverting 63d55ba90a9f): performance tuning works better via SQL.Database.execute_batch_statement;
wenzelm
parents:
78607
diff
changeset
|
752 |
server = db_server, server_close = server_close) |
78347 | 753 |
} |
78366 | 754 |
catch { case exn: Throwable if server_close => db_server.close(); throw exn } |
78347 | 755 |
} |
65292
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
756 |
|
65009 | 757 |
class Database private[PostgreSQL]( |
75393 | 758 |
val connection: Connection, |
78347 | 759 |
print: String, |
760 |
server: SSH.Server, |
|
79722 | 761 |
server_close: Boolean, |
762 |
receiver_delay: Time |
|
75393 | 763 |
) extends SQL.Database { |
78347 | 764 |
override def toString: String = print |
65008 | 765 |
|
77527
790085b1002f
more detailed table "isabelle_build_serial": allow to monitor activity of build_process instances;
wenzelm
parents:
77403
diff
changeset
|
766 |
override def now(): Date = { |
790085b1002f
more detailed table "isabelle_build_serial": allow to monitor activity of build_process instances;
wenzelm
parents:
77403
diff
changeset
|
767 |
val now = SQL.Column.date("now") |
77552 | 768 |
execute_query_statementO[Date]("SELECT NOW() as " + now.ident, res => res.date(now)) |
769 |
.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
|
770 |
} |
790085b1002f
more detailed table "isabelle_build_serial": allow to monitor activity of build_process instances;
wenzelm
parents:
77403
diff
changeset
|
771 |
|
78598 | 772 |
def sql_type(T: SQL.Type): SQL.Source = SQL.sql_type_postgresql(T) |
65009 | 773 |
|
65021 | 774 |
// see https://jdbc.postgresql.org/documentation/head/8-date-time.html |
65748 | 775 |
def update_date(stmt: SQL.Statement, i: Int, date: Date): Unit = |
65740 | 776 |
if (date == null) stmt.rep.setObject(i, null) |
69980 | 777 |
else stmt.rep.setObject(i, OffsetDateTime.from(date.to(Date.timezone_utc).rep)) |
65598 | 778 |
|
75393 | 779 |
def date(res: SQL.Result, column: SQL.Column): Date = { |
65740 | 780 |
val obj = res.rep.getObject(column.name, classOf[OffsetDateTime]) |
65704 | 781 |
if (obj == null) null else Date.instant(obj.toInstant) |
782 |
} |
|
65021 | 783 |
|
65730 | 784 |
def insert_permissive(table: SQL.Table, sql: SQL.Source = ""): SQL.Source = |
77377 | 785 |
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
|
786 |
|
77039 | 787 |
|
77590
edc96be6b939
explicit locking for PostgreSQL --- neither available nor required for SQLite;
wenzelm
parents:
77552
diff
changeset
|
788 |
/* explicit locking: only applicable to PostgreSQL within transaction context */ |
79724 | 789 |
// see https://www.postgresql.org/docs/14/sql-lock.html |
790 |
// see https://www.postgresql.org/docs/14/explicit-locking.html |
|
77590
edc96be6b939
explicit locking for PostgreSQL --- neither available nor required for SQLite;
wenzelm
parents:
77552
diff
changeset
|
791 |
|
77596 | 792 |
override def lock_tables(tables: List[SQL.Table]): PostgreSQL.Source = |
78253 | 793 |
if_proper(tables, "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
|
794 |
|
edc96be6b939
explicit locking for PostgreSQL --- neither available nor required for SQLite;
wenzelm
parents:
77552
diff
changeset
|
795 |
|
77039 | 796 |
/* notifications: IPC via database server */ |
79721
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
797 |
/* |
79724 | 798 |
- see https://www.postgresql.org/docs/14/sql-notify.html |
79721
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
799 |
- self-notifications and repeated notifications are suppressed |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
800 |
- notifications are sorted by local system time (nano seconds) |
79725
abb5eedfeecf
clarified signature: more convenient send/receive operations;
wenzelm
parents:
79724
diff
changeset
|
801 |
- receive() == None means that IPC is inactive or unavailable (SQLite) |
79721
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
802 |
*/ |
77039 | 803 |
|
79721
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
804 |
private var _receiver_buffer: Option[Map[SQL.Notification, Long]] = None |
77039 | 805 |
|
79721
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
806 |
private lazy val _receiver_thread = |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
807 |
Isabelle_Thread.fork(name = "PostgreSQL.receiver", daemon = true, uninterruptible = true) { |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
808 |
val conn = the_postgresql_connection |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
809 |
val self_pid = conn.getBackendPID |
77039 | 810 |
|
79721
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
811 |
try { |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
812 |
while (true) { |
79726
621676d7fb9a
more robust shutdown: interruptible database connection;
wenzelm
parents:
79725
diff
changeset
|
813 |
Isabelle_Thread.interruptible { receiver_delay.sleep(); Option(conn.getNotifications())} |
621676d7fb9a
more robust shutdown: interruptible database connection;
wenzelm
parents:
79725
diff
changeset
|
814 |
match { |
79721
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
815 |
case Some(array) if array.nonEmpty => |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
816 |
synchronized { |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
817 |
var received = _receiver_buffer.getOrElse(Map.empty) |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
818 |
for (a <- array.iterator if a.getPID != self_pid) { |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
819 |
val msg = SQL.Notification(a.getName, a.getParameter) |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
820 |
if (!received.isDefinedAt(msg)) { |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
821 |
val stamp = System.nanoTime() |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
822 |
received = received + (msg -> stamp) |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
823 |
} |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
824 |
} |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
825 |
_receiver_buffer = Some(received) |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
826 |
} |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
827 |
case _ => |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
828 |
} |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
829 |
} |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
830 |
} |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
831 |
catch { case Exn.Interrupt() => } |
77039 | 832 |
} |
833 |
||
79721
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
834 |
private def receiver_shutdown(): Unit = synchronized { |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
835 |
if (_receiver_buffer.isDefined) { |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
836 |
_receiver_thread.interrupt() |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
837 |
Some(_receiver_thread) |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
838 |
} |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
839 |
else None |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
840 |
}.foreach(_.join()) |
77039 | 841 |
|
79721
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
842 |
private def synchronized_receiver[A](body: => A): A = synchronized { |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
843 |
if (_receiver_buffer.isEmpty) { |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
844 |
_receiver_buffer = Some(Map.empty) |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
845 |
_receiver_thread |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
846 |
} |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
847 |
body |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
848 |
} |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
849 |
|
79727
529a6e35aaa9
tuned signature: follow PostgreSQL syntax instead of JDBC API;
wenzelm
parents:
79726
diff
changeset
|
850 |
override def listen(channel: String): Unit = synchronized_receiver { |
529a6e35aaa9
tuned signature: follow PostgreSQL syntax instead of JDBC API;
wenzelm
parents:
79726
diff
changeset
|
851 |
execute_statement("LISTEN " + SQL.ident(channel)) |
79721
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
852 |
} |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
853 |
|
79727
529a6e35aaa9
tuned signature: follow PostgreSQL syntax instead of JDBC API;
wenzelm
parents:
79726
diff
changeset
|
854 |
override def unlisten(channel: String = "*"): Unit = synchronized_receiver { |
529a6e35aaa9
tuned signature: follow PostgreSQL syntax instead of JDBC API;
wenzelm
parents:
79726
diff
changeset
|
855 |
execute_statement("UNLISTEN " + (if (channel == "*") channel else SQL.ident(channel))) |
79721
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
856 |
} |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
857 |
|
79727
529a6e35aaa9
tuned signature: follow PostgreSQL syntax instead of JDBC API;
wenzelm
parents:
79726
diff
changeset
|
858 |
override def send(channel: String, payload: String): Unit = synchronized_receiver { |
79721
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
859 |
execute_statement( |
79727
529a6e35aaa9
tuned signature: follow PostgreSQL syntax instead of JDBC API;
wenzelm
parents:
79726
diff
changeset
|
860 |
"NOTIFY " + SQL.ident(channel) + if_proper(payload, ", " + SQL.string(payload))) |
79721
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
861 |
} |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
862 |
|
79725
abb5eedfeecf
clarified signature: more convenient send/receive operations;
wenzelm
parents:
79724
diff
changeset
|
863 |
override def receive( |
abb5eedfeecf
clarified signature: more convenient send/receive operations;
wenzelm
parents:
79724
diff
changeset
|
864 |
filter: SQL.Notification => Boolean = _ => true |
abb5eedfeecf
clarified signature: more convenient send/receive operations;
wenzelm
parents:
79724
diff
changeset
|
865 |
): Option[List[SQL.Notification]] = synchronized { |
abb5eedfeecf
clarified signature: more convenient send/receive operations;
wenzelm
parents:
79724
diff
changeset
|
866 |
_receiver_buffer.map { received => |
79721
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
867 |
val filtered = received.keysIterator.filter(filter).toList |
79725
abb5eedfeecf
clarified signature: more convenient send/receive operations;
wenzelm
parents:
79724
diff
changeset
|
868 |
if (filtered.nonEmpty) { |
79721
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
869 |
_receiver_buffer = Some(received -- filtered) |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
870 |
filtered.map(msg => msg -> received(msg)).sortBy(_._2).map(_._1) |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
871 |
} |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
872 |
else Nil |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
873 |
} |
79725
abb5eedfeecf
clarified signature: more convenient send/receive operations;
wenzelm
parents:
79724
diff
changeset
|
874 |
} |
79721
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
875 |
|
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
876 |
override def close(): Unit = { |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
877 |
receiver_shutdown() |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
878 |
super.close() |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
879 |
if (server_close) server.close() |
a5629eade476
clarified IPC via database server: receive notifications quasi-spontaneously via auxiliary thread;
wenzelm
parents:
78891
diff
changeset
|
880 |
} |
65006 | 881 |
} |
882 |
} |