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