1   /**
2    * Copyright (c) 2000-2009 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.dao.db;
24  
25  import com.liferay.portal.kernel.dao.db.DB;
26  import com.liferay.portal.kernel.util.FileUtil;
27  import com.liferay.portal.kernel.util.StringUtil;
28  
29  import java.io.BufferedReader;
30  import java.io.IOException;
31  import java.io.StringReader;
32  
33  /**
34   * <a href="SQLServerDB.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Alexander Chow
37   * @author Sandeep Soni
38   * @author Ganesh Ram
39   */
40  public class SQLServerDB extends BaseDB {
41  
42      public static DB getInstance() {
43          return _instance;
44      }
45  
46      public String buildSQL(String template) throws IOException {
47          template = convertTimestamp(template);
48          template = replaceTemplate(template, getTemplate());
49  
50          template = reword(template);
51          template = StringUtil.replace(template, "\ngo;\n", "\ngo\n");
52          template = StringUtil.replace(
53              template,
54              new String[] {"\\\\", "\\'", "\\\"", "\\n", "\\r"},
55              new String[] {"\\", "''", "\"", "\n", "\r"});
56  
57          return template;
58      }
59  
60      public boolean isSupportsAlterColumnType() {
61          return _SUPPORTS_ALTER_COLUMN_TYPE;
62      }
63  
64      protected SQLServerDB() {
65          super(TYPE_SQLSERVER);
66      }
67  
68      protected String buildCreateFileContent(String databaseName, int population)
69          throws IOException {
70  
71          String suffix = getSuffix(population);
72  
73          StringBuilder sb = new StringBuilder();
74  
75          sb.append("drop database " + databaseName + ";\n");
76          sb.append("create database " + databaseName + ";\n");
77          sb.append("\n");
78          sb.append("go\n");
79          sb.append("\n");
80          sb.append("use " + databaseName + ";\n\n");
81          sb.append(
82              FileUtil.read(
83                  "../sql/portal" + suffix + "/portal" + suffix +
84                      "-sql-server.sql"));
85          sb.append("\n\n");
86          sb.append(FileUtil.read("../sql/indexes/indexes-sql-server.sql"));
87          sb.append("\n\n");
88          sb.append(FileUtil.read("../sql/sequences/sequences-sql-server.sql"));
89  
90          return sb.toString();
91      }
92  
93      protected String getServerName() {
94          return "sql-server";
95      }
96  
97      protected String[] getTemplate() {
98          return _SQL_SERVER;
99      }
100 
101     protected String reword(String data) throws IOException {
102         BufferedReader br = new BufferedReader(new StringReader(data));
103 
104         StringBuilder sb = new StringBuilder();
105 
106         String line = null;
107 
108         while ((line = br.readLine()) != null) {
109             if (line.startsWith(ALTER_COLUMN_NAME)) {
110                 String[] template = buildColumnNameTokens(line);
111 
112                 line = StringUtil.replace(
113                     "exec sp_rename '@table@.@old-column@', '@new-column@', " +
114                         "'column';",
115                     REWORD_TEMPLATE, template);
116             }
117             else if (line.startsWith(ALTER_COLUMN_TYPE)) {
118                 String[] template = buildColumnTypeTokens(line);
119 
120                 line = StringUtil.replace(
121                     "alter table @table@ alter column @old-column@ @type@;",
122                     REWORD_TEMPLATE, template);
123             }
124 
125             sb.append(line);
126             sb.append("\n");
127         }
128 
129         br.close();
130 
131         return sb.toString();
132     }
133 
134     private static String[] _SQL_SERVER = {
135         "--", "1", "0",
136         "'19700101'", "GetDate()",
137         " image", " bit", " datetime",
138         " float", " int", " bigint",
139         " nvarchar(2000)", " ntext", " nvarchar",
140         "  identity(1,1)", "go"
141     };
142 
143     private static boolean _SUPPORTS_ALTER_COLUMN_TYPE;
144 
145     private static SQLServerDB _instance = new SQLServerDB();
146 
147 }