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