1
14
15 package com.liferay.portal.dao.db;
16
17 import com.liferay.portal.kernel.dao.db.DB;
18 import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
19 import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
20 import com.liferay.portal.kernel.log.Log;
21 import com.liferay.portal.kernel.log.LogFactoryUtil;
22 import com.liferay.portal.kernel.util.StringBundler;
23 import com.liferay.portal.kernel.util.StringUtil;
24
25 import java.io.IOException;
26
27
32 public class IngresDB extends BaseDB {
33
34 public static DB getInstance() {
35 return _instance;
36 }
37
38 public String buildSQL(String template) throws IOException {
39 template = convertTimestamp(template);
40 template = replaceTemplate(template, getTemplate());
41
42 template = reword(template);
43 template = StringUtil.replace(template, "\\n", "'+x'0a'+'");
44
45 return template;
46 }
47
48 public boolean isSupportsAlterColumnName() {
49 return _SUPPORTS_ALTER_COLUMN_NAME;
50 }
51
52 protected IngresDB() {
53 super(TYPE_INGRES);
54 }
55
56 protected String buildCreateFileContent(
57 String sqlDir, String databaseName, int population) {
58
59 return null;
60 }
61
62 protected String getServerName() {
63 return "ingres";
64 }
65
66 protected String[] getTemplate() {
67 return _INGRES;
68 }
69
70 protected String replaceTemplate(String template, String[] actual) {
71 if ((template == null) || (TEMPLATE == null) || (actual == null)) {
72 return null;
73 }
74
75 if (TEMPLATE.length != actual.length) {
76 return template;
77 }
78
79 for (int i = 0; i < TEMPLATE.length; i++) {
80 if (TEMPLATE[i].equals("##") ||
81 TEMPLATE[i].equals("'01/01/1970'")) {
82
83 template = template.replaceAll(TEMPLATE[i], actual[i]);
84 }
85 else if (TEMPLATE[i].equals("COMMIT_TRANSACTION")) {
86 template = StringUtil.replace(
87 template, TEMPLATE[i] + ";", actual[i]);
88 }
89 else {
90 template = template.replaceAll(
91 "\\b" + TEMPLATE[i] + "\\b", actual[i]);
92 }
93 }
94
95 return template;
96 }
97
98 protected String reword(String data) throws IOException {
99 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
100 new UnsyncStringReader(data));
101
102 StringBundler sb = new StringBundler();
103
104 String line = null;
105
106 while ((line = unsyncBufferedReader.readLine()) != null) {
107 if (line.startsWith(ALTER_COLUMN_NAME)) {
108 line = "-- " + line;
109
110 if (_log.isWarnEnabled()) {
111 _log.warn(
112 "This statement is not supported by Ingres: " + line);
113 }
114 }
115 else if (line.startsWith(ALTER_COLUMN_TYPE)) {
116 String[] template = buildColumnTypeTokens(line);
117
118 line = StringUtil.replace(
119 "alter table @table@ alter @old-column@ @type@;",
120 REWORD_TEMPLATE, template);
121 }
122 else if (line.indexOf(DROP_INDEX) != -1) {
123 String[] tokens = StringUtil.split(line, " ");
124
125 line = StringUtil.replace(
126 "drop index @index@;", "@index@", tokens[2]);
127 }
128 else if (line.indexOf(DROP_PRIMARY_KEY) != -1) {
129 String[] tokens = StringUtil.split(line, " ");
130
131 line = StringUtil.replace(
132 "alter table @table@ drop constraint @table@_pkey;",
133 "@table@", 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 private static String[] _INGRES = {
146 "--", "1", "0",
147 "'1970-01-01'", "date('now')",
148 " byte varying", " tinyint", " timestamp",
149 " float", " integer", " bigint",
150 " varchar(1000)", " long varchar", " varchar",
151 "", "commit;\\g"
152 };
153
154 private static boolean _SUPPORTS_ALTER_COLUMN_NAME;
155
156 private static Log _log = LogFactoryUtil.getLog(IngresDB.class);
157
158 private static IngresDB _instance = new IngresDB();
159
160 }