KickJava   Java API By Example, From Geeks To Geeks.

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


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.Reader JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.io.InputStreamReader JavaDoc;
21 import java.sql.SQLException JavaDoc;
22 import java.sql.Connection JavaDoc;
23 import java.sql.Statement JavaDoc;
24 import java.sql.DriverManager JavaDoc;
25
26 public class PostgresqlDatabaseSpecifics implements DatabaseSpecifics {
27     private final char statementSeparator = ';';
28     private final boolean dropStatementSeparator = false;
29
30     public String JavaDoc getCurrentDateTimeFunction() {
31         return "localtimestamp(0)";
32     }
33
34     public String JavaDoc[] getPreStatements() {
35         return new String JavaDoc[0];
36     }
37
38     public String JavaDoc[] getPostStatements() {
39         return new String JavaDoc[0];
40     }
41
42     public char getStatementSeparator() {
43         return statementSeparator;
44     }
45
46     public boolean getDropStatementSeparator() {
47         return dropStatementSeparator;
48     }
49
50     public Reader JavaDoc getSchemaScript() {
51         InputStream JavaDoc schemaScriptIS = getClass().getResourceAsStream("/org/outerj/daisy/install/postgresql-daisy-schema.sql");
52         return new InputStreamReader JavaDoc(schemaScriptIS);
53     }
54
55     public Reader JavaDoc getDataScript() {
56         InputStream JavaDoc dataScriptIS = getClass().getResourceAsStream("/org/outerj/daisy/install/postgresql-daisy-data.sql");
57         return new InputStreamReader JavaDoc(dataScriptIS);
58     }
59
60     public void clearDatabase(String JavaDoc dbUrl, String JavaDoc dbName, String JavaDoc dbUser, String JavaDoc dbPassword) throws SQLException JavaDoc {
61         dbUrl = dbUrl.replaceAll(dbName, "template1");
62         Connection JavaDoc conn = null;
63         Statement JavaDoc 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 JavaDoc getForeignKeyStatement(String JavaDoc table, String JavaDoc field, String JavaDoc otherTable, String JavaDoc otherField, String JavaDoc constraintName) {
80         return "alter table " + table + " add constraint " + constraintName + " foreign key (" + field + ") references " + otherTable + " (" + otherField + ")";
81     }
82
83     public String JavaDoc getTestStatement() {
84         return "select current_date";
85     }
86 }
87
Popular Tags