1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.tools;
16  
17  import com.liferay.portal.kernel.dao.db.DB;
18  import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
19  import com.liferay.portal.kernel.util.FileUtil;
20  import com.liferay.portal.kernel.util.StringUtil;
21  import com.liferay.portal.util.InitUtil;
22  
23  import java.io.IOException;
24  
25  /**
26   * <a href="DBBuilder.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Brian Wing Shun Chan
29   * @author Charles May
30   * @author Alexander Chow
31   */
32  public class DBBuilder {
33  
34      public static void main(String[] args) {
35          InitUtil.initWithSpring();
36  
37          if (args.length == 1) {
38              new DBBuilder(args[0], DB.TYPE_ALL);
39          }
40          else if (args.length == 2) {
41              new DBBuilder(args[0], StringUtil.split(args[1]));
42          }
43          else {
44              throw new IllegalArgumentException();
45          }
46      }
47  
48      public DBBuilder(String databaseName, String[] databaseTypes) {
49          try {
50              _databaseName = databaseName;
51              _databaseTypes = databaseTypes;
52  
53              String sqlDir = System.getProperty("sql.dir", "../sql");
54  
55              _buildSQLFile(sqlDir, "portal");
56              _buildSQLFile(sqlDir, "portal-minimal");
57              _buildSQLFile(sqlDir, "indexes");
58              _buildSQLFile(sqlDir, "sequences");
59              _buildSQLFile(sqlDir, "update-4.2.0-4.3.0");
60              _buildSQLFile(sqlDir, "update-4.3.0-4.3.1");
61              _buildSQLFile(sqlDir, "update-4.3.1-4.3.2");
62              _buildSQLFile(sqlDir, "update-4.3.2-4.3.3");
63              _buildSQLFile(sqlDir, "update-4.3.3-4.3.4");
64              _buildSQLFile(sqlDir, "update-4.3.6-4.4.0");
65              _buildSQLFile(sqlDir, "update-4.4.0-5.0.0");
66              _buildSQLFile(sqlDir, "update-5.0.1-5.1.0");
67              _buildSQLFile(sqlDir, "update-5.1.1-5.1.2");
68              _buildSQLFile(sqlDir, "update-5.1.2-5.2.0");
69              _buildSQLFile(sqlDir, "update-5.2.0-5.2.1");
70              _buildSQLFile(sqlDir, "update-5.2.2-5.2.3");
71              _buildSQLFile(sqlDir, "update-5.2.3-6.0.0");
72  
73              _buildCreateFile(sqlDir);
74          }
75          catch (Exception e) {
76              e.printStackTrace();
77          }
78      }
79  
80      private void _buildCreateFile(String sqlDir) throws IOException {
81          for (int i = 0; i < _databaseTypes.length; i++) {
82              String databaseType = _databaseTypes[i];
83  
84              if (databaseType.equals(DB.TYPE_HYPERSONIC) ||
85                  databaseType.equals(DB.TYPE_INTERBASE) ||
86                  databaseType.equals(DB.TYPE_JDATASTORE) ||
87                  databaseType.equals(DB.TYPE_SAP)) {
88  
89                  continue;
90              }
91  
92              DB db = DBFactoryUtil.getDB(_databaseTypes[i]);
93  
94              if (db != null) {
95                  db.buildCreateFile(sqlDir, _databaseName);
96              }
97          }
98      }
99  
100     private void _buildSQLFile(String sqlDir, String fileName)
101         throws IOException {
102 
103         if (!FileUtil.exists(sqlDir + "/" + fileName + ".sql")) {
104             return;
105         }
106 
107         for (int i = 0; i < _databaseTypes.length; i++) {
108             DB db = DBFactoryUtil.getDB(_databaseTypes[i]);
109 
110             if (db != null) {
111                 db.buildSQLFile(sqlDir, fileName);
112             }
113         }
114     }
115 
116     private String _databaseName;
117     private String[] _databaseTypes;
118 
119 }