author | wenzelm |
Thu, 22 Nov 2018 17:34:30 +0100 | |
changeset 69327 | 264b44dce6be |
parent 68091 | 0c7820590236 |
child 69393 | ed0824ef337e |
permissions | -rw-r--r-- |
63788 | 1 |
/* Title: Pure/General/sql.scala |
63778 | 2 |
Author: Makarius |
3 |
||
65006 | 4 |
Support for SQL databases: SQLite and PostgreSQL. |
63778 | 5 |
*/ |
6 |
||
7 |
package isabelle |
|
8 |
||
65021 | 9 |
import java.time.OffsetDateTime |
10 |
import java.sql.{DriverManager, Connection, PreparedStatement, ResultSet} |
|
63779 | 11 |
|
65740 | 12 |
import scala.collection.mutable |
13 |
||
63779 | 14 |
|
63778 | 15 |
object SQL |
16 |
{ |
|
65006 | 17 |
/** SQL language **/ |
18 |
||
65730 | 19 |
type Source = String |
20 |
||
21 |
||
63778 | 22 |
/* concrete syntax */ |
23 |
||
65321 | 24 |
def escape_char(c: Char): String = |
63778 | 25 |
c match { |
26 |
case '\u0000' => "\\0" |
|
27 |
case '\'' => "\\'" |
|
28 |
case '\"' => "\\\"" |
|
29 |
case '\b' => "\\b" |
|
30 |
case '\n' => "\\n" |
|
31 |
case '\r' => "\\r" |
|
32 |
case '\t' => "\\t" |
|
33 |
case '\u001a' => "\\Z" |
|
34 |
case '\\' => "\\\\" |
|
35 |
case _ => c.toString |
|
36 |
} |
|
37 |
||
65730 | 38 |
def string(s: String): Source = |
69327 | 39 |
s.iterator.map(escape_char(_)).mkString("'", "", "'") |
63778 | 40 |
|
65730 | 41 |
def ident(s: String): Source = |
65677
7d25b8dbdbfa
proper quote (amending 546020a98a91): e.g. relevant for "ISABELLE_BUILD_OPTIONS";
wenzelm
parents:
65673
diff
changeset
|
42 |
Long_Name.implode(Long_Name.explode(s).map(a => quote(a.replace("\"", "\"\"")))) |
63779 | 43 |
|
65730 | 44 |
def enclose(s: Source): Source = "(" + s + ")" |
45 |
def enclosure(ss: Iterable[Source]): Source = ss.mkString("(", ", ", ")") |
|
63791 | 46 |
|
65774 | 47 |
def select(columns: List[Column] = Nil, distinct: Boolean = false): Source = |
48 |
"SELECT " + (if (distinct) "DISTINCT " else "") + |
|
49 |
(if (columns.isEmpty) "*" else commas(columns.map(_.ident))) + " FROM " |
|
65644
7ef438495a02
support for qualified names, which are not quoted (e.g. for SQLite);
wenzelm
parents:
65641
diff
changeset
|
50 |
|
65775 | 51 |
val join_outer: Source = " LEFT OUTER JOIN " |
52 |
val join_inner: Source = " INNER JOIN " |
|
65776 | 53 |
def join(outer: Boolean): Source = if (outer) join_outer else join_inner |
65775 | 54 |
|
65804 | 55 |
def member(x: Source, set: Iterable[String]): Source = |
56 |
set.iterator.map(a => x + " = " + SQL.string(a)).mkString("(", " OR ", ")") |
|
57 |
||
63779 | 58 |
|
65008 | 59 |
/* types */ |
60 |
||
61 |
object Type extends Enumeration |
|
62 |
{ |
|
65011 | 63 |
val Boolean = Value("BOOLEAN") |
65008 | 64 |
val Int = Value("INTEGER") |
65 |
val Long = Value("BIGINT") |
|
66 |
val Double = Value("DOUBLE PRECISION") |
|
67 |
val String = Value("TEXT") |
|
68 |
val Bytes = Value("BLOB") |
|
65014 | 69 |
val Date = Value("TIMESTAMP WITH TIME ZONE") |
65008 | 70 |
} |
71 |
||
65730 | 72 |
def sql_type_default(T: Type.Value): Source = T.toString |
65013 | 73 |
|
65730 | 74 |
def sql_type_sqlite(T: Type.Value): Source = |
65019 | 75 |
if (T == Type.Boolean) "INTEGER" |
76 |
else if (T == Type.Date) "TEXT" |
|
77 |
else sql_type_default(T) |
|
65013 | 78 |
|
65730 | 79 |
def sql_type_postgresql(T: Type.Value): Source = |
65019 | 80 |
if (T == Type.Bytes) "BYTEA" |
81 |
else sql_type_default(T) |
|
65008 | 82 |
|
83 |
||
63779 | 84 |
/* columns */ |
85 |
||
86 |
object Column |
|
87 |
{ |
|
65280 | 88 |
def bool(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 89 |
Column(name, Type.Boolean, strict, primary_key) |
65280 | 90 |
def int(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 91 |
Column(name, Type.Int, strict, primary_key) |
65280 | 92 |
def long(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 93 |
Column(name, Type.Long, strict, primary_key) |
65280 | 94 |
def double(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 95 |
Column(name, Type.Double, strict, primary_key) |
65280 | 96 |
def string(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 97 |
Column(name, Type.String, strict, primary_key) |
65280 | 98 |
def bytes(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 99 |
Column(name, Type.Bytes, strict, primary_key) |
65280 | 100 |
def date(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 101 |
Column(name, Type.Date, strict, primary_key) |
63779 | 102 |
} |
103 |
||
65018 | 104 |
sealed case class Column( |
65780 | 105 |
name: String, T: Type.Value, strict: Boolean = false, primary_key: Boolean = false, |
106 |
expr: SQL.Source = "") |
|
63779 | 107 |
{ |
66857 | 108 |
def make_primary_key: Column = copy(primary_key = true) |
109 |
||
65691 | 110 |
def apply(table: Table): Column = |
111 |
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
|
112 |
|
65780 | 113 |
def ident: Source = |
114 |
if (expr == "") SQL.ident(name) |
|
115 |
else enclose(expr) + " AS " + SQL.ident(name) |
|
65695 | 116 |
|
65730 | 117 |
def decl(sql_type: Type.Value => Source): Source = |
65695 | 118 |
ident + " " + sql_type(T) + (if (strict || primary_key) " NOT NULL" else "") |
63781 | 119 |
|
66856 | 120 |
def defined: String = ident + " IS NOT NULL" |
121 |
def undefined: String = ident + " IS NULL" |
|
122 |
||
68091 | 123 |
def equal(s: String): Source = ident + " = " + string(s) |
124 |
def where_equal(s: String): Source = "WHERE " + equal(s) |
|
65593 | 125 |
|
65730 | 126 |
override def toString: Source = ident |
63779 | 127 |
} |
128 |
||
63780 | 129 |
|
130 |
/* tables */ |
|
131 |
||
65730 | 132 |
sealed case class Table(name: String, columns: List[Column], body: Source = "") |
63780 | 133 |
{ |
63790 | 134 |
private val columns_index: Map[String, Int] = |
135 |
columns.iterator.map(_.name).zipWithIndex.toMap |
|
136 |
||
63781 | 137 |
Library.duplicates(columns.map(_.name)) match { |
138 |
case Nil => |
|
139 |
case bad => error("Duplicate column names " + commas_quote(bad) + " for table " + quote(name)) |
|
140 |
} |
|
141 |
||
65730 | 142 |
def ident: Source = SQL.ident(name) |
65691 | 143 |
|
65730 | 144 |
def query: Source = |
65696 | 145 |
if (body == "") error("Missing SQL body for table " + quote(name)) |
146 |
else SQL.enclose(body) |
|
65649 | 147 |
|
65778 | 148 |
def query_named: Source = query + " AS " + SQL.ident(name) |
65702 | 149 |
|
65730 | 150 |
def create(strict: Boolean = false, sql_type: Type.Value => Source): Source = |
65325 | 151 |
{ |
152 |
val primary_key = |
|
153 |
columns.filter(_.primary_key).map(_.name) match { |
|
154 |
case Nil => Nil |
|
155 |
case keys => List("PRIMARY KEY " + enclosure(keys)) |
|
156 |
} |
|
65701 | 157 |
"CREATE TABLE " + (if (strict) "" else "IF NOT EXISTS ") + |
158 |
ident + " " + enclosure(columns.map(_.decl(sql_type)) ::: primary_key) |
|
63781 | 159 |
} |
160 |
||
65698 | 161 |
def create_index(index_name: String, index_columns: List[Column], |
65730 | 162 |
strict: Boolean = false, unique: Boolean = false): Source = |
63791 | 163 |
"CREATE " + (if (unique) "UNIQUE " else "") + "INDEX " + |
65695 | 164 |
(if (strict) "" else "IF NOT EXISTS ") + SQL.ident(index_name) + " ON " + |
165 |
ident + " " + enclosure(index_columns.map(_.name)) |
|
63791 | 166 |
|
65730 | 167 |
def insert_cmd(cmd: Source, sql: Source = ""): Source = |
65703
cead65c19f2e
more direct insert_permissive statement, which avoids somewhat fragile nested transactions;
wenzelm
parents:
65702
diff
changeset
|
168 |
cmd + " INTO " + ident + " VALUES " + enclosure(columns.map(_ => "?")) + |
65698 | 169 |
(if (sql == "") "" else " " + sql) |
63791 | 170 |
|
65730 | 171 |
def insert(sql: Source = ""): Source = insert_cmd("INSERT", sql) |
65703
cead65c19f2e
more direct insert_permissive statement, which avoids somewhat fragile nested transactions;
wenzelm
parents:
65702
diff
changeset
|
172 |
|
65730 | 173 |
def delete(sql: Source = ""): Source = |
65698 | 174 |
"DELETE FROM " + ident + |
175 |
(if (sql == "") "" else " " + sql) |
|
65319 | 176 |
|
65774 | 177 |
def select( |
178 |
select_columns: List[Column] = Nil, sql: Source = "", distinct: Boolean = false): Source = |
|
65698 | 179 |
SQL.select(select_columns, distinct = distinct) + ident + |
180 |
(if (sql == "") "" else " " + sql) |
|
63790 | 181 |
|
65730 | 182 |
override def toString: Source = ident |
63780 | 183 |
} |
63790 | 184 |
|
185 |
||
65012 | 186 |
|
187 |
/** SQL database operations **/ |
|
188 |
||
65740 | 189 |
/* statements */ |
190 |
||
191 |
class Statement private[SQL](val db: Database, val rep: PreparedStatement) |
|
192 |
{ |
|
193 |
stmt => |
|
194 |
||
65748 | 195 |
object bool |
65740 | 196 |
{ |
65748 | 197 |
def update(i: Int, x: Boolean) { rep.setBoolean(i, x) } |
198 |
def update(i: Int, x: Option[Boolean]) |
|
199 |
{ |
|
200 |
if (x.isDefined) update(i, x.get) |
|
201 |
else rep.setNull(i, java.sql.Types.BOOLEAN) |
|
202 |
} |
|
65740 | 203 |
} |
65748 | 204 |
object int |
65740 | 205 |
{ |
65748 | 206 |
def update(i: Int, x: Int) { rep.setInt(i, x) } |
207 |
def update(i: Int, x: Option[Int]) |
|
208 |
{ |
|
209 |
if (x.isDefined) update(i, x.get) |
|
210 |
else rep.setNull(i, java.sql.Types.INTEGER) |
|
211 |
} |
|
65740 | 212 |
} |
65748 | 213 |
object long |
65740 | 214 |
{ |
65748 | 215 |
def update(i: Int, x: Long) { rep.setLong(i, x) } |
216 |
def update(i: Int, x: Option[Long]) |
|
217 |
{ |
|
218 |
if (x.isDefined) update(i, x.get) |
|
219 |
else rep.setNull(i, java.sql.Types.BIGINT) |
|
220 |
} |
|
65740 | 221 |
} |
65748 | 222 |
object double |
65740 | 223 |
{ |
65748 | 224 |
def update(i: Int, x: Double) { rep.setDouble(i, x) } |
225 |
def update(i: Int, x: Option[Double]) |
|
226 |
{ |
|
227 |
if (x.isDefined) update(i, x.get) |
|
228 |
else rep.setNull(i, java.sql.Types.DOUBLE) |
|
229 |
} |
|
230 |
} |
|
231 |
object string |
|
232 |
{ |
|
233 |
def update(i: Int, x: String) { rep.setString(i, x) } |
|
234 |
def update(i: Int, x: Option[String]): Unit = update(i, x.orNull) |
|
65740 | 235 |
} |
65748 | 236 |
object bytes |
65740 | 237 |
{ |
65748 | 238 |
def update(i: Int, bytes: Bytes) |
239 |
{ |
|
240 |
if (bytes == null) rep.setBytes(i, null) |
|
241 |
else rep.setBinaryStream(i, bytes.stream(), bytes.length) |
|
242 |
} |
|
243 |
def update(i: Int, bytes: Option[Bytes]): Unit = update(i, bytes.orNull) |
|
65740 | 244 |
} |
65748 | 245 |
object date |
246 |
{ |
|
247 |
def update(i: Int, date: Date): Unit = db.update_date(stmt, i, date) |
|
248 |
def update(i: Int, date: Option[Date]): Unit = update(i, date.orNull) |
|
249 |
} |
|
65740 | 250 |
|
251 |
def execute(): Boolean = rep.execute() |
|
252 |
def execute_query(): Result = new Result(this, rep.executeQuery()) |
|
253 |
||
254 |
def close(): Unit = rep.close |
|
255 |
} |
|
256 |
||
257 |
||
63790 | 258 |
/* results */ |
259 |
||
65740 | 260 |
class Result private[SQL](val stmt: Statement, val rep: ResultSet) |
63790 | 261 |
{ |
65740 | 262 |
res => |
263 |
||
264 |
def next(): Boolean = rep.next() |
|
265 |
||
266 |
def iterator[A](get: Result => A): Iterator[A] = new Iterator[A] |
|
267 |
{ |
|
268 |
private var _next: Boolean = res.next() |
|
269 |
def hasNext: Boolean = _next |
|
270 |
def next: A = { val x = get(res); _next = res.next(); x } |
|
271 |
} |
|
272 |
||
273 |
def bool(column: Column): Boolean = rep.getBoolean(column.name) |
|
274 |
def int(column: Column): Int = rep.getInt(column.name) |
|
275 |
def long(column: Column): Long = rep.getLong(column.name) |
|
276 |
def double(column: Column): Double = rep.getDouble(column.name) |
|
277 |
def string(column: Column): String = |
|
278 |
{ |
|
279 |
val s = rep.getString(column.name) |
|
280 |
if (s == null) "" else s |
|
281 |
} |
|
282 |
def bytes(column: Column): Bytes = |
|
283 |
{ |
|
284 |
val bs = rep.getBytes(column.name) |
|
285 |
if (bs == null) Bytes.empty else Bytes(bs) |
|
286 |
} |
|
287 |
def date(column: Column): Date = stmt.db.date(res, column) |
|
288 |
||
65741 | 289 |
def timing(c1: Column, c2: Column, c3: Column) = |
290 |
Timing(Time.ms(long(c1)), Time.ms(long(c2)), Time.ms(long(c3))) |
|
291 |
||
65740 | 292 |
def get[A](column: Column, f: Column => A): Option[A] = |
293 |
{ |
|
294 |
val x = f(column) |
|
295 |
if (rep.wasNull) None else Some(x) |
|
296 |
} |
|
297 |
def get_bool(column: Column): Option[Boolean] = get(column, bool _) |
|
298 |
def get_int(column: Column): Option[Int] = get(column, int _) |
|
299 |
def get_long(column: Column): Option[Long] = get(column, long _) |
|
300 |
def get_double(column: Column): Option[Double] = get(column, double _) |
|
301 |
def get_string(column: Column): Option[String] = get(column, string _) |
|
302 |
def get_bytes(column: Column): Option[Bytes] = get(column, bytes _) |
|
303 |
def get_date(column: Column): Option[Date] = get(column, date _) |
|
63790 | 304 |
} |
65006 | 305 |
|
65740 | 306 |
|
307 |
/* database */ |
|
308 |
||
65006 | 309 |
trait Database |
310 |
{ |
|
65740 | 311 |
db => |
312 |
||
313 |
||
65008 | 314 |
/* types */ |
315 |
||
65730 | 316 |
def sql_type(T: Type.Value): Source |
65008 | 317 |
|
318 |
||
65006 | 319 |
/* connection */ |
320 |
||
321 |
def connection: Connection |
|
322 |
||
323 |
def close() { connection.close } |
|
324 |
||
325 |
def transaction[A](body: => A): A = |
|
326 |
{ |
|
327 |
val auto_commit = connection.getAutoCommit |
|
328 |
try { |
|
329 |
connection.setAutoCommit(false) |
|
65022 | 330 |
val savepoint = connection.setSavepoint |
331 |
try { |
|
332 |
val result = body |
|
333 |
connection.commit |
|
334 |
result |
|
335 |
} |
|
336 |
catch { case exn: Throwable => connection.rollback(savepoint); throw exn } |
|
65006 | 337 |
} |
338 |
finally { connection.setAutoCommit(auto_commit) } |
|
339 |
} |
|
340 |
||
341 |
||
65740 | 342 |
/* statements and results */ |
343 |
||
344 |
def statement(sql: Source): Statement = |
|
345 |
new Statement(db, connection.prepareStatement(sql)) |
|
65006 | 346 |
|
65740 | 347 |
def using_statement[A](sql: Source)(f: Statement => A): A = |
348 |
using(statement(sql))(f) |
|
65006 | 349 |
|
65748 | 350 |
def update_date(stmt: Statement, i: Int, date: Date): Unit |
65740 | 351 |
def date(res: Result, column: Column): Date |
65006 | 352 |
|
65730 | 353 |
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
|
354 |
|
65006 | 355 |
|
65669 | 356 |
/* tables and views */ |
65006 | 357 |
|
358 |
def tables: List[String] = |
|
65740 | 359 |
{ |
360 |
val result = new mutable.ListBuffer[String] |
|
361 |
val rs = connection.getMetaData.getTables(null, null, "%", null) |
|
362 |
while (rs.next) { result += rs.getString(3) } |
|
363 |
result.toList |
|
364 |
} |
|
65006 | 365 |
|
65730 | 366 |
def create_table(table: Table, strict: Boolean = false, sql: Source = ""): Unit = |
65698 | 367 |
using_statement( |
368 |
table.create(strict, sql_type) + (if (sql == "") "" else " " + sql))(_.execute()) |
|
65006 | 369 |
|
65018 | 370 |
def create_index(table: Table, name: String, columns: List[Column], |
65280 | 371 |
strict: Boolean = false, unique: Boolean = false): Unit = |
65698 | 372 |
using_statement(table.create_index(name, columns, strict, unique))(_.execute()) |
65006 | 373 |
|
65691 | 374 |
def create_view(table: Table, strict: Boolean = false): Unit = |
375 |
{ |
|
376 |
if (strict || !tables.contains(table.name)) { |
|
65731 | 377 |
val sql = "CREATE VIEW " + table + " AS " + { table.query; table.body } |
65698 | 378 |
using_statement(sql)(_.execute()) |
65690 | 379 |
} |
65691 | 380 |
} |
65006 | 381 |
} |
63778 | 382 |
} |
65006 | 383 |
|
384 |
||
385 |
||
386 |
/** SQLite **/ |
|
387 |
||
388 |
object SQLite |
|
389 |
{ |
|
65021 | 390 |
// see https://www.sqlite.org/lang_datefunc.html |
391 |
val date_format: Date.Format = Date.Format("uuuu-MM-dd HH:mm:ss.SSS x") |
|
392 |
||
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
|
393 |
lazy val init_jdbc: Unit = |
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
|
394 |
{ |
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
|
395 |
val lib_path = Path.explode("$ISABELLE_SQLITE_HOME/" + Platform.jvm_platform) |
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
|
396 |
val 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
|
397 |
File.find_files(lib_path.file) match { |
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
|
398 |
case List(file) => file.getName |
65918 | 399 |
case _ => error("Exactly one file expected in directory " + lib_path.expand) |
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
|
400 |
} |
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
|
401 |
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
|
402 |
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
|
403 |
|
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
|
404 |
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
|
405 |
} |
65292
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
406 |
|
65006 | 407 |
def open_database(path: Path): Database = |
408 |
{ |
|
65292
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
409 |
init_jdbc |
65006 | 410 |
val path0 = path.expand |
411 |
val s0 = File.platform_path(path0) |
|
412 |
val s1 = if (Platform.is_windows) s0.replace('\\', '/') else s0 |
|
413 |
val connection = DriverManager.getConnection("jdbc:sqlite:" + s1) |
|
65007 | 414 |
new Database(path0.toString, connection) |
65006 | 415 |
} |
416 |
||
65007 | 417 |
class Database private[SQLite](name: String, val connection: Connection) extends SQL.Database |
65006 | 418 |
{ |
65007 | 419 |
override def toString: String = name |
65006 | 420 |
|
65730 | 421 |
def sql_type(T: SQL.Type.Value): SQL.Source = SQL.sql_type_sqlite(T) |
65011 | 422 |
|
65748 | 423 |
def update_date(stmt: SQL.Statement, i: Int, date: Date): Unit = |
424 |
if (date == null) stmt.string(i) = (null: String) |
|
425 |
else stmt.string(i) = date_format(date) |
|
65598 | 426 |
|
65740 | 427 |
def date(res: SQL.Result, column: SQL.Column): Date = |
428 |
date_format.parse(res.string(column)) |
|
65021 | 429 |
|
65730 | 430 |
def insert_permissive(table: SQL.Table, sql: SQL.Source = ""): SQL.Source = |
65703
cead65c19f2e
more direct insert_permissive statement, which avoids somewhat fragile nested transactions;
wenzelm
parents:
65702
diff
changeset
|
431 |
table.insert_cmd("INSERT OR IGNORE", sql = sql) |
cead65c19f2e
more direct insert_permissive statement, which avoids somewhat fragile nested transactions;
wenzelm
parents:
65702
diff
changeset
|
432 |
|
65698 | 433 |
def rebuild { using_statement("VACUUM")(_.execute()) } |
65006 | 434 |
} |
435 |
} |
|
436 |
||
437 |
||
438 |
||
439 |
/** PostgreSQL **/ |
|
440 |
||
441 |
object PostgreSQL |
|
442 |
{ |
|
443 |
val default_port = 5432 |
|
444 |
||
65292
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
445 |
lazy val init_jdbc: Unit = Class.forName("org.postgresql.Driver") |
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
446 |
|
65006 | 447 |
def open_database( |
448 |
user: String, |
|
449 |
password: String, |
|
450 |
database: String = "", |
|
451 |
host: String = "", |
|
65594 | 452 |
port: Int = 0, |
65636
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65620
diff
changeset
|
453 |
ssh: Option[SSH.Session] = None, |
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65620
diff
changeset
|
454 |
ssh_close: Boolean = false): Database = |
65006 | 455 |
{ |
65292
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
456 |
init_jdbc |
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
457 |
|
65641 | 458 |
if (user == "") error("Undefined database user") |
65009 | 459 |
|
65717 | 460 |
val db_host = proper_string(host) getOrElse "localhost" |
65594 | 461 |
val db_port = if (port > 0 && port != default_port) ":" + port else "" |
65717 | 462 |
val db_name = "/" + (proper_string(database) getOrElse user) |
65009 | 463 |
|
65010 | 464 |
val (url, name, port_forwarding) = |
65009 | 465 |
ssh match { |
65010 | 466 |
case None => |
467 |
val spec = db_host + db_port + db_name |
|
468 |
val url = "jdbc:postgresql://" + spec |
|
469 |
val name = user + "@" + spec |
|
470 |
(url, name, None) |
|
65009 | 471 |
case Some(ssh) => |
65594 | 472 |
val fw = |
473 |
ssh.port_forwarding(remote_host = db_host, |
|
65636
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65620
diff
changeset
|
474 |
remote_port = if (port > 0) port else default_port, |
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65620
diff
changeset
|
475 |
ssh_close = ssh_close) |
65010 | 476 |
val url = "jdbc:postgresql://localhost:" + fw.local_port + db_name |
477 |
val name = user + "@" + fw + db_name + " via ssh " + ssh |
|
478 |
(url, name, Some(fw)) |
|
65009 | 479 |
} |
480 |
try { |
|
65010 | 481 |
val connection = DriverManager.getConnection(url, user, password) |
482 |
new Database(name, connection, port_forwarding) |
|
65009 | 483 |
} |
484 |
catch { case exn: Throwable => port_forwarding.foreach(_.close); throw exn } |
|
65006 | 485 |
} |
486 |
||
65009 | 487 |
class Database private[PostgreSQL]( |
488 |
name: String, val connection: Connection, port_forwarding: Option[SSH.Port_Forwarding]) |
|
489 |
extends SQL.Database |
|
65006 | 490 |
{ |
65010 | 491 |
override def toString: String = name |
65008 | 492 |
|
65730 | 493 |
def sql_type(T: SQL.Type.Value): SQL.Source = SQL.sql_type_postgresql(T) |
65009 | 494 |
|
65021 | 495 |
// see https://jdbc.postgresql.org/documentation/head/8-date-time.html |
65748 | 496 |
def update_date(stmt: SQL.Statement, i: Int, date: Date): Unit = |
65740 | 497 |
if (date == null) stmt.rep.setObject(i, null) |
498 |
else stmt.rep.setObject(i, OffsetDateTime.from(date.to_utc.rep)) |
|
65598 | 499 |
|
65740 | 500 |
def date(res: SQL.Result, column: SQL.Column): Date = |
65704 | 501 |
{ |
65740 | 502 |
val obj = res.rep.getObject(column.name, classOf[OffsetDateTime]) |
65704 | 503 |
if (obj == null) null else Date.instant(obj.toInstant) |
504 |
} |
|
65021 | 505 |
|
65730 | 506 |
def insert_permissive(table: SQL.Table, sql: SQL.Source = ""): SQL.Source = |
65703
cead65c19f2e
more direct insert_permissive statement, which avoids somewhat fragile nested transactions;
wenzelm
parents:
65702
diff
changeset
|
507 |
table.insert_cmd("INSERT", |
cead65c19f2e
more direct insert_permissive statement, which avoids somewhat fragile nested transactions;
wenzelm
parents:
65702
diff
changeset
|
508 |
sql = sql + (if (sql == "") "" else " ") + "ON CONFLICT DO NOTHING") |
cead65c19f2e
more direct insert_permissive statement, which avoids somewhat fragile nested transactions;
wenzelm
parents:
65702
diff
changeset
|
509 |
|
65009 | 510 |
override def close() { super.close; port_forwarding.foreach(_.close) } |
65006 | 511 |
} |
512 |
} |