1
19
20 package com.liferay.portal.tools.sql;
21
22 import com.liferay.portal.kernel.util.FileUtil;
23 import com.liferay.portal.kernel.util.StringUtil;
24 import com.liferay.portal.util.PropsValues;
25
26 import java.io.BufferedReader;
27 import java.io.File;
28 import java.io.IOException;
29 import java.io.StringReader;
30
31
39 public class MySQLUtil 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, "\\'", "''");
51
52 return template;
53 }
54
55 protected MySQLUtil() {
56 super(DB_TYPE_MYSQL);
57 }
58
59 protected void buildCreateFile(String databaseName, boolean minimal)
60 throws IOException {
61
62 String minimalSuffix = getMinimalSuffix(minimal);
63
64 File file = new File(
65 "../sql/create" + minimalSuffix + "/create" + minimalSuffix +
66 "-mysql.sql");
67
68 StringBuilder sb = new StringBuilder();
69
70 sb.append("drop database if exists " + databaseName + ";\n");
71 sb.append("create database " + databaseName + " character set utf8;\n");
72 sb.append("use ");
73 sb.append(databaseName);
74 sb.append(";\n\n");
75 sb.append(
76 FileUtil.read(
77 "../sql/portal" + minimalSuffix + "/portal" + minimalSuffix +
78 "-mysql.sql"));
79 sb.append("\n\n");
80 sb.append(FileUtil.read("../sql/indexes/indexes-mysql.sql"));
81 sb.append("\n\n");
82 sb.append(FileUtil.read("../sql/sequences/sequences-mysql.sql"));
83
84 FileUtil.write(file, sb.toString());
85 }
86
87 protected String getServerName() {
88 return "mysql";
89 }
90
91 protected String[] getTemplate() {
92 return _MYSQL;
93 }
94
95 protected String reword(String data) throws IOException {
96 BufferedReader br = new BufferedReader(new StringReader(data));
97
98 boolean createTable = false;
99
100 StringBuilder sb = new StringBuilder();
101
102 String line = null;
103
104 while ((line = br.readLine()) != null) {
105 if (StringUtil.startsWith(line, "create table")) {
106 createTable = true;
107 }
108 else if (line.startsWith(ALTER_COLUMN_TYPE)) {
109 String[] template = buildColumnTypeTokens(line);
110
111 line = StringUtil.replace(
112 "alter table @table@ modify @old-column@ @type@;",
113 REWORD_TEMPLATE, template);
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
124 int pos = line.indexOf(";");
125
126 if (createTable && (pos != -1)) {
127 createTable = false;
128
129 line =
130 line.substring(0, pos) + " engine " +
131 PropsValues.DATABASE_MYSQL_ENGINE + line.substring(pos);
132 }
133
134 sb.append(line);
135 sb.append("\n");
136 }
137
138 br.close();
139
140 return sb.toString();
141 }
142
143 private static String[] _MYSQL = {
144 "##", "1", "0",
145 "'1970-01-01'", "now()",
146 " blob", " tinyint", " datetime",
147 " double", " integer", " bigint",
148 " longtext", " longtext", " varchar",
149 " auto_increment", "commit"
150 };
151
152 private static MySQLUtil _instance = new MySQLUtil();
153
154 }