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.log.Log;
29  import com.liferay.portal.kernel.log.LogFactoryUtil;
30  import com.liferay.portal.kernel.util.StringUtil;
31  
32  import java.io.IOException;
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         UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
107             new UnsyncStringReader(data));
108 
109         StringBuilder sb = new StringBuilder();
110 
111         String line = null;
112 
113         while ((line = unsyncBufferedReader.readLine()) != null) {
114             if (line.startsWith(ALTER_COLUMN_NAME)) {
115                 line = "-- " + line;
116 
117                 if (_log.isWarnEnabled()) {
118                     _log.warn(
119                         "This statement is not supported by Ingres: " + line);
120                 }
121             }
122             else if (line.startsWith(ALTER_COLUMN_TYPE)) {
123                 String[] template = buildColumnTypeTokens(line);
124 
125                 line = StringUtil.replace(
126                     "alter table @table@ alter @old-column@ @type@;",
127                     REWORD_TEMPLATE, template);
128             }
129             else if (line.indexOf(DROP_PRIMARY_KEY) != -1) {
130                 String[] tokens = StringUtil.split(line, " ");
131 
132                 line = StringUtil.replace(
133                     "alter table @table@ drop constraint @table@_pkey;",
134                     "@table@", tokens[2]);
135             }
136 
137             sb.append(line);
138             sb.append("\n");
139         }
140 
141         unsyncBufferedReader.close();
142 
143         return sb.toString();
144     }
145 
146     private static String[] _INGRES = {
147         "--", "1", "0",
148         "'1970-01-01'", "date('now')",
149         " byte varying", " tinyint", " timestamp",
150         " float", " integer", " bigint",
151         " varchar(1000)", " long varchar", " varchar",
152         "", "commit;\\g"
153     };
154 
155     private static boolean _SUPPORTS_ALTER_COLUMN_NAME;
156 
157     private static Log _log = LogFactoryUtil.getLog(IngresDB.class);
158 
159     private static IngresDB _instance = new IngresDB();
160 
161 }