author | wenzelm |
Sun, 16 Jul 2023 19:30:10 +0200 | |
changeset 78370 | fda3f7a158b9 |
parent 78369 | ba71ea02d965 |
child 78371 | 928e031b7c52 |
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 |
|
6 |
See https://docs.oracle.com/en/java/javase/17/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 |
13 |
import java.sql.{DriverManager, Connection, PreparedStatement, ResultSet} |
|
63779 | 14 |
|
77037
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
15 |
import org.sqlite.jdbc4.JDBC4Connection |
77039 | 16 |
import org.postgresql.{PGConnection, PGNotification} |
77037
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
17 |
|
65740 | 18 |
import scala.collection.mutable |
19 |
||
63779 | 20 |
|
75393 | 21 |
object SQL { |
65006 | 22 |
/** SQL language **/ |
23 |
||
65730 | 24 |
type Source = String |
25 |
||
26 |
||
63778 | 27 |
/* concrete syntax */ |
28 |
||
65321 | 29 |
def escape_char(c: Char): String = |
63778 | 30 |
c match { |
31 |
case '\u0000' => "\\0" |
|
32 |
case '\'' => "\\'" |
|
33 |
case '\"' => "\\\"" |
|
34 |
case '\b' => "\\b" |
|
35 |
case '\n' => "\\n" |
|
36 |
case '\r' => "\\r" |
|
37 |
case '\t' => "\\t" |
|
38 |
case '\u001a' => "\\Z" |
|
39 |
case '\\' => "\\\\" |
|
40 |
case _ => c.toString |
|
41 |
} |
|
42 |
||
65730 | 43 |
def string(s: String): Source = |
71601 | 44 |
s.iterator.map(escape_char).mkString("'", "", "'") |
63778 | 45 |
|
65730 | 46 |
def ident(s: String): Source = |
65677
7d25b8dbdbfa
proper quote (amending 546020a98a91): e.g. relevant for "ISABELLE_BUILD_OPTIONS";
wenzelm
parents:
65673
diff
changeset
|
47 |
Long_Name.implode(Long_Name.explode(s).map(a => quote(a.replace("\"", "\"\"")))) |
63779 | 48 |
|
65730 | 49 |
def enclose(s: Source): Source = "(" + s + ")" |
50 |
def enclosure(ss: Iterable[Source]): Source = ss.mkString("(", ", ", ")") |
|
63791 | 51 |
|
77381
a86e346b20d8
misc tuning and clarification: more uniform use of optional "sql" in SQL.Table.delete/select;
wenzelm
parents:
77377
diff
changeset
|
52 |
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
|
53 |
(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
|
54 |
|
77403
be8e14c7da80
clarified signature, although "sql" argument is de-facto mandatory;
wenzelm
parents:
77381
diff
changeset
|
55 |
def select(columns: List[Column] = Nil, distinct: Boolean = false, sql: Source = ""): Source = |
65774 | 56 |
"SELECT " + (if (distinct) "DISTINCT " else "") + |
77403
be8e14c7da80
clarified signature, although "sql" argument is de-facto mandatory;
wenzelm
parents:
77381
diff
changeset
|
57 |
(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
|
58 |
|
65775 | 59 |
val join_outer: Source = " LEFT OUTER JOIN " |
60 |
val join_inner: Source = " INNER JOIN " |
|
65776 | 61 |
def join(outer: Boolean): Source = if (outer) join_outer else join_inner |
65775 | 62 |
|
77370 | 63 |
def infix(op: Source, args: Iterable[Source]): Source = { |
64 |
val body = args.iterator.filter(_.nonEmpty).mkString(" " + op + " ") |
|
65 |
if_proper(body, enclose(body)) |
|
66 |
} |
|
67 |
||
68 |
def AND(args: Iterable[Source]): Source = infix("AND", args) |
|
69 |
def OR(args: Iterable[Source]): Source = infix("OR", args) |
|
70 |
||
71 |
def and(args: Source*): Source = AND(args) |
|
72 |
def or(args: Source*): Source = OR(args) |
|
73 |
||
74 |
val TRUE: Source = "TRUE" |
|
75 |
val FALSE: Source = "FALSE" |
|
76 |
||
78152 | 77 |
def equal(sql: Source, x: Int): Source = sql + " = " + x |
78 |
def equal(sql: Source, x: Long): Source = sql + " = " + x |
|
79 |
def equal(sql: Source, x: String): Source = sql + " = " + string(x) |
|
77375 | 80 |
|
81 |
def member(sql: Source, set: Iterable[String]): Source = |
|
77370 | 82 |
if (set.isEmpty) FALSE |
77375 | 83 |
else OR(set.iterator.map(equal(sql, _)).toList) |
65804 | 84 |
|
77370 | 85 |
def where(sql: Source): Source = if_proper(sql, " WHERE " + sql) |
78153 | 86 |
def where_and(args: Source*): Source = where(and(args:_*)) |
87 |
def where_or(args: Source*): Source = where(or(args:_*)) |
|
77370 | 88 |
|
89 |
||
65008 | 90 |
/* types */ |
91 |
||
75393 | 92 |
object Type extends Enumeration { |
65011 | 93 |
val Boolean = Value("BOOLEAN") |
65008 | 94 |
val Int = Value("INTEGER") |
95 |
val Long = Value("BIGINT") |
|
96 |
val Double = Value("DOUBLE PRECISION") |
|
97 |
val String = Value("TEXT") |
|
98 |
val Bytes = Value("BLOB") |
|
65014 | 99 |
val Date = Value("TIMESTAMP WITH TIME ZONE") |
65008 | 100 |
} |
101 |
||
65730 | 102 |
def sql_type_default(T: Type.Value): Source = T.toString |
65013 | 103 |
|
65730 | 104 |
def sql_type_sqlite(T: Type.Value): Source = |
65019 | 105 |
if (T == Type.Boolean) "INTEGER" |
106 |
else if (T == Type.Date) "TEXT" |
|
107 |
else sql_type_default(T) |
|
65013 | 108 |
|
65730 | 109 |
def sql_type_postgresql(T: Type.Value): Source = |
65019 | 110 |
if (T == Type.Bytes) "BYTEA" |
111 |
else sql_type_default(T) |
|
65008 | 112 |
|
113 |
||
63779 | 114 |
/* columns */ |
115 |
||
75393 | 116 |
object Column { |
65280 | 117 |
def bool(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 118 |
Column(name, Type.Boolean, strict, primary_key) |
65280 | 119 |
def int(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 120 |
Column(name, Type.Int, strict, primary_key) |
65280 | 121 |
def long(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 122 |
Column(name, Type.Long, strict, primary_key) |
65280 | 123 |
def double(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 124 |
Column(name, Type.Double, strict, primary_key) |
65280 | 125 |
def string(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 126 |
Column(name, Type.String, strict, primary_key) |
65280 | 127 |
def bytes(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 128 |
Column(name, Type.Bytes, strict, primary_key) |
65280 | 129 |
def date(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 130 |
Column(name, Type.Date, strict, primary_key) |
63779 | 131 |
} |
132 |
||
65018 | 133 |
sealed case class Column( |
75393 | 134 |
name: String, |
135 |
T: Type.Value, |
|
136 |
strict: Boolean = false, |
|
137 |
primary_key: Boolean = false, |
|
138 |
expr: SQL.Source = "" |
|
139 |
) { |
|
66857 | 140 |
def make_primary_key: Column = copy(primary_key = true) |
141 |
||
65691 | 142 |
def apply(table: Table): Column = |
143 |
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
|
144 |
|
65780 | 145 |
def ident: Source = |
146 |
if (expr == "") SQL.ident(name) |
|
147 |
else enclose(expr) + " AS " + SQL.ident(name) |
|
65695 | 148 |
|
65730 | 149 |
def decl(sql_type: Type.Value => Source): Source = |
65695 | 150 |
ident + " " + sql_type(T) + (if (strict || primary_key) " NOT NULL" else "") |
63781 | 151 |
|
66856 | 152 |
def defined: String = ident + " IS NOT NULL" |
153 |
def undefined: String = ident + " IS NULL" |
|
154 |
||
78152 | 155 |
def equal(x: Int): Source = SQL.equal(ident, x) |
156 |
def equal(x: Long): Source = SQL.equal(ident, x) |
|
157 |
def equal(x: String): Source = SQL.equal(ident, x) |
|
158 |
||
159 |
def where_equal(x: Int): Source = SQL.where(equal(x)) |
|
160 |
def where_equal(x: Long): Source = SQL.where(equal(x)) |
|
161 |
def where_equal(x: String): Source = SQL.where(equal(x)) |
|
162 |
||
77375 | 163 |
def member(set: Iterable[String]): Source = SQL.member(ident, set) |
164 |
def where_member(set: Iterable[String]): Source = SQL.where(member(set)) |
|
65593 | 165 |
|
78151 | 166 |
def max: Column = copy(expr = "MAX(" + ident + ")") |
167 |
||
65730 | 168 |
override def toString: Source = ident |
63779 | 169 |
} |
170 |
||
76870 | 171 |
def order_by(columns: List[Column], descending: Boolean = false): Source = |
172 |
" ORDER BY " + columns.mkString(", ") + (if (descending) " DESC" else "") |
|
173 |
||
63780 | 174 |
|
175 |
/* tables */ |
|
176 |
||
75393 | 177 |
sealed case class Table(name: String, columns: List[Column], body: Source = "") { |
63781 | 178 |
Library.duplicates(columns.map(_.name)) match { |
179 |
case Nil => |
|
180 |
case bad => error("Duplicate column names " + commas_quote(bad) + " for table " + quote(name)) |
|
181 |
} |
|
182 |
||
65730 | 183 |
def ident: Source = SQL.ident(name) |
65691 | 184 |
|
65730 | 185 |
def query: Source = |
65696 | 186 |
if (body == "") error("Missing SQL body for table " + quote(name)) |
187 |
else SQL.enclose(body) |
|
65649 | 188 |
|
65778 | 189 |
def query_named: Source = query + " AS " + SQL.ident(name) |
65702 | 190 |
|
75966 | 191 |
def create(strict: Boolean, sql_type: Type.Value => Source): Source = { |
65325 | 192 |
val primary_key = |
193 |
columns.filter(_.primary_key).map(_.name) match { |
|
194 |
case Nil => Nil |
|
195 |
case keys => List("PRIMARY KEY " + enclosure(keys)) |
|
196 |
} |
|
65701 | 197 |
"CREATE TABLE " + (if (strict) "" else "IF NOT EXISTS ") + |
198 |
ident + " " + enclosure(columns.map(_.decl(sql_type)) ::: primary_key) |
|
63781 | 199 |
} |
200 |
||
65698 | 201 |
def create_index(index_name: String, index_columns: List[Column], |
65730 | 202 |
strict: Boolean = false, unique: Boolean = false): Source = |
63791 | 203 |
"CREATE " + (if (unique) "UNIQUE " else "") + "INDEX " + |
65695 | 204 |
(if (strict) "" else "IF NOT EXISTS ") + SQL.ident(index_name) + " ON " + |
205 |
ident + " " + enclosure(index_columns.map(_.name)) |
|
63791 | 206 |
|
77365 | 207 |
def insert_cmd(cmd: Source = "INSERT", sql: Source = ""): Source = |
77377 | 208 |
cmd + " INTO " + ident + " VALUES " + enclosure(columns.map(_ => "?")) + SQL.separate(sql) |
63791 | 209 |
|
77365 | 210 |
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
|
211 |
|
65730 | 212 |
def delete(sql: Source = ""): Source = |
77377 | 213 |
"DELETE FROM " + ident + SQL.separate(sql) |
65319 | 214 |
|
77537 | 215 |
def update(update_columns: List[Column] = Nil, sql: Source = ""): Source = |
216 |
"UPDATE " + ident + " SET " + commas(update_columns.map(c => c.ident + " = ?")) + |
|
217 |
SQL.separate(sql) |
|
218 |
||
65774 | 219 |
def select( |
77381
a86e346b20d8
misc tuning and clarification: more uniform use of optional "sql" in SQL.Table.delete/select;
wenzelm
parents:
77377
diff
changeset
|
220 |
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
|
221 |
distinct: Boolean = false, |
a86e346b20d8
misc tuning and clarification: more uniform use of optional "sql" in SQL.Table.delete/select;
wenzelm
parents:
77377
diff
changeset
|
222 |
sql: Source = "" |
77403
be8e14c7da80
clarified signature, although "sql" argument is de-facto mandatory;
wenzelm
parents:
77381
diff
changeset
|
223 |
): Source = SQL.select(select_columns, distinct = distinct, sql = ident + SQL.separate(sql)) |
63790 | 224 |
|
65730 | 225 |
override def toString: Source = ident |
63780 | 226 |
} |
63790 | 227 |
|
228 |
||
77596 | 229 |
/* table groups */ |
230 |
||
231 |
object Tables { |
|
232 |
def list(list: List[Table]): Tables = new Tables(list) |
|
77664
f5d3ade80d15
more specific vacuum operation, which is also relevant to PostgreSQL;
wenzelm
parents:
77598
diff
changeset
|
233 |
val empty: Tables = list(Nil) |
77596 | 234 |
def apply(args: Table*): Tables = list(args.toList) |
235 |
} |
|
236 |
||
237 |
final class Tables private(val list: List[Table]) extends Iterable[Table] { |
|
238 |
override def toString: String = list.mkString("SQL.Tables(", ", ", ")") |
|
239 |
||
77665 | 240 |
def ::: (other: Tables): Tables = new Tables(other.list ::: list) |
241 |
||
77596 | 242 |
def iterator: Iterator[Table] = list.iterator |
243 |
||
244 |
// requires transaction |
|
78355
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
245 |
def lock(db: Database, create: Boolean = false): Boolean = { |
78154 | 246 |
if (create) foreach(db.create_table(_)) |
77596 | 247 |
val sql = db.lock_tables(list) |
78355
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
248 |
if (sql.nonEmpty) { db.execute_statement(sql); true } |
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
249 |
else false |
77596 | 250 |
} |
251 |
} |
|
252 |
||
78187
2df0f3604a67
clarified signature: more explicit class SQL.Data;
wenzelm
parents:
78167
diff
changeset
|
253 |
abstract class Data(table_prefix: String = "") { |
2df0f3604a67
clarified signature: more explicit class SQL.Data;
wenzelm
parents:
78167
diff
changeset
|
254 |
def tables: Tables = Tables.empty |
2df0f3604a67
clarified signature: more explicit class SQL.Data;
wenzelm
parents:
78167
diff
changeset
|
255 |
|
2df0f3604a67
clarified signature: more explicit class SQL.Data;
wenzelm
parents:
78167
diff
changeset
|
256 |
def transaction_lock[A]( |
2df0f3604a67
clarified signature: more explicit class SQL.Data;
wenzelm
parents:
78167
diff
changeset
|
257 |
db: Database, |
78207 | 258 |
create: Boolean = false, |
78356 | 259 |
label: String = "", |
78369
ba71ea02d965
more robust Java/Scala multithreading: transaction is always connection.synchronized;
wenzelm
parents:
78366
diff
changeset
|
260 |
log: Logger = new System_Logger |
78207 | 261 |
)(body: => A): A = { |
78369
ba71ea02d965
more robust Java/Scala multithreading: transaction is always connection.synchronized;
wenzelm
parents:
78366
diff
changeset
|
262 |
db.transaction_lock(tables, create = create, label = label, log = log)(body) |
78207 | 263 |
} |
78187
2df0f3604a67
clarified signature: more explicit class SQL.Data;
wenzelm
parents:
78167
diff
changeset
|
264 |
|
78266 | 265 |
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
|
266 |
val table_name = |
2df0f3604a67
clarified signature: more explicit class SQL.Data;
wenzelm
parents:
78167
diff
changeset
|
267 |
List(proper_string(table_prefix), proper_string(name)).flatten.mkString("_") |
2df0f3604a67
clarified signature: more explicit class SQL.Data;
wenzelm
parents:
78167
diff
changeset
|
268 |
require(table_name.nonEmpty, "Undefined database table name") |
2df0f3604a67
clarified signature: more explicit class SQL.Data;
wenzelm
parents:
78167
diff
changeset
|
269 |
Table(table_name, columns, body = body) |
2df0f3604a67
clarified signature: more explicit class SQL.Data;
wenzelm
parents:
78167
diff
changeset
|
270 |
} |
2df0f3604a67
clarified signature: more explicit class SQL.Data;
wenzelm
parents:
78167
diff
changeset
|
271 |
} |
2df0f3604a67
clarified signature: more explicit class SQL.Data;
wenzelm
parents:
78167
diff
changeset
|
272 |
|
77596 | 273 |
|
65012 | 274 |
|
275 |
/** SQL database operations **/ |
|
276 |
||
65740 | 277 |
/* statements */ |
278 |
||
75393 | 279 |
class Statement private[SQL](val db: Database, val rep: PreparedStatement) extends AutoCloseable { |
65740 | 280 |
stmt => |
281 |
||
75393 | 282 |
object bool { |
73340 | 283 |
def update(i: Int, x: Boolean): Unit = rep.setBoolean(i, x) |
75393 | 284 |
def update(i: Int, x: Option[Boolean]): Unit = { |
65748 | 285 |
if (x.isDefined) update(i, x.get) |
286 |
else rep.setNull(i, java.sql.Types.BOOLEAN) |
|
287 |
} |
|
65740 | 288 |
} |
75393 | 289 |
object int { |
73340 | 290 |
def update(i: Int, x: Int): Unit = rep.setInt(i, x) |
75393 | 291 |
def update(i: Int, x: Option[Int]): Unit = { |
65748 | 292 |
if (x.isDefined) update(i, x.get) |
293 |
else rep.setNull(i, java.sql.Types.INTEGER) |
|
294 |
} |
|
65740 | 295 |
} |
75393 | 296 |
object long { |
73340 | 297 |
def update(i: Int, x: Long): Unit = rep.setLong(i, x) |
75393 | 298 |
def update(i: Int, x: Option[Long]): Unit = { |
65748 | 299 |
if (x.isDefined) update(i, x.get) |
300 |
else rep.setNull(i, java.sql.Types.BIGINT) |
|
301 |
} |
|
65740 | 302 |
} |
75393 | 303 |
object double { |
73340 | 304 |
def update(i: Int, x: Double): Unit = rep.setDouble(i, x) |
75393 | 305 |
def update(i: Int, x: Option[Double]): Unit = { |
65748 | 306 |
if (x.isDefined) update(i, x.get) |
307 |
else rep.setNull(i, java.sql.Types.DOUBLE) |
|
308 |
} |
|
309 |
} |
|
75393 | 310 |
object string { |
73340 | 311 |
def update(i: Int, x: String): Unit = rep.setString(i, x) |
65748 | 312 |
def update(i: Int, x: Option[String]): Unit = update(i, x.orNull) |
65740 | 313 |
} |
75393 | 314 |
object bytes { |
315 |
def update(i: Int, bytes: Bytes): Unit = { |
|
65748 | 316 |
if (bytes == null) rep.setBytes(i, null) |
317 |
else rep.setBinaryStream(i, bytes.stream(), bytes.length) |
|
318 |
} |
|
319 |
def update(i: Int, bytes: Option[Bytes]): Unit = update(i, bytes.orNull) |
|
65740 | 320 |
} |
75393 | 321 |
object date { |
65748 | 322 |
def update(i: Int, date: Date): Unit = db.update_date(stmt, i, date) |
323 |
def update(i: Int, date: Option[Date]): Unit = update(i, date.orNull) |
|
324 |
} |
|
65740 | 325 |
|
326 |
def execute(): Boolean = rep.execute() |
|
327 |
def execute_query(): Result = new Result(this, rep.executeQuery()) |
|
328 |
||
78353 | 329 |
override def close(): Unit = rep.close() |
65740 | 330 |
} |
331 |
||
332 |
||
63790 | 333 |
/* results */ |
334 |
||
78352 | 335 |
class Result private[SQL](val stmt: Statement, val rep: ResultSet) extends AutoCloseable { |
65740 | 336 |
res => |
337 |
||
338 |
def next(): Boolean = rep.next() |
|
339 |
||
75393 | 340 |
def iterator[A](get: Result => A): Iterator[A] = new Iterator[A] { |
65740 | 341 |
private var _next: Boolean = res.next() |
342 |
def hasNext: Boolean = _next |
|
73337 | 343 |
def next(): A = { val x = get(res); _next = res.next(); x } |
65740 | 344 |
} |
345 |
||
346 |
def bool(column: Column): Boolean = rep.getBoolean(column.name) |
|
347 |
def int(column: Column): Int = rep.getInt(column.name) |
|
348 |
def long(column: Column): Long = rep.getLong(column.name) |
|
349 |
def double(column: Column): Double = rep.getDouble(column.name) |
|
75393 | 350 |
def string(column: Column): String = { |
65740 | 351 |
val s = rep.getString(column.name) |
352 |
if (s == null) "" else s |
|
353 |
} |
|
75393 | 354 |
def bytes(column: Column): Bytes = { |
65740 | 355 |
val bs = rep.getBytes(column.name) |
356 |
if (bs == null) Bytes.empty else Bytes(bs) |
|
357 |
} |
|
358 |
def date(column: Column): Date = stmt.db.date(res, column) |
|
359 |
||
71601 | 360 |
def timing(c1: Column, c2: Column, c3: Column): Timing = |
65741 | 361 |
Timing(Time.ms(long(c1)), Time.ms(long(c2)), Time.ms(long(c3))) |
362 |
||
75393 | 363 |
def get[A](column: Column, f: Column => A): Option[A] = { |
65740 | 364 |
val x = f(column) |
77598 | 365 |
if (rep.wasNull || x == null) None else Some(x) |
65740 | 366 |
} |
71601 | 367 |
def get_bool(column: Column): Option[Boolean] = get(column, bool) |
368 |
def get_int(column: Column): Option[Int] = get(column, int) |
|
369 |
def get_long(column: Column): Option[Long] = get(column, long) |
|
370 |
def get_double(column: Column): Option[Double] = get(column, double) |
|
371 |
def get_string(column: Column): Option[String] = get(column, string) |
|
372 |
def get_bytes(column: Column): Option[Bytes] = get(column, bytes) |
|
373 |
def get_date(column: Column): Option[Date] = get(column, date) |
|
78352 | 374 |
|
375 |
override def close(): Unit = rep.close() |
|
63790 | 376 |
} |
65006 | 377 |
|
65740 | 378 |
|
379 |
/* database */ |
|
380 |
||
75393 | 381 |
trait Database extends AutoCloseable { |
65740 | 382 |
db => |
383 |
||
77542 | 384 |
def is_sqlite: Boolean = isInstanceOf[SQLite.Database] |
385 |
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
|
386 |
|
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
|
387 |
def vacuum(tables: SQL.Tables = SQL.Tables.empty): Unit = |
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
|
388 |
if (tables.list.nonEmpty) { |
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
|
389 |
postgresql_major_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
|
390 |
case Some(m) if m <= 10 => |
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
|
391 |
for (table <- tables) execute_statement("VACUUM " + table.ident) |
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
|
392 |
case Some(_) => |
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
|
393 |
execute_statement("VACUUM" + commas(tables.list.map(_.ident))) |
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
|
394 |
case None => execute_statement("VACUUM") |
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
|
395 |
} |
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
|
396 |
} |
77348 | 397 |
|
77527
790085b1002f
more detailed table "isabelle_build_serial": allow to monitor activity of build_process instances;
wenzelm
parents:
77403
diff
changeset
|
398 |
def now(): Date |
790085b1002f
more detailed table "isabelle_build_serial": allow to monitor activity of build_process instances;
wenzelm
parents:
77403
diff
changeset
|
399 |
|
65740 | 400 |
|
65008 | 401 |
/* types */ |
402 |
||
65730 | 403 |
def sql_type(T: Type.Value): Source |
65008 | 404 |
|
405 |
||
65006 | 406 |
/* connection */ |
407 |
||
408 |
def connection: Connection |
|
409 |
||
77037
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
410 |
def sqlite_connection: Option[JDBC4Connection] = |
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
411 |
connection match { case conn: JDBC4Connection => Some(conn) case _ => None } |
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
412 |
|
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
413 |
def postgresql_connection: Option[PGConnection] = |
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
414 |
connection match { case conn: PGConnection => Some(conn) case _ => None } |
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
415 |
|
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
416 |
def the_sqlite_connection: JDBC4Connection = |
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
417 |
sqlite_connection getOrElse |
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
418 |
error("SQLite connection expected, but found " + connection.getClass.getName) |
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
419 |
|
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
420 |
def the_postgresql_connection: PGConnection = |
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
421 |
postgresql_connection getOrElse |
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
422 |
error("PostgreSQL connection expected, but found " + connection.getClass.getName) |
164a21e5d568
support specific connection types, for additional operations;
wenzelm
parents:
76870
diff
changeset
|
423 |
|
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
|
424 |
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
|
425 |
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
|
426 |
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
|
427 |
|
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
|
428 |
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
|
429 |
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
|
430 |
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
|
431 |
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
|
432 |
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
|
433 |
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
|
434 |
} |
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
|
435 |
} |
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
|
436 |
} |
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
|
437 |
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
|
438 |
|
78353 | 439 |
override def close(): Unit = connection.close() |
65006 | 440 |
|
78369
ba71ea02d965
more robust Java/Scala multithreading: transaction is always connection.synchronized;
wenzelm
parents:
78366
diff
changeset
|
441 |
def transaction[A](body: => A): A = connection.synchronized { |
75776
72e77c8307ec
tuned signature, following hints by IntelliJ IDEA;
wenzelm
parents:
75393
diff
changeset
|
442 |
val auto_commit = connection.getAutoCommit() |
65006 | 443 |
try { |
444 |
connection.setAutoCommit(false) |
|
75776
72e77c8307ec
tuned signature, following hints by IntelliJ IDEA;
wenzelm
parents:
75393
diff
changeset
|
445 |
val savepoint = connection.setSavepoint() |
65022 | 446 |
try { |
447 |
val result = body |
|
75776
72e77c8307ec
tuned signature, following hints by IntelliJ IDEA;
wenzelm
parents:
75393
diff
changeset
|
448 |
connection.commit() |
65022 | 449 |
result |
450 |
} |
|
451 |
catch { case exn: Throwable => connection.rollback(savepoint); throw exn } |
|
65006 | 452 |
} |
453 |
finally { connection.setAutoCommit(auto_commit) } |
|
454 |
} |
|
455 |
||
78355
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
456 |
def transaction_lock[A]( |
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
457 |
tables: Tables, |
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
458 |
create: Boolean = false, |
78356 | 459 |
label: String = "", |
78355
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
460 |
log: Logger = new System_Logger |
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
461 |
)(body: => A): A = { |
78358
f5cf8e500dee
clarified isabelle.transaction_log: support time_min (in ms);
wenzelm
parents:
78356
diff
changeset
|
462 |
val prop = "isabelle.transaction_trace" |
f5cf8e500dee
clarified isabelle.transaction_log: support time_min (in ms);
wenzelm
parents:
78356
diff
changeset
|
463 |
val trace_min = |
f5cf8e500dee
clarified isabelle.transaction_log: support time_min (in ms);
wenzelm
parents:
78356
diff
changeset
|
464 |
System.getProperty(prop, "") match { |
78365 | 465 |
case Value.Seconds(t) => t |
78358
f5cf8e500dee
clarified isabelle.transaction_log: support time_min (in ms);
wenzelm
parents:
78356
diff
changeset
|
466 |
case "true" => Time.min |
f5cf8e500dee
clarified isabelle.transaction_log: support time_min (in ms);
wenzelm
parents:
78356
diff
changeset
|
467 |
case "false" | "" => Time.max |
f5cf8e500dee
clarified isabelle.transaction_log: support time_min (in ms);
wenzelm
parents:
78356
diff
changeset
|
468 |
case s => error("Bad system property " + prop + ": " + quote(s)) |
f5cf8e500dee
clarified isabelle.transaction_log: support time_min (in ms);
wenzelm
parents:
78356
diff
changeset
|
469 |
} |
78355
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
470 |
|
78362 | 471 |
val trace_count = - SQL.transaction_count() |
78355
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
472 |
val trace_start = Time.now() |
78361 | 473 |
var trace_nl = false |
78355
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
474 |
|
78361 | 475 |
def trace(msg: String, nl: Boolean = false): Unit = { |
78358
f5cf8e500dee
clarified isabelle.transaction_log: support time_min (in ms);
wenzelm
parents:
78356
diff
changeset
|
476 |
val trace_time = Time.now() - trace_start |
f5cf8e500dee
clarified isabelle.transaction_log: support time_min (in ms);
wenzelm
parents:
78356
diff
changeset
|
477 |
if (trace_time >= trace_min) { |
78363 | 478 |
val nl = if (trace_nl) "" else { trace_nl = true; "\n" } |
78361 | 479 |
log(nl + trace_time + " transaction " + trace_count + |
480 |
if_proper(label, " " + label) + ": " + msg) |
|
78355
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
481 |
} |
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
482 |
} |
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
483 |
|
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
484 |
val res = |
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
485 |
transaction { |
78361 | 486 |
trace("begin") |
78355
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
487 |
if (tables.lock(db, create = create)) trace("locked") |
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
488 |
val res = Exn.capture { body } |
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
489 |
trace("end") |
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
490 |
res |
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
491 |
} |
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
492 |
trace("commit") |
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
493 |
Exn.release(res) |
9fbc6a043268
support trace of transaction_lock via property "isabelle.transaction_log";
wenzelm
parents:
78354
diff
changeset
|
494 |
} |
77596 | 495 |
|
496 |
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
|
497 |
|
65006 | 498 |
|
65740 | 499 |
/* statements and results */ |
500 |
||
501 |
def statement(sql: Source): Statement = |
|
502 |
new Statement(db, connection.prepareStatement(sql)) |
|
65006 | 503 |
|
65740 | 504 |
def using_statement[A](sql: Source)(f: Statement => A): A = |
505 |
using(statement(sql))(f) |
|
65006 | 506 |
|
77541 | 507 |
def execute_statement(sql: Source, body: Statement => Unit = _ => ()): Unit = |
508 |
using_statement(sql) { stmt => body(stmt); stmt.execute() } |
|
77540 | 509 |
|
77552 | 510 |
def execute_query_statement[A, B]( |
511 |
sql: Source, |
|
512 |
make_result: Iterator[A] => B, |
|
513 |
get: Result => A |
|
78352 | 514 |
): B = { |
515 |
using_statement(sql) { stmt => |
|
516 |
using(stmt.execute_query()) { res => make_result(res.iterator(get)) } |
|
517 |
} |
|
518 |
} |
|
77552 | 519 |
|
520 |
def execute_query_statementO[A](sql: Source, get: Result => A): Option[A] = |
|
521 |
execute_query_statement[A, Option[A]](sql, _.nextOption, get) |
|
522 |
||
523 |
def execute_query_statementB(sql: Source): Boolean = |
|
78352 | 524 |
using_statement(sql)(stmt => using(stmt.execute_query())(_.next())) |
77552 | 525 |
|
65748 | 526 |
def update_date(stmt: Statement, i: Int, date: Date): Unit |
65740 | 527 |
def date(res: Result, column: Column): Date |
65006 | 528 |
|
65730 | 529 |
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
|
530 |
|
65006 | 531 |
|
65669 | 532 |
/* tables and views */ |
65006 | 533 |
|
75393 | 534 |
def tables: List[String] = { |
65740 | 535 |
val result = new mutable.ListBuffer[String] |
536 |
val rs = connection.getMetaData.getTables(null, null, "%", null) |
|
537 |
while (rs.next) { result += rs.getString(3) } |
|
538 |
result.toList |
|
539 |
} |
|
65006 | 540 |
|
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
|
541 |
def create_table(table: Table, strict: Boolean = false, sql: Source = ""): Unit = { |
77540 | 542 |
execute_statement(table.create(strict, sql_type) + SQL.separate(sql)) |
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
|
543 |
if (is_postgresql) { |
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
|
544 |
for (column <- table.columns if column.T == SQL.Type.Bytes) { |
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
|
545 |
execute_statement( |
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
|
546 |
"ALTER TABLE " + table + " ALTER COLUMN " + column + " SET STORAGE EXTERNAL") |
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
|
547 |
} |
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
|
548 |
} |
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
|
549 |
} |
65006 | 550 |
|
65018 | 551 |
def create_index(table: Table, name: String, columns: List[Column], |
65280 | 552 |
strict: Boolean = false, unique: Boolean = false): Unit = |
77540 | 553 |
execute_statement(table.create_index(name, columns, strict, unique)) |
65006 | 554 |
|
75393 | 555 |
def create_view(table: Table, strict: Boolean = false): Unit = { |
65691 | 556 |
if (strict || !tables.contains(table.name)) { |
77540 | 557 |
execute_statement("CREATE VIEW " + table + " AS " + { table.query; table.body }) |
65690 | 558 |
} |
65691 | 559 |
} |
65006 | 560 |
} |
78362 | 561 |
|
562 |
||
563 |
private val transaction_count = Counter.make() |
|
63778 | 564 |
} |
65006 | 565 |
|
566 |
||
567 |
||
568 |
/** SQLite **/ |
|
569 |
||
75393 | 570 |
object SQLite { |
65021 | 571 |
// see https://www.sqlite.org/lang_datefunc.html |
572 |
val date_format: Date.Format = Date.Format("uuuu-MM-dd HH:mm:ss.SSS x") |
|
573 |
||
75393 | 574 |
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
|
575 |
val lib_path = Path.explode("$ISABELLE_SQLITE_HOME/" + Platform.jvm_platform) |
76529 | 576 |
val lib_name = File.get_file(lib_path).file_name |
577 |
||
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
|
578 |
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
|
579 |
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
|
580 |
|
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
|
581 |
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
|
582 |
} |
65292
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
583 |
|
78163 | 584 |
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
|
585 |
init_jdbc |
65006 | 586 |
val path0 = path.expand |
587 |
val s0 = File.platform_path(path0) |
|
588 |
val s1 = if (Platform.is_windows) s0.replace('\\', '/') else s0 |
|
589 |
val connection = DriverManager.getConnection("jdbc:sqlite:" + s1) |
|
78163 | 590 |
val db = new Database(path0.toString, connection) |
591 |
||
592 |
try { if (restrict) File.restrict(path0) } |
|
593 |
catch { case exn: Throwable => db.close(); throw exn } |
|
594 |
||
595 |
db |
|
65006 | 596 |
} |
597 |
||
75393 | 598 |
class Database private[SQLite](name: String, val connection: Connection) extends SQL.Database { |
65007 | 599 |
override def toString: String = name |
77664
f5d3ade80d15
more specific vacuum operation, which is also relevant to PostgreSQL;
wenzelm
parents:
77598
diff
changeset
|
600 |
|
77527
790085b1002f
more detailed table "isabelle_build_serial": allow to monitor activity of build_process instances;
wenzelm
parents:
77403
diff
changeset
|
601 |
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
|
602 |
|
65730 | 603 |
def sql_type(T: SQL.Type.Value): SQL.Source = SQL.sql_type_sqlite(T) |
65011 | 604 |
|
65748 | 605 |
def update_date(stmt: SQL.Statement, i: Int, date: Date): Unit = |
606 |
if (date == null) stmt.string(i) = (null: String) |
|
607 |
else stmt.string(i) = date_format(date) |
|
65598 | 608 |
|
65740 | 609 |
def date(res: SQL.Result, column: SQL.Column): Date = |
77598 | 610 |
proper_string(res.string(column)) match { |
611 |
case None => null |
|
612 |
case Some(s) => date_format.parse(s) |
|
613 |
} |
|
65021 | 614 |
|
65730 | 615 |
def insert_permissive(table: SQL.Table, sql: SQL.Source = ""): SQL.Source = |
77365 | 616 |
table.insert_cmd(cmd = "INSERT OR IGNORE", sql = sql) |
65006 | 617 |
} |
618 |
} |
|
619 |
||
620 |
||
621 |
||
622 |
/** PostgreSQL **/ |
|
623 |
||
78351 | 624 |
// see https://www.postgresql.org/docs/current/index.html |
625 |
// see https://jdbc.postgresql.org/documentation |
|
626 |
||
75393 | 627 |
object PostgreSQL { |
75967
ff164add75cd
maintain "uuid" column in session build database, to identity the original build process uniquely;
wenzelm
parents:
75966
diff
changeset
|
628 |
type Source = SQL.Source |
ff164add75cd
maintain "uuid" column in session build database, to identity the original build process uniquely;
wenzelm
parents:
75966
diff
changeset
|
629 |
|
78347 | 630 |
lazy val init_jdbc: Unit = Class.forName("org.postgresql.Driver") |
631 |
||
78345 | 632 |
val default_server: SSH.Server = SSH.local_server(port = 5432) |
65006 | 633 |
|
78366 | 634 |
def open_server( |
635 |
options: Options, |
|
636 |
host: String = "", |
|
637 |
port: Int = 0, |
|
638 |
ssh_host: String = "", |
|
639 |
ssh_port: Int = 0, |
|
640 |
ssh_user: String = "" |
|
641 |
): SSH.Server = { |
|
642 |
val server_host = proper_string(host).getOrElse(default_server.host) |
|
643 |
val server_port = if (port > 0) port else default_server.port |
|
644 |
||
645 |
if (ssh_host.isEmpty) SSH.local_server(host = server_host, port = server_port) |
|
646 |
else { |
|
647 |
SSH.open_server(options, host = ssh_host, port = ssh_port, user = ssh_user, |
|
648 |
remote_host = server_host, remote_port = server_port) |
|
649 |
} |
|
650 |
} |
|
651 |
||
78347 | 652 |
def open_database_server( |
653 |
options: Options, |
|
654 |
user: String = "", |
|
655 |
password: String = "", |
|
656 |
database: String = "", |
|
78366 | 657 |
server: SSH.Server = SSH.no_server, |
78347 | 658 |
host: String = "", |
659 |
port: Int = 0, |
|
660 |
ssh_host: String = "", |
|
661 |
ssh_port: Int = 0, |
|
78366 | 662 |
ssh_user: String = "" |
78347 | 663 |
): PostgreSQL.Database = { |
78366 | 664 |
val db_server = |
665 |
if (server.defined) server |
|
78347 | 666 |
else { |
78366 | 667 |
open_server(options, host = host, port = port, ssh_host = ssh_host, |
668 |
ssh_port = ssh_port, ssh_user = ssh_user) |
|
78347 | 669 |
} |
78366 | 670 |
val server_close = !server.defined |
78347 | 671 |
try { |
672 |
open_database(user = user, password = password, database = database, |
|
673 |
server = db_server, server_close = server_close) |
|
674 |
} |
|
78366 | 675 |
catch { case exn: Throwable if server_close => db_server.close(); throw exn } |
78347 | 676 |
} |
65292
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
677 |
|
65006 | 678 |
def open_database( |
679 |
user: String, |
|
680 |
password: String, |
|
681 |
database: String = "", |
|
78347 | 682 |
server: SSH.Server = default_server, |
683 |
server_close: Boolean = false, |
|
75393 | 684 |
): Database = { |
65292
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
685 |
init_jdbc |
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
686 |
|
78347 | 687 |
if (user.isEmpty) error("Undefined database user") |
688 |
if (server.host.isEmpty) error("Undefined database server host") |
|
689 |
if (server.port <= 0) error("Undefined database server port") |
|
65009 | 690 |
|
78347 | 691 |
val name = proper_string(database) getOrElse user |
692 |
val url = "jdbc:postgresql://" + server.host + ":" + server.port + "/" + name |
|
693 |
val ssh = server.ssh_system.ssh_session |
|
694 |
val print = user + "@" + server + "/" + name + if_proper(ssh, " via ssh " + ssh.get) |
|
695 |
||
696 |
val connection = DriverManager.getConnection(url, user, password) |
|
697 |
new Database(connection, print, server, server_close) |
|
65006 | 698 |
} |
699 |
||
65009 | 700 |
class Database private[PostgreSQL]( |
75393 | 701 |
val connection: Connection, |
78347 | 702 |
print: String, |
703 |
server: SSH.Server, |
|
704 |
server_close: Boolean |
|
75393 | 705 |
) extends SQL.Database { |
78347 | 706 |
override def toString: String = print |
65008 | 707 |
|
77527
790085b1002f
more detailed table "isabelle_build_serial": allow to monitor activity of build_process instances;
wenzelm
parents:
77403
diff
changeset
|
708 |
override def now(): Date = { |
790085b1002f
more detailed table "isabelle_build_serial": allow to monitor activity of build_process instances;
wenzelm
parents:
77403
diff
changeset
|
709 |
val now = SQL.Column.date("now") |
77552 | 710 |
execute_query_statementO[Date]("SELECT NOW() as " + now.ident, res => res.date(now)) |
711 |
.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
|
712 |
} |
790085b1002f
more detailed table "isabelle_build_serial": allow to monitor activity of build_process instances;
wenzelm
parents:
77403
diff
changeset
|
713 |
|
65730 | 714 |
def sql_type(T: SQL.Type.Value): SQL.Source = SQL.sql_type_postgresql(T) |
65009 | 715 |
|
65021 | 716 |
// see https://jdbc.postgresql.org/documentation/head/8-date-time.html |
65748 | 717 |
def update_date(stmt: SQL.Statement, i: Int, date: Date): Unit = |
65740 | 718 |
if (date == null) stmt.rep.setObject(i, null) |
69980 | 719 |
else stmt.rep.setObject(i, OffsetDateTime.from(date.to(Date.timezone_utc).rep)) |
65598 | 720 |
|
75393 | 721 |
def date(res: SQL.Result, column: SQL.Column): Date = { |
65740 | 722 |
val obj = res.rep.getObject(column.name, classOf[OffsetDateTime]) |
65704 | 723 |
if (obj == null) null else Date.instant(obj.toInstant) |
724 |
} |
|
65021 | 725 |
|
65730 | 726 |
def insert_permissive(table: SQL.Table, sql: SQL.Source = ""): SQL.Source = |
77377 | 727 |
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
|
728 |
|
77039 | 729 |
|
77590
edc96be6b939
explicit locking for PostgreSQL --- neither available nor required for SQLite;
wenzelm
parents:
77552
diff
changeset
|
730 |
/* 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
|
731 |
// 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
|
732 |
// 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
|
733 |
|
77596 | 734 |
override def lock_tables(tables: List[SQL.Table]): PostgreSQL.Source = |
78253 | 735 |
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
|
736 |
|
78370
fda3f7a158b9
make double-sure that this is a transaction context, notably for LOCK TABLE;
wenzelm
parents:
78369
diff
changeset
|
737 |
override def transaction[A](body: => A): A = super.transaction { |
fda3f7a158b9
make double-sure that this is a transaction context, notably for LOCK TABLE;
wenzelm
parents:
78369
diff
changeset
|
738 |
execute_statement("START TRANSACTION") |
fda3f7a158b9
make double-sure that this is a transaction context, notably for LOCK TABLE;
wenzelm
parents:
78369
diff
changeset
|
739 |
body |
fda3f7a158b9
make double-sure that this is a transaction context, notably for LOCK TABLE;
wenzelm
parents:
78369
diff
changeset
|
740 |
} |
77590
edc96be6b939
explicit locking for PostgreSQL --- neither available nor required for SQLite;
wenzelm
parents:
77552
diff
changeset
|
741 |
|
77039 | 742 |
/* notifications: IPC via database server */ |
743 |
// see https://www.postgresql.org/docs/current/sql-notify.html |
|
744 |
||
745 |
def listen(name: String): Unit = |
|
77541 | 746 |
execute_statement("LISTEN " + SQL.ident(name)) |
77039 | 747 |
|
748 |
def unlisten(name: String = "*"): Unit = |
|
77541 | 749 |
execute_statement("UNLISTEN " + (if (name == "*") name else SQL.ident(name))) |
77039 | 750 |
|
751 |
def notify(name: String, payload: String = ""): Unit = |
|
77543 | 752 |
execute_statement("NOTIFY " + SQL.ident(name) + if_proper(payload, ", " + SQL.string(payload))) |
77039 | 753 |
|
754 |
def get_notifications(): List[PGNotification] = |
|
755 |
the_postgresql_connection.getNotifications() match { |
|
756 |
case null => Nil |
|
757 |
case array => array.toList |
|
758 |
} |
|
759 |
||
760 |
||
78347 | 761 |
override def close(): Unit = { super.close(); if (server_close) server.close() } |
65006 | 762 |
} |
763 |
} |