1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
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.StringBundler;
23  import com.liferay.portal.kernel.util.StringUtil;
24  
25  import java.io.IOException;
26  
27  /**
28   * <a href="DerbyDB.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Alexander Chow
31   * @author Sandeep Soni
32   * @author Ganesh Ram
33   */
34  public class DerbyDB extends BaseDB {
35  
36      public static DB getInstance() {
37          return _instance;
38      }
39  
40      public String buildSQL(String template) throws IOException {
41          template = convertTimestamp(template);
42          template = replaceTemplate(template, getTemplate());
43  
44          template = reword(template );
45          //template = _removeLongInserts(derby);
46          template = removeNull(template);
47          template = StringUtil.replace(template , "\\'", "''");
48  
49          return template;
50      }
51  
52      public boolean isSupportsAlterColumnName() {
53          return _SUPPORTS_ALTER_COLUMN_NAME;
54      }
55  
56      public boolean isSupportsAlterColumnType() {
57          return _SUPPORTS_ALTER_COLUMN_TYPE;
58      }
59  
60      protected DerbyDB() {
61          super(TYPE_DERBY);
62      }
63  
64      protected String buildCreateFileContent(
65              String sqlDir, String databaseName, int population)
66          throws IOException {
67  
68          String suffix = getSuffix(population);
69  
70          StringBundler sb = new StringBundler(14);
71  
72          sb.append("drop database ");
73          sb.append(databaseName);
74          sb.append(";\n");
75          sb.append("create database ");
76          sb.append(databaseName);
77          sb.append(";\n");
78          sb.append("connect to ");
79          sb.append(databaseName);
80          sb.append(";\n");
81          sb.append(
82              readFile(
83                  sqlDir + "/portal" + suffix + "/portal" + suffix +
84                      "-derby.sql"));
85          sb.append("\n\n");
86          sb.append(readFile(sqlDir + "/indexes/indexes-derby.sql"));
87          sb.append("\n\n");
88          sb.append(readFile(sqlDir + "/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         StringBundler sb = new StringBundler();
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             else if (line.indexOf(DROP_INDEX) != -1) {
121                 String[] tokens = StringUtil.split(line, " ");
122 
123                 line = StringUtil.replace(
124                     "drop index @index@;", "@index@", tokens[2]);
125             }
126 
127             sb.append(line);
128             sb.append("\n");
129         }
130 
131         unsyncBufferedReader.close();
132 
133         return sb.toString();
134     }
135 
136     private static String[] _DERBY = {
137         "--", "1", "0",
138         "'1970-01-01-00.00.00.000000'", "current timestamp",
139         " blob", " smallint", " timestamp",
140         " double", " integer", " bigint",
141         " varchar(4000)", " clob", " varchar",
142         " generated always as identity", "commit"
143     };
144 
145     private static final boolean _SUPPORTS_ALTER_COLUMN_NAME = false;
146 
147     private static final boolean _SUPPORTS_ALTER_COLUMN_TYPE = false;
148 
149     private static Log _log = LogFactoryUtil.getLog(DerbyDB.class);
150 
151     private static DerbyDB _instance = new DerbyDB();
152 
153 }