1
19
20 package com.liferay.portal.tools;
21
22 import com.liferay.portal.kernel.util.StringPool;
23 import com.liferay.portal.kernel.util.StringUtil;
24 import com.liferay.portal.kernel.util.Validator;
25 import com.liferay.portal.util.FileImpl;
26
27 import java.io.BufferedReader;
28 import java.io.ByteArrayInputStream;
29 import java.io.ByteArrayOutputStream;
30 import java.io.StringReader;
31
32 import java.sql.Connection;
33 import java.sql.DriverManager;
34 import java.sql.PreparedStatement;
35 import java.sql.Statement;
36
37 import org.apache.derby.tools.ij;
38
39
45 public class DBLoader {
46
47 public static void main(String[] args) {
48 if (args.length == 2) {
49 new DBLoader(args[0], args[1], StringPool.BLANK);
50 }
51 else if (args.length == 3) {
52 new DBLoader(args[0], args[1], args[2]);
53 }
54 else {
55 throw new IllegalArgumentException();
56 }
57 }
58
59 public DBLoader(String databaseType, String databaseName, String fileName) {
60 try {
61 _databaseType = databaseType;
62 _databaseName = databaseName;
63 _fileName = fileName;
64
65 if (_databaseType.equals("derby")) {
66 _loadDerby();
67 }
68 else if (_databaseType.equals("hypersonic")) {
69 _loadHypersonic();
70 }
71 }
72 catch (Exception e) {
73 e.printStackTrace();
74 }
75 }
76
77 private void _loadDerby() throws Exception {
78 Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
79
80 Connection con = DriverManager.getConnection(
81 "jdbc:derby:" + _databaseName + ";create=true", "", "");
82
83 if (Validator.isNull(_fileName)) {
84 _loadDerby(con, "../sql/portal/portal-derby.sql");
85 _loadDerby(con, "../sql/indexes.sql");
86 }
87 else {
88 _loadDerby(con, _fileName);
89 }
90 }
91
92 private void _loadDerby(Connection con, String fileName)
93 throws Exception {
94
95 StringBuilder sb = new StringBuilder();
96
97 BufferedReader br = new BufferedReader(
98 new StringReader(_fileUtil.read(fileName)));
99
100 String line = null;
101
102 while ((line = br.readLine()) != null) {
103 if (!line.startsWith("--")) {
104 sb.append(line);
105
106 if (line.endsWith(";")) {
107 String sql = sb.toString();
108
109 sql =
110 StringUtil.replace(
111 sql,
112 new String[] {
113 "\\'",
114 "\\\"",
115 "\\\\",
116 "\\n",
117 "\\r"
118 },
119 new String[] {
120 "''",
121 "\"",
122 "\\",
123 "\n",
124 "\r"
125 });
126
127 sql = sql.substring(0, sql.length() - 1);
128
129 sb = new StringBuilder();
130
131 if (sql.startsWith("commit")) {
132 continue;
133 }
134
135 ij.runScript(
136 con,
137 new ByteArrayInputStream(sql.getBytes(StringPool.UTF8)),
138 StringPool.UTF8, new ByteArrayOutputStream(),
139 StringPool.UTF8);
140 }
141 }
142 }
143
144 br.close();
145 }
146
147 private void _loadHypersonic() throws Exception {
148 Class.forName("org.hsqldb.jdbcDriver");
149
150
153 Connection con = DriverManager.getConnection(
154 "jdbc:hsqldb:" + _databaseName + ";shutdown=true", "sa", "");
155
156 if (Validator.isNull(_fileName)) {
157 _loadHypersonic(con, "../sql/portal/portal-hypersonic.sql");
158 _loadHypersonic(con, "../sql/indexes.sql");
159 }
160 else {
161 _loadHypersonic(con, _fileName);
162 }
163
164
166 Statement statement = con.createStatement();
167
168 statement.execute("SHUTDOWN COMPACT");
169
170 statement.close();
171
172 con.close();
173
174
177 String content = _fileUtil.read(_databaseName + ".script");
178
179 content = StringUtil.replace(content, "\\u005cu", "\\u");
180
181 _fileUtil.write(_databaseName + ".script", content);
182 }
183
184 private void _loadHypersonic(Connection con, String fileName)
185 throws Exception {
186
187 StringBuilder sb = new StringBuilder();
188
189 BufferedReader br = new BufferedReader(
190 new StringReader(_fileUtil.read(fileName)));
191
192 String line = null;
193
194 while ((line = br.readLine()) != null) {
195 if (!line.startsWith("//")) {
196 sb.append(line);
197
198 if (line.endsWith(";")) {
199 String sql = sb.toString();
200
201 sql =
202 StringUtil.replace(
203 sql,
204 new String[] {
205 "\\\"",
206 "\\\\",
207 "\\n",
208 "\\r"
209 },
210 new String[] {
211 "\"",
212 "\\",
213 "\\u000a",
214 "\\u000a"
215 });
216
217 sb = new StringBuilder();
218
219 PreparedStatement ps = con.prepareStatement(sql);
220
221 ps.executeUpdate();
222
223 ps.close();
224 }
225 }
226 }
227
228 br.close();
229 }
230
231 private static FileImpl _fileUtil = FileImpl.getInstance();
232
233 private String _databaseType;
234 private String _databaseName;
235 private String _fileName;
236
237 }