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.StringUtil;
26  import com.liferay.portal.tools.sql.DBUtil;
27  import com.liferay.util.FileUtil;
28  
29  import java.io.IOException;
30  
31  /**
32   * <a href="DBBuilder.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   * @author Charles May
36   * @author Alexander Chow
37   *
38   */
39  public class DBBuilder {
40  
41      public static void main(String[] args) {
42          if (args.length == 1) {
43              new DBBuilder(args[0], DBUtil.DB_TYPE_ALL);
44          }
45          else if (args.length == 2) {
46              new DBBuilder(args[0], StringUtil.split(args[1]));
47          }
48          else {
49              throw new IllegalArgumentException();
50          }
51      }
52  
53      public DBBuilder(String databaseName, String[] databaseTypes) {
54          try {
55              _databaseName = databaseName;
56              _databaseTypes = databaseTypes;
57  
58              _buildSQLFile("portal");
59              _buildSQLFile("portal-minimal");
60              _buildSQLFile("indexes");
61              _buildSQLFile("sequences");
62              _buildSQLFile("update-4.2.0-4.3.0");
63              _buildSQLFile("update-4.3.0-4.3.1");
64              _buildSQLFile("update-4.3.1-4.3.2");
65              _buildSQLFile("update-4.3.2-4.3.3");
66              _buildSQLFile("update-4.3.3-4.3.4");
67              _buildSQLFile("update-4.3.6-4.4.0");
68              _buildSQLFile("update-4.4.0-5.0.0");
69  
70              _buildCreateFile();
71          }
72          catch (Exception e) {
73              e.printStackTrace();
74          }
75      }
76  
77      private void _buildCreateFile() throws IOException {
78          for (int i = 0; i < _databaseTypes.length; i++) {
79              String databaseType = _databaseTypes[i];
80  
81              if (databaseType.equals(DBUtil.DB_TYPE_HYPERSONIC) ||
82                  databaseType.equals(DBUtil.DB_TYPE_INTERBASE) ||
83                  databaseType.equals(DBUtil.DB_TYPE_JDATASTORE) ||
84                  databaseType.equals(DBUtil.DB_TYPE_SAP)) {
85  
86                  continue;
87              }
88  
89              DBUtil dbUtil = _getDBUtil(_databaseTypes[i]);
90  
91              if (dbUtil != null) {
92                  dbUtil.buildCreateFile(_databaseName);
93              }
94          }
95      }
96  
97      private void _buildSQLFile(String fileName) throws IOException {
98          if (!FileUtil.exists("../sql/" + fileName + ".sql")) {
99              return;
100         }
101 
102         for (int i = 0; i < _databaseTypes.length; i++) {
103             DBUtil dbUtil = _getDBUtil(_databaseTypes[i]);
104 
105             if (dbUtil != null) {
106                 dbUtil.buildSQLFile(fileName);
107             }
108         }
109     }
110 
111     private DBUtil _getDBUtil(String dbType) {
112         return DBUtil.getInstance(dbType);
113     }
114 
115     private String _databaseName;
116 
117     private String[] _databaseTypes;
118 
119 }