1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.dao.db;
16  
17  import com.liferay.portal.kernel.dao.db.DB;
18  import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
19  import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
20  import com.liferay.portal.kernel.log.Log;
21  import com.liferay.portal.kernel.log.LogFactoryUtil;
22  import com.liferay.portal.kernel.util.FileUtil;
23  import com.liferay.portal.kernel.util.StringBundler;
24  import com.liferay.portal.kernel.util.StringUtil;
25  
26  import java.io.IOException;
27  
28  /**
29   * <a href="DerbyDB.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Alexander Chow
32   * @author Sandeep Soni
33   * @author Ganesh Ram
34   */
35  public class DerbyDB extends BaseDB {
36  
37      public static DB getInstance() {
38          return _instance;
39      }
40  
41      public String buildSQL(String template) throws IOException {
42          template = convertTimestamp(template);
43          template = replaceTemplate(template, getTemplate());
44  
45          template = reword(template );
46          //template = _removeLongInserts(derby);
47          template = removeNull(template);
48          template = StringUtil.replace(template , "\\'", "''");
49  
50          return template;
51      }
52  
53      public boolean isSupportsAlterColumnName() {
54          return _SUPPORTS_ALTER_COLUMN_NAME;
55      }
56  
57      public boolean isSupportsAlterColumnType() {
58          return _SUPPORTS_ALTER_COLUMN_TYPE;
59      }
60  
61      protected DerbyDB() {
62          super(TYPE_DERBY);
63      }
64  
65      protected String buildCreateFileContent(
66              String sqlDir, String databaseName, int population)
67          throws IOException {
68  
69          String suffix = getSuffix(population);
70  
71          StringBundler sb = new StringBundler(14);
72  
73          sb.append("drop database ");
74          sb.append(databaseName);
75          sb.append(";\n");
76          sb.append("create database ");
77          sb.append(databaseName);
78          sb.append(";\n");
79          sb.append("connect to ");
80          sb.append(databaseName);
81          sb.append(";\n");
82          sb.append(
83              FileUtil.read(
84                  sqlDir + "/portal" + suffix + "/portal" + suffix +
85                      "-derby.sql"));
86          sb.append("\n\n");
87          sb.append(FileUtil.read(sqlDir + "/indexes/indexes-derby.sql"));
88          sb.append("\n\n");
89          sb.append(FileUtil.read(sqlDir + "/sequences/sequences-derby.sql"));
90  
91          return sb.toString();
92      }
93  
94      protected String getServerName() {
95          return "derby";
96      }
97  
98      protected String[] getTemplate() {
99          return _DERBY;
100     }
101 
102     protected String reword(String data) throws IOException {
103         UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
104             new UnsyncStringReader(data));
105 
106         StringBundler sb = new StringBundler();
107 
108         String line = null;
109 
110         while ((line = unsyncBufferedReader.readLine()) != null) {
111             if (line.startsWith(ALTER_COLUMN_NAME) ||
112                 line.startsWith(ALTER_COLUMN_TYPE)) {
113 
114                 line = "-- " + line;
115 
116                 if (_log.isWarnEnabled()) {
117                     _log.warn(
118                         "This statement is not supported by Derby: " + line);
119                 }
120             }
121             else if (line.indexOf(DROP_INDEX) != -1) {
122                 String[] tokens = StringUtil.split(line, " ");
123 
124                 line = StringUtil.replace(
125                     "drop index @index@;", "@index@", tokens[2]);
126             }
127 
128             sb.append(line);
129             sb.append("\n");
130         }
131 
132         unsyncBufferedReader.close();
133 
134         return sb.toString();
135     }
136 
137     private static String[] _DERBY = {
138         "--", "1", "0",
139         "'1970-01-01-00.00.00.000000'", "current timestamp",
140         " blob", " smallint", " timestamp",
141         " double", " integer", " bigint",
142         " varchar(4000)", " clob", " varchar",
143         " generated always as identity", "commit"
144     };
145 
146     private static boolean _SUPPORTS_ALTER_COLUMN_NAME;
147 
148     private static boolean _SUPPORTS_ALTER_COLUMN_TYPE;
149 
150     private static Log _log = LogFactoryUtil.getLog(DerbyDB.class);
151 
152     private static DerbyDB _instance = new DerbyDB();
153 
154 }