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              _buildSQLFile(sqlDir, "update-5.2.5-6.0.0");
73              _buildSQLFile(sqlDir, "update-5.2.7-6.0.0");
74              _buildSQLFile(sqlDir, "update-6.0.0-6.0.1");
75              _buildSQLFile(sqlDir, "update-6.0.1-6.0.2");
76  
77              _buildCreateFile(sqlDir);
78          }
79          catch (Exception e) {
80              e.printStackTrace();
81          }
82      }
83  
84      private void _buildCreateFile(String sqlDir) throws IOException {
85          for (int i = 0; i < _databaseTypes.length; i++) {
86              String databaseType = _databaseTypes[i];
87  
88              if (databaseType.equals(DB.TYPE_HYPERSONIC) ||
89                  databaseType.equals(DB.TYPE_INTERBASE) ||
90                  databaseType.equals(DB.TYPE_JDATASTORE) ||
91                  databaseType.equals(DB.TYPE_SAP)) {
92  
93                  continue;
94              }
95  
96              DB db = DBFactoryUtil.getDB(_databaseTypes[i]);
97  
98              if (db != null) {
99                  db.buildCreateFile(sqlDir, _databaseName);
100             }
101         }
102     }
103 
104     private void _buildSQLFile(String sqlDir, String fileName)
105         throws IOException {
106 
107         if (!FileUtil.exists(sqlDir + "/" + fileName + ".sql")) {
108             return;
109         }
110 
111         for (int i = 0; i < _databaseTypes.length; i++) {
112             DB db = DBFactoryUtil.getDB(_databaseTypes[i]);
113 
114             if (db != null) {
115                 db.buildSQLFile(sqlDir, fileName);
116             }
117         }
118     }
119 
120     private String _databaseName;
121     private String[] _databaseTypes;
122 
123 }