65000
|
1 |
/* Title: Pure/General/postgresql.scala
|
|
2 |
Author: Makarius
|
|
3 |
|
|
4 |
Support for PostgreSQL databases.
|
|
5 |
*/
|
|
6 |
|
|
7 |
package isabelle
|
|
8 |
|
|
9 |
|
|
10 |
import java.sql.{DriverManager, Connection}
|
|
11 |
|
|
12 |
|
|
13 |
object PostgreSQL
|
|
14 |
{
|
|
15 |
/** database connection **/
|
|
16 |
|
|
17 |
val default_host = "localhost"
|
|
18 |
val default_port = 5432
|
|
19 |
|
|
20 |
def open_database(
|
|
21 |
user: String,
|
|
22 |
password: String,
|
|
23 |
database: String = "",
|
|
24 |
host: String = "",
|
|
25 |
port: Int = default_port): Database =
|
|
26 |
{
|
|
27 |
require(user != "")
|
|
28 |
|
|
29 |
val spec =
|
|
30 |
(if (host != "") host else "localhost") +
|
|
31 |
(if (port != default_port) ":" + port else "") + "/" +
|
|
32 |
(if (database != "") database else user)
|
|
33 |
val connection = DriverManager.getConnection("jdbc:postgresql://" + spec, user, password)
|
|
34 |
new Database(spec, connection)
|
|
35 |
}
|
|
36 |
|
|
37 |
class Database private[PostgreSQL](spec: String, val connection: Connection)
|
|
38 |
{
|
|
39 |
override def toString: String = spec
|
|
40 |
}
|
|
41 |
}
|