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.dao.db;
24  
25  import com.liferay.portal.kernel.dao.db.DB;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.util.FileUtil;
29  import com.liferay.portal.kernel.util.StringUtil;
30  
31  import java.io.BufferedReader;
32  import java.io.IOException;
33  import java.io.StringReader;
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         BufferedReader br = new BufferedReader(new StringReader(data));
103 
104         StringBuilder sb = new StringBuilder();
105 
106         String line = null;
107 
108         while ((line = br.readLine()) != null) {
109             if (line.startsWith(ALTER_COLUMN_NAME) ||
110                 line.startsWith(ALTER_COLUMN_TYPE)) {
111 
112                 line = "-- " + line;
113 
114                 if (_log.isWarnEnabled()) {
115                     _log.warn(
116                         "This statement is not supported by Derby: " + line);
117                 }
118             }
119 
120             sb.append(line);
121             sb.append("\n");
122         }
123 
124         br.close();
125 
126         return sb.toString();
127     }
128 
129     private static String[] _DERBY = {
130         "--", "1", "0",
131         "'1970-01-01-00.00.00.000000'", "current timestamp",
132         " blob", " smallint", " timestamp",
133         " double", " integer", " bigint",
134         " varchar(4000)", " clob", " varchar",
135         " generated always as identity", "commit"
136     };
137 
138     private static boolean _SUPPORTS_ALTER_COLUMN_NAME;
139 
140     private static boolean _SUPPORTS_ALTER_COLUMN_TYPE;
141 
142     private static Log _log = LogFactoryUtil.getLog(DerbyDB.class);
143 
144     private static DerbyDB _instance = new DerbyDB();
145 
146 }