1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.tools;
24  
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.util.StringUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.util.FileImpl;
29  
30  import java.io.BufferedReader;
31  import java.io.ByteArrayInputStream;
32  import java.io.ByteArrayOutputStream;
33  import java.io.StringReader;
34  
35  import java.sql.Connection;
36  import java.sql.DriverManager;
37  import java.sql.PreparedStatement;
38  import java.sql.Statement;
39  
40  import org.apache.derby.tools.ij;
41  
42  /**
43   * <a href="DBLoader.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
48  public class DBLoader {
49  
50      public static void main(String[] args) {
51          if (args.length == 2) {
52              new DBLoader(args[0], args[1], StringPool.BLANK);
53          }
54          else if (args.length == 3) {
55              new DBLoader(args[0], args[1], args[2]);
56          }
57          else {
58              throw new IllegalArgumentException();
59          }
60      }
61  
62      public DBLoader(String databaseType, String databaseName, String fileName) {
63          try {
64              _databaseType = databaseType;
65              _databaseName = databaseName;
66              _fileName = fileName;
67  
68              if (_databaseType.equals("derby")) {
69                  _loadDerby();
70              }
71              else if (_databaseType.equals("hypersonic")) {
72                  _loadHypersonic();
73              }
74          }
75          catch (Exception e) {
76              e.printStackTrace();
77          }
78      }
79  
80      private void _loadDerby() throws Exception {
81          Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
82  
83          Connection con = DriverManager.getConnection(
84              "jdbc:derby:" + _databaseName + ";create=true", "", "");
85  
86          if (Validator.isNull(_fileName)) {
87              _loadDerby(con, "../sql/portal/portal-derby.sql");
88              _loadDerby(con, "../sql/indexes.sql");
89          }
90          else {
91              _loadDerby(con, _fileName);
92          }
93      }
94  
95      private void _loadDerby(Connection con, String fileName)
96          throws Exception {
97  
98          StringBuilder sb = new StringBuilder();
99  
100         BufferedReader br = new BufferedReader(
101             new StringReader(_fileUtil.read(fileName)));
102 
103         String line = null;
104 
105         while ((line = br.readLine()) != null) {
106             if (!line.startsWith("--")) {
107                 sb.append(line);
108 
109                 if (line.endsWith(";")) {
110                     String sql = sb.toString();
111 
112                     sql =
113                         StringUtil.replace(
114                             sql,
115                             new String[] {
116                                 "\\'",
117                                 "\\\"",
118                                 "\\\\",
119                                 "\\n",
120                                 "\\r"
121                             },
122                             new String[] {
123                                 "''",
124                                 "\"",
125                                 "\\",
126                                 "\n",
127                                 "\r"
128                             });
129 
130                     sql = sql.substring(0, sql.length() - 1);
131 
132                     sb = new StringBuilder();
133 
134                     if (sql.startsWith("commit")) {
135                         continue;
136                     }
137 
138                     ij.runScript(
139                         con,
140                         new ByteArrayInputStream(sql.getBytes(StringPool.UTF8)),
141                         StringPool.UTF8, new ByteArrayOutputStream(),
142                         StringPool.UTF8);
143                 }
144             }
145         }
146 
147         br.close();
148     }
149 
150     private void _loadHypersonic() throws Exception {
151         Class.forName("org.hsqldb.jdbcDriver");
152 
153         // See LEP-2927. Appending ;shutdown=true to the database connection URL
154         // guarantees that ${_databaseName}.log is purged.
155 
156         Connection con = DriverManager.getConnection(
157             "jdbc:hsqldb:" + _databaseName + ";shutdown=true", "sa", "");
158 
159         if (Validator.isNull(_fileName)) {
160             _loadHypersonic(con, "../sql/portal/portal-hypersonic.sql");
161             _loadHypersonic(con, "../sql/indexes.sql");
162         }
163         else {
164             _loadHypersonic(con, _fileName);
165         }
166 
167         // Shutdown Hypersonic
168 
169         Statement statement = con.createStatement();
170 
171         statement.execute("SHUTDOWN COMPACT");
172 
173         statement.close();
174 
175         con.close();
176 
177         // Hypersonic will encode unicode characters twice, this will undo
178         // it
179 
180         String content = _fileUtil.read(_databaseName + ".script");
181 
182         content = StringUtil.replace(content, "\\u005cu", "\\u");
183 
184         _fileUtil.write(_databaseName + ".script", content);
185     }
186 
187     private void _loadHypersonic(Connection con, String fileName)
188         throws Exception {
189 
190         StringBuilder sb = new StringBuilder();
191 
192         BufferedReader br = new BufferedReader(
193             new StringReader(_fileUtil.read(fileName)));
194 
195         String line = null;
196 
197         while ((line = br.readLine()) != null) {
198             if (!line.startsWith("//")) {
199                 sb.append(line);
200 
201                 if (line.endsWith(";")) {
202                     String sql = sb.toString();
203 
204                     sql =
205                         StringUtil.replace(
206                             sql,
207                             new String[] {
208                                 "\\\"",
209                                 "\\\\",
210                                 "\\n",
211                                 "\\r"
212                             },
213                             new String[] {
214                                 "\"",
215                                 "\\",
216                                 "\\u000a",
217                                 "\\u000a"
218                             });
219 
220                     sb = new StringBuilder();
221 
222                     PreparedStatement ps = con.prepareStatement(sql);
223 
224                     ps.executeUpdate();
225 
226                     ps.close();
227                 }
228             }
229         }
230 
231         br.close();
232     }
233 
234     private static FileImpl _fileUtil = FileImpl.getInstance();
235 
236     private String _databaseType;
237     private String _databaseName;
238     private String _fileName;
239 
240 }