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