KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > install > MySQLDatabaseSpecifics


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.install;
17
18 import java.io.InputStream JavaDoc;
19 import java.io.InputStreamReader JavaDoc;
20 import java.io.Reader JavaDoc;
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 JavaDoc getCurrentDateTimeFunction() {
28         return "NOW()";
29     }
30
31     public String JavaDoc[] getPreStatements() {
32         return new String JavaDoc[] {"SET FOREIGN_KEY_CHECKS = 0;"};
33     }
34
35     public String JavaDoc[] getPostStatements() {
36         return new String JavaDoc[] {"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 JavaDoc getSchemaScript() {
48         InputStream JavaDoc schemaScriptIS = getClass().getResourceAsStream("/org/outerj/daisy/install/mysql-daisy-schema.sql");
49         return new InputStreamReader JavaDoc(schemaScriptIS);
50     }
51
52     public Reader JavaDoc getDataScript() {
53         InputStream JavaDoc dataScriptIS = getClass().getResourceAsStream("/org/outerj/daisy/install/mysql-daisy-data.sql");
54         return new InputStreamReader JavaDoc(dataScriptIS);
55     }
56
57     public void clearDatabase(String JavaDoc dbUrl, String JavaDoc dbName, String JavaDoc dbUser, String JavaDoc 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 JavaDoc 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 JavaDoc e) { /* ignore */ }
75             if (stmt != null)
76                 stmt.close();
77             if (conn != null)
78                 conn.close();
79         }
80     }
81
82     public String JavaDoc getForeignKeyStatement(String JavaDoc table, String JavaDoc field, String JavaDoc otherTable, String JavaDoc otherField, String JavaDoc constraintName) {
83         return "alter table " + table + " add constraint " + constraintName + " foreign key (" + field + ") references " + otherTable + " (" + otherField + ")";
84     }
85
86     public String JavaDoc getTestStatement() {
87         return "select current_date";
88     }
89 }
90
Popular Tags