View Javadoc
1   package edu.internet2.middleware.grouper.ext.org.apache.ddlutils.platform.derby;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * The platform implementation for Derby.
34   * 
35   * @version $Revision: 231306 $
36   */
37  public class DerbyPlatform extends CloudscapePlatform
38  {
39      /** Database name of this platform. */
40      public static final String DATABASENAME         = "Derby";
41      /** The derby jdbc driver for use as a client for a normal server. */
42      public static final String JDBC_DRIVER          = "org.apache.derby.jdbc.ClientDriver";
43      /** The derby jdbc driver for use as an embedded database. */
44      public static final String JDBC_DRIVER_EMBEDDED = "org.apache.derby.jdbc.EmbeddedDriver";
45      /** The subprotocol used by the derby drivers. */
46      public static final String JDBC_SUBPROTOCOL     = "derby";
47  
48      /**
49       * Creates a new Derby platform instance.
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       * {@inheritDoc}
62       */
63      public String getName()
64      {
65          return DATABASENAME;
66      }
67  
68      /**
69       * {@inheritDoc}
70       */
71      public void createDatabase(String jdbcDriverClassName, String connectionUrl, String username, String password, Map parameters) throws DatabaseOperationException, UnsupportedOperationException
72      {
73          // For Derby, you create databases by simply appending ";create=true" to the connection url
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                      // no need to specify create twice (and create=false wouldn't help anyway)
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 }