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