1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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.FileUtil;
21  import com.liferay.portal.kernel.util.StringBundler;
22  import com.liferay.portal.kernel.util.StringUtil;
23  
24  import java.io.IOException;
25  
26  /**
27   * <a href="SybaseDB.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Alexander Chow
30   * @author Bruno Farache
31   * @author Sandeep Soni
32   * @author Ganesh Ram
33   */
34  public class SybaseDB extends BaseDB {
35  
36      public static DB getInstance() {
37          return _instance;
38      }
39  
40      public String buildSQL(String template) throws IOException {
41          template = convertTimestamp(template);
42          template = replaceTemplate(template, getTemplate());
43  
44          template = reword(template);
45          template = StringUtil.replace(template, ");\n", ")\ngo\n");
46          template = StringUtil.replace(template, "\ngo;\n", "\ngo\n");
47          template = StringUtil.replace(
48              template,
49              new String[] {"\\\\", "\\'", "\\\"", "\\n", "\\r"},
50              new String[] {"\\", "''", "\"", "\n", "\r"});
51  
52          return template;
53      }
54  
55      protected SybaseDB() {
56          super(TYPE_SYBASE);
57      }
58  
59      protected String buildCreateFileContent(
60              String sqlDir, String databaseName, int population)
61          throws IOException {
62  
63          String suffix = getSuffix(population);
64  
65          StringBundler sb = new StringBundler(19);
66  
67          sb.append("use master\n");
68          sb.append("exec sp_dboption '");
69          sb.append(databaseName);
70          sb.append("', ");
71          sb.append("'allow nulls by default' , true\n");
72          sb.append("go\n\n");
73          sb.append("exec sp_dboption '");
74          sb.append(databaseName);
75          sb.append("', ");
76          sb.append("'select into/bulkcopy/pllsort' , true\n");
77          sb.append("go\n\n");
78  
79          sb.append("use ");
80          sb.append(databaseName);
81          sb.append("\n\n");
82          sb.append(
83              FileUtil.read(
84                  sqlDir + "/portal" + suffix + "/portal" + suffix +
85                      "-sybase.sql"));
86          sb.append("\n\n");
87          sb.append(FileUtil.read(sqlDir + "/indexes/indexes-sybase.sql"));
88          sb.append("\n\n");
89          sb.append(FileUtil.read(sqlDir + "/sequences/sequences-sybase.sql"));
90  
91          return sb.toString();
92      }
93  
94      protected String getServerName() {
95          return "sybase";
96      }
97  
98      protected String[] getTemplate() {
99          return _SYBASE;
100     }
101 
102     protected String reword(String data) throws IOException {
103         UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
104             new UnsyncStringReader(data));
105 
106         StringBundler sb = new StringBundler();
107 
108         String line = null;
109 
110         while ((line = unsyncBufferedReader.readLine()) != null) {
111             if (line.indexOf(DROP_COLUMN) != -1) {
112                 line = StringUtil.replace(line, " drop column ", " drop ");
113             }
114 
115             if (line.startsWith(ALTER_COLUMN_NAME)) {
116                 String[] template = buildColumnNameTokens(line);
117 
118                 line = StringUtil.replace(
119                     "exec sp_rename '@table@.@old-column@', '@new-column@', " +
120                         "'column';",
121                     REWORD_TEMPLATE, template);
122             }
123             else 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.indexOf(DROP_INDEX) != -1) {
131                 String[] tokens = StringUtil.split(line, " ");
132 
133                 line = StringUtil.replace(
134                     "drop index @table@.@index@;", "@table@", tokens[4]);
135                 line = StringUtil.replace(line, "@index@", tokens[2]);
136             }
137 
138             sb.append(line);
139             sb.append("\n");
140         }
141 
142         unsyncBufferedReader.close();
143 
144         return sb.toString();
145     }
146 
147     protected static String DROP_COLUMN = "drop column";
148 
149     private static String[] _SYBASE = {
150         "--", "1", "0",
151         "'19700101'", "getdate()",
152         " image", " int", " datetime",
153         " float", " int", " decimal(20,0)",
154         " varchar(1000)", " text", " varchar",
155         "  identity(1,1)", "go"
156     };
157 
158     private static SybaseDB _instance = new SybaseDB();
159 
160 }