634 user: String, |
634 user: String, |
635 password: String, |
635 password: String, |
636 database: String = "", |
636 database: String = "", |
637 server: SSH.Server = default_server, |
637 server: SSH.Server = default_server, |
638 server_close: Boolean = false, |
638 server_close: Boolean = false, |
|
639 synchronous_commit: String = "off" |
639 ): Database = { |
640 ): Database = { |
640 init_jdbc |
641 init_jdbc |
641 |
642 |
642 if (user.isEmpty) error("Undefined database user") |
643 if (user.isEmpty) error("Undefined database user") |
643 if (server.host.isEmpty) error("Undefined database server host") |
644 if (server.host.isEmpty) error("Undefined database server host") |
647 val url = "jdbc:postgresql://" + server.host + ":" + server.port + "/" + name |
648 val url = "jdbc:postgresql://" + server.host + ":" + server.port + "/" + name |
648 val ssh = server.ssh_system.ssh_session |
649 val ssh = server.ssh_system.ssh_session |
649 val print = user + "@" + server + "/" + name + if_proper(ssh, " via ssh " + ssh.get) |
650 val print = user + "@" + server + "/" + name + if_proper(ssh, " via ssh " + ssh.get) |
650 |
651 |
651 val connection = DriverManager.getConnection(url, user, password) |
652 val connection = DriverManager.getConnection(url, user, password) |
652 new Database(connection, print, server, server_close) |
653 val db = new Database(connection, print, server, server_close) |
|
654 |
|
655 try { |
|
656 db.execute_statement("SET synchronous_commit = " + SQL.string(synchronous_commit)) |
|
657 } |
|
658 catch { case exn: Throwable => db.close(); throw exn } |
|
659 |
|
660 db |
653 } |
661 } |
654 |
662 |
655 def open_server( |
663 def open_server( |
656 options: Options, |
664 options: Options, |
657 host: String = "", |
665 host: String = "", |
678 server: SSH.Server = SSH.no_server, |
686 server: SSH.Server = SSH.no_server, |
679 host: String = "", |
687 host: String = "", |
680 port: Int = 0, |
688 port: Int = 0, |
681 ssh_host: String = "", |
689 ssh_host: String = "", |
682 ssh_port: Int = 0, |
690 ssh_port: Int = 0, |
683 ssh_user: String = "" |
691 ssh_user: String = "", |
|
692 synchronous_commit: String = "off" |
684 ): PostgreSQL.Database = { |
693 ): PostgreSQL.Database = { |
685 val db_server = |
694 val db_server = |
686 if (server.defined) server |
695 if (server.defined) server |
687 else { |
696 else { |
688 open_server(options, host = host, port = port, ssh_host = ssh_host, |
697 open_server(options, host = host, port = port, ssh_host = ssh_host, |
689 ssh_port = ssh_port, ssh_user = ssh_user) |
698 ssh_port = ssh_port, ssh_user = ssh_user) |
690 } |
699 } |
691 val server_close = !server.defined |
700 val server_close = !server.defined |
692 try { |
701 try { |
693 open_database(user = user, password = password, database = database, |
702 open_database(user = user, password = password, database = database, |
694 server = db_server, server_close = server_close) |
703 server = db_server, server_close = server_close, synchronous_commit = synchronous_commit) |
695 } |
704 } |
696 catch { case exn: Throwable if server_close => db_server.close(); throw exn } |
705 catch { case exn: Throwable if server_close => db_server.close(); throw exn } |
697 } |
706 } |
698 |
707 |
699 class Database private[PostgreSQL]( |
708 class Database private[PostgreSQL]( |