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.FileUtil;
26  import com.liferay.portal.kernel.util.StringUtil;
27  
28  import java.io.BufferedReader;
29  import java.io.IOException;
30  import java.io.StringReader;
31  
32  /**
33   * <a href="SybaseUtil.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Alexander Chow
36   * @author Bruno Farache
37   * @author Sandeep Soni
38   * @author Ganesh Ram
39   */
40  public class SybaseUtil extends DBUtil {
41  
42      public static DBUtil getInstance() {
43          return _instance;
44      }
45  
46      public String buildSQL(String template) throws IOException {
47          template = convertTimestamp(template);
48          template = replaceTemplate(template, getTemplate());
49  
50          template = reword(template);
51          template = StringUtil.replace(template, ");\n", ")\ngo\n");
52          template = StringUtil.replace(template, "\ngo;\n", "\ngo\n");
53          template = StringUtil.replace(
54              template,
55              new String[] {"\\\\", "\\'", "\\\"", "\\n", "\\r"},
56              new String[] {"\\", "''", "\"", "\n", "\r"});
57  
58          return template;
59      }
60  
61      protected SybaseUtil() {
62          super(TYPE_SYBASE);
63      }
64  
65      protected String buildCreateFileContent(String databaseName, int population)
66          throws IOException {
67  
68          String suffix = getSuffix(population);
69  
70          StringBuilder sb = new StringBuilder();
71  
72          sb = new StringBuilder();
73  
74          sb.append("use master\n");
75          sb.append(
76              "exec sp_dboption '" + databaseName + "', " +
77                  "'allow nulls by default' , true\n");
78          sb.append("go\n\n");
79          sb.append(
80              "exec sp_dboption '" + databaseName + "', " +
81                  "'select into/bulkcopy/pllsort' , true\n");
82          sb.append("go\n\n");
83  
84          sb.append("use " + databaseName + "\n\n");
85          sb.append(
86              FileUtil.read(
87                  "../sql/portal" + suffix + "/portal" + suffix + "-sybase.sql"));
88          sb.append("\n\n");
89          sb.append(FileUtil.read("../sql/indexes/indexes-sybase.sql"));
90          sb.append("\n\n");
91          sb.append(FileUtil.read("../sql/sequences/sequences-sybase.sql"));
92  
93          return sb.toString();
94      }
95  
96      protected String getServerName() {
97          return "sybase";
98      }
99  
100     protected String[] getTemplate() {
101         return _SYBASE;
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 
113             if (line.indexOf(DROP_COLUMN) != -1) {
114                 line = StringUtil.replace(line, " drop column ", " drop ");
115             }
116 
117             if (line.startsWith(ALTER_COLUMN_NAME)) {
118                 String[] template = buildColumnNameTokens(line);
119 
120                 line = StringUtil.replace(
121                     "exec sp_rename '@table@.@old-column@', '@new-column@', " +
122                         "'column';",
123                     REWORD_TEMPLATE, template);
124             }
125             else if (line.startsWith(ALTER_COLUMN_TYPE)) {
126                 String[] template = buildColumnTypeTokens(line);
127 
128                 line = StringUtil.replace(
129                     "alter table @table@ alter column @old-column@ @type@;",
130                     REWORD_TEMPLATE, template);
131             }
132 
133             sb.append(line);
134             sb.append("\n");
135         }
136 
137         br.close();
138 
139         return sb.toString();
140     }
141 
142     protected static String DROP_COLUMN = "drop column";
143 
144     private static String[] _SYBASE = {
145         "--", "1", "0",
146         "'19700101'", "getdate()",
147         " image", " int", " datetime",
148         " float", " int", " decimal(20,0)",
149         " varchar(1000)", " text", " varchar",
150         "  identity(1,1)", "go"
151     };
152 
153     private static SybaseUtil _instance = new SybaseUtil();
154 
155 }