1 package org.cacert.gigi.util;
4 import java.io.FileInputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.InputStreamReader;
9 import java.sql.Connection;
10 import java.sql.DriverManager;
11 import java.sql.SQLException;
12 import java.sql.Statement;
13 import java.util.Properties;
15 import org.cacert.gigi.database.DatabaseConnection;
16 import org.cacert.gigi.database.SQLFileManager;
17 import org.cacert.gigi.database.SQLFileManager.ImportType;
19 public class DatabaseManager {
21 public static void main(String[] args) throws SQLException, ClassNotFoundException, IOException {
23 if (args.length >= 1 && args[0].equals("--test")) {
25 String[] ne = new String[args.length - 1];
26 System.arraycopy(args, 1, ne, 0, ne.length);
29 if (args.length == 0) {
30 Properties p = new Properties();
31 try (Reader reader = new InputStreamReader(new FileInputStream("config/gigi.properties"), "UTF-8")) {
35 p.getProperty("sql.driver"), p.getProperty("sql.url"), p.getProperty("sql.user"), p.getProperty("sql.password")
38 if (args.length < 4) {
39 System.err.println("Usage: com.mysql.jdbc.Driver jdbc:mysql://localhost/cacert user password");
42 run(args, test ? ImportType.TEST : ImportType.PRODUCTION);
45 public static void run(String[] args, ImportType truncate) throws ClassNotFoundException, SQLException, IOException {
46 Class.forName(args[0]);
47 final Connection conn = DriverManager.getConnection(args[1], args[2], args[3]);
49 conn.setAutoCommit(false);
50 Statement stmt = conn.createStatement();
52 try (InputStream structure = DatabaseConnection.class.getResourceAsStream("tableStructure.sql")) {
53 SQLFileManager.addFile(stmt, structure, truncate);
55 File localData = new File("doc/sampleData.sql");
56 if (localData.exists()) {
57 try (FileInputStream f = new FileInputStream(localData)) {
58 SQLFileManager.addFile(stmt, f, ImportType.SAMPLE_DATA);
66 } catch (SQLException e) {
67 e.getNextException().printStackTrace();