author | wenzelm |
Fri, 13 Oct 2017 21:15:04 +0200 | |
changeset 66856 | 6b90c688a6dc |
parent 65918 | b873cefb3b6a |
child 66857 | f8f42289c4df |
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 = |
65321 | 39 |
"'" + s.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 |
{ |
65691 | 108 |
def apply(table: Table): Column = |
109 |
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
|
110 |
|
65780 | 111 |
def ident: Source = |
112 |
if (expr == "") SQL.ident(name) |
|
113 |
else enclose(expr) + " AS " + SQL.ident(name) |
|
65695 | 114 |
|
65730 | 115 |
def decl(sql_type: Type.Value => Source): Source = |
65695 | 116 |
ident + " " + sql_type(T) + (if (strict || primary_key) " NOT NULL" else "") |
63781 | 117 |
|
66856 | 118 |
def defined: String = ident + " IS NOT NULL" |
119 |
def undefined: String = ident + " IS NULL" |
|
120 |
||
65730 | 121 |
def where_equal(s: String): Source = "WHERE " + ident + " = " + string(s) |
65593 | 122 |
|
65730 | 123 |
override def toString: Source = ident |
63779 | 124 |
} |
125 |
||
63780 | 126 |
|
127 |
/* tables */ |
|
128 |
||
65730 | 129 |
sealed case class Table(name: String, columns: List[Column], body: Source = "") |
63780 | 130 |
{ |
63790 | 131 |
private val columns_index: Map[String, Int] = |
132 |
columns.iterator.map(_.name).zipWithIndex.toMap |
|
133 |
||
63781 | 134 |
Library.duplicates(columns.map(_.name)) match { |
135 |
case Nil => |
|
136 |
case bad => error("Duplicate column names " + commas_quote(bad) + " for table " + quote(name)) |
|
137 |
} |
|
138 |
||
65730 | 139 |
def ident: Source = SQL.ident(name) |
65691 | 140 |
|
65730 | 141 |
def query: Source = |
65696 | 142 |
if (body == "") error("Missing SQL body for table " + quote(name)) |
143 |
else SQL.enclose(body) |
|
65649 | 144 |
|
65778 | 145 |
def query_named: Source = query + " AS " + SQL.ident(name) |
65702 | 146 |
|
65730 | 147 |
def create(strict: Boolean = false, sql_type: Type.Value => Source): Source = |
65325 | 148 |
{ |
149 |
val primary_key = |
|
150 |
columns.filter(_.primary_key).map(_.name) match { |
|
151 |
case Nil => Nil |
|
152 |
case keys => List("PRIMARY KEY " + enclosure(keys)) |
|
153 |
} |
|
65701 | 154 |
"CREATE TABLE " + (if (strict) "" else "IF NOT EXISTS ") + |
155 |
ident + " " + enclosure(columns.map(_.decl(sql_type)) ::: primary_key) |
|
63781 | 156 |
} |
157 |
||
65698 | 158 |
def create_index(index_name: String, index_columns: List[Column], |
65730 | 159 |
strict: Boolean = false, unique: Boolean = false): Source = |
63791 | 160 |
"CREATE " + (if (unique) "UNIQUE " else "") + "INDEX " + |
65695 | 161 |
(if (strict) "" else "IF NOT EXISTS ") + SQL.ident(index_name) + " ON " + |
162 |
ident + " " + enclosure(index_columns.map(_.name)) |
|
63791 | 163 |
|
65730 | 164 |
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
|
165 |
cmd + " INTO " + ident + " VALUES " + enclosure(columns.map(_ => "?")) + |
65698 | 166 |
(if (sql == "") "" else " " + sql) |
63791 | 167 |
|
65730 | 168 |
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
|
169 |
|
65730 | 170 |
def delete(sql: Source = ""): Source = |
65698 | 171 |
"DELETE FROM " + ident + |
172 |
(if (sql == "") "" else " " + sql) |
|
65319 | 173 |
|
65774 | 174 |
def select( |
175 |
select_columns: List[Column] = Nil, sql: Source = "", distinct: Boolean = false): Source = |
|
65698 | 176 |
SQL.select(select_columns, distinct = distinct) + ident + |
177 |
(if (sql == "") "" else " " + sql) |
|
63790 | 178 |
|
65730 | 179 |
override def toString: Source = ident |
63780 | 180 |
} |
63790 | 181 |
|
182 |
||
65012 | 183 |
|
184 |
/** SQL database operations **/ |
|
185 |
||
65740 | 186 |
/* statements */ |
187 |
||
188 |
class Statement private[SQL](val db: Database, val rep: PreparedStatement) |
|
189 |
{ |
|
190 |
stmt => |
|
191 |
||
65748 | 192 |
object bool |
65740 | 193 |
{ |
65748 | 194 |
def update(i: Int, x: Boolean) { rep.setBoolean(i, x) } |
195 |
def update(i: Int, x: Option[Boolean]) |
|
196 |
{ |
|
197 |
if (x.isDefined) update(i, x.get) |
|
198 |
else rep.setNull(i, java.sql.Types.BOOLEAN) |
|
199 |
} |
|
65740 | 200 |
} |
65748 | 201 |
object int |
65740 | 202 |
{ |
65748 | 203 |
def update(i: Int, x: Int) { rep.setInt(i, x) } |
204 |
def update(i: Int, x: Option[Int]) |
|
205 |
{ |
|
206 |
if (x.isDefined) update(i, x.get) |
|
207 |
else rep.setNull(i, java.sql.Types.INTEGER) |
|
208 |
} |
|
65740 | 209 |
} |
65748 | 210 |
object long |
65740 | 211 |
{ |
65748 | 212 |
def update(i: Int, x: Long) { rep.setLong(i, x) } |
213 |
def update(i: Int, x: Option[Long]) |
|
214 |
{ |
|
215 |
if (x.isDefined) update(i, x.get) |
|
216 |
else rep.setNull(i, java.sql.Types.BIGINT) |
|
217 |
} |
|
65740 | 218 |
} |
65748 | 219 |
object double |
65740 | 220 |
{ |
65748 | 221 |
def update(i: Int, x: Double) { rep.setDouble(i, x) } |
222 |
def update(i: Int, x: Option[Double]) |
|
223 |
{ |
|
224 |
if (x.isDefined) update(i, x.get) |
|
225 |
else rep.setNull(i, java.sql.Types.DOUBLE) |
|
226 |
} |
|
227 |
} |
|
228 |
object string |
|
229 |
{ |
|
230 |
def update(i: Int, x: String) { rep.setString(i, x) } |
|
231 |
def update(i: Int, x: Option[String]): Unit = update(i, x.orNull) |
|
65740 | 232 |
} |
65748 | 233 |
object bytes |
65740 | 234 |
{ |
65748 | 235 |
def update(i: Int, bytes: Bytes) |
236 |
{ |
|
237 |
if (bytes == null) rep.setBytes(i, null) |
|
238 |
else rep.setBinaryStream(i, bytes.stream(), bytes.length) |
|
239 |
} |
|
240 |
def update(i: Int, bytes: Option[Bytes]): Unit = update(i, bytes.orNull) |
|
65740 | 241 |
} |
65748 | 242 |
object date |
243 |
{ |
|
244 |
def update(i: Int, date: Date): Unit = db.update_date(stmt, i, date) |
|
245 |
def update(i: Int, date: Option[Date]): Unit = update(i, date.orNull) |
|
246 |
} |
|
65740 | 247 |
|
248 |
def execute(): Boolean = rep.execute() |
|
249 |
def execute_query(): Result = new Result(this, rep.executeQuery()) |
|
250 |
||
251 |
def close(): Unit = rep.close |
|
252 |
} |
|
253 |
||
254 |
||
63790 | 255 |
/* results */ |
256 |
||
65740 | 257 |
class Result private[SQL](val stmt: Statement, val rep: ResultSet) |
63790 | 258 |
{ |
65740 | 259 |
res => |
260 |
||
261 |
def next(): Boolean = rep.next() |
|
262 |
||
263 |
def iterator[A](get: Result => A): Iterator[A] = new Iterator[A] |
|
264 |
{ |
|
265 |
private var _next: Boolean = res.next() |
|
266 |
def hasNext: Boolean = _next |
|
267 |
def next: A = { val x = get(res); _next = res.next(); x } |
|
268 |
} |
|
269 |
||
270 |
def bool(column: Column): Boolean = rep.getBoolean(column.name) |
|
271 |
def int(column: Column): Int = rep.getInt(column.name) |
|
272 |
def long(column: Column): Long = rep.getLong(column.name) |
|
273 |
def double(column: Column): Double = rep.getDouble(column.name) |
|
274 |
def string(column: Column): String = |
|
275 |
{ |
|
276 |
val s = rep.getString(column.name) |
|
277 |
if (s == null) "" else s |
|
278 |
} |
|
279 |
def bytes(column: Column): Bytes = |
|
280 |
{ |
|
281 |
val bs = rep.getBytes(column.name) |
|
282 |
if (bs == null) Bytes.empty else Bytes(bs) |
|
283 |
} |
|
284 |
def date(column: Column): Date = stmt.db.date(res, column) |
|
285 |
||
65741 | 286 |
def timing(c1: Column, c2: Column, c3: Column) = |
287 |
Timing(Time.ms(long(c1)), Time.ms(long(c2)), Time.ms(long(c3))) |
|
288 |
||
65740 | 289 |
def get[A](column: Column, f: Column => A): Option[A] = |
290 |
{ |
|
291 |
val x = f(column) |
|
292 |
if (rep.wasNull) None else Some(x) |
|
293 |
} |
|
294 |
def get_bool(column: Column): Option[Boolean] = get(column, bool _) |
|
295 |
def get_int(column: Column): Option[Int] = get(column, int _) |
|
296 |
def get_long(column: Column): Option[Long] = get(column, long _) |
|
297 |
def get_double(column: Column): Option[Double] = get(column, double _) |
|
298 |
def get_string(column: Column): Option[String] = get(column, string _) |
|
299 |
def get_bytes(column: Column): Option[Bytes] = get(column, bytes _) |
|
300 |
def get_date(column: Column): Option[Date] = get(column, date _) |
|
63790 | 301 |
} |
65006 | 302 |
|
65740 | 303 |
|
304 |
/* database */ |
|
305 |
||
65006 | 306 |
trait Database |
307 |
{ |
|
65740 | 308 |
db => |
309 |
||
310 |
||
65008 | 311 |
/* types */ |
312 |
||
65730 | 313 |
def sql_type(T: Type.Value): Source |
65008 | 314 |
|
315 |
||
65006 | 316 |
/* connection */ |
317 |
||
318 |
def connection: Connection |
|
319 |
||
320 |
def close() { connection.close } |
|
321 |
||
322 |
def transaction[A](body: => A): A = |
|
323 |
{ |
|
324 |
val auto_commit = connection.getAutoCommit |
|
325 |
try { |
|
326 |
connection.setAutoCommit(false) |
|
65022 | 327 |
val savepoint = connection.setSavepoint |
328 |
try { |
|
329 |
val result = body |
|
330 |
connection.commit |
|
331 |
result |
|
332 |
} |
|
333 |
catch { case exn: Throwable => connection.rollback(savepoint); throw exn } |
|
65006 | 334 |
} |
335 |
finally { connection.setAutoCommit(auto_commit) } |
|
336 |
} |
|
337 |
||
338 |
||
65740 | 339 |
/* statements and results */ |
340 |
||
341 |
def statement(sql: Source): Statement = |
|
342 |
new Statement(db, connection.prepareStatement(sql)) |
|
65006 | 343 |
|
65740 | 344 |
def using_statement[A](sql: Source)(f: Statement => A): A = |
345 |
using(statement(sql))(f) |
|
65006 | 346 |
|
65748 | 347 |
def update_date(stmt: Statement, i: Int, date: Date): Unit |
65740 | 348 |
def date(res: Result, column: Column): Date |
65006 | 349 |
|
65730 | 350 |
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
|
351 |
|
65006 | 352 |
|
65669 | 353 |
/* tables and views */ |
65006 | 354 |
|
355 |
def tables: List[String] = |
|
65740 | 356 |
{ |
357 |
val result = new mutable.ListBuffer[String] |
|
358 |
val rs = connection.getMetaData.getTables(null, null, "%", null) |
|
359 |
while (rs.next) { result += rs.getString(3) } |
|
360 |
result.toList |
|
361 |
} |
|
65006 | 362 |
|
65730 | 363 |
def create_table(table: Table, strict: Boolean = false, sql: Source = ""): Unit = |
65698 | 364 |
using_statement( |
365 |
table.create(strict, sql_type) + (if (sql == "") "" else " " + sql))(_.execute()) |
|
65006 | 366 |
|
65018 | 367 |
def create_index(table: Table, name: String, columns: List[Column], |
65280 | 368 |
strict: Boolean = false, unique: Boolean = false): Unit = |
65698 | 369 |
using_statement(table.create_index(name, columns, strict, unique))(_.execute()) |
65006 | 370 |
|
65691 | 371 |
def create_view(table: Table, strict: Boolean = false): Unit = |
372 |
{ |
|
373 |
if (strict || !tables.contains(table.name)) { |
|
65731 | 374 |
val sql = "CREATE VIEW " + table + " AS " + { table.query; table.body } |
65698 | 375 |
using_statement(sql)(_.execute()) |
65690 | 376 |
} |
65691 | 377 |
} |
65006 | 378 |
} |
63778 | 379 |
} |
65006 | 380 |
|
381 |
||
382 |
||
383 |
/** SQLite **/ |
|
384 |
||
385 |
object SQLite |
|
386 |
{ |
|
65021 | 387 |
// see https://www.sqlite.org/lang_datefunc.html |
388 |
val date_format: Date.Format = Date.Format("uuuu-MM-dd HH:mm:ss.SSS x") |
|
389 |
||
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
|
390 |
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
|
391 |
{ |
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
|
392 |
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
|
393 |
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
|
394 |
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
|
395 |
case List(file) => file.getName |
65918 | 396 |
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
|
397 |
} |
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 |
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
|
399 |
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
|
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 |
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
|
402 |
} |
65292
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
403 |
|
65006 | 404 |
def open_database(path: Path): Database = |
405 |
{ |
|
65292
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
406 |
init_jdbc |
65006 | 407 |
val path0 = path.expand |
408 |
val s0 = File.platform_path(path0) |
|
409 |
val s1 = if (Platform.is_windows) s0.replace('\\', '/') else s0 |
|
410 |
val connection = DriverManager.getConnection("jdbc:sqlite:" + s1) |
|
65007 | 411 |
new Database(path0.toString, connection) |
65006 | 412 |
} |
413 |
||
65007 | 414 |
class Database private[SQLite](name: String, val connection: Connection) extends SQL.Database |
65006 | 415 |
{ |
65007 | 416 |
override def toString: String = name |
65006 | 417 |
|
65730 | 418 |
def sql_type(T: SQL.Type.Value): SQL.Source = SQL.sql_type_sqlite(T) |
65011 | 419 |
|
65748 | 420 |
def update_date(stmt: SQL.Statement, i: Int, date: Date): Unit = |
421 |
if (date == null) stmt.string(i) = (null: String) |
|
422 |
else stmt.string(i) = date_format(date) |
|
65598 | 423 |
|
65740 | 424 |
def date(res: SQL.Result, column: SQL.Column): Date = |
425 |
date_format.parse(res.string(column)) |
|
65021 | 426 |
|
65730 | 427 |
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
|
428 |
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
|
429 |
|
65698 | 430 |
def rebuild { using_statement("VACUUM")(_.execute()) } |
65006 | 431 |
} |
432 |
} |
|
433 |
||
434 |
||
435 |
||
436 |
/** PostgreSQL **/ |
|
437 |
||
438 |
object PostgreSQL |
|
439 |
{ |
|
440 |
val default_port = 5432 |
|
441 |
||
65292
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
442 |
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
|
443 |
|
65006 | 444 |
def open_database( |
445 |
user: String, |
|
446 |
password: String, |
|
447 |
database: String = "", |
|
448 |
host: String = "", |
|
65594 | 449 |
port: Int = 0, |
65636
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65620
diff
changeset
|
450 |
ssh: Option[SSH.Session] = None, |
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65620
diff
changeset
|
451 |
ssh_close: Boolean = false): Database = |
65006 | 452 |
{ |
65292
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
453 |
init_jdbc |
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
454 |
|
65641 | 455 |
if (user == "") error("Undefined database user") |
65009 | 456 |
|
65717 | 457 |
val db_host = proper_string(host) getOrElse "localhost" |
65594 | 458 |
val db_port = if (port > 0 && port != default_port) ":" + port else "" |
65717 | 459 |
val db_name = "/" + (proper_string(database) getOrElse user) |
65009 | 460 |
|
65010 | 461 |
val (url, name, port_forwarding) = |
65009 | 462 |
ssh match { |
65010 | 463 |
case None => |
464 |
val spec = db_host + db_port + db_name |
|
465 |
val url = "jdbc:postgresql://" + spec |
|
466 |
val name = user + "@" + spec |
|
467 |
(url, name, None) |
|
65009 | 468 |
case Some(ssh) => |
65594 | 469 |
val fw = |
470 |
ssh.port_forwarding(remote_host = db_host, |
|
65636
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65620
diff
changeset
|
471 |
remote_port = if (port > 0) port else default_port, |
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65620
diff
changeset
|
472 |
ssh_close = ssh_close) |
65010 | 473 |
val url = "jdbc:postgresql://localhost:" + fw.local_port + db_name |
474 |
val name = user + "@" + fw + db_name + " via ssh " + ssh |
|
475 |
(url, name, Some(fw)) |
|
65009 | 476 |
} |
477 |
try { |
|
65010 | 478 |
val connection = DriverManager.getConnection(url, user, password) |
479 |
new Database(name, connection, port_forwarding) |
|
65009 | 480 |
} |
481 |
catch { case exn: Throwable => port_forwarding.foreach(_.close); throw exn } |
|
65006 | 482 |
} |
483 |
||
65009 | 484 |
class Database private[PostgreSQL]( |
485 |
name: String, val connection: Connection, port_forwarding: Option[SSH.Port_Forwarding]) |
|
486 |
extends SQL.Database |
|
65006 | 487 |
{ |
65010 | 488 |
override def toString: String = name |
65008 | 489 |
|
65730 | 490 |
def sql_type(T: SQL.Type.Value): SQL.Source = SQL.sql_type_postgresql(T) |
65009 | 491 |
|
65021 | 492 |
// see https://jdbc.postgresql.org/documentation/head/8-date-time.html |
65748 | 493 |
def update_date(stmt: SQL.Statement, i: Int, date: Date): Unit = |
65740 | 494 |
if (date == null) stmt.rep.setObject(i, null) |
495 |
else stmt.rep.setObject(i, OffsetDateTime.from(date.to_utc.rep)) |
|
65598 | 496 |
|
65740 | 497 |
def date(res: SQL.Result, column: SQL.Column): Date = |
65704 | 498 |
{ |
65740 | 499 |
val obj = res.rep.getObject(column.name, classOf[OffsetDateTime]) |
65704 | 500 |
if (obj == null) null else Date.instant(obj.toInstant) |
501 |
} |
|
65021 | 502 |
|
65730 | 503 |
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
|
504 |
table.insert_cmd("INSERT", |
cead65c19f2e
more direct insert_permissive statement, which avoids somewhat fragile nested transactions;
wenzelm
parents:
65702
diff
changeset
|
505 |
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
|
506 |
|
65009 | 507 |
override def close() { super.close; port_forwarding.foreach(_.close) } |
65006 | 508 |
} |
509 |
} |