1
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
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
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 }