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