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