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