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
28 import java.io.BufferedReader;
29 import java.io.File;
30 import java.io.IOException;
31 import java.io.StringReader;
32
33
41 public class MySQLUtil extends DBUtil {
42
43 public static DBUtil getInstance() {
44 return _instance;
45 }
46
47 public String buildSQL(String template) throws IOException {
48 template = convertTimestamp(template);
49 template = replaceTemplate(template, getTemplate());
50
51 template = reword(template);
52 template = StringUtil.replace(template, "\\'", "''");
53
54 return template;
55 }
56
57 protected MySQLUtil() {
58 super(DB_TYPE_MYSQL);
59 }
60
61 protected void buildCreateFile(String databaseName, boolean minimal)
62 throws IOException {
63
64 String minimalSuffix = getMinimalSuffix(minimal);
65
66 File file = new File(
67 "../sql/create" + minimalSuffix + "/create" + minimalSuffix +
68 "-mysql.sql");
69
70 StringBuilder sb = new StringBuilder();
71
72 sb.append("drop database if exists " + databaseName + ";\n");
73 sb.append("create database " + databaseName + " character set utf8;\n");
74 sb.append("use ");
75 sb.append(databaseName);
76 sb.append(";\n\n");
77 sb.append(
78 FileUtil.read(
79 "../sql/portal" + minimalSuffix + "/portal" + minimalSuffix +
80 "-mysql.sql"));
81 sb.append("\n\n");
82 sb.append(FileUtil.read("../sql/indexes/indexes-mysql.sql"));
83 sb.append("\n\n");
84 sb.append(FileUtil.read("../sql/sequences/sequences-mysql.sql"));
85
86 FileUtil.write(file, sb.toString());
87 }
88
89 protected String getServerName() {
90 return "mysql";
91 }
92
93 protected String[] getTemplate() {
94 return _MYSQL;
95 }
96
97 protected String reword(String data) throws IOException {
98 BufferedReader br = new BufferedReader(new StringReader(data));
99
100 StringBuilder sb = new StringBuilder();
101
102 String line = null;
103
104 while ((line = br.readLine()) != null) {
105 if (line.startsWith(ALTER_COLUMN_TYPE)) {
106 String[] template = buildColumnTypeTokens(line);
107
108 line = StringUtil.replace(
109 "alter table @table@ modify @old-column@ @type@;",
110 REWORD_TEMPLATE, template);
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
121 sb.append(line);
122 sb.append("\n");
123 }
124
125 br.close();
126
127 return sb.toString();
128 }
129
130 private static String[] _MYSQL = {
131 "##", "1", "0",
132 "'1970-01-01'", "now()",
133 " blob", " tinyint", " datetime",
134 " double", " integer", " bigint",
135 " longtext", " longtext", " varchar",
136 " auto_increment", "commit"
137 };
138
139 private static MySQLUtil _instance = new MySQLUtil();
140
141 }