1 16 package org.outerj.daisy.install; 17 18 import java.io.Reader ; 19 import java.io.InputStream ; 20 import java.io.InputStreamReader ; 21 import java.sql.SQLException ; 22 import java.sql.Connection ; 23 import java.sql.Statement ; 24 import java.sql.DriverManager ; 25 26 public class PostgresqlDatabaseSpecifics implements DatabaseSpecifics { 27 private final char statementSeparator = ';'; 28 private final boolean dropStatementSeparator = false; 29 30 public String getCurrentDateTimeFunction() { 31 return "localtimestamp(0)"; 32 } 33 34 public String [] getPreStatements() { 35 return new String [0]; 36 } 37 38 public String [] getPostStatements() { 39 return new String [0]; 40 } 41 42 public char getStatementSeparator() { 43 return statementSeparator; 44 } 45 46 public boolean getDropStatementSeparator() { 47 return dropStatementSeparator; 48 } 49 50 public Reader getSchemaScript() { 51 InputStream schemaScriptIS = getClass().getResourceAsStream("/org/outerj/daisy/install/postgresql-daisy-schema.sql"); 52 return new InputStreamReader (schemaScriptIS); 53 } 54 55 public Reader getDataScript() { 56 InputStream dataScriptIS = getClass().getResourceAsStream("/org/outerj/daisy/install/postgresql-daisy-data.sql"); 57 return new InputStreamReader (dataScriptIS); 58 } 59 60 public void clearDatabase(String dbUrl, String dbName, String dbUser, String dbPassword) throws SQLException { 61 dbUrl = dbUrl.replaceAll(dbName, "template1"); 62 Connection conn = null; 63 Statement stmt = null; 64 try { 65 conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword); 66 stmt = conn.createStatement(); 67 68 stmt.execute("DROP DATABASE " + dbName); 69 stmt.close(); 70 stmt.execute("CREATE DATABASE " + dbName + " ENCODING = 'UTF-8'"); 71 } finally { 72 if (stmt != null) 73 stmt.close(); 74 if (conn != null) 75 conn.close(); 76 } 77 } 78 79 public String getForeignKeyStatement(String table, String field, String otherTable, String otherField, String constraintName) { 80 return "alter table " + table + " add constraint " + constraintName + " foreign key (" + field + ") references " + otherTable + " (" + otherField + ")"; 81 } 82 83 public String getTestStatement() { 84 return "select current_date"; 85 } 86 } 87 | Popular Tags |