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