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.tools.sql;
24  
25  import com.liferay.portal.kernel.util.StringUtil;
26  
27  import java.io.BufferedReader;
28  import java.io.IOException;
29  import java.io.StringReader;
30  
31  /**
32   * <a href="FirebirdUtil.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Alexander Chow
35   * @author Sandeep Soni
36   * @author Ganesh Ram
37   */
38  public class FirebirdUtil extends DBUtil {
39  
40      public static DBUtil getInstance() {
41          return _instance;
42      }
43  
44      public String buildSQL(String template) throws IOException {
45          template = convertTimestamp(template);
46          template = replaceTemplate(template, getTemplate());
47  
48          template = reword(template);
49          template = removeInserts(template);
50          template = removeNull(template);
51  
52          return template;
53      }
54  
55      protected FirebirdUtil() {
56          super(TYPE_FIREBIRD);
57      }
58  
59      protected FirebirdUtil(String type) {
60          super(type);
61      }
62  
63      protected String buildCreateFileContent(String databaseName, int population)
64          throws IOException {
65  
66          String suffix = getSuffix(population);
67  
68          StringBuilder sb = new StringBuilder();
69  
70          sb.append(
71              "create database '" + databaseName +
72                  ".gdb' page_size 8192 user 'sysdba' password 'masterkey';\n");
73          sb.append(
74              "connect '" + databaseName +
75                  ".gdb' user 'sysdba' password 'masterkey';\n");
76          sb.append(
77              readSQL(
78                  "../sql/portal" + suffix + "/portal" + suffix + "-firebird.sql",
79                  _FIREBIRD[0], ";\n"));
80  
81          return sb.toString();
82      }
83  
84      protected String getServerName() {
85          return "firebird";
86      }
87  
88      protected String[] getTemplate() {
89          return _FIREBIRD;
90      }
91  
92      protected String reword(String data) throws IOException {
93          BufferedReader br = new BufferedReader(new StringReader(data));
94  
95          StringBuilder sb = new StringBuilder();
96  
97          String line = null;
98  
99          while ((line = br.readLine()) != null) {
100             if (line.startsWith(ALTER_COLUMN_NAME)) {
101                 String[] template = buildColumnNameTokens(line);
102 
103                 line = StringUtil.replace(
104                     "alter table @table@ alter column \"@old-column@\" to " +
105                         "\"@new-column@\";",
106                     REWORD_TEMPLATE, template);
107             }
108             else if (line.startsWith(ALTER_COLUMN_TYPE)) {
109                 String[] template = buildColumnTypeTokens(line);
110 
111                 line = StringUtil.replace(
112                     "alter table @table@ alter column \"@old-column@\" " +
113                         "type @type@;",
114                     REWORD_TEMPLATE, template);
115             }
116 
117             sb.append(line);
118             sb.append("\n");
119         }
120 
121         br.close();
122 
123         return sb.toString();
124     }
125 
126     private static String[] _FIREBIRD = {
127         "--", "1", "0",
128         "'01/01/1970'", "current_timestamp",
129         " blob", " smallint", " timestamp",
130         " double precision", " integer", " int64",
131         " varchar(4000)", " blob", " varchar",
132         "", "commit"
133     };
134 
135     private static FirebirdUtil _instance = new FirebirdUtil();
136 
137 }