1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.File;
32  import java.io.IOException;
33  import java.io.StringReader;
34  
35  /**
36   * <a href="DerbyUtil.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Alexander Chow
39   * @author Sandeep Soni
40   * @author Ganesh Ram
41   *
42   */
43  public class DerbyUtil extends DBUtil {
44  
45      public static DBUtil getInstance() {
46          return _instance;
47      }
48  
49      public String buildSQL(String template) throws IOException {
50          template = convertTimestamp(template);
51          template = replaceTemplate(template, getTemplate());
52  
53          template = reword(template );
54          //template = _removeLongInserts(derby);
55          template = removeNull(template);
56          template = StringUtil.replace(template , "\\'", "''");
57  
58          return template;
59      }
60  
61      public boolean isSupportsAlterColumnName() {
62          return _SUPPORTS_ALTER_COLUMN_NAME;
63      }
64  
65      public boolean isSupportsAlterColumnType() {
66          return _SUPPORTS_ALTER_COLUMN_TYPE;
67      }
68  
69      protected DerbyUtil() {
70          super(TYPE_DERBY);
71      }
72  
73      protected void buildCreateFile(String databaseName, boolean minimal)
74          throws IOException {
75  
76          String minimalSuffix = getMinimalSuffix(minimal);
77  
78          File file = new File(
79              "../sql/create" + minimalSuffix + "/create" + minimalSuffix +
80                  "-derby.sql");
81  
82          StringBuilder sb = new StringBuilder();
83  
84          sb.append("drop database " + databaseName + ";\n");
85          sb.append("create database " + databaseName + ";\n");
86          sb.append("connect to " + databaseName + ";\n");
87          sb.append(
88              FileUtil.read(
89                  "../sql/portal" + minimalSuffix + "/portal" + minimalSuffix +
90                      "-derby.sql"));
91          sb.append("\n\n");
92          sb.append(FileUtil.read("../sql/indexes/indexes-derby.sql"));
93          sb.append("\n\n");
94          sb.append(FileUtil.read("../sql/sequences/sequences-derby.sql"));
95  
96          FileUtil.write(file, sb.toString());
97      }
98  
99      protected String getServerName() {
100         return "derby";
101     }
102 
103     protected String[] getTemplate() {
104         return _DERBY;
105     }
106 
107     protected String reword(String data) throws IOException {
108         BufferedReader br = new BufferedReader(new StringReader(data));
109 
110         StringBuilder sb = new StringBuilder();
111 
112         String line = null;
113 
114         while ((line = br.readLine()) != null) {
115             if (line.startsWith(ALTER_COLUMN_NAME) ||
116                 line.startsWith(ALTER_COLUMN_TYPE)) {
117 
118                 line = "-- " + line;
119 
120                 if (_log.isWarnEnabled()) {
121                     _log.warn(
122                         "This statement is not supported by Derby: " + line);
123                 }
124             }
125 
126             sb.append(line);
127             sb.append("\n");
128         }
129 
130         br.close();
131 
132         return sb.toString();
133     }
134 
135     private static String[] _DERBY = {
136         "--", "1", "0",
137         "'1970-01-01-00.00.00.000000'", "current timestamp",
138         " blob", " smallint", " timestamp",
139         " double", " integer", " bigint",
140         " long varchar", " clob", " varchar",
141         " generated always as identity", "commit"
142     };
143 
144     private static boolean _SUPPORTS_ALTER_COLUMN_NAME;
145 
146     private static boolean _SUPPORTS_ALTER_COLUMN_TYPE;
147 
148     private static Log _log = LogFactoryUtil.getLog(DerbyUtil.class);
149 
150     private static DerbyUtil _instance = new DerbyUtil();
151 
152 }