1 package edu.internet2.middleware.grouper.ext.org.apache.ddlutils.platform.derby;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.sql.Connection;
23 import java.sql.DriverManager;
24 import java.sql.SQLException;
25 import java.sql.Types;
26 import java.util.Iterator;
27 import java.util.Map;
28
29 import edu.internet2.middleware.grouper.ext.org.apache.ddlutils.DatabaseOperationException;
30 import edu.internet2.middleware.grouper.ext.org.apache.ddlutils.platform.cloudscape.CloudscapePlatform;
31
32
33
34
35
36
37 public class DerbyPlatform extends CloudscapePlatform
38 {
39
40 public static final String DATABASENAME = "Derby";
41
42 public static final String JDBC_DRIVER = "org.apache.derby.jdbc.ClientDriver";
43
44 public static final String JDBC_DRIVER_EMBEDDED = "org.apache.derby.jdbc.EmbeddedDriver";
45
46 public static final String JDBC_SUBPROTOCOL = "derby";
47
48
49
50
51 public DerbyPlatform()
52 {
53 super();
54 getPlatformInfo().addNativeTypeMapping(Types.DOUBLE, "DOUBLE");
55 getPlatformInfo().addNativeTypeMapping(Types.FLOAT, "DOUBLE", Types.DOUBLE);
56 setSqlBuilder(new DerbyBuilder(this));
57 setModelReader(new DerbyModelReader(this));
58 }
59
60
61
62
63 public String getName()
64 {
65 return DATABASENAME;
66 }
67
68
69
70
71 public void createDatabase(String jdbcDriverClassName, String connectionUrl, String username, String password, Map parameters) throws DatabaseOperationException, UnsupportedOperationException
72 {
73
74 if (JDBC_DRIVER.equals(jdbcDriverClassName) ||
75 JDBC_DRIVER_EMBEDDED.equals(jdbcDriverClassName))
76 {
77 StringBuffer creationUrl = new StringBuffer();
78 Connection connection = null;
79
80 creationUrl.append(connectionUrl);
81 creationUrl.append(";create=true");
82 if ((parameters != null) && !parameters.isEmpty())
83 {
84 for (Iterator it = parameters.entrySet().iterator(); it.hasNext();)
85 {
86 Map.Entry entry = (Map.Entry)it.next();
87
88
89 if (!"create".equalsIgnoreCase(entry.getKey().toString()))
90 {
91 creationUrl.append(";");
92 creationUrl.append(entry.getKey().toString());
93 creationUrl.append("=");
94 if (entry.getValue() != null)
95 {
96 creationUrl.append(entry.getValue().toString());
97 }
98 }
99 }
100 }
101 if (getLog().isDebugEnabled())
102 {
103 getLog().debug("About to create database using this URL: "+creationUrl.toString());
104 }
105 try
106 {
107 Class.forName(jdbcDriverClassName);
108
109 connection = DriverManager.getConnection(creationUrl.toString(), username, password);
110 logWarnings(connection);
111 }
112 catch (Exception ex)
113 {
114 throw new DatabaseOperationException("Error while trying to create a database", ex);
115 }
116 finally
117 {
118 if (connection != null)
119 {
120 try
121 {
122 connection.close();
123 }
124 catch (SQLException ex)
125 {}
126 }
127 }
128 }
129 else
130 {
131 throw new UnsupportedOperationException("Unable to create a Derby database via the driver "+jdbcDriverClassName);
132 }
133 }
134 }