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