001
014
015 package com.liferay.portal.tools;
016
017 import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
018 import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
019 import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
020 import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
021 import com.liferay.portal.kernel.util.StringBundler;
022 import com.liferay.portal.kernel.util.StringPool;
023 import com.liferay.portal.kernel.util.StringUtil;
024 import com.liferay.portal.kernel.util.Validator;
025 import com.liferay.portal.util.FileImpl;
026
027 import java.sql.Connection;
028 import java.sql.DriverManager;
029 import java.sql.PreparedStatement;
030 import java.sql.Statement;
031
032 import org.apache.derby.tools.ij;
033
034
037 public class DBLoader {
038
039 public static void main(String[] args) {
040 if (args.length == 2) {
041 new DBLoader(args[0], args[1], StringPool.BLANK);
042 }
043 else if (args.length == 3) {
044 new DBLoader(args[0], args[1], args[2]);
045 }
046 else {
047 throw new IllegalArgumentException();
048 }
049 }
050
051 public DBLoader(String databaseType, String databaseName, String fileName) {
052 try {
053 _databaseType = databaseType;
054 _databaseName = databaseName;
055 _fileName = fileName;
056
057 if (_databaseType.equals("derby")) {
058 _loadDerby();
059 }
060 else if (_databaseType.equals("hypersonic")) {
061 _loadHypersonic();
062 }
063 }
064 catch (Exception e) {
065 e.printStackTrace();
066 }
067 }
068
069 private void _loadDerby() throws Exception {
070 Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
071
072 Connection con = DriverManager.getConnection(
073 "jdbc:derby:" + _databaseName + ";create=true", "", "");
074
075 if (Validator.isNull(_fileName)) {
076 _loadDerby(con, "../sql/portal/portal-derby.sql");
077 _loadDerby(con, "../sql/indexes.sql");
078 }
079 else {
080 _loadDerby(con, _fileName);
081 }
082 }
083
084 private void _loadDerby(Connection con, String fileName)
085 throws Exception {
086
087 StringBundler sb = new StringBundler();
088
089 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
090 new UnsyncStringReader(_fileUtil.read(fileName)));
091
092 String line = null;
093
094 while ((line = unsyncBufferedReader.readLine()) != null) {
095 if (!line.startsWith("--")) {
096 sb.append(line);
097
098 if (line.endsWith(";")) {
099 String sql = sb.toString();
100
101 sql =
102 StringUtil.replace(
103 sql,
104 new String[] {
105 "\\'",
106 "\\\"",
107 "\\\\",
108 "\\n",
109 "\\r"
110 },
111 new String[] {
112 "''",
113 "\"",
114 "\\",
115 "\n",
116 "\r"
117 });
118
119 sql = sql.substring(0, sql.length() - 1);
120
121 sb.setIndex(0);
122
123 if (sql.startsWith("commit")) {
124 continue;
125 }
126
127 ij.runScript(
128 con,
129 new UnsyncByteArrayInputStream(
130 sql.getBytes(StringPool.UTF8)),
131 StringPool.UTF8, new UnsyncByteArrayOutputStream(),
132 StringPool.UTF8);
133 }
134 }
135 }
136
137 unsyncBufferedReader.close();
138 }
139
140 private void _loadHypersonic() throws Exception {
141 Class.forName("org.hsqldb.jdbcDriver");
142
143
144
145
146 Connection con = DriverManager.getConnection(
147 "jdbc:hsqldb:" + _databaseName + ";shutdown=true", "sa", "");
148
149 if (Validator.isNull(_fileName)) {
150 _loadHypersonic(con, "../sql/portal/portal-hypersonic.sql");
151 _loadHypersonic(con, "../sql/indexes.sql");
152 }
153 else {
154 _loadHypersonic(con, _fileName);
155 }
156
157
158
159 Statement statement = con.createStatement();
160
161 statement.execute("SHUTDOWN COMPACT");
162
163 statement.close();
164
165 con.close();
166
167
168
169
170 String content = _fileUtil.read(_databaseName + ".script");
171
172 content = StringUtil.replace(content, "\\u005cu", "\\u");
173
174 _fileUtil.write(_databaseName + ".script", content);
175 }
176
177 private void _loadHypersonic(Connection con, String fileName)
178 throws Exception {
179
180 StringBundler sb = new StringBundler();
181
182 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
183 new UnsyncStringReader(_fileUtil.read(fileName)));
184
185 String line = null;
186
187 while ((line = unsyncBufferedReader.readLine()) != null) {
188 if (!line.startsWith("
189 sb.append(line);
190
191 if (line.endsWith(";")) {
192 String sql = sb.toString();
193
194 sql =
195 StringUtil.replace(
196 sql,
197 new String[] {
198 "\\\"",
199 "\\\\",
200 "\\n",
201 "\\r"
202 },
203 new String[] {
204 "\"",
205 "\\",
206 "\\u000a",
207 "\\u000a"
208 });
209
210 sb.setIndex(0);
211
212 PreparedStatement ps = con.prepareStatement(sql);
213
214 ps.executeUpdate();
215
216 ps.close();
217 }
218 }
219 }
220
221 unsyncBufferedReader.close();
222 }
223
224 private static FileImpl _fileUtil = FileImpl.getInstance();
225
226 private String _databaseType;
227 private String _databaseName;
228 private String _fileName;
229
230 }