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