author | wenzelm |
Thu, 04 May 2017 14:58:19 +0200 | |
changeset 65717 | 556c34fd0554 |
parent 65706 | 595bc96005f9 |
child 65729 | 3f40afe30feb |
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 |
|
65703
cead65c19f2e
more direct insert_permissive statement, which avoids somewhat fragile nested transactions;
wenzelm
parents:
65702
diff
changeset
|
153 |
def insert_cmd(cmd: String, sql: String = ""): String = |
cead65c19f2e
more direct insert_permissive statement, which avoids somewhat fragile nested transactions;
wenzelm
parents:
65702
diff
changeset
|
154 |
cmd + " INTO " + ident + " VALUES " + enclosure(columns.map(_ => "?")) + |
65698 | 155 |
(if (sql == "") "" else " " + sql) |
63791 | 156 |
|
65703
cead65c19f2e
more direct insert_permissive statement, which avoids somewhat fragile nested transactions;
wenzelm
parents:
65702
diff
changeset
|
157 |
def insert(sql: String = ""): String = insert_cmd("INSERT", sql) |
cead65c19f2e
more direct insert_permissive statement, which avoids somewhat fragile nested transactions;
wenzelm
parents:
65702
diff
changeset
|
158 |
|
65698 | 159 |
def delete(sql: String = ""): String = |
160 |
"DELETE FROM " + ident + |
|
161 |
(if (sql == "") "" else " " + sql) |
|
65319 | 162 |
|
65698 | 163 |
def select(select_columns: List[Column], sql: String = "", distinct: Boolean = false): String = |
164 |
SQL.select(select_columns, distinct = distinct) + ident + |
|
165 |
(if (sql == "") "" else " " + sql) |
|
63790 | 166 |
|
65701 | 167 |
override def toString: String = ident |
63780 | 168 |
} |
63790 | 169 |
|
170 |
||
65012 | 171 |
|
172 |
/** SQL database operations **/ |
|
173 |
||
63790 | 174 |
/* results */ |
175 |
||
176 |
def iterator[A](rs: ResultSet)(get: ResultSet => A): Iterator[A] = new Iterator[A] |
|
177 |
{ |
|
178 |
private var _next: Boolean = rs.next() |
|
179 |
def hasNext: Boolean = _next |
|
180 |
def next: A = { val x = get(rs); _next = rs.next(); x } |
|
181 |
} |
|
65006 | 182 |
|
183 |
trait Database |
|
184 |
{ |
|
65008 | 185 |
/* types */ |
186 |
||
65019 | 187 |
def sql_type(T: Type.Value): String |
65008 | 188 |
|
189 |
||
65006 | 190 |
/* connection */ |
191 |
||
192 |
def connection: Connection |
|
193 |
||
194 |
def close() { connection.close } |
|
195 |
||
196 |
def transaction[A](body: => A): A = |
|
197 |
{ |
|
198 |
val auto_commit = connection.getAutoCommit |
|
199 |
try { |
|
200 |
connection.setAutoCommit(false) |
|
65022 | 201 |
val savepoint = connection.setSavepoint |
202 |
try { |
|
203 |
val result = body |
|
204 |
connection.commit |
|
205 |
result |
|
206 |
} |
|
207 |
catch { case exn: Throwable => connection.rollback(savepoint); throw exn } |
|
65006 | 208 |
} |
209 |
finally { connection.setAutoCommit(auto_commit) } |
|
210 |
} |
|
211 |
||
212 |
||
213 |
/* statements */ |
|
214 |
||
65698 | 215 |
def statement(sql: String): PreparedStatement = |
216 |
connection.prepareStatement(sql) |
|
65006 | 217 |
|
65698 | 218 |
def using_statement[A](sql: String)(f: PreparedStatement => A): A = |
219 |
using(statement(sql))(f) |
|
65006 | 220 |
|
65703
cead65c19f2e
more direct insert_permissive statement, which avoids somewhat fragile nested transactions;
wenzelm
parents:
65702
diff
changeset
|
221 |
def insert_permissive(table: Table, sql: String = ""): String |
cead65c19f2e
more direct insert_permissive statement, which avoids somewhat fragile nested transactions;
wenzelm
parents:
65702
diff
changeset
|
222 |
|
65006 | 223 |
|
65020 | 224 |
/* input */ |
225 |
||
226 |
def set_bool(stmt: PreparedStatement, i: Int, x: Boolean) { stmt.setBoolean(i, x) } |
|
65612 | 227 |
def set_bool(stmt: PreparedStatement, i: Int, x: Option[Boolean]) |
228 |
{ |
|
229 |
if (x.isDefined) set_bool(stmt, i, x.get) |
|
230 |
else stmt.setNull(i, java.sql.Types.BOOLEAN) |
|
231 |
} |
|
65615
67974c59ba93
tuned signature: avoid null in regular Scala code;
wenzelm
parents:
65612
diff
changeset
|
232 |
|
65020 | 233 |
def set_int(stmt: PreparedStatement, i: Int, x: Int) { stmt.setInt(i, x) } |
65612 | 234 |
def set_int(stmt: PreparedStatement, i: Int, x: Option[Int]) |
235 |
{ |
|
236 |
if (x.isDefined) set_int(stmt, i, x.get) |
|
237 |
else stmt.setNull(i, java.sql.Types.INTEGER) |
|
238 |
} |
|
65615
67974c59ba93
tuned signature: avoid null in regular Scala code;
wenzelm
parents:
65612
diff
changeset
|
239 |
|
65020 | 240 |
def set_long(stmt: PreparedStatement, i: Int, x: Long) { stmt.setLong(i, x) } |
65612 | 241 |
def set_long(stmt: PreparedStatement, i: Int, x: Option[Long]) |
242 |
{ |
|
243 |
if (x.isDefined) set_long(stmt, i, x.get) |
|
244 |
else stmt.setNull(i, java.sql.Types.BIGINT) |
|
245 |
} |
|
65615
67974c59ba93
tuned signature: avoid null in regular Scala code;
wenzelm
parents:
65612
diff
changeset
|
246 |
|
65020 | 247 |
def set_double(stmt: PreparedStatement, i: Int, x: Double) { stmt.setDouble(i, x) } |
65612 | 248 |
def set_double(stmt: PreparedStatement, i: Int, x: Option[Double]) |
249 |
{ |
|
250 |
if (x.isDefined) set_double(stmt, i, x.get) |
|
251 |
else stmt.setNull(i, java.sql.Types.DOUBLE) |
|
252 |
} |
|
65615
67974c59ba93
tuned signature: avoid null in regular Scala code;
wenzelm
parents:
65612
diff
changeset
|
253 |
|
65020 | 254 |
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
|
255 |
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
|
256 |
set_string(stmt, i, x.orNull) |
67974c59ba93
tuned signature: avoid null in regular Scala code;
wenzelm
parents:
65612
diff
changeset
|
257 |
|
65020 | 258 |
def set_bytes(stmt: PreparedStatement, i: Int, bytes: Bytes) |
65610 | 259 |
{ |
260 |
if (bytes == null) stmt.setBytes(i, null) |
|
261 |
else stmt.setBinaryStream(i, bytes.stream(), bytes.length) |
|
262 |
} |
|
65615
67974c59ba93
tuned signature: avoid null in regular Scala code;
wenzelm
parents:
65612
diff
changeset
|
263 |
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
|
264 |
set_bytes(stmt, i, bytes.orNull) |
67974c59ba93
tuned signature: avoid null in regular Scala code;
wenzelm
parents:
65612
diff
changeset
|
265 |
|
67974c59ba93
tuned signature: avoid null in regular Scala code;
wenzelm
parents:
65612
diff
changeset
|
266 |
def set_date(stmt: PreparedStatement, i: Int, date: Date): Unit |
67974c59ba93
tuned signature: avoid null in regular Scala code;
wenzelm
parents:
65612
diff
changeset
|
267 |
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
|
268 |
set_date(stmt, i, date.orNull) |
65020 | 269 |
|
65022 | 270 |
|
65020 | 271 |
/* output */ |
65018 | 272 |
|
65620 | 273 |
def bool(rs: ResultSet, column: Column): Boolean = rs.getBoolean(column.name) |
274 |
def int(rs: ResultSet, column: Column): Int = rs.getInt(column.name) |
|
275 |
def long(rs: ResultSet, column: Column): Long = rs.getLong(column.name) |
|
276 |
def double(rs: ResultSet, column: Column): Double = rs.getDouble(column.name) |
|
277 |
def string(rs: ResultSet, column: Column): String = |
|
65018 | 278 |
{ |
65620 | 279 |
val s = rs.getString(column.name) |
65018 | 280 |
if (s == null) "" else s |
281 |
} |
|
65620 | 282 |
def bytes(rs: ResultSet, column: Column): Bytes = |
65018 | 283 |
{ |
65620 | 284 |
val bs = rs.getBytes(column.name) |
65018 | 285 |
if (bs == null) Bytes.empty else Bytes(bs) |
286 |
} |
|
65620 | 287 |
def date(rs: ResultSet, column: Column): Date |
65018 | 288 |
|
65620 | 289 |
def get[A](rs: ResultSet, column: Column, f: (ResultSet, Column) => A): Option[A] = |
65018 | 290 |
{ |
65620 | 291 |
val x = f(rs, column) |
65018 | 292 |
if (rs.wasNull) None else Some(x) |
293 |
} |
|
65697 | 294 |
def get_bool(rs: ResultSet, column: Column): Option[Boolean] = get(rs, column, bool _) |
295 |
def get_int(rs: ResultSet, column: Column): Option[Int] = get(rs, column, int _) |
|
296 |
def get_long(rs: ResultSet, column: Column): Option[Long] = get(rs, column, long _) |
|
297 |
def get_double(rs: ResultSet, column: Column): Option[Double] = get(rs, column, double _) |
|
298 |
def get_string(rs: ResultSet, column: Column): Option[String] = get(rs, column, string _) |
|
299 |
def get_bytes(rs: ResultSet, column: Column): Option[Bytes] = get(rs, column, bytes _) |
|
300 |
def get_date(rs: ResultSet, column: Column): Option[Date] = get(rs, column, date _) |
|
65018 | 301 |
|
302 |
||
65669 | 303 |
/* tables and views */ |
65006 | 304 |
|
305 |
def tables: List[String] = |
|
306 |
iterator(connection.getMetaData.getTables(null, null, "%", null))(_.getString(3)).toList |
|
307 |
||
65327
e886aed88b2c
clarified signature: rowid is specific to SQLite;
wenzelm
parents:
65325
diff
changeset
|
308 |
def create_table(table: Table, strict: Boolean = false, sql: String = ""): Unit = |
65698 | 309 |
using_statement( |
310 |
table.create(strict, sql_type) + (if (sql == "") "" else " " + sql))(_.execute()) |
|
65006 | 311 |
|
65018 | 312 |
def create_index(table: Table, name: String, columns: List[Column], |
65280 | 313 |
strict: Boolean = false, unique: Boolean = false): Unit = |
65698 | 314 |
using_statement(table.create_index(name, columns, strict, unique))(_.execute()) |
65006 | 315 |
|
65691 | 316 |
def create_view(table: Table, strict: Boolean = false): Unit = |
317 |
{ |
|
318 |
if (strict || !tables.contains(table.name)) { |
|
65706 | 319 |
val sql = "CREATE VIEW " + table.ident + " AS " + { table.query; table.body } |
65698 | 320 |
using_statement(sql)(_.execute()) |
65690 | 321 |
} |
65691 | 322 |
} |
65006 | 323 |
} |
63778 | 324 |
} |
65006 | 325 |
|
326 |
||
327 |
||
328 |
/** SQLite **/ |
|
329 |
||
330 |
object SQLite |
|
331 |
{ |
|
65021 | 332 |
// see https://www.sqlite.org/lang_datefunc.html |
333 |
val date_format: Date.Format = Date.Format("uuuu-MM-dd HH:mm:ss.SSS x") |
|
334 |
||
65292
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
335 |
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
|
336 |
|
65006 | 337 |
def open_database(path: Path): Database = |
338 |
{ |
|
65292
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
339 |
init_jdbc |
65006 | 340 |
val path0 = path.expand |
341 |
val s0 = File.platform_path(path0) |
|
342 |
val s1 = if (Platform.is_windows) s0.replace('\\', '/') else s0 |
|
343 |
val connection = DriverManager.getConnection("jdbc:sqlite:" + s1) |
|
65007 | 344 |
new Database(path0.toString, connection) |
65006 | 345 |
} |
346 |
||
65007 | 347 |
class Database private[SQLite](name: String, val connection: Connection) extends SQL.Database |
65006 | 348 |
{ |
65007 | 349 |
override def toString: String = name |
65006 | 350 |
|
65019 | 351 |
def sql_type(T: SQL.Type.Value): String = SQL.sql_type_sqlite(T) |
65011 | 352 |
|
65021 | 353 |
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
|
354 |
if (date == null) set_string(stmt, i, null: String) |
65598 | 355 |
else set_string(stmt, i, date_format(date)) |
356 |
||
65620 | 357 |
def date(rs: ResultSet, column: SQL.Column): Date = |
358 |
date_format.parse(string(rs, column)) |
|
65021 | 359 |
|
65703
cead65c19f2e
more direct insert_permissive statement, which avoids somewhat fragile nested transactions;
wenzelm
parents:
65702
diff
changeset
|
360 |
def insert_permissive(table: SQL.Table, sql: String = ""): String = |
cead65c19f2e
more direct insert_permissive statement, which avoids somewhat fragile nested transactions;
wenzelm
parents:
65702
diff
changeset
|
361 |
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
|
362 |
|
65698 | 363 |
def rebuild { using_statement("VACUUM")(_.execute()) } |
65006 | 364 |
} |
365 |
} |
|
366 |
||
367 |
||
368 |
||
369 |
/** PostgreSQL **/ |
|
370 |
||
371 |
object PostgreSQL |
|
372 |
{ |
|
373 |
val default_port = 5432 |
|
374 |
||
65292
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
375 |
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
|
376 |
|
65006 | 377 |
def open_database( |
378 |
user: String, |
|
379 |
password: String, |
|
380 |
database: String = "", |
|
381 |
host: String = "", |
|
65594 | 382 |
port: Int = 0, |
65636
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65620
diff
changeset
|
383 |
ssh: Option[SSH.Session] = None, |
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65620
diff
changeset
|
384 |
ssh_close: Boolean = false): Database = |
65006 | 385 |
{ |
65292
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
386 |
init_jdbc |
e3bd1e7ddd23
more robust JDBC initialization, e.g. required for Isabelle/jEdit startup;
wenzelm
parents:
65280
diff
changeset
|
387 |
|
65641 | 388 |
if (user == "") error("Undefined database user") |
65009 | 389 |
|
65717 | 390 |
val db_host = proper_string(host) getOrElse "localhost" |
65594 | 391 |
val db_port = if (port > 0 && port != default_port) ":" + port else "" |
65717 | 392 |
val db_name = "/" + (proper_string(database) getOrElse user) |
65009 | 393 |
|
65010 | 394 |
val (url, name, port_forwarding) = |
65009 | 395 |
ssh match { |
65010 | 396 |
case None => |
397 |
val spec = db_host + db_port + db_name |
|
398 |
val url = "jdbc:postgresql://" + spec |
|
399 |
val name = user + "@" + spec |
|
400 |
(url, name, None) |
|
65009 | 401 |
case Some(ssh) => |
65594 | 402 |
val fw = |
403 |
ssh.port_forwarding(remote_host = db_host, |
|
65636
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65620
diff
changeset
|
404 |
remote_port = if (port > 0) port else default_port, |
df804cdba5f9
ssh_close for proper termination after use of database;
wenzelm
parents:
65620
diff
changeset
|
405 |
ssh_close = ssh_close) |
65010 | 406 |
val url = "jdbc:postgresql://localhost:" + fw.local_port + db_name |
407 |
val name = user + "@" + fw + db_name + " via ssh " + ssh |
|
408 |
(url, name, Some(fw)) |
|
65009 | 409 |
} |
410 |
try { |
|
65010 | 411 |
val connection = DriverManager.getConnection(url, user, password) |
412 |
new Database(name, connection, port_forwarding) |
|
65009 | 413 |
} |
414 |
catch { case exn: Throwable => port_forwarding.foreach(_.close); throw exn } |
|
65006 | 415 |
} |
416 |
||
65009 | 417 |
class Database private[PostgreSQL]( |
418 |
name: String, val connection: Connection, port_forwarding: Option[SSH.Port_Forwarding]) |
|
419 |
extends SQL.Database |
|
65006 | 420 |
{ |
65010 | 421 |
override def toString: String = name |
65008 | 422 |
|
65019 | 423 |
def sql_type(T: SQL.Type.Value): String = SQL.sql_type_postgresql(T) |
65009 | 424 |
|
65021 | 425 |
// see https://jdbc.postgresql.org/documentation/head/8-date-time.html |
426 |
def set_date(stmt: PreparedStatement, i: Int, date: Date): Unit = |
|
65598 | 427 |
if (date == null) stmt.setObject(i, null) |
428 |
else stmt.setObject(i, OffsetDateTime.from(date.to_utc.rep)) |
|
429 |
||
65620 | 430 |
def date(rs: ResultSet, column: SQL.Column): Date = |
65704 | 431 |
{ |
432 |
val obj = rs.getObject(column.name, classOf[OffsetDateTime]) |
|
433 |
if (obj == null) null else Date.instant(obj.toInstant) |
|
434 |
} |
|
65021 | 435 |
|
65703
cead65c19f2e
more direct insert_permissive statement, which avoids somewhat fragile nested transactions;
wenzelm
parents:
65702
diff
changeset
|
436 |
def insert_permissive(table: SQL.Table, sql: String = ""): String = |
cead65c19f2e
more direct insert_permissive statement, which avoids somewhat fragile nested transactions;
wenzelm
parents:
65702
diff
changeset
|
437 |
table.insert_cmd("INSERT", |
cead65c19f2e
more direct insert_permissive statement, which avoids somewhat fragile nested transactions;
wenzelm
parents:
65702
diff
changeset
|
438 |
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
|
439 |
|
65009 | 440 |
override def close() { super.close; port_forwarding.foreach(_.close) } |
65006 | 441 |
} |
442 |
} |