1
22
23 package com.liferay.portal.tools.sql;
24
25 import com.liferay.portal.kernel.util.StringMaker;
26 import com.liferay.portal.kernel.util.StringUtil;
27 import com.liferay.util.FileUtil;
28
29 import java.io.BufferedReader;
30 import java.io.File;
31 import java.io.IOException;
32 import java.io.StringReader;
33
34
40 public class SQLServerUtil extends DBUtil {
41
42 public static DBUtil getInstance() {
43 return _instance;
44 }
45
46 public String buildSQL(String template) throws IOException {
47 template = convertTimestamp(template);
48 template = StringUtil.replace(template, TEMPLATE, getTemplate());
49
50 template = reword(template);
51 template = StringUtil.replace(template, "\ngo;\n", "\ngo\n");
52 template = StringUtil.replace(
53 template,
54 new String[] {"\\\\", "\\'", "\\\"", "\\n", "\\r"},
55 new String[] {"\\", "''", "\"", "\n", "\r"});
56
57 return template;
58 }
59
60 protected SQLServerUtil() {
61 }
62
63 protected void buildCreateFile(String databaseName, boolean minimal)
64 throws IOException {
65
66 String minimalSuffix = getMinimalSuffix(minimal);
67
68 File file = new File(
69 "../sql/create" + minimalSuffix + "/create" + minimalSuffix +
70 "-sql-server.sql");
71
72 StringMaker sm = new StringMaker();
73
74 sm.append("drop database " + databaseName + ";\n");
75 sm.append("create database " + databaseName + ";\n");
76 sm.append("\n");
77 sm.append("go\n");
78 sm.append("\n");
79 sm.append("use " + databaseName + ";\n\n");
80 sm.append(
81 FileUtil.read(
82 "../sql/portal" + minimalSuffix + "/portal" + minimalSuffix +
83 "-sql-server.sql"));
84 sm.append("\n\n");
85 sm.append(FileUtil.read("../sql/indexes/indexes-sql-server.sql"));
86 sm.append("\n\n");
87 sm.append(FileUtil.read("../sql/sequences/sequences-sql-server.sql"));
88
89 FileUtil.write(file, sm.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 StringMaker sm = new StringMaker();
104
105 String line = null;
106
107 while ((line = br.readLine()) != null) {
108 if (line.startsWith(ALTER_COLUMN_TYPE)) {
109 String[] template = buildColumnTypeTokens(line);
110
111 line = StringUtil.replace(
112 "alter table @table@ alter column @old-column@ @type@;",
113 REWORD_TEMPLATE, template);
114 }
115 else if (line.startsWith(ALTER_COLUMN_NAME)) {
116 String[] template = buildColumnNameTokens(line);
117
118 line = StringUtil.replace(
119 "exec sp_rename '@table@.@old-column@', '@new-column@', " +
120 "'column';",
121 REWORD_TEMPLATE, template);
122 }
123
124 sm.append(line);
125 sm.append("\n");
126 }
127
128 br.close();
129
130 return sm.toString();
131 }
132
133 private static String[] _SQL_SERVER = {
134 "--", "1", "0",
135 "'19700101'", "GetDate()",
136 " bit", " datetime", " float",
137 " int", " bigint",
138 " varchar(2000)", " text", " varchar",
139 " identity(1,1)", "go"
140 };
141
142 private static SQLServerUtil _instance = new SQLServerUtil();
143
144 }