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