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