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