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.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.StringUtil;
28  
29  import java.io.BufferedReader;
30  import java.io.IOException;
31  import java.io.StringReader;
32  
33  /**
34   * <a href="IngresUtil.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author David Maier
37   */
38  public class IngresUtil 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 = StringUtil.replace(template, "\\n", "'+x'0a'+'");
50  
51          return template;
52      }
53  
54      public boolean isSupportsAlterColumnName() {
55          return _SUPPORTS_ALTER_COLUMN_NAME;
56      }
57  
58      protected IngresUtil() {
59          super(TYPE_INGRES);
60      }
61  
62      protected String buildCreateFileContent(
63          String databaseName, int population) {
64  
65          return null;
66      }
67  
68      protected String getServerName() {
69          return "ingres";
70      }
71  
72      protected String[] getTemplate() {
73          return _INGRES;
74      }
75  
76      protected String replaceTemplate(String template, String[] actual) {
77          if ((template == null) || (TEMPLATE == null) || (actual == null)) {
78              return null;
79          }
80  
81          if (TEMPLATE.length != actual.length) {
82              return template;
83          }
84  
85          for (int i = 0; i < TEMPLATE.length; i++) {
86              if (TEMPLATE[i].equals("##") ||
87                  TEMPLATE[i].equals("'01/01/1970'")) {
88  
89                  template = template.replaceAll(TEMPLATE[i], actual[i]);
90              }
91              else if (TEMPLATE[i].equals("COMMIT_TRANSACTION")) {
92                  template = StringUtil.replace(
93                      template, TEMPLATE[i] + ";", actual[i]);
94              }
95              else {
96                  template = template.replaceAll(
97                      "\\b" + TEMPLATE[i] + "\\b", actual[i]);
98              }
99          }
100 
101         return template;
102     }
103 
104     protected String reword(String data) throws IOException {
105         BufferedReader br = new BufferedReader(new StringReader(data));
106 
107         StringBuilder sb = new StringBuilder();
108 
109         String line = null;
110 
111         while ((line = br.readLine()) != null) {
112             if (line.startsWith(ALTER_COLUMN_NAME)) {
113                 line = "-- " + line;
114 
115                 if (_log.isWarnEnabled()) {
116                     _log.warn(
117                         "This statement is not supported by Ingres: " + line);
118                 }
119             }
120             else if (line.startsWith(ALTER_COLUMN_TYPE)) {
121                 String[] template = buildColumnTypeTokens(line);
122 
123                 line = StringUtil.replace(
124                     "alter table @table@ alter @old-column@ @type@;",
125                     REWORD_TEMPLATE, template);
126             }
127             else if (line.indexOf(DROP_PRIMARY_KEY) != -1) {
128                 String[] tokens = StringUtil.split(line, " ");
129 
130                 line = StringUtil.replace(
131                     "alter table @table@ drop constraint @table@_pkey;",
132                     "@table@", tokens[2]);
133             }
134 
135             sb.append(line);
136             sb.append("\n");
137         }
138 
139         br.close();
140 
141         return sb.toString();
142     }
143 
144     private static String[] _INGRES = {
145         "--", "1", "0",
146         "'1970-01-01'", "date('now')",
147         " byte varying", " tinyint", " timestamp",
148         " float", " integer", " bigint",
149         " varchar(1000)", " long varchar", " varchar",
150         "", "commit;\\g"
151     };
152 
153     private static boolean _SUPPORTS_ALTER_COLUMN_NAME;
154 
155     private static Log _log = LogFactoryUtil.getLog(IngresUtil.class);
156 
157     private static IngresUtil _instance = new IngresUtil();
158 
159 }