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