1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.tools.sql;
21  
22  import com.liferay.portal.kernel.util.FileUtil;
23  import com.liferay.portal.kernel.util.StringUtil;
24  
25  import java.io.BufferedReader;
26  import java.io.File;
27  import java.io.IOException;
28  import java.io.StringReader;
29  
30  /**
31   * <a href="SQLServerUtil.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Alexander Chow
34   * @author Sandeep Soni
35   * @author Ganesh Ram
36   *
37   */
38  public class SQLServerUtil extends DBUtil {
39  
40      public static DBUtil getInstance() {
41          return _instance;
42      }
43  
44      public String buildSQL(String template) throws IOException {
45          template = convertTimestamp(template);
46          template = replaceTemplate(template, getTemplate());
47  
48          template = reword(template);
49          template = StringUtil.replace(template, "\ngo;\n", "\ngo\n");
50          template = StringUtil.replace(
51              template,
52              new String[] {"\\\\", "\\'", "\\\"", "\\n", "\\r"},
53              new String[] {"\\", "''", "\"", "\n", "\r"});
54  
55          return template;
56      }
57  
58      public boolean isSupportsAlterColumnName() {
59  
60          // We actually have a way of getting SQL Server to alter column names.
61          // The problem is that it will break in scenarios where the column has
62          // an index. To make things simpler, we disallow altering of column
63          // names for SQL Server.
64  
65          return _SUPPORTS_ALTER_COLUMN_NAME;
66      }
67  
68      protected SQLServerUtil() {
69          super(TYPE_SQLSERVER);
70      }
71  
72      protected void buildCreateFile(String databaseName, boolean minimal)
73          throws IOException {
74  
75          String minimalSuffix = getMinimalSuffix(minimal);
76  
77          File file = new File(
78              "../sql/create" + minimalSuffix + "/create" + minimalSuffix +
79                  "-sql-server.sql");
80  
81          StringBuilder sb = new StringBuilder();
82  
83          sb.append("drop database " + databaseName + ";\n");
84          sb.append("create database " + databaseName + ";\n");
85          sb.append("\n");
86          sb.append("go\n");
87          sb.append("\n");
88          sb.append("use " + databaseName + ";\n\n");
89          sb.append(
90              FileUtil.read(
91                  "../sql/portal" + minimalSuffix + "/portal" + minimalSuffix +
92                      "-sql-server.sql"));
93          sb.append("\n\n");
94          sb.append(FileUtil.read("../sql/indexes/indexes-sql-server.sql"));
95          sb.append("\n\n");
96          sb.append(FileUtil.read("../sql/sequences/sequences-sql-server.sql"));
97  
98          FileUtil.write(file, sb.toString());
99      }
100 
101     protected String getServerName() {
102         return "sql-server";
103     }
104 
105     protected String[] getTemplate() {
106         return _SQL_SERVER;
107     }
108 
109     protected String reword(String data) throws IOException {
110         BufferedReader br = new BufferedReader(new StringReader(data));
111 
112         StringBuilder sb = new StringBuilder();
113 
114         String line = null;
115 
116         while ((line = br.readLine()) != null) {
117             if (line.startsWith(ALTER_COLUMN_NAME)) {
118                 String[] template = buildColumnNameTokens(line);
119 
120                 line = StringUtil.replace(
121                     "exec sp_rename '@table@.@old-column@', '@new-column@', " +
122                         "'column';",
123                     REWORD_TEMPLATE, template);
124             }
125             else if (line.startsWith(ALTER_COLUMN_TYPE)) {
126                 String[] template = buildColumnTypeTokens(line);
127 
128                 line = StringUtil.replace(
129                     "alter table @table@ alter column @old-column@ @type@;",
130                     REWORD_TEMPLATE, template);
131             }
132 
133             sb.append(line);
134             sb.append("\n");
135         }
136 
137         br.close();
138 
139         return sb.toString();
140     }
141 
142     private static String[] _SQL_SERVER = {
143         "--", "1", "0",
144         "'19700101'", "GetDate()",
145         " image", " bit", " datetime",
146         " float", " int", " bigint",
147         " varchar(2000)", " text", " varchar",
148         "  identity(1,1)", "go"
149     };
150 
151     private static boolean _SUPPORTS_ALTER_COLUMN_NAME;
152 
153     private static SQLServerUtil _instance = new SQLServerUtil();
154 
155 }