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.FileUtil;
021 import com.liferay.portal.kernel.util.StringBundler;
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 public String buildSQL(String template) throws IOException {
039 template = convertTimestamp(template);
040 template = replaceTemplate(template, getTemplate());
041
042 template = reword(template);
043 template = StringUtil.replace(template, ");\n", ")\ngo\n");
044 template = StringUtil.replace(template, "\ngo;\n", "\ngo\n");
045 template = StringUtil.replace(
046 template,
047 new String[] {"\\\\", "\\'", "\\\"", "\\n", "\\r"},
048 new String[] {"\\", "''", "\"", "\n", "\r"});
049
050 return template;
051 }
052
053 protected SybaseDB() {
054 super(TYPE_SYBASE);
055 }
056
057 protected String buildCreateFileContent(
058 String sqlDir, String databaseName, int population)
059 throws IOException {
060
061 String suffix = getSuffix(population);
062
063 StringBundler sb = new StringBundler(19);
064
065 sb.append("use master\n");
066 sb.append("exec sp_dboption '");
067 sb.append(databaseName);
068 sb.append("', ");
069 sb.append("'allow nulls by default' , true\n");
070 sb.append("go\n\n");
071 sb.append("exec sp_dboption '");
072 sb.append(databaseName);
073 sb.append("', ");
074 sb.append("'select into/bulkcopy/pllsort' , true\n");
075 sb.append("go\n\n");
076
077 sb.append("use ");
078 sb.append(databaseName);
079 sb.append("\n\n");
080 sb.append(
081 FileUtil.read(
082 sqlDir + "/portal" + suffix + "/portal" + suffix +
083 "-sybase.sql"));
084 sb.append("\n\n");
085 sb.append(FileUtil.read(sqlDir + "/indexes/indexes-sybase.sql"));
086 sb.append("\n\n");
087 sb.append(FileUtil.read(sqlDir + "/sequences/sequences-sybase.sql"));
088
089 return sb.toString();
090 }
091
092 protected String getServerName() {
093 return "sybase";
094 }
095
096 protected String[] getTemplate() {
097 return _SYBASE;
098 }
099
100 protected String reword(String data) throws IOException {
101 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
102 new UnsyncStringReader(data));
103
104 StringBundler sb = new StringBundler();
105
106 String line = null;
107
108 while ((line = unsyncBufferedReader.readLine()) != null) {
109 if (line.indexOf(DROP_COLUMN) != -1) {
110 line = StringUtil.replace(line, " drop column ", " drop ");
111 }
112
113 if (line.startsWith(ALTER_COLUMN_NAME)) {
114 String[] template = buildColumnNameTokens(line);
115
116 line = StringUtil.replace(
117 "exec sp_rename '@table@.@old-column@', '@new-column@', " +
118 "'column';",
119 REWORD_TEMPLATE, template);
120 }
121 else if (line.startsWith(ALTER_COLUMN_TYPE)) {
122 String[] template = buildColumnTypeTokens(line);
123
124 line = StringUtil.replace(
125 "alter table @table@ alter column @old-column@ @type@;",
126 REWORD_TEMPLATE, template);
127 }
128 else if (line.indexOf(DROP_INDEX) != -1) {
129 String[] tokens = StringUtil.split(line, " ");
130
131 line = StringUtil.replace(
132 "drop index @table@.@index@;", "@table@", tokens[4]);
133 line = StringUtil.replace(line, "@index@", tokens[2]);
134 }
135
136 sb.append(line);
137 sb.append("\n");
138 }
139
140 unsyncBufferedReader.close();
141
142 return sb.toString();
143 }
144
145 protected static String DROP_COLUMN = "drop column";
146
147 private static String[] _SYBASE = {
148 "--", "1", "0",
149 "'19700101'", "getdate()",
150 " image", " int", " datetime",
151 " float", " int", " decimal(20,0)",
152 " varchar(1000)", " text", " varchar",
153 " identity(1,1)", "go"
154 };
155
156 private static SybaseDB _instance = new SybaseDB();
157
158 }