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 protected SQLServerUtil() {
72 super(TYPE_SQLSERVER);
73 }
74
75 protected void buildCreateFile(String databaseName, boolean minimal)
76 throws IOException {
77
78 String minimalSuffix = getMinimalSuffix(minimal);
79
80 File file = new File(
81 "../sql/create" + minimalSuffix + "/create" + minimalSuffix +
82 "-sql-server.sql");
83
84 StringBuilder sb = new StringBuilder();
85
86 sb.append("drop database " + databaseName + ";\n");
87 sb.append("create database " + databaseName + ";\n");
88 sb.append("\n");
89 sb.append("go\n");
90 sb.append("\n");
91 sb.append("use " + databaseName + ";\n\n");
92 sb.append(
93 FileUtil.read(
94 "../sql/portal" + minimalSuffix + "/portal" + minimalSuffix +
95 "-sql-server.sql"));
96 sb.append("\n\n");
97 sb.append(FileUtil.read("../sql/indexes/indexes-sql-server.sql"));
98 sb.append("\n\n");
99 sb.append(FileUtil.read("../sql/sequences/sequences-sql-server.sql"));
100
101 FileUtil.write(file, sb.toString());
102 }
103
104 protected String getServerName() {
105 return "sql-server";
106 }
107
108 protected String[] getTemplate() {
109 return _SQL_SERVER;
110 }
111
112 protected String reword(String data) throws IOException {
113 BufferedReader br = new BufferedReader(new StringReader(data));
114
115 StringBuilder sb = new StringBuilder();
116
117 String line = null;
118
119 while ((line = br.readLine()) != null) {
120 if (line.startsWith(ALTER_COLUMN_NAME)) {
121 String[] template = buildColumnNameTokens(line);
122
123 line = StringUtil.replace(
124 "exec sp_rename '@table@.@old-column@', '@new-column@', " +
125 "'column';",
126 REWORD_TEMPLATE, template);
127 }
128 else if (line.startsWith(ALTER_COLUMN_TYPE)) {
129 String[] template = buildColumnTypeTokens(line);
130
131 line = StringUtil.replace(
132 "alter table @table@ alter column @old-column@ @type@;",
133 REWORD_TEMPLATE, template);
134 }
135
136 sb.append(line);
137 sb.append("\n");
138 }
139
140 br.close();
141
142 return sb.toString();
143 }
144
145 private static String[] _SQL_SERVER = {
146 "--", "1", "0",
147 "'19700101'", "GetDate()",
148 " image", " bit", " datetime",
149 " float", " int", " bigint",
150 " varchar(2000)", " text", " varchar",
151 " identity(1,1)", "go"
152 };
153
154 private static boolean _SUPPORTS_ALTER_COLUMN_NAME;
155
156 private static SQLServerUtil _instance = new SQLServerUtil();
157
158 }