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