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.log.Log;
021 import com.liferay.portal.kernel.log.LogFactoryUtil;
022 import com.liferay.portal.kernel.util.StringBundler;
023 import com.liferay.portal.kernel.util.StringUtil;
024
025 import java.io.IOException;
026
027
030 public class IngresDB extends BaseDB {
031
032 public static DB getInstance() {
033 return _instance;
034 }
035
036 @Override
037 public String buildSQL(String template) throws IOException {
038 template = convertTimestamp(template);
039 template = replaceTemplate(template, getTemplate());
040
041 template = reword(template);
042 template = StringUtil.replace(template, "\\n", "'+x'0a'+'");
043
044 return template;
045 }
046
047 @Override
048 public boolean isSupportsAlterColumnName() {
049 return _SUPPORTS_ALTER_COLUMN_NAME;
050 }
051
052 protected IngresDB() {
053 super(TYPE_INGRES);
054 }
055
056 @Override
057 protected String buildCreateFileContent(
058 String sqlDir, String databaseName, int population) {
059
060 return null;
061 }
062
063 @Override
064 protected String getServerName() {
065 return "ingres";
066 }
067
068 @Override
069 protected String[] getTemplate() {
070 return _INGRES;
071 }
072
073 @Override
074 protected String replaceTemplate(String template, String[] actual) {
075 if ((template == null) || (TEMPLATE == null) || (actual == null)) {
076 return null;
077 }
078
079 if (TEMPLATE.length != actual.length) {
080 return template;
081 }
082
083 for (int i = 0; i < TEMPLATE.length; i++) {
084 if (TEMPLATE[i].equals("##") ||
085 TEMPLATE[i].equals("'01/01/1970'")) {
086
087 template = template.replaceAll(TEMPLATE[i], actual[i]);
088 }
089 else if (TEMPLATE[i].equals("COMMIT_TRANSACTION")) {
090 template = StringUtil.replace(
091 template, TEMPLATE[i] + ";", actual[i]);
092 }
093 else {
094 template = template.replaceAll(
095 "\\b" + TEMPLATE[i] + "\\b", actual[i]);
096 }
097 }
098
099 return template;
100 }
101
102 @Override
103 protected String reword(String data) throws IOException {
104 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
105 new UnsyncStringReader(data));
106
107 StringBundler sb = new StringBundler();
108
109 String line = null;
110
111 while ((line = unsyncBufferedReader.readLine()) != null) {
112 if (line.startsWith(ALTER_COLUMN_NAME)) {
113 line = "-- " + line;
114
115 if (_log.isWarnEnabled()) {
116 _log.warn(
117 "This statement is not supported by Ingres: " + line);
118 }
119 }
120 else if (line.startsWith(ALTER_COLUMN_TYPE)) {
121 String[] template = buildColumnTypeTokens(line);
122
123 line = StringUtil.replace(
124 "alter table @table@ alter @old-column@ @type@;",
125 REWORD_TEMPLATE, template);
126 }
127 else if (line.indexOf(DROP_INDEX) != -1) {
128 String[] tokens = StringUtil.split(line, ' ');
129
130 line = StringUtil.replace(
131 "drop index @index@;", "@index@", tokens[2]);
132 }
133 else if (line.indexOf(DROP_PRIMARY_KEY) != -1) {
134 String[] tokens = StringUtil.split(line, ' ');
135
136 line = StringUtil.replace(
137 "alter table @table@ drop constraint @table@_pkey;",
138 "@table@", tokens[2]);
139 }
140
141 sb.append(line);
142 sb.append("\n");
143 }
144
145 unsyncBufferedReader.close();
146
147 return sb.toString();
148 }
149
150 private static final String[] _INGRES = {
151 "--", "1", "0", "'1970-01-01'", "date('now')", " blob", " blob",
152 " tinyint", " timestamp", " float", " integer", " bigint",
153 " varchar(1000)", " long varchar", " varchar", "", "commit;\\g"
154 };
155
156 private static final boolean _SUPPORTS_ALTER_COLUMN_NAME = false;
157
158 private static Log _log = LogFactoryUtil.getLog(IngresDB.class);
159
160 private static IngresDB _instance = new IngresDB();
161
162 }