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