1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.dao.db;
16  
17  import com.liferay.portal.kernel.dao.db.DB;
18  import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
19  import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
20  import com.liferay.portal.kernel.util.StringBundler;
21  import com.liferay.portal.kernel.util.StringUtil;
22  
23  import java.io.IOException;
24  
25  /**
26   * <a href="SybaseDB.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Alexander Chow
29   * @author Bruno Farache
30   * @author Sandeep Soni
31   * @author Ganesh Ram
32   */
33  public class SybaseDB extends BaseDB {
34  
35      public static DB getInstance() {
36          return _instance;
37      }
38  
39      public String buildSQL(String template) throws IOException {
40          template = convertTimestamp(template);
41          template = replaceTemplate(template, getTemplate());
42  
43          template = reword(template);
44          template = StringUtil.replace(template, ");\n", ")\ngo\n");
45          template = StringUtil.replace(template, "\ngo;\n", "\ngo\n");
46          template = StringUtil.replace(
47              template,
48              new String[] {"\\\\", "\\'", "\\\"", "\\n", "\\r"},
49              new String[] {"\\", "''", "\"", "\n", "\r"});
50  
51          return template;
52      }
53  
54      protected SybaseDB() {
55          super(TYPE_SYBASE);
56      }
57  
58      protected String buildCreateFileContent(
59              String sqlDir, String databaseName, int population)
60          throws IOException {
61  
62          String suffix = getSuffix(population);
63  
64          StringBundler sb = new StringBundler(19);
65  
66          sb.append("use master\n");
67          sb.append("exec sp_dboption '");
68          sb.append(databaseName);
69          sb.append("', ");
70          sb.append("'allow nulls by default' , true\n");
71          sb.append("go\n\n");
72          sb.append("exec sp_dboption '");
73          sb.append(databaseName);
74          sb.append("', ");
75          sb.append("'select into/bulkcopy/pllsort' , true\n");
76          sb.append("go\n\n");
77  
78          sb.append("use ");
79          sb.append(databaseName);
80          sb.append("\n\n");
81          sb.append(
82              readFile(
83                  sqlDir + "/portal" + suffix + "/portal" + suffix +
84                      "-sybase.sql"));
85          sb.append("\n\n");
86          sb.append(readFile(sqlDir + "/indexes/indexes-sybase.sql"));
87          sb.append("\n\n");
88          sb.append(readFile(sqlDir + "/sequences/sequences-sybase.sql"));
89  
90          return sb.toString();
91      }
92  
93      protected String getServerName() {
94          return "sybase";
95      }
96  
97      protected String[] getTemplate() {
98          return _SYBASE;
99      }
100 
101     protected String reword(String data) throws IOException {
102         UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
103             new UnsyncStringReader(data));
104 
105         StringBundler sb = new StringBundler();
106 
107         String line = null;
108 
109         while ((line = unsyncBufferedReader.readLine()) != null) {
110             if (line.indexOf(DROP_COLUMN) != -1) {
111                 line = StringUtil.replace(line, " drop column ", " drop ");
112             }
113 
114             if (line.startsWith(ALTER_COLUMN_NAME)) {
115                 String[] template = buildColumnNameTokens(line);
116 
117                 line = StringUtil.replace(
118                     "exec sp_rename '@table@.@old-column@', '@new-column@', " +
119                         "'column';",
120                     REWORD_TEMPLATE, template);
121             }
122             else 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.indexOf(DROP_INDEX) != -1) {
130                 String[] tokens = StringUtil.split(line, " ");
131 
132                 line = StringUtil.replace(
133                     "drop index @table@.@index@;", "@table@", tokens[4]);
134                 line = StringUtil.replace(line, "@index@", tokens[2]);
135             }
136 
137             sb.append(line);
138             sb.append("\n");
139         }
140 
141         unsyncBufferedReader.close();
142 
143         return sb.toString();
144     }
145 
146     protected static String DROP_COLUMN = "drop column";
147 
148     private static String[] _SYBASE = {
149         "--", "1", "0",
150         "'19700101'", "getdate()",
151         " image", " int", " datetime",
152         " float", " int", " decimal(20,0)",
153         " varchar(1000)", " text", " varchar",
154         "  identity(1,1)", "go"
155     };
156 
157     private static SybaseDB _instance = new SybaseDB();
158 
159 }