001
014
015 package com.liferay.portal.dao.db;
016
017 import com.liferay.portal.kernel.dao.db.DB;
018 import com.liferay.portal.kernel.dao.db.Index;
019 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
020 import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
021 import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
022 import com.liferay.portal.kernel.util.StringBundler;
023 import com.liferay.portal.kernel.util.StringPool;
024 import com.liferay.portal.kernel.util.StringUtil;
025
026 import java.io.IOException;
027
028 import java.sql.Connection;
029 import java.sql.DatabaseMetaData;
030 import java.sql.PreparedStatement;
031 import java.sql.ResultSet;
032 import java.sql.SQLException;
033
034 import java.util.ArrayList;
035 import java.util.List;
036
037
042 public class SQLServerDB extends BaseDB {
043
044 public static DB getInstance() {
045 return _instance;
046 }
047
048 @Override
049 public String buildSQL(String template) throws IOException {
050 template = convertTimestamp(template);
051 template = replaceTemplate(template, getTemplate());
052
053 template = reword(template);
054 template = StringUtil.replace(template, "\ngo;\n", "\ngo\n");
055 template = StringUtil.replace(
056 template,
057 new String[] {"\\\\", "\\'", "\\\"", "\\n", "\\r"},
058 new String[] {"\\", "''", "\"", "\n", "\r"});
059
060 return template;
061 }
062
063 @Override
064 public List<Index> getIndexes(Connection con) throws SQLException {
065 List<Index> indexes = new ArrayList<Index>();
066
067 PreparedStatement ps = null;
068 ResultSet rs = null;
069
070 try {
071 DatabaseMetaData databaseMetaData = con.getMetaData();
072
073 if (databaseMetaData.getDatabaseMajorVersion() <=
074 _SQL_SERVER_2000) {
075
076 return indexes;
077 }
078
079 StringBundler sb = new StringBundler(6);
080
081 sb.append("select sys.tables.name as table_name, ");
082 sb.append("sys.indexes.name as index_name, is_unique from ");
083 sb.append("sys.indexes inner join sys.tables on ");
084 sb.append("sys.tables.object_id = sys.indexes.object_id where ");
085 sb.append("sys.indexes.name like 'LIFERAY_%' or sys.indexes.name ");
086 sb.append("like 'IX_%'");
087
088 String sql = sb.toString();
089
090 ps = con.prepareStatement(sql);
091
092 rs = ps.executeQuery();
093
094 while (rs.next()) {
095 String indexName = rs.getString("index_name");
096 String tableName = rs.getString("table_name");
097 boolean unique = !rs.getBoolean("is_unique");
098
099 indexes.add(new Index(indexName, tableName, unique));
100 }
101 }
102 finally {
103 DataAccess.cleanUp(null, ps, rs);
104 }
105
106 return indexes;
107 }
108
109 @Override
110 public boolean isSupportsAlterColumnType() {
111 return _SUPPORTS_ALTER_COLUMN_TYPE;
112 }
113
114 protected SQLServerDB() {
115 super(TYPE_SQLSERVER);
116 }
117
118 @Override
119 protected String buildCreateFileContent(
120 String sqlDir, String databaseName, int population)
121 throws IOException {
122
123 String suffix = getSuffix(population);
124
125 StringBundler sb = new StringBundler(17);
126
127 sb.append("drop database ");
128 sb.append(databaseName);
129 sb.append(";\n");
130 sb.append("create database ");
131 sb.append(databaseName);
132 sb.append(";\n");
133 sb.append("\n");
134 sb.append("go\n");
135 sb.append("\n");
136
137 if (population != BARE) {
138 sb.append("use ");
139 sb.append(databaseName);
140 sb.append(";\n\n");
141 sb.append(getCreateTablesContent(sqlDir, suffix));
142 sb.append("\n\n");
143 sb.append(readFile(sqlDir + "/indexes/indexes-sql-server.sql"));
144 sb.append("\n\n");
145 sb.append(readFile(sqlDir + "/sequences/sequences-sql-server.sql"));
146 }
147
148 return sb.toString();
149 }
150
151 @Override
152 protected String getServerName() {
153 return "sql-server";
154 }
155
156 @Override
157 protected String[] getTemplate() {
158 return _SQL_SERVER;
159 }
160
161 @Override
162 protected String reword(String data) throws IOException {
163 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
164 new UnsyncStringReader(data));
165
166 StringBundler sb = new StringBundler();
167
168 String line = null;
169
170 while ((line = unsyncBufferedReader.readLine()) != null) {
171 if (line.startsWith(ALTER_COLUMN_NAME)) {
172 String[] template = buildColumnNameTokens(line);
173
174 line = StringUtil.replace(
175 "exec sp_rename '@table@.@old-column@', '@new-column@', " +
176 "'column';",
177 REWORD_TEMPLATE, template);
178 }
179 else if (line.startsWith(ALTER_COLUMN_TYPE)) {
180 String[] template = buildColumnTypeTokens(line);
181
182 line = StringUtil.replace(
183 "alter table @table@ alter column @old-column@ @type@;",
184 REWORD_TEMPLATE, template);
185 }
186 else if (line.startsWith(ALTER_TABLE_NAME)) {
187 String[] template = buildTableNameTokens(line);
188
189 line = StringUtil.replace(
190 "exec sp_rename '@old-table@', '@new-table@';",
191 RENAME_TABLE_TEMPLATE, template);
192 }
193 else if (line.contains(DROP_INDEX)) {
194 String[] tokens = StringUtil.split(line, ' ');
195
196 String tableName = tokens[4];
197
198 if (tableName.endsWith(StringPool.SEMICOLON)) {
199 tableName = tableName.substring(0, tableName.length() - 1);
200 }
201
202 line = StringUtil.replace(
203 "drop index @table@.@index@;", "@table@", tableName);
204 line = StringUtil.replace(line, "@index@", tokens[2]);
205 }
206
207 sb.append(line);
208 sb.append("\n");
209 }
210
211 unsyncBufferedReader.close();
212
213 return sb.toString();
214 }
215
216 private static final String[] _SQL_SERVER = {
217 "--", "1", "0", "'19700101'", "GetDate()", " image", " image", " bit",
218 " datetime", " float", " int", " bigint", " nvarchar(2000)",
219 " nvarchar(max)", " nvarchar", " identity(1,1)", "go"
220 };
221
222 private static final int _SQL_SERVER_2000 = 8;
223
224 private static final boolean _SUPPORTS_ALTER_COLUMN_TYPE = false;
225
226 private static SQLServerDB _instance = new SQLServerDB();
227
228 }