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