author | wenzelm |
Tue, 02 May 2017 21:57:32 +0200 | |
changeset 65690 | 74ec3cfcb6bf |
parent 65677 | 7d25b8dbdbfa |
child 65691 | 2229276a1f99 |
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 |
|
12 |
||
63778 | 13 |
object SQL |
14 |
{ |
|
65006 | 15 |
/** SQL language **/ |
16 |
||
63778 | 17 |
/* concrete syntax */ |
18 |
||
65321 | 19 |
def escape_char(c: Char): String = |
63778 | 20 |
c match { |
21 |
case '\u0000' => "\\0" |
|
22 |
case '\'' => "\\'" |
|
23 |
case '\"' => "\\\"" |
|
24 |
case '\b' => "\\b" |
|
25 |
case '\n' => "\\n" |
|
26 |
case '\r' => "\\r" |
|
27 |
case '\t' => "\\t" |
|
28 |
case '\u001a' => "\\Z" |
|
29 |
case '\\' => "\\\\" |
|
30 |
case _ => c.toString |
|
31 |
} |
|
32 |
||
65648 | 33 |
def string(s: String): String = |
65321 | 34 |
"'" + s.map(escape_char(_)).mkString + "'" |
63778 | 35 |
|
65672 | 36 |
def identifer(s: String): String = |
65677
7d25b8dbdbfa
proper quote (amending 546020a98a91): e.g. relevant for "ISABELLE_BUILD_OPTIONS";
wenzelm
parents:
65673
diff
changeset
|
37 |
Long_Name.implode(Long_Name.explode(s).map(a => quote(a.replace("\"", "\"\"")))) |
63779 | 38 |
|
65668 | 39 |
def enclose(s: String): String = "(" + s + ")" |
63791 | 40 |
def enclosure(ss: Iterable[String]): String = ss.mkString("(", ", ", ")") |
41 |
||
65644
7ef438495a02
support for qualified names, which are not quoted (e.g. for SQLite);
wenzelm
parents:
65641
diff
changeset
|
42 |
def select(columns: List[Column], distinct: Boolean = false): String = |
65649 | 43 |
"SELECT " + (if (distinct) "DISTINCT " else "") + commas(columns.map(_.sql)) + " FROM " |
65644
7ef438495a02
support for qualified names, which are not quoted (e.g. for SQLite);
wenzelm
parents:
65641
diff
changeset
|
44 |
|
65668 | 45 |
def join(table1: Table, table2: Table, sql: String = "", outer: Boolean = false): String = |
46 |
table1.sql + (if (outer) " LEFT OUTER JOIN " else " INNER JOIN ") + table2.sql + |
|
47 |
(if (sql == "") "" else " ON " + sql) |
|
48 |
||
49 |
def join_outer(table1: Table, table2: Table, sql: String = ""): String = |
|
50 |
join(table1, table2, sql, outer = true) |
|
51 |
||
63779 | 52 |
|
65008 | 53 |
/* types */ |
54 |
||
55 |
object Type extends Enumeration |
|
56 |
{ |
|
65011 | 57 |
val Boolean = Value("BOOLEAN") |
65008 | 58 |
val Int = Value("INTEGER") |
59 |
val Long = Value("BIGINT") |
|
60 |
val Double = Value("DOUBLE PRECISION") |
|
61 |
val String = Value("TEXT") |
|
62 |
val Bytes = Value("BLOB") |
|
65014 | 63 |
val Date = Value("TIMESTAMP WITH TIME ZONE") |
65008 | 64 |
} |
65 |
||
65019 | 66 |
def sql_type_default(T: Type.Value): String = T.toString |
65013 | 67 |
|
65019 | 68 |
def sql_type_sqlite(T: Type.Value): String = |
69 |
if (T == Type.Boolean) "INTEGER" |
|
70 |
else if (T == Type.Date) "TEXT" |
|
71 |
else sql_type_default(T) |
|
65013 | 72 |
|
65019 | 73 |
def sql_type_postgresql(T: Type.Value): String = |
74 |
if (T == Type.Bytes) "BYTEA" |
|
75 |
else sql_type_default(T) |
|
65008 | 76 |
|
77 |
||
63779 | 78 |
/* columns */ |
79 |
||
65669 | 80 |
trait Qualifier |
81 |
{ |
|
82 |
def name: String |
|
83 |
} |
|
84 |
||
63779 | 85 |
object Column |
86 |
{ |
|
65280 | 87 |
def bool(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 88 |
Column(name, Type.Boolean, strict, primary_key) |
65280 | 89 |
def int(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 90 |
Column(name, Type.Int, strict, primary_key) |
65280 | 91 |
def long(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 92 |
Column(name, Type.Long, strict, primary_key) |
65280 | 93 |
def double(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 94 |
Column(name, Type.Double, strict, primary_key) |
65280 | 95 |
def string(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 96 |
Column(name, Type.String, strict, primary_key) |
65280 | 97 |
def bytes(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 98 |
Column(name, Type.Bytes, strict, primary_key) |
65280 | 99 |
def date(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 100 |
Column(name, Type.Date, strict, primary_key) |
63779 | 101 |
} |
102 |
||
65018 | 103 |
sealed case class Column( |
65280 | 104 |
name: String, T: Type.Value, strict: Boolean = false, primary_key: Boolean = false) |
63779 | 105 |
{ |
65669 | 106 |
def apply(qual: Qualifier): Column = |
107 |
Column(Long_Name.qualify(qual.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
|
108 |
|
65672 | 109 |
def sql: String = identifer(name) |
65019 | 110 |
def sql_decl(sql_type: Type.Value => String): String = |
65673 | 111 |
identifer(name) + " " + sql_type(T) + (if (strict || primary_key) " NOT NULL" else "") |
63781 | 112 |
|
65673 | 113 |
def sql_where_eq: String = "WHERE " + identifer(name) + " = " |
65648 | 114 |
def sql_where_equal(s: String): String = sql_where_eq + string(s) |
65593 | 115 |
|
65019 | 116 |
override def toString: String = sql_decl(sql_type_default) |
63779 | 117 |
} |
118 |
||
63780 | 119 |
|
120 |
/* tables */ |
|
121 |
||
65669 | 122 |
sealed case class Table(name: String, columns: List[Column]) extends Qualifier |
63780 | 123 |
{ |
63790 | 124 |
private val columns_index: Map[String, Int] = |
125 |
columns.iterator.map(_.name).zipWithIndex.toMap |
|
126 |
||
63781 | 127 |
Library.duplicates(columns.map(_.name)) match { |
128 |
case Nil => |
|
129 |
case bad => error("Duplicate column names " + commas_quote(bad) + " for table " + quote(name)) |
|
130 |
} |
|
131 |
||
65672 | 132 |
def sql: String = identifer(name) |
65649 | 133 |
|
65325 | 134 |
def sql_columns(sql_type: Type.Value => String): String = |
135 |
{ |
|
136 |
val primary_key = |
|
137 |
columns.filter(_.primary_key).map(_.name) match { |
|
138 |
case Nil => Nil |
|
139 |
case keys => List("PRIMARY KEY " + enclosure(keys)) |
|
140 |
} |
|
141 |
enclosure(columns.map(_.sql_decl(sql_type)) ::: primary_key) |
|
63781 | 142 |
} |
143 |
||
65327
e886aed88b2c
clarified signature: rowid is specific to SQLite;
wenzelm
parents:
65325
diff
changeset
|
144 |
def sql_create(strict: Boolean, sql_type: Type.Value => String): String = |
63784 | 145 |
"CREATE TABLE " + (if (strict) "" else "IF NOT EXISTS ") + |
65672 | 146 |
identifer(name) + " " + sql_columns(sql_type) |
63780 | 147 |
|
148 |
def sql_drop(strict: Boolean): String = |
|
65672 | 149 |
"DROP TABLE " + (if (strict) "" else "IF EXISTS ") + identifer(name) |
63783 | 150 |
|
63791 | 151 |
def sql_create_index( |
65018 | 152 |
index_name: String, index_columns: List[Column], |
63791 | 153 |
strict: Boolean, unique: Boolean): String = |
154 |
"CREATE " + (if (unique) "UNIQUE " else "") + "INDEX " + |
|
65672 | 155 |
(if (strict) "" else "IF NOT EXISTS ") + identifer(index_name) + " ON " + |
156 |
identifer(name) + " " + enclosure(index_columns.map(_.name)) |
|
63791 | 157 |
|
158 |
def sql_drop_index(index_name: String, strict: Boolean): String = |
|
65672 | 159 |
"DROP INDEX " + (if (strict) "" else "IF EXISTS ") + identifer(index_name) |
63791 | 160 |
|
63790 | 161 |
def sql_insert: String = |
65672 | 162 |
"INSERT INTO " + identifer(name) + " VALUES " + enclosure(columns.map(_ => "?")) |
63791 | 163 |
|
65319 | 164 |
def sql_delete: String = |
65672 | 165 |
"DELETE FROM " + identifer(name) |
65319 | 166 |
|
65644
7ef438495a02
support for qualified names, which are not quoted (e.g. for SQLite);
wenzelm
parents:
65641
diff
changeset
|
167 |
def sql_select(select_columns: List[Column], distinct: Boolean = false): String = |
65672 | 168 |
select(select_columns, distinct = distinct) + identifer(name) |
63790 | 169 |
|
63783 | 170 |
override def toString: String = |
65672 | 171 |
"TABLE " + identifer(name) + " " + sql_columns(sql_type_default) |
63780 | 172 |
} |
63790 | 173 |
|
174 |
||
65669 | 175 |
/* views */ |
176 |
||
65690 | 177 |
sealed case class View(name: String, columns: List[Column], query: String) extends Qualifier |
65669 | 178 |
{ |
65672 | 179 |
def sql: String = identifer(name) |
65690 | 180 |
def sql_create: String = "CREATE VIEW " + identifer(name) + " AS " + query |
181 |
||
65669 | 182 |
override def toString: String = |
65672 | 183 |
"VIEW " + identifer(name) + " " + enclosure(columns.map(_.sql_decl(sql_type_default))) |
65669 | 184 |
} |
185 |
||
65012 | 186 |
|
187 |
/** SQL database operations **/ |
|
188 |
||
63790 | 189 |
/* results */ |
190 |
||
191 |
def iterator[A](rs: ResultSet)(get: ResultSet => A): Iterator[A] = new Iterator[A] |
|
192 |
{ |
|
193 |
private var _next: Boolean = rs.next() |
|
194 |
def hasNext: Boolean = _next |
|
195 |
def next: A = { val x = get(rs); _next = rs.next(); x } |
|
196 |
} |
|
65006 | 197 |
|
198 |
trait Database |
|
199 |
{ |
|
65008 | 200 |
/* types */ |
201 |
||
65019 | 202 |
def sql_type(T: Type.Value): String |
65008 | 203 |
|
204 |
||
65006 | 205 |
/* connection */ |
206 |
||
207 |
def connection: Connection |
|
208 |
||
209 |
def close() { connection.close } |
|
210 |
||
211 |
def transaction[A](body: => A): A = |
|
212 |
{ |
|
213 |
val auto_commit = connection.getAutoCommit |
|
214 |
try { |
|
215 |
connection.setAutoCommit(false) |
|
65022 | 216 |
val savepoint = connection.setSavepoint |
217 |
try { |
|
218 |
val result = body |
|
219 |
connection.commit |
|
220 |
result |
|
221 |
} |
|
222 |
catch { case exn: Throwable => connection.rollback(savepoint); throw exn } |
|
65006 | 223 |
} |
224 |
finally { connection.setAutoCommit(auto_commit) } |
|
225 |
} |
|
226 |
||
227 |
||
228 |
/* statements */ |
|
229 |
||
230 |
def statement(sql: String): PreparedStatement = connection.prepareStatement(sql) |
|
231 |
||
65619 | 232 |
def insert(table: Table): PreparedStatement = statement(table.sql_insert) |
65006 | 233 |
|
65619 | 234 |
def delete(table: Table, sql: String = ""): PreparedStatement = |
65319 | 235 |
statement(table.sql_delete + (if (sql == "") "" else " " + sql)) |
236 |
||
65619 | 237 |
def select(table: Table, columns: List[Column], sql: String = "", distinct: Boolean = false) |
238 |
: PreparedStatement = |
|
65644
7ef438495a02
support for qualified names, which are not quoted (e.g. for SQLite);
wenzelm
parents:
65641
diff
changeset
|
239 |
statement(table.sql_select(columns, distinct = distinct) + (if (sql == "") "" else " " + sql)) |
65006 | 240 |
|
241 |
||
65020 | 242 |
/* input */ |
243 |
||
244 |
def set_bool(stmt: PreparedStatement, i: Int, x: Boolean) { stmt.setBoolean(i, x) } |
|
65612 | 245 |
def set_bool(stmt: PreparedStatement, i: Int, x: Option[Boolean]) |
246 |
{ |
|
247 |
if (x.isDefined) set_bool(stmt, i, x.get) |
|
248 |
else stmt.setNull(i, java.sql.Types.BOOLEAN) |
|
249 |
} |
|
65615
67974c59ba93
tuned signature: avoid null in regular Scala code;
wenzelm
parents:
65612
diff
changeset
|
250 |
|
65020 | 251 |
def set_int(stmt: PreparedStatement, i: Int, x: Int) { stmt.setInt(i, x) } |
65612 | 252 |
def set_int(stmt: PreparedStatement, i: Int, x: Option[Int]) |
253 |
{ |
|
254 |
if (x.isDefined) set_int(stmt, i, x.get) |
|
255 |
else stmt.setNull(i, java.sql.Types.INTEGER) |
|
256 |
} |
|
65615
67974c59ba93
tuned signature: avoid null in regular Scala code;
wenzelm
parents:
65612
diff
changeset
|
257 |
|
65020 | 258 |
def set_long(stmt: PreparedStatement, i: Int, x: Long) { stmt.setLong(i, x) } |
65612 | 259 |
def set_long(stmt: PreparedStatement, i: Int, x: Option[Long]) |
260 |
{ |
|
261 |
if (x.isDefined) set_long(stmt, i, x.get) |
|
262 |
else stmt.setNull(i, java.sql.Types.BIGINT) |
|
263 |
} |
|
65615
67974c59ba93
tuned signature: avoid null in regular Scala code;
wenzelm
parents:
65612
diff
changeset
|
264 |
|
65020 | 265 |
def set_double(stmt: PreparedStatement, i: Int, x: Double) { stmt.setDouble(i, x) } |
65612 | 266 |
def set_double(stmt: PreparedStatement, i: Int, x: Option[Double]) |
267 |
{ |
|
268 |
if (x.isDefined) set_double(stmt, i, x.get) |
|
269 |
else stmt.setNull(i, java.sql.Types.DOUBLE) |
|
270 |
} |
|
65615
67974c59ba93
tuned signature: avoid null in regular Scala code;
wenzelm
parents:
65612
diff
changeset
|
271 |
|
65020 | 272 |
def set_string(stmt: PreparedStatement, i: Int, x: String) { stmt.setString(i, x) } |
65615
67974c59ba93
tuned signature: avoid null in regular Scala code;
wenzelm
parents:
65612
diff
changeset
|
273 |
def set_string(stmt: PreparedStatement, i: Int, x: Option[String]): Unit = |
67974c59ba93
tuned signature: avoid null in regular Scala code;
wenzelm
parents:
65612
diff
changeset
|
274 |
set_string(stmt, i, x.orNull) |
67974c59ba93
tuned signature: avoid null in regular Scala code;
wenzelm
parents:
65612
diff
changeset
|
275 |
|
65020 | 276 |
def set_bytes(stmt: PreparedStatement, i: Int, bytes: Bytes) |
65610 | 277 |
{ |
278 |
if (bytes == null) stmt.setBytes(i, null) |
|
279 |
else stmt.setBinaryStream(i, bytes.stream(), bytes.length) |
|
280 |
} |
|
65615
67974c59ba93
tuned signature: avoid null in regular Scala code;
wenzelm
parents:
65612
diff
changeset
|
281 |
def set_bytes(stmt: PreparedStatement, i: Int, bytes: Option[Bytes]): Unit = |
67974c59ba93
tuned signature: avoid null in regular Scala code;
wenzelm
parents:
65612
diff
changeset
|
282 |
set_bytes(stmt, i, bytes.orNull) |
67974c59ba93
tuned signature: avoid null in regular Scala code;
wenzelm
parents:
65612
diff
changeset
|
283 |
|
67974c59ba93
tuned signature: avoid null in regular Scala code;
wenzelm
parents:
65612
diff
changeset
|
284 |
def set_date(stmt: PreparedStatement, i: Int, date: Date): Unit |
67974c59ba93
tuned signature: avoid null in regular Scala code;
wenzelm
parents:
65612
diff
changeset
|
285 |
def set_date(stmt: PreparedStatement, i: Int, date: Option[Date]): Unit = |
67974c59ba93
tuned signature: avoid null in regular Scala code;
wenzelm
parents:
65612
diff
changeset
|
286 |
set_date(stmt, i, date.orNull) |
65020 | 287 |
|
65022 | 288 |
|
65020 | 289 |
/* output */ |
65018 | 290 |
|
65620 | 291 |
def bool(rs: ResultSet, column: Column): Boolean = rs.getBoolean(column.name) |
292 |
def int(rs: ResultSet, column: Column): Int = rs.getInt(column.name) |
|
293 |
def long(rs: ResultSet, column: Column): Long = rs.getLong(column.name) |
|
294 |
def double(rs: ResultSet, column: Column): Double = rs.getDouble(column.name) |
|
295 |
def string(rs: ResultSet, column: Column): String = |
|
65018 | 296 |
{ |
65620 | 297 |
val s = rs.getString(column.name) |
65018 | 298 |
if (s == null) "" else s |
299 |
} |
|
65620 | 300 |
def bytes(rs: ResultSet, column: Column): Bytes = |
65018 | 301 |
{ |
65620 | 302 |
val bs = rs.getBytes(column.name) |
65018 | 303 |
if (bs == null) Bytes.empty else Bytes(bs) |
304 |
} |
|
65620 | 305 |
def date(rs: ResultSet, column: Column): Date |
65018 | 306 |
|
65620 | 307 |
def get[A](rs: ResultSet, column: Column, f: (ResultSet, Column) => A): Option[A] = |
65018 | 308 |
{ |
65620 | 309 |
val x = f(rs, column) |
65018 | 310 |
if (rs.wasNull) None else Some(x) |
311 |
} |
|
312 |
||
313 |
||
65669 | 314 |
/* tables and views */ |
65006 | 315 |
|
316 |
def tables: List[String] = |
|
317 |
iterator(connection.getMetaData.getTables(null, null, "%", null))(_.getString(3)).toList |
|
318 |
||
65327
e886aed88b2c
clarified signature: rowid is specific to SQLite;
wenzelm
parents:
65325
diff
changeset
|
319 |
def create_table(table: Table, strict: Boolean = false, sql: String = ""): Unit = |
e886aed88b2c
clarified signature: rowid is specific to SQLite;
wenzelm
parents:
65325
diff
changeset
|
320 |
using(statement(table.sql_create(strict, sql_type) + (if (sql == "") "" else " " + sql)))( |
e886aed88b2c
clarified signature: rowid is specific to SQLite;
wenzelm
parents:
65325
diff
changeset
|
321 |
_.execute()) |
65006 | 322 |
|
65280 | 323 |
def drop_table(table: Table, strict: Boolean = false): Unit = |
65006 | 324 |
using(statement(table.sql_drop(strict)))(_.execute()) |
325 |
||
65018 | 326 |
def create_index(table: Table, name: String, columns: List[Column], |
65280 | 327 |
strict: Boolean = false, unique: Boolean = false): Unit = |
65006 | 328 |
using(statement(table.sql_create_index(name, columns, strict, unique)))(_.execute()) |
329 |
||
65280 | 330 |
def drop_index(table: Table, name: String, strict: Boolean = false): Unit = |
65006 | 331 |
using(statement(table.sql_drop_index(name, strict)))(_.execute()) |
65669 | 332 |
|
65690 | 333 |
def create_view(view: View, strict: Boolean = false): Unit = |
334 |
if (strict || !tables.contains(view.name)) { |
|
335 |
using(statement(view.sql_create))(_.execute()) |
|
336 |
} |
|
65006 | 337 |
} |
63778 | 338 |
} |
65006 | 339 |
|
340 |
||
341 |
||
342 |
/** SQLite **/ |
|
343 |
||
344 |
object SQLite |
|
345 |
{ |
|
65021 | 346 |
// see https://www.sqlite.org/lang_datefunc.html |
347 |
val date_format: Date.Format = Date.Format("uuuu-MM-dd HH:mm:ss.SSS x") |
|
348 |
||
65292
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
349 |
lazy val init_jdbc: Unit = Class.forName("org.sqlite.JDBC") |
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
350 |
|
65006 | 351 |
def open_database(path: Path): Database = |
352 |
{ |
|
65292
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
353 |
init_jdbc |
65006 | 354 |
val path0 = path.expand |
355 |
val s0 = File.platform_path(path0) |
|
356 |
val s1 = if (Platform.is_windows) s0.replace('\\', '/') else s0 |
|
357 |
val connection = DriverManager.getConnection("jdbc:sqlite:" + s1) |
|
65007 | 358 |
new Database(path0.toString, connection) |
65006 | 359 |
} |
360 |
||
65007 | 361 |
class Database private[SQLite](name: String, val connection: Connection) extends SQL.Database |
65006 | 362 |
{ |
65007 | 363 |
override def toString: String = name |
65006 | 364 |
|
65019 | 365 |
def sql_type(T: SQL.Type.Value): String = SQL.sql_type_sqlite(T) |
65011 | 366 |
|
65021 | 367 |
def set_date(stmt: PreparedStatement, i: Int, date: Date): Unit = |
65615
67974c59ba93
tuned signature: avoid null in regular Scala code;
wenzelm
parents:
65612
diff
changeset
|
368 |
if (date == null) set_string(stmt, i, null: String) |
65598 | 369 |
else set_string(stmt, i, date_format(date)) |
370 |
||
65620 | 371 |
def date(rs: ResultSet, column: SQL.Column): Date = |
372 |
date_format.parse(string(rs, column)) |
|
65021 | 373 |
|
65006 | 374 |
def rebuild { using(statement("VACUUM"))(_.execute()) } |
375 |
} |
|
376 |
} |
|
377 |
||
378 |
||
379 |
||
380 |
/** PostgreSQL **/ |
|
381 |
||
382 |
object PostgreSQL |
|
383 |
{ |
|
384 |
val default_port = 5432 |
|
385 |
||
65292
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
386 |
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
|
387 |
|
65006 | 388 |
def open_database( |
389 |
user: String, |
|
390 |
password: String, |
|
391 |
database: String = "", |
|
392 |
host: String = "", |
|
65594 | 393 |
port: Int = 0, |
65636
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65620
diff
changeset
|
394 |
ssh: Option[SSH.Session] = None, |
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65620
diff
changeset
|
395 |
ssh_close: Boolean = false): Database = |
65006 | 396 |
{ |
65292
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
397 |
init_jdbc |
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
398 |
|
65641 | 399 |
if (user == "") error("Undefined database user") |
65009 | 400 |
|
401 |
val db_host = if (host != "") host else "localhost" |
|
65594 | 402 |
val db_port = if (port > 0 && port != default_port) ":" + port else "" |
65009 | 403 |
val db_name = "/" + (if (database != "") database else user) |
404 |
||
65010 | 405 |
val (url, name, port_forwarding) = |
65009 | 406 |
ssh match { |
65010 | 407 |
case None => |
408 |
val spec = db_host + db_port + db_name |
|
409 |
val url = "jdbc:postgresql://" + spec |
|
410 |
val name = user + "@" + spec |
|
411 |
(url, name, None) |
|
65009 | 412 |
case Some(ssh) => |
65594 | 413 |
val fw = |
414 |
ssh.port_forwarding(remote_host = db_host, |
|
65636
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65620
diff
changeset
|
415 |
remote_port = if (port > 0) port else default_port, |
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65620
diff
changeset
|
416 |
ssh_close = ssh_close) |
65010 | 417 |
val url = "jdbc:postgresql://localhost:" + fw.local_port + db_name |
418 |
val name = user + "@" + fw + db_name + " via ssh " + ssh |
|
419 |
(url, name, Some(fw)) |
|
65009 | 420 |
} |
421 |
try { |
|
65010 | 422 |
val connection = DriverManager.getConnection(url, user, password) |
423 |
new Database(name, connection, port_forwarding) |
|
65009 | 424 |
} |
425 |
catch { case exn: Throwable => port_forwarding.foreach(_.close); throw exn } |
|
65006 | 426 |
} |
427 |
||
65009 | 428 |
class Database private[PostgreSQL]( |
429 |
name: String, val connection: Connection, port_forwarding: Option[SSH.Port_Forwarding]) |
|
430 |
extends SQL.Database |
|
65006 | 431 |
{ |
65010 | 432 |
override def toString: String = name |
65008 | 433 |
|
65019 | 434 |
def sql_type(T: SQL.Type.Value): String = SQL.sql_type_postgresql(T) |
65009 | 435 |
|
65021 | 436 |
// see https://jdbc.postgresql.org/documentation/head/8-date-time.html |
437 |
def set_date(stmt: PreparedStatement, i: Int, date: Date): Unit = |
|
65598 | 438 |
if (date == null) stmt.setObject(i, null) |
439 |
else stmt.setObject(i, OffsetDateTime.from(date.to_utc.rep)) |
|
440 |
||
65620 | 441 |
def date(rs: ResultSet, column: SQL.Column): Date = |
442 |
Date.instant(rs.getObject(column.name, classOf[OffsetDateTime]).toInstant) |
|
65021 | 443 |
|
65009 | 444 |
override def close() { super.close; port_forwarding.foreach(_.close) } |
65006 | 445 |
} |
446 |
} |