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.FileUtil;
23  import com.liferay.portal.kernel.util.StringUtil;
24  
25  import java.io.BufferedReader;
26  import java.io.File;
27  import java.io.IOException;
28  import java.io.StringReader;
29  
30  /**
31   * <a href="FirebirdUtil.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Alexander Chow
34   * @author Sandeep Soni
35   * @author Ganesh Ram
36   *
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 void buildCreateFile(String databaseName, boolean minimal)
64          throws IOException {
65  
66          String minimalSuffix = getMinimalSuffix(minimal);
67  
68          File file = new File(
69              "../sql/create" + minimalSuffix + "/create" + minimalSuffix +
70                  "-firebird.sql");
71  
72          StringBuilder sb = new StringBuilder();
73  
74          sb.append(
75              "create database '" + databaseName +
76                  ".gdb' page_size 8192 user 'sysdba' password 'masterkey';\n");
77          sb.append(
78              "connect '" + databaseName +
79                  ".gdb' user 'sysdba' password 'masterkey';\n");
80          sb.append(
81              readSQL(
82                  "../sql/portal" + minimalSuffix + "/portal" + minimalSuffix +
83                      "-firebird.sql",
84                  _FIREBIRD[0], ";\n"));
85  
86          FileUtil.write(file, sb.toString());
87      }
88  
89      protected String getServerName() {
90          return "firebird";
91      }
92  
93      protected String[] getTemplate() {
94          return _FIREBIRD;
95      }
96  
97      protected String reword(String data) throws IOException {
98          BufferedReader br = new BufferedReader(new StringReader(data));
99  
100         StringBuilder sb = new StringBuilder();
101 
102         String line = null;
103 
104         while ((line = br.readLine()) != null) {
105             if (line.startsWith(ALTER_COLUMN_NAME)) {
106                 String[] template = buildColumnNameTokens(line);
107 
108                 line = StringUtil.replace(
109                     "alter table @table@ alter column \"@old-column@\" to " +
110                         "\"@new-column@\";",
111                     REWORD_TEMPLATE, template);
112             }
113             else if (line.startsWith(ALTER_COLUMN_TYPE)) {
114                 String[] template = buildColumnTypeTokens(line);
115 
116                 line = StringUtil.replace(
117                     "alter table @table@ alter column \"@old-column@\" " +
118                         "type @type@;",
119                     REWORD_TEMPLATE, template);
120             }
121 
122             sb.append(line);
123             sb.append("\n");
124         }
125 
126         br.close();
127 
128         return sb.toString();
129     }
130 
131     private static String[] _FIREBIRD = {
132         "--", "1", "0",
133         "'01/01/1970'", "current_timestamp",
134         " blob", " smallint", " timestamp",
135         " double precision", " integer", " int64",
136         " varchar(4000)", " blob", " varchar",
137         "", "commit"
138     };
139 
140     private static FirebirdUtil _instance = new FirebirdUtil();
141 
142 }