1
22
23 package com.liferay.portal.tools.sql;
24
25 import com.liferay.portal.kernel.util.StringMaker;
26 import com.liferay.portal.kernel.util.StringUtil;
27 import com.liferay.util.FileUtil;
28
29 import java.io.BufferedReader;
30 import java.io.File;
31 import java.io.IOException;
32 import java.io.StringReader;
33
34
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 = StringUtil.replace(template, TEMPLATE, getTemplate());
49
50 template = reword(template);
51 template = StringUtil.replace(template, "\\'", "''");
52
53 return template;
54 }
55
56 protected MySQLUtil() {
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 StringMaker sm = new StringMaker();
69
70 sm.append("drop database if exists " + databaseName + ";\n");
71 sm.append("create database " + databaseName + " character set utf8;\n");
72 sm.append("use ");
73 sm.append(databaseName);
74 sm.append(";\n\n");
75 sm.append(
76 FileUtil.read(
77 "../sql/portal" + minimalSuffix + "/portal" + minimalSuffix +
78 "-mysql.sql"));
79 sm.append("\n\n");
80 sm.append(FileUtil.read("../sql/indexes/indexes-mysql.sql"));
81 sm.append("\n\n");
82 sm.append(FileUtil.read("../sql/sequences/sequences-mysql.sql"));
83
84 FileUtil.write(file, sm.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 StringMaker sm = new StringMaker();
99
100 String line = null;
101
102 while ((line = br.readLine()) != null) {
103 if (line.startsWith(ALTER_COLUMN_TYPE)) {
104 String[] template = buildColumnTypeTokens(line);
105
106 line = StringUtil.replace(
107 "alter table @table@ modify @old-column@ @type@;",
108 REWORD_TEMPLATE, template);
109 }
110 else if (line.startsWith(ALTER_COLUMN_NAME)) {
111 String[] template = buildColumnNameTokens(line);
112
113 line = StringUtil.replace(
114 "alter table @table@ change column @old-column@ " +
115 "@new-column@ @type@;",
116 REWORD_TEMPLATE, template);
117 }
118
119 sm.append(line);
120 sm.append("\n");
121 }
122
123 br.close();
124
125 return sm.toString();
126 }
127
128 private static String[] _MYSQL = {
129 "##", "1", "0",
130 "'1970-01-01'", "now()",
131 " tinyint", " datetime", " double",
132 " integer", " bigint",
133 " longtext", " longtext", " varchar",
134 " auto_increment", "commit"
135 };
136
137 private static MySQLUtil _instance = new MySQLUtil();
138
139 }