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.dao.db;
24  
25  import com.liferay.portal.kernel.dao.db.DB;
26  import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
27  import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
28  import com.liferay.portal.kernel.log.Log;
29  import com.liferay.portal.kernel.log.LogFactoryUtil;
30  import com.liferay.portal.kernel.util.FileUtil;
31  import com.liferay.portal.kernel.util.StringUtil;
32  
33  import java.io.IOException;
34  
35  /**
36   * <a href="DerbyDB.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Alexander Chow
39   * @author Sandeep Soni
40   * @author Ganesh Ram
41   */
42  public class DerbyDB extends BaseDB {
43  
44      public static DB getInstance() {
45          return _instance;
46      }
47  
48      public String buildSQL(String template) throws IOException {
49          template = convertTimestamp(template);
50          template = replaceTemplate(template, getTemplate());
51  
52          template = reword(template );
53          //template = _removeLongInserts(derby);
54          template = removeNull(template);
55          template = StringUtil.replace(template , "\\'", "''");
56  
57          return template;
58      }
59  
60      public boolean isSupportsAlterColumnName() {
61          return _SUPPORTS_ALTER_COLUMN_NAME;
62      }
63  
64      public boolean isSupportsAlterColumnType() {
65          return _SUPPORTS_ALTER_COLUMN_TYPE;
66      }
67  
68      protected DerbyDB() {
69          super(TYPE_DERBY);
70      }
71  
72      protected String buildCreateFileContent(String databaseName, int population)
73          throws IOException {
74  
75          String suffix = getSuffix(population);
76  
77          StringBuilder sb = new StringBuilder();
78  
79          sb.append("drop database " + databaseName + ";\n");
80          sb.append("create database " + databaseName + ";\n");
81          sb.append("connect to " + databaseName + ";\n");
82          sb.append(
83              FileUtil.read(
84                  "../sql/portal" + suffix + "/portal" + suffix + "-derby.sql"));
85          sb.append("\n\n");
86          sb.append(FileUtil.read("../sql/indexes/indexes-derby.sql"));
87          sb.append("\n\n");
88          sb.append(FileUtil.read("../sql/sequences/sequences-derby.sql"));
89  
90          return sb.toString();
91      }
92  
93      protected String getServerName() {
94          return "derby";
95      }
96  
97      protected String[] getTemplate() {
98          return _DERBY;
99      }
100 
101     protected String reword(String data) throws IOException {
102         UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
103             new UnsyncStringReader(data));
104 
105         StringBuilder sb = new StringBuilder();
106 
107         String line = null;
108 
109         while ((line = unsyncBufferedReader.readLine()) != null) {
110             if (line.startsWith(ALTER_COLUMN_NAME) ||
111                 line.startsWith(ALTER_COLUMN_TYPE)) {
112 
113                 line = "-- " + line;
114 
115                 if (_log.isWarnEnabled()) {
116                     _log.warn(
117                         "This statement is not supported by Derby: " + line);
118                 }
119             }
120 
121             sb.append(line);
122             sb.append("\n");
123         }
124 
125         unsyncBufferedReader.close();
126 
127         return sb.toString();
128     }
129 
130     private static String[] _DERBY = {
131         "--", "1", "0",
132         "'1970-01-01-00.00.00.000000'", "current timestamp",
133         " blob", " smallint", " timestamp",
134         " double", " integer", " bigint",
135         " varchar(4000)", " clob", " varchar",
136         " generated always as identity", "commit"
137     };
138 
139     private static boolean _SUPPORTS_ALTER_COLUMN_NAME;
140 
141     private static boolean _SUPPORTS_ALTER_COLUMN_TYPE;
142 
143     private static Log _log = LogFactoryUtil.getLog(DerbyDB.class);
144 
145     private static DerbyDB _instance = new DerbyDB();
146 
147 }