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      protected DerbyUtil() {
62          super(TYPE_DERBY);
63      }
64  
65      protected void buildCreateFile(String databaseName, boolean minimal)
66          throws IOException {
67  
68          String minimalSuffix = getMinimalSuffix(minimal);
69  
70          File file = new File(
71              "../sql/create" + minimalSuffix + "/create" + minimalSuffix +
72                  "-derby.sql");
73  
74          StringBuilder sb = new StringBuilder();
75  
76          sb.append("drop database " + databaseName + ";\n");
77          sb.append("create database " + databaseName + ";\n");
78          sb.append("connect to " + databaseName + ";\n");
79          sb.append(
80              FileUtil.read(
81                  "../sql/portal" + minimalSuffix + "/portal" + minimalSuffix +
82                      "-derby.sql"));
83          sb.append("\n\n");
84          sb.append(FileUtil.read("../sql/indexes/indexes-derby.sql"));
85          sb.append("\n\n");
86          sb.append(FileUtil.read("../sql/sequences/sequences-derby.sql"));
87  
88          FileUtil.write(file, sb.toString());
89      }
90  
91      protected String getServerName() {
92          return "derby";
93      }
94  
95      protected String[] getTemplate() {
96          return _DERBY;
97      }
98  
99      protected String reword(String data) throws IOException {
100         BufferedReader br = new BufferedReader(new StringReader(data));
101 
102         StringBuilder sb = new StringBuilder();
103 
104         String line = null;
105 
106         while ((line = br.readLine()) != null) {
107             if (line.startsWith(ALTER_COLUMN_TYPE) ||
108                 line.startsWith(ALTER_COLUMN_NAME)) {
109 
110                 line = "-- " + line;
111 
112                 if (_log.isWarnEnabled()) {
113                     _log.warn(
114                         "This statement is not supported by Derby: " + line);
115                 }
116             }
117 
118             sb.append(line);
119             sb.append("\n");
120         }
121 
122         br.close();
123 
124         return sb.toString();
125     }
126 
127     private static String[] _DERBY = {
128         "--", "1", "0",
129         "'1970-01-01-00.00.00.000000'", "current timestamp",
130         " blob", " smallint", " timestamp",
131         " double", " integer", " bigint",
132         " varchar(4000)", " clob", " varchar",
133         " generated always as identity", "commit"
134     };
135 
136     private static Log _log = LogFactoryUtil.getLog(DBUtil.class);
137 
138     private static DerbyUtil _instance = new DerbyUtil();
139 
140 }