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.io.unsync.UnsyncBufferedReader;
019 import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
020 import com.liferay.portal.kernel.util.StringBundler;
021 import com.liferay.portal.kernel.util.StringPool;
022 import com.liferay.portal.kernel.util.StringUtil;
023
024 import java.io.IOException;
025
026
032 public class SybaseDB extends BaseDB {
033
034 public static DB getInstance() {
035 return _instance;
036 }
037
038 @Override
039 public String buildSQL(String template) throws IOException {
040 template = convertTimestamp(template);
041 template = replaceTemplate(template, getTemplate());
042
043 template = reword(template);
044 template = StringUtil.replace(template, ");\n", ")\ngo\n");
045 template = StringUtil.replace(template, "\ngo;\n", "\ngo\n");
046 template = StringUtil.replace(
047 template,
048 new String[] {"\\\\", "\\'", "\\\"", "\\n", "\\r"},
049 new String[] {"\\", "''", "\"", "\n", "\r"});
050
051 return template;
052 }
053
054 @Override
055 public boolean isSupportsInlineDistinct() {
056 return _SUPPORTS_INLINE_DISTINCT;
057 }
058
059 protected SybaseDB() {
060 super(TYPE_SYBASE);
061 }
062
063 @Override
064 protected String buildCreateFileContent(
065 String sqlDir, String databaseName, int population)
066 throws IOException {
067
068 String suffix = getSuffix(population);
069
070 StringBundler sb = new StringBundler(19);
071
072 sb.append("use master\n");
073 sb.append("exec sp_dboption '");
074 sb.append(databaseName);
075 sb.append("', ");
076 sb.append("'allow nulls by default' , true\n");
077 sb.append("go\n\n");
078 sb.append("exec sp_dboption '");
079 sb.append(databaseName);
080 sb.append("', ");
081 sb.append("'select into/bulkcopy/pllsort' , true\n");
082 sb.append("go\n\n");
083
084 if (population != BARE) {
085 sb.append("use ");
086 sb.append(databaseName);
087 sb.append("\n\n");
088 sb.append(getCreateTablesContent(sqlDir, suffix));
089 sb.append("\n\n");
090 sb.append(readFile(sqlDir + "/indexes/indexes-sybase.sql"));
091 sb.append("\n\n");
092 sb.append(readFile(sqlDir + "/sequences/sequences-sybase.sql"));
093 }
094
095 return sb.toString();
096 }
097
098 @Override
099 protected String getServerName() {
100 return "sybase";
101 }
102
103 @Override
104 protected String[] getTemplate() {
105 return _SYBASE;
106 }
107
108 @Override
109 protected String reword(String data) throws IOException {
110 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
111 new UnsyncStringReader(data));
112
113 StringBundler sb = new StringBundler();
114
115 String line = null;
116
117 while ((line = unsyncBufferedReader.readLine()) != null) {
118 if (line.contains(DROP_COLUMN)) {
119 line = StringUtil.replace(line, " drop column ", " drop ");
120 }
121
122 if (line.startsWith(ALTER_COLUMN_NAME)) {
123 String[] template = buildColumnNameTokens(line);
124
125 line = StringUtil.replace(
126 "exec sp_rename '@table@.@old-column@', '@new-column@', " +
127 "'column';",
128 REWORD_TEMPLATE, template);
129 }
130 else if (line.startsWith(ALTER_COLUMN_TYPE)) {
131 String[] template = buildColumnTypeTokens(line);
132
133 line = StringUtil.replace(
134 "alter table @table@ modify @old-column@ @type@;",
135 REWORD_TEMPLATE, template);
136 }
137
138 else if (line.startsWith(ALTER_TABLE_NAME)) {
139 String[] template = buildTableNameTokens(line);
140
141 line = StringUtil.replace(
142 "exec sp_rename @old-table@, @new-table@;",
143 RENAME_TABLE_TEMPLATE, template);
144 }
145 else if (line.contains(DROP_INDEX)) {
146 String[] tokens = StringUtil.split(line, ' ');
147
148 String tableName = tokens[4];
149
150 if (tableName.endsWith(StringPool.SEMICOLON)) {
151 tableName = tableName.substring(0, tableName.length() - 1);
152 }
153
154 line = StringUtil.replace(
155 "drop index @table@.@index@;", "@table@", tableName);
156 line = StringUtil.replace(line, "@index@", tokens[2]);
157 }
158
159 sb.append(line);
160 sb.append("\n");
161 }
162
163 unsyncBufferedReader.close();
164
165 return sb.toString();
166 }
167
168 protected static final String DROP_COLUMN = "drop column";
169
170 private static final boolean _SUPPORTS_INLINE_DISTINCT = false;
171
172 private static final String[] _SYBASE = {
173 "--", "1", "0", "'19700101'", "getdate()", " image", " image", " int",
174 " datetime", " float", " int", " decimal(20,0)", " varchar(1000)",
175 " text", " varchar", " identity(1,1)", "go"
176 };
177
178 private static SybaseDB _instance = new SybaseDB();
179
180 }