1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.util.StringUtil;
29  
30  import java.io.BufferedReader;
31  import java.io.IOException;
32  import java.io.StringReader;
33  
34  /**
35   * <a href="IngresDB.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author David Maier
38   */
39  public class IngresDB 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 = StringUtil.replace(template, "\\n", "'+x'0a'+'");
51  
52          return template;
53      }
54  
55      public boolean isSupportsAlterColumnName() {
56          return _SUPPORTS_ALTER_COLUMN_NAME;
57      }
58  
59      protected IngresDB() {
60          super(TYPE_INGRES);
61      }
62  
63      protected String buildCreateFileContent(
64          String databaseName, int population) {
65  
66          return null;
67      }
68  
69      protected String getServerName() {
70          return "ingres";
71      }
72  
73      protected String[] getTemplate() {
74          return _INGRES;
75      }
76  
77      protected String replaceTemplate(String template, String[] actual) {
78          if ((template == null) || (TEMPLATE == null) || (actual == null)) {
79              return null;
80          }
81  
82          if (TEMPLATE.length != actual.length) {
83              return template;
84          }
85  
86          for (int i = 0; i < TEMPLATE.length; i++) {
87              if (TEMPLATE[i].equals("##") ||
88                  TEMPLATE[i].equals("'01/01/1970'")) {
89  
90                  template = template.replaceAll(TEMPLATE[i], actual[i]);
91              }
92              else if (TEMPLATE[i].equals("COMMIT_TRANSACTION")) {
93                  template = StringUtil.replace(
94                      template, TEMPLATE[i] + ";", actual[i]);
95              }
96              else {
97                  template = template.replaceAll(
98                      "\\b" + TEMPLATE[i] + "\\b", actual[i]);
99              }
100         }
101 
102         return template;
103     }
104 
105     protected String reword(String data) throws IOException {
106         BufferedReader br = new BufferedReader(new StringReader(data));
107 
108         StringBuilder sb = new StringBuilder();
109 
110         String line = null;
111 
112         while ((line = br.readLine()) != null) {
113             if (line.startsWith(ALTER_COLUMN_NAME)) {
114                 line = "-- " + line;
115 
116                 if (_log.isWarnEnabled()) {
117                     _log.warn(
118                         "This statement is not supported by Ingres: " + line);
119                 }
120             }
121             else if (line.startsWith(ALTER_COLUMN_TYPE)) {
122                 String[] template = buildColumnTypeTokens(line);
123 
124                 line = StringUtil.replace(
125                     "alter table @table@ alter @old-column@ @type@;",
126                     REWORD_TEMPLATE, template);
127             }
128             else if (line.indexOf(DROP_PRIMARY_KEY) != -1) {
129                 String[] tokens = StringUtil.split(line, " ");
130 
131                 line = StringUtil.replace(
132                     "alter table @table@ drop constraint @table@_pkey;",
133                     "@table@", tokens[2]);
134             }
135 
136             sb.append(line);
137             sb.append("\n");
138         }
139 
140         br.close();
141 
142         return sb.toString();
143     }
144 
145     private static String[] _INGRES = {
146         "--", "1", "0",
147         "'1970-01-01'", "date('now')",
148         " byte varying", " tinyint", " timestamp",
149         " float", " integer", " bigint",
150         " varchar(1000)", " long varchar", " varchar",
151         "", "commit;\\g"
152     };
153 
154     private static boolean _SUPPORTS_ALTER_COLUMN_NAME;
155 
156     private static Log _log = LogFactoryUtil.getLog(IngresDB.class);
157 
158     private static IngresDB _instance = new IngresDB();
159 
160 }