1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.tools.sql;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.FileUtil;
28  import com.liferay.portal.kernel.util.StringUtil;
29  
30  import java.io.BufferedReader;
31  import java.io.IOException;
32  import java.io.StringReader;
33  
34  /**
35   * <a href="DerbyUtil.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Alexander Chow
38   * @author Sandeep Soni
39   * @author Ganesh Ram
40   */
41  public class DerbyUtil extends DBUtil {
42  
43      public static DBUtil getInstance() {
44          return _instance;
45      }
46  
47      public String buildSQL(String template) throws IOException {
48          template = convertTimestamp(template);
49          template = replaceTemplate(template, getTemplate());
50  
51          template = reword(template );
52          //template = _removeLongInserts(derby);
53          template = removeNull(template);
54          template = StringUtil.replace(template , "\\'", "''");
55  
56          return template;
57      }
58  
59      public boolean isSupportsAlterColumnName() {
60          return _SUPPORTS_ALTER_COLUMN_NAME;
61      }
62  
63      public boolean isSupportsAlterColumnType() {
64          return _SUPPORTS_ALTER_COLUMN_TYPE;
65      }
66  
67      protected DerbyUtil() {
68          super(TYPE_DERBY);
69      }
70  
71      protected String buildCreateFileContent(String databaseName, int population)
72          throws IOException {
73  
74          String suffix = getSuffix(population);
75  
76          StringBuilder sb = new StringBuilder();
77  
78          sb.append("drop database " + databaseName + ";\n");
79          sb.append("create database " + databaseName + ";\n");
80          sb.append("connect to " + databaseName + ";\n");
81          sb.append(
82              FileUtil.read(
83                  "../sql/portal" + suffix + "/portal" + suffix + "-derby.sql"));
84          sb.append("\n\n");
85          sb.append(FileUtil.read("../sql/indexes/indexes-derby.sql"));
86          sb.append("\n\n");
87          sb.append(FileUtil.read("../sql/sequences/sequences-derby.sql"));
88  
89          return sb.toString();
90      }
91  
92      protected String getServerName() {
93          return "derby";
94      }
95  
96      protected String[] getTemplate() {
97          return _DERBY;
98      }
99  
100     protected String reword(String data) throws IOException {
101         BufferedReader br = new BufferedReader(new StringReader(data));
102 
103         StringBuilder sb = new StringBuilder();
104 
105         String line = null;
106 
107         while ((line = br.readLine()) != null) {
108             if (line.startsWith(ALTER_COLUMN_NAME) ||
109                 line.startsWith(ALTER_COLUMN_TYPE)) {
110 
111                 line = "-- " + line;
112 
113                 if (_log.isWarnEnabled()) {
114                     _log.warn(
115                         "This statement is not supported by Derby: " + line);
116                 }
117             }
118 
119             sb.append(line);
120             sb.append("\n");
121         }
122 
123         br.close();
124 
125         return sb.toString();
126     }
127 
128     private static String[] _DERBY = {
129         "--", "1", "0",
130         "'1970-01-01-00.00.00.000000'", "current timestamp",
131         " blob", " smallint", " timestamp",
132         " double", " integer", " bigint",
133         " long varchar", " clob", " varchar",
134         " generated always as identity", "commit"
135     };
136 
137     private static boolean _SUPPORTS_ALTER_COLUMN_NAME;
138 
139     private static boolean _SUPPORTS_ALTER_COLUMN_TYPE;
140 
141     private static Log _log = LogFactoryUtil.getLog(DerbyUtil.class);
142 
143     private static DerbyUtil _instance = new DerbyUtil();
144 
145 }