1
19
20 package com.liferay.portal.tools.sql;
21
22 import com.liferay.portal.kernel.log.Log;
23 import com.liferay.portal.kernel.log.LogFactoryUtil;
24 import com.liferay.portal.kernel.util.StringUtil;
25
26 import java.io.BufferedReader;
27 import java.io.IOException;
28 import java.io.StringReader;
29
30
36 public class IngresUtil extends DBUtil {
37
38 public static DBUtil getInstance() {
39 return _instance;
40 }
41
42 public String buildSQL(String template) throws IOException {
43 template = convertTimestamp(template);
44 template = replaceTemplate(template, getTemplate());
45
46 template = reword(template);
47 template = StringUtil.replace(template, "\\n", "'+x'0a'+'");
48
49 return template;
50 }
51
52 public boolean isSupportsAlterColumnName() {
53 return _SUPPORTS_ALTER_COLUMN_NAME;
54 }
55
56 protected IngresUtil() {
57 super(TYPE_INGRES);
58 }
59
60 protected void buildCreateFile(String databaseName, boolean minimal) {
61 }
62
63 protected String getServerName() {
64 return "ingres";
65 }
66
67 protected String[] getTemplate() {
68 return _INGRES;
69 }
70
71 protected String replaceTemplate(String template, String[] actual) {
72 if ((template == null) || (TEMPLATE == null) || (actual == null)) {
73 return null;
74 }
75
76 if (TEMPLATE.length != actual.length) {
77 return template;
78 }
79
80 for (int i = 0; i < TEMPLATE.length; i++) {
81 if (TEMPLATE[i].equals("##") ||
82 TEMPLATE[i].equals("'01/01/1970'")) {
83
84 template = template.replaceAll(TEMPLATE[i], actual[i]);
85 }
86 else if (TEMPLATE[i].equals("COMMIT_TRANSACTION")) {
87 template = StringUtil.replace(
88 template, TEMPLATE[i] + ";", actual[i]);
89 }
90 else {
91 template = template.replaceAll(
92 "\\b" + TEMPLATE[i] + "\\b", actual[i]);
93 }
94 }
95
96 return template;
97 }
98
99 protected String reword(String data) throws IOException {
100 BufferedReader br = new BufferedReader(new StringReader(data));
101
102 StringBuilder sb = new StringBuilder();
103
104 String line = null;
105
106 while ((line = br.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_PRIMARY_KEY) != -1) {
123 String[] tokens = StringUtil.split(line, " ");
124
125 line = StringUtil.replace(
126 "alter table @table@ drop constraint @table@_pkey;",
127 "@table@", tokens[2]);
128 }
129
130 sb.append(line);
131 sb.append("\n");
132 }
133
134 br.close();
135
136 return sb.toString();
137 }
138
139 private static String[] _INGRES = {
140 "--", "1", "0",
141 "'1970-01-01'", "date('now')",
142 " byte varying", " tinyint", " timestamp",
143 " float", " integer", " bigint",
144 " varchar(1000)", " long varchar", " varchar",
145 "", "commit;\\g"
146 };
147
148 private static boolean _SUPPORTS_ALTER_COLUMN_NAME;
149
150 private static Log _log = LogFactoryUtil.getLog(IngresUtil.class);
151
152 private static IngresUtil _instance = new IngresUtil();
153
154 }