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