1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.tools.sql;
21  
22  import com.liferay.portal.kernel.util.StringUtil;
23  
24  import java.io.BufferedReader;
25  import java.io.IOException;
26  import java.io.StringReader;
27  
28  /**
29   * <a href="HypersonicUtil.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Alexander Chow
32   * @author Sandeep Soni
33   * @author Ganesh Ram
34   *
35   */
36  public class HypersonicUtil extends DBUtil {
37  
38      public static DBUtil getInstance() {
39          return _instance;
40      }
41  
42      public String buildSQL(String template) throws IOException {
43          template = convertTimestamp(template);
44          template = replaceTemplate(template, getTemplate());
45  
46          template = reword(template);
47          template = StringUtil.replace(template, "\\'", "''");
48  
49          return template;
50      }
51  
52      protected HypersonicUtil() {
53          super(TYPE_HYPERSONIC);
54      }
55  
56      protected void buildCreateFile(String databaseName, boolean minimal) {
57      }
58  
59      protected String getServerName() {
60          return "hypersonic";
61      }
62  
63      protected String[] getTemplate() {
64          return _HYPERSONIC;
65      }
66  
67      protected String reword(String data) throws IOException {
68          BufferedReader br = new BufferedReader(new StringReader(data));
69  
70          StringBuilder sb = new StringBuilder();
71  
72          String line = null;
73  
74          while ((line = br.readLine()) != null) {
75              if (line.startsWith(ALTER_COLUMN_NAME)) {
76                  String[] template = buildColumnNameTokens(line);
77  
78                  line = StringUtil.replace(
79                      "alter table @table@ alter column @old-column@ rename to " +
80                          "@new-column@;",
81                      REWORD_TEMPLATE, template);
82              }
83              else if (line.startsWith(ALTER_COLUMN_TYPE)) {
84                  String[] template = buildColumnTypeTokens(line);
85  
86                  line = StringUtil.replace(
87                      "alter table @table@ alter column @old-column@ @type@ " +
88                          "@nullable@;",
89                      REWORD_TEMPLATE, template);
90              }
91  
92              sb.append(line);
93              sb.append("\n");
94          }
95  
96          br.close();
97  
98          return sb.toString();
99      }
100 
101     private static String[] _HYPERSONIC = {
102         "//", "true", "false",
103         "'1970-01-01'", "now()",
104         " binary", " bit", " timestamp",
105         " double", " int", " bigint",
106         " longvarchar", " longvarchar", " varchar",
107         "", "commit"
108     };
109 
110     private static HypersonicUtil _instance = new HypersonicUtil();
111 
112 }