1
22
23 package com.liferay.portal.tools.sql;
24
25 import com.liferay.portal.kernel.util.FileUtil;
26 import com.liferay.portal.kernel.util.StringUtil;
27
28 import java.io.BufferedReader;
29 import java.io.File;
30 import java.io.IOException;
31 import java.io.StringReader;
32
33
41 public class SQLServerUtil extends DBUtil {
42
43 public static DBUtil 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, "\ngo;\n", "\ngo\n");
53 template = StringUtil.replace(
54 template,
55 new String[] {"\\\\", "\\'", "\\\"", "\\n", "\\r"},
56 new String[] {"\\", "''", "\"", "\n", "\r"});
57
58 return template;
59 }
60
61 public boolean isSupportsAlterColumnName() {
62
63
68 return _SUPPORTS_ALTER_COLUMN_NAME;
69 }
70
71 public boolean isSupportsAlterColumnType() {
72 return _SUPPORTS_ALTER_COLUMN_TYPE;
73 }
74
75 protected SQLServerUtil() {
76 super(TYPE_SQLSERVER);
77 }
78
79 protected void buildCreateFile(String databaseName, boolean minimal)
80 throws IOException {
81
82 String minimalSuffix = getMinimalSuffix(minimal);
83
84 File file = new File(
85 "../sql/create" + minimalSuffix + "/create" + minimalSuffix +
86 "-sql-server.sql");
87
88 StringBuilder sb = new StringBuilder();
89
90 sb.append("drop database " + databaseName + ";\n");
91 sb.append("create database " + databaseName + ";\n");
92 sb.append("\n");
93 sb.append("go\n");
94 sb.append("\n");
95 sb.append("use " + databaseName + ";\n\n");
96 sb.append(
97 FileUtil.read(
98 "../sql/portal" + minimalSuffix + "/portal" + minimalSuffix +
99 "-sql-server.sql"));
100 sb.append("\n\n");
101 sb.append(FileUtil.read("../sql/indexes/indexes-sql-server.sql"));
102 sb.append("\n\n");
103 sb.append(FileUtil.read("../sql/sequences/sequences-sql-server.sql"));
104
105 FileUtil.write(file, sb.toString());
106 }
107
108 protected String getServerName() {
109 return "sql-server";
110 }
111
112 protected String[] getTemplate() {
113 return _SQL_SERVER;
114 }
115
116 protected String reword(String data) throws IOException {
117 BufferedReader br = new BufferedReader(new StringReader(data));
118
119 StringBuilder sb = new StringBuilder();
120
121 String line = null;
122
123 while ((line = br.readLine()) != null) {
124 if (line.startsWith(ALTER_COLUMN_NAME)) {
125 String[] template = buildColumnNameTokens(line);
126
127 line = StringUtil.replace(
128 "exec sp_rename '@table@.@old-column@', '@new-column@', " +
129 "'column';",
130 REWORD_TEMPLATE, template);
131 }
132 else if (line.startsWith(ALTER_COLUMN_TYPE)) {
133 String[] template = buildColumnTypeTokens(line);
134
135 line = StringUtil.replace(
136 "alter table @table@ alter column @old-column@ @type@;",
137 REWORD_TEMPLATE, template);
138 }
139
140 sb.append(line);
141 sb.append("\n");
142 }
143
144 br.close();
145
146 return sb.toString();
147 }
148
149 private static String[] _SQL_SERVER = {
150 "--", "1", "0",
151 "'19700101'", "GetDate()",
152 " image", " bit", " datetime",
153 " float", " int", " bigint",
154 " varchar(2000)", " text", " varchar",
155 " identity(1,1)", "go"
156 };
157
158 private static boolean _SUPPORTS_ALTER_COLUMN_NAME;
159
160 private static boolean _SUPPORTS_ALTER_COLUMN_TYPE;
161
162 private static SQLServerUtil _instance = new SQLServerUtil();
163
164 }