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.util.StringUtil;
26  
27  import java.io.BufferedReader;
28  import java.io.IOException;
29  import java.io.StringReader;
30  
31  /**
32   * <a href="SAPUtil.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Alexander Chow
35   * @author Sandeep Soni
36   * @author Ganesh Ram
37   */
38  public class SAPUtil 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  
50          return template;
51      }
52  
53      protected SAPUtil() {
54          super(TYPE_SAP);
55      }
56  
57      protected String buildCreateFileContent(
58          String databaseName, int population) {
59  
60          return null;
61      }
62  
63      protected String getServerName() {
64          return "sap";
65      }
66  
67      protected String[] getTemplate() {
68          return _SAP;
69      }
70  
71      protected String reword(String data) throws IOException {
72          BufferedReader br = new BufferedReader(new StringReader(data));
73  
74          StringBuilder sb = new StringBuilder();
75  
76          String line = null;
77  
78          while ((line = br.readLine()) != null) {
79              if (line.startsWith(ALTER_COLUMN_NAME)) {
80                  String[] template = buildColumnNameTokens(line);
81  
82                  line = StringUtil.replace(
83                      "rename column @table@.@old-column@ to @new-column@;",
84                      REWORD_TEMPLATE, template);
85              }
86              else if (line.startsWith(ALTER_COLUMN_TYPE)) {
87                  String[] template = buildColumnTypeTokens(line);
88  
89                  line = StringUtil.replace(
90                      "alter table @table@ modify @old-column@ @type@;",
91                      REWORD_TEMPLATE, template);
92              }
93  
94              sb.append(line);
95              sb.append("\n");
96          }
97  
98          br.close();
99  
100         return sb.toString();
101     }
102 
103     private static String[] _SAP = {
104         "##", "TRUE", "FALSE",
105         "'1970-01-01 00:00:00.000000'", "timestamp",
106         " long byte", " boolean", " timestamp",
107         " float", " int", " bigint",
108         " varchar", " varchar", " varchar",
109         "", "commit"
110     };
111 
112     private static SAPUtil _instance = new SAPUtil();
113 
114 }