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