1
22
23 package com.liferay.portal.tools.sql;
24
25 import com.liferay.portal.kernel.util.FileUtil;
26 import com.liferay.portal.kernel.util.StringUtil;
27 import com.liferay.portal.util.PropsValues;
28
29 import java.io.BufferedReader;
30 import java.io.IOException;
31 import java.io.StringReader;
32
33
40 public class MySQLUtil extends DBUtil {
41
42 public static DBUtil getInstance() {
43 return _instance;
44 }
45
46 public String buildSQL(String template) throws IOException {
47 template = convertTimestamp(template);
48 template = replaceTemplate(template, getTemplate());
49
50 template = reword(template);
51 template = StringUtil.replace(template, "\\'", "''");
52
53 return template;
54 }
55
56 public boolean isSupportsDateMilliseconds() {
57 return _SUPPORTS_DATE_MILLISECONDS;
58 }
59
60 public boolean isSupportsUpdateWithInnerJoin() {
61 return _SUPPORTS_UPDATE_WITH_INNER_JOIN;
62 }
63
64 protected MySQLUtil() {
65 super(TYPE_MYSQL);
66 }
67
68 protected String buildCreateFileContent(String databaseName, int population)
69 throws IOException {
70
71 String suffix = getSuffix(population);
72
73 StringBuilder sb = new StringBuilder();
74
75 sb.append("drop database if exists " + databaseName + ";\n");
76 sb.append("create database " + databaseName + " character set utf8;\n");
77 sb.append("use ");
78 sb.append(databaseName);
79 sb.append(";\n\n");
80 sb.append(
81 FileUtil.read(
82 "../sql/portal" + suffix + "/portal" + suffix + "-mysql.sql"));
83 sb.append("\n\n");
84 sb.append(FileUtil.read("../sql/indexes/indexes-mysql.sql"));
85 sb.append("\n\n");
86 sb.append(FileUtil.read("../sql/sequences/sequences-mysql.sql"));
87
88 return sb.toString();
89 }
90
91 protected String getServerName() {
92 return "mysql";
93 }
94
95 protected String[] getTemplate() {
96 return _MYSQL;
97 }
98
99 protected String reword(String data) throws IOException {
100 BufferedReader br = new BufferedReader(new StringReader(data));
101
102 boolean createTable = false;
103
104 StringBuilder sb = new StringBuilder();
105
106 String line = null;
107
108 while ((line = br.readLine()) != null) {
109 if (StringUtil.startsWith(line, "create table")) {
110 createTable = true;
111 }
112 else if (line.startsWith(ALTER_COLUMN_NAME)) {
113 String[] template = buildColumnNameTokens(line);
114
115 line = StringUtil.replace(
116 "alter table @table@ change column @old-column@ " +
117 "@new-column@ @type@;",
118 REWORD_TEMPLATE, template);
119 }
120 else if (line.startsWith(ALTER_COLUMN_TYPE)) {
121 String[] template = buildColumnTypeTokens(line);
122
123 line = StringUtil.replace(
124 "alter table @table@ modify @old-column@ @type@;",
125 REWORD_TEMPLATE, template);
126 }
127
128 int pos = line.indexOf(";");
129
130 if (createTable && (pos != -1)) {
131 createTable = false;
132
133 line =
134 line.substring(0, pos) + " engine " +
135 PropsValues.DATABASE_MYSQL_ENGINE + line.substring(pos);
136 }
137
138 sb.append(line);
139 sb.append("\n");
140 }
141
142 br.close();
143
144 return sb.toString();
145 }
146
147 private static String[] _MYSQL = {
148 "##", "1", "0",
149 "'1970-01-01'", "now()",
150 " blob", " tinyint", " datetime",
151 " double", " integer", " bigint",
152 " longtext", " longtext", " varchar",
153 " auto_increment", "commit"
154 };
155
156 private static boolean _SUPPORTS_DATE_MILLISECONDS = false;
157
158 private static boolean _SUPPORTS_UPDATE_WITH_INNER_JOIN = true;
159
160 private static MySQLUtil _instance = new MySQLUtil();
161
162 }