author | wenzelm |
Wed, 03 May 2017 17:00:50 +0200 | |
changeset 65702 | 7c6a91deb212 |
parent 65701 | d788c11176e5 |
child 65703 | cead65c19f2e |
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 |
|
65695 | 36 |
def ident(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 = |
65695 | 43 |
"SELECT " + (if (distinct) "DISTINCT " 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
|
44 |
|
65668 | 45 |
def join(table1: Table, table2: Table, sql: String = "", outer: Boolean = false): String = |
65695 | 46 |
table1.ident + (if (outer) " LEFT OUTER JOIN " else " INNER JOIN ") + table2.ident + |
65668 | 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 |
||
80 |
object Column |
|
81 |
{ |
|
65280 | 82 |
def bool(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 83 |
Column(name, Type.Boolean, strict, primary_key) |
65280 | 84 |
def int(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 85 |
Column(name, Type.Int, strict, primary_key) |
65280 | 86 |
def long(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 87 |
Column(name, Type.Long, strict, primary_key) |
65280 | 88 |
def double(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 89 |
Column(name, Type.Double, strict, primary_key) |
65280 | 90 |
def string(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 91 |
Column(name, Type.String, strict, primary_key) |
65280 | 92 |
def bytes(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 93 |
Column(name, Type.Bytes, strict, primary_key) |
65280 | 94 |
def date(name: String, strict: Boolean = false, primary_key: Boolean = false): Column = |
65018 | 95 |
Column(name, Type.Date, strict, primary_key) |
63779 | 96 |
} |
97 |
||
65018 | 98 |
sealed case class Column( |
65280 | 99 |
name: String, T: Type.Value, strict: Boolean = false, primary_key: Boolean = false) |
63779 | 100 |
{ |
65691 | 101 |
def apply(table: Table): Column = |
102 |
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
|
103 |
|
65695 | 104 |
def ident: String = SQL.ident(name) |
105 |
||
65701 | 106 |
def decl(sql_type: Type.Value => String): String = |
65695 | 107 |
ident + " " + sql_type(T) + (if (strict || primary_key) " NOT NULL" else "") |
63781 | 108 |
|
65699 | 109 |
def where_equal(s: String): String = "WHERE " + ident + " = " + string(s) |
65593 | 110 |
|
65701 | 111 |
override def toString: String = ident |
63779 | 112 |
} |
113 |
||
63780 | 114 |
|
115 |
/* tables */ |
|
116 |
||
65696 | 117 |
sealed case class Table(name: String, columns: List[Column], body: String = "") |
63780 | 118 |
{ |
63790 | 119 |
private val columns_index: Map[String, Int] = |
120 |
columns.iterator.map(_.name).zipWithIndex.toMap |
|
121 |
||
63781 | 122 |
Library.duplicates(columns.map(_.name)) match { |
123 |
case Nil => |
|
124 |
case bad => error("Duplicate column names " + commas_quote(bad) + " for table " + quote(name)) |
|
125 |
} |
|
126 |
||
65696 | 127 |
def ident: String = SQL.ident(name) |
65691 | 128 |
|
65702 | 129 |
def query: String = |
65696 | 130 |
if (body == "") error("Missing SQL body for table " + quote(name)) |
131 |
else SQL.enclose(body) |
|
65649 | 132 |
|
65702 | 133 |
def query_alias(alias: String = name): String = |
134 |
query + " AS " + SQL.ident(alias) |
|
135 |
||
65701 | 136 |
def create(strict: Boolean = false, sql_type: Type.Value => String): String = |
65325 | 137 |
{ |
138 |
val primary_key = |
|
139 |
columns.filter(_.primary_key).map(_.name) match { |
|
140 |
case Nil => Nil |
|
141 |
case keys => List("PRIMARY KEY " + enclosure(keys)) |
|
142 |
} |
|
65701 | 143 |
"CREATE TABLE " + (if (strict) "" else "IF NOT EXISTS ") + |
144 |
ident + " " + enclosure(columns.map(_.decl(sql_type)) ::: primary_key) |
|
63781 | 145 |
} |
146 |
||
65698 | 147 |
def create_index(index_name: String, index_columns: List[Column], |
148 |
strict: Boolean = false, unique: Boolean = false): String = |
|
63791 | 149 |
"CREATE " + (if (unique) "UNIQUE " else "") + "INDEX " + |
65695 | 150 |
(if (strict) "" else "IF NOT EXISTS ") + SQL.ident(index_name) + " ON " + |
151 |
ident + " " + enclosure(index_columns.map(_.name)) |
|
63791 | 152 |
|
65698 | 153 |
def insert(sql: String = ""): String = |
154 |
"INSERT INTO " + ident + " VALUES " + enclosure(columns.map(_ => "?")) + |
|
155 |
(if (sql == "") "" else " " + sql) |
|
63791 | 156 |
|
65698 | 157 |
def delete(sql: String = ""): String = |
158 |
"DELETE FROM " + ident + |
|
159 |
(if (sql == "") "" else " " + sql) |
|
65319 | 160 |
|
65698 | 161 |
def select(select_columns: List[Column], sql: String = "", distinct: Boolean = false): String = |
162 |
SQL.select(select_columns, distinct = distinct) + ident + |
|
163 |
(if (sql == "") "" else " " + sql) |
|
63790 | 164 |
|
65701 | 165 |
override def toString: String = ident |
63780 | 166 |
} |
63790 | 167 |
|
168 |
||
65012 | 169 |
|
170 |
/** SQL database operations **/ |
|
171 |
||
63790 | 172 |
/* results */ |
173 |
||
174 |
def iterator[A](rs: ResultSet)(get: ResultSet => A): Iterator[A] = new Iterator[A] |
|
175 |
{ |
|
176 |
private var _next: Boolean = rs.next() |
|
177 |
def hasNext: Boolean = _next |
|
178 |
def next: A = { val x = get(rs); _next = rs.next(); x } |
|
179 |
} |
|
65006 | 180 |
|
181 |
trait Database |
|
182 |
{ |
|
65008 | 183 |
/* types */ |
184 |
||
65019 | 185 |
def sql_type(T: Type.Value): String |
65008 | 186 |
|
187 |
||
65006 | 188 |
/* connection */ |
189 |
||
190 |
def connection: Connection |
|
191 |
||
192 |
def close() { connection.close } |
|
193 |
||
194 |
def transaction[A](body: => A): A = |
|
195 |
{ |
|
196 |
val auto_commit = connection.getAutoCommit |
|
197 |
try { |
|
198 |
connection.setAutoCommit(false) |
|
65022 | 199 |
val savepoint = connection.setSavepoint |
200 |
try { |
|
201 |
val result = body |
|
202 |
connection.commit |
|
203 |
result |
|
204 |
} |
|
205 |
catch { case exn: Throwable => connection.rollback(savepoint); throw exn } |
|
65006 | 206 |
} |
207 |
finally { connection.setAutoCommit(auto_commit) } |
|
208 |
} |
|
209 |
||
210 |
||
211 |
/* statements */ |
|
212 |
||
65698 | 213 |
def statement(sql: String): PreparedStatement = |
214 |
connection.prepareStatement(sql) |
|
65006 | 215 |
|
65698 | 216 |
def using_statement[A](sql: String)(f: PreparedStatement => A): A = |
217 |
using(statement(sql))(f) |
|
65006 | 218 |
|
219 |
||
65020 | 220 |
/* input */ |
221 |
||
222 |
def set_bool(stmt: PreparedStatement, i: Int, x: Boolean) { stmt.setBoolean(i, x) } |
|
65612 | 223 |
def set_bool(stmt: PreparedStatement, i: Int, x: Option[Boolean]) |
224 |
{ |
|
225 |
if (x.isDefined) set_bool(stmt, i, x.get) |
|
226 |
else stmt.setNull(i, java.sql.Types.BOOLEAN) |
|
227 |
} |
|
65615
67974c59ba93
tuned signature: avoid null in regular Scala code;
wenzelm
parents:
65612
diff
changeset
|
228 |
|
65020 | 229 |
def set_int(stmt: PreparedStatement, i: Int, x: Int) { stmt.setInt(i, x) } |
65612 | 230 |
def set_int(stmt: PreparedStatement, i: Int, x: Option[Int]) |
231 |
{ |
|
232 |
if (x.isDefined) set_int(stmt, i, x.get) |
|
233 |
else stmt.setNull(i, java.sql.Types.INTEGER) |
|
234 |
} |
|
65615
67974c59ba93
tuned signature: avoid null in regular Scala code;
wenzelm
parents:
65612
diff
changeset
|
235 |
|
65020 | 236 |
def set_long(stmt: PreparedStatement, i: Int, x: Long) { stmt.setLong(i, x) } |
65612 | 237 |
def set_long(stmt: PreparedStatement, i: Int, x: Option[Long]) |
238 |
{ |
|
239 |
if (x.isDefined) set_long(stmt, i, x.get) |
|
240 |
else stmt.setNull(i, java.sql.Types.BIGINT) |
|
241 |
} |
|
65615
67974c59ba93
tuned signature: avoid null in regular Scala code;
wenzelm
parents:
65612
diff
changeset
|
242 |
|
65020 | 243 |
def set_double(stmt: PreparedStatement, i: Int, x: Double) { stmt.setDouble(i, x) } |
65612 | 244 |
def set_double(stmt: PreparedStatement, i: Int, x: Option[Double]) |
245 |
{ |
|
246 |
if (x.isDefined) set_double(stmt, i, x.get) |
|
247 |
else stmt.setNull(i, java.sql.Types.DOUBLE) |
|
248 |
} |
|
65615
67974c59ba93
tuned signature: avoid null in regular Scala code;
wenzelm
parents:
65612
diff
changeset
|
249 |
|
65020 | 250 |
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
|
251 |
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
|
252 |
set_string(stmt, i, x.orNull) |
67974c59ba93
tuned signature: avoid null in regular Scala code;
wenzelm
parents:
65612
diff
changeset
|
253 |
|
65020 | 254 |
def set_bytes(stmt: PreparedStatement, i: Int, bytes: Bytes) |
65610 | 255 |
{ |
256 |
if (bytes == null) stmt.setBytes(i, null) |
|
257 |
else stmt.setBinaryStream(i, bytes.stream(), bytes.length) |
|
258 |
} |
|
65615
67974c59ba93
tuned signature: avoid null in regular Scala code;
wenzelm
parents:
65612
diff
changeset
|
259 |
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
|
260 |
set_bytes(stmt, i, bytes.orNull) |
67974c59ba93
tuned signature: avoid null in regular Scala code;
wenzelm
parents:
65612
diff
changeset
|
261 |
|
67974c59ba93
tuned signature: avoid null in regular Scala code;
wenzelm
parents:
65612
diff
changeset
|
262 |
def set_date(stmt: PreparedStatement, i: Int, date: Date): Unit |
67974c59ba93
tuned signature: avoid null in regular Scala code;
wenzelm
parents:
65612
diff
changeset
|
263 |
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
|
264 |
set_date(stmt, i, date.orNull) |
65020 | 265 |
|
65022 | 266 |
|
65020 | 267 |
/* output */ |
65018 | 268 |
|
65620 | 269 |
def bool(rs: ResultSet, column: Column): Boolean = rs.getBoolean(column.name) |
270 |
def int(rs: ResultSet, column: Column): Int = rs.getInt(column.name) |
|
271 |
def long(rs: ResultSet, column: Column): Long = rs.getLong(column.name) |
|
272 |
def double(rs: ResultSet, column: Column): Double = rs.getDouble(column.name) |
|
273 |
def string(rs: ResultSet, column: Column): String = |
|
65018 | 274 |
{ |
65620 | 275 |
val s = rs.getString(column.name) |
65018 | 276 |
if (s == null) "" else s |
277 |
} |
|
65620 | 278 |
def bytes(rs: ResultSet, column: Column): Bytes = |
65018 | 279 |
{ |
65620 | 280 |
val bs = rs.getBytes(column.name) |
65018 | 281 |
if (bs == null) Bytes.empty else Bytes(bs) |
282 |
} |
|
65620 | 283 |
def date(rs: ResultSet, column: Column): Date |
65018 | 284 |
|
65620 | 285 |
def get[A](rs: ResultSet, column: Column, f: (ResultSet, Column) => A): Option[A] = |
65018 | 286 |
{ |
65620 | 287 |
val x = f(rs, column) |
65018 | 288 |
if (rs.wasNull) None else Some(x) |
289 |
} |
|
65697 | 290 |
def get_bool(rs: ResultSet, column: Column): Option[Boolean] = get(rs, column, bool _) |
291 |
def get_int(rs: ResultSet, column: Column): Option[Int] = get(rs, column, int _) |
|
292 |
def get_long(rs: ResultSet, column: Column): Option[Long] = get(rs, column, long _) |
|
293 |
def get_double(rs: ResultSet, column: Column): Option[Double] = get(rs, column, double _) |
|
294 |
def get_string(rs: ResultSet, column: Column): Option[String] = get(rs, column, string _) |
|
295 |
def get_bytes(rs: ResultSet, column: Column): Option[Bytes] = get(rs, column, bytes _) |
|
296 |
def get_date(rs: ResultSet, column: Column): Option[Date] = get(rs, column, date _) |
|
65018 | 297 |
|
298 |
||
65669 | 299 |
/* tables and views */ |
65006 | 300 |
|
301 |
def tables: List[String] = |
|
302 |
iterator(connection.getMetaData.getTables(null, null, "%", null))(_.getString(3)).toList |
|
303 |
||
65327
e886aed88b2c
clarified signature: rowid is specific to SQLite;
wenzelm
parents:
65325
diff
changeset
|
304 |
def create_table(table: Table, strict: Boolean = false, sql: String = ""): Unit = |
65698 | 305 |
using_statement( |
306 |
table.create(strict, sql_type) + (if (sql == "") "" else " " + sql))(_.execute()) |
|
65006 | 307 |
|
65018 | 308 |
def create_index(table: Table, name: String, columns: List[Column], |
65280 | 309 |
strict: Boolean = false, unique: Boolean = false): Unit = |
65698 | 310 |
using_statement(table.create_index(name, columns, strict, unique))(_.execute()) |
65006 | 311 |
|
65691 | 312 |
def create_view(table: Table, strict: Boolean = false): Unit = |
313 |
{ |
|
314 |
if (strict || !tables.contains(table.name)) { |
|
65702 | 315 |
val sql = "CREATE VIEW " + table.ident + " AS " + table.query |
65698 | 316 |
using_statement(sql)(_.execute()) |
65690 | 317 |
} |
65691 | 318 |
} |
65006 | 319 |
} |
63778 | 320 |
} |
65006 | 321 |
|
322 |
||
323 |
||
324 |
/** SQLite **/ |
|
325 |
||
326 |
object SQLite |
|
327 |
{ |
|
65021 | 328 |
// see https://www.sqlite.org/lang_datefunc.html |
329 |
val date_format: Date.Format = Date.Format("uuuu-MM-dd HH:mm:ss.SSS x") |
|
330 |
||
65292
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
331 |
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
|
332 |
|
65006 | 333 |
def open_database(path: Path): Database = |
334 |
{ |
|
65292
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
335 |
init_jdbc |
65006 | 336 |
val path0 = path.expand |
337 |
val s0 = File.platform_path(path0) |
|
338 |
val s1 = if (Platform.is_windows) s0.replace('\\', '/') else s0 |
|
339 |
val connection = DriverManager.getConnection("jdbc:sqlite:" + s1) |
|
65007 | 340 |
new Database(path0.toString, connection) |
65006 | 341 |
} |
342 |
||
65007 | 343 |
class Database private[SQLite](name: String, val connection: Connection) extends SQL.Database |
65006 | 344 |
{ |
65007 | 345 |
override def toString: String = name |
65006 | 346 |
|
65019 | 347 |
def sql_type(T: SQL.Type.Value): String = SQL.sql_type_sqlite(T) |
65011 | 348 |
|
65021 | 349 |
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
|
350 |
if (date == null) set_string(stmt, i, null: String) |
65598 | 351 |
else set_string(stmt, i, date_format(date)) |
352 |
||
65620 | 353 |
def date(rs: ResultSet, column: SQL.Column): Date = |
354 |
date_format.parse(string(rs, column)) |
|
65021 | 355 |
|
65698 | 356 |
def rebuild { using_statement("VACUUM")(_.execute()) } |
65006 | 357 |
} |
358 |
} |
|
359 |
||
360 |
||
361 |
||
362 |
/** PostgreSQL **/ |
|
363 |
||
364 |
object PostgreSQL |
|
365 |
{ |
|
366 |
val default_port = 5432 |
|
367 |
||
65292
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
368 |
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
|
369 |
|
65006 | 370 |
def open_database( |
371 |
user: String, |
|
372 |
password: String, |
|
373 |
database: String = "", |
|
374 |
host: String = "", |
|
65594 | 375 |
port: Int = 0, |
65636
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65620
diff
changeset
|
376 |
ssh: Option[SSH.Session] = None, |
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65620
diff
changeset
|
377 |
ssh_close: Boolean = false): Database = |
65006 | 378 |
{ |
65292
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
379 |
init_jdbc |
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
380 |
|
65641 | 381 |
if (user == "") error("Undefined database user") |
65009 | 382 |
|
383 |
val db_host = if (host != "") host else "localhost" |
|
65594 | 384 |
val db_port = if (port > 0 && port != default_port) ":" + port else "" |
65009 | 385 |
val db_name = "/" + (if (database != "") database else user) |
386 |
||
65010 | 387 |
val (url, name, port_forwarding) = |
65009 | 388 |
ssh match { |
65010 | 389 |
case None => |
390 |
val spec = db_host + db_port + db_name |
|
391 |
val url = "jdbc:postgresql://" + spec |
|
392 |
val name = user + "@" + spec |
|
393 |
(url, name, None) |
|
65009 | 394 |
case Some(ssh) => |
65594 | 395 |
val fw = |
396 |
ssh.port_forwarding(remote_host = db_host, |
|
65636
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65620
diff
changeset
|
397 |
remote_port = if (port > 0) port else default_port, |
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65620
diff
changeset
|
398 |
ssh_close = ssh_close) |
65010 | 399 |
val url = "jdbc:postgresql://localhost:" + fw.local_port + db_name |
400 |
val name = user + "@" + fw + db_name + " via ssh " + ssh |
|
401 |
(url, name, Some(fw)) |
|
65009 | 402 |
} |
403 |
try { |
|
65010 | 404 |
val connection = DriverManager.getConnection(url, user, password) |
405 |
new Database(name, connection, port_forwarding) |
|
65009 | 406 |
} |
407 |
catch { case exn: Throwable => port_forwarding.foreach(_.close); throw exn } |
|
65006 | 408 |
} |
409 |
||
65009 | 410 |
class Database private[PostgreSQL]( |
411 |
name: String, val connection: Connection, port_forwarding: Option[SSH.Port_Forwarding]) |
|
412 |
extends SQL.Database |
|
65006 | 413 |
{ |
65010 | 414 |
override def toString: String = name |
65008 | 415 |
|
65019 | 416 |
def sql_type(T: SQL.Type.Value): String = SQL.sql_type_postgresql(T) |
65009 | 417 |
|
65021 | 418 |
// see https://jdbc.postgresql.org/documentation/head/8-date-time.html |
419 |
def set_date(stmt: PreparedStatement, i: Int, date: Date): Unit = |
|
65598 | 420 |
if (date == null) stmt.setObject(i, null) |
421 |
else stmt.setObject(i, OffsetDateTime.from(date.to_utc.rep)) |
|
422 |
||
65620 | 423 |
def date(rs: ResultSet, column: SQL.Column): Date = |
424 |
Date.instant(rs.getObject(column.name, classOf[OffsetDateTime]).toInstant) |
|
65021 | 425 |
|
65009 | 426 |
override def close() { super.close; port_forwarding.foreach(_.close) } |
65006 | 427 |
} |
428 |
} |