001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.tools;
016    
017    import com.liferay.portal.kernel.dao.db.DB;
018    import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
019    import com.liferay.portal.kernel.util.FileUtil;
020    import com.liferay.portal.kernel.util.StringUtil;
021    
022    import java.io.IOException;
023    
024    import java.util.Map;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     * @author Charles May
029     * @author Alexander Chow
030     * @author Raymond Aug??
031     */
032    public class DBBuilder {
033    
034            public static void main(String[] args) throws Exception {
035                    ToolDependencies.wireBasic();
036    
037                    Map<String, String> arguments = ArgumentsUtil.parseArguments(args);
038    
039                    String databaseName = arguments.get("db.database.name");
040    
041                    String databaseTypesString = arguments.get("db.database.types");
042    
043                    String[] databaseTypes = null;
044    
045                    if (databaseTypesString == null) {
046                            databaseTypes = DB.TYPE_ALL;
047                    }
048                    else {
049                            databaseTypes = StringUtil.split(databaseTypesString);
050                    }
051    
052                    String sqlDir = arguments.get("db.sql.dir");
053    
054                    try {
055                            new DBBuilder(databaseName, databaseTypes, sqlDir);
056                    }
057                    catch (Exception e) {
058                            ArgumentsUtil.processMainException(arguments, e);
059                    }
060            }
061    
062            public DBBuilder(String databaseName, String[] databaseTypes, String sqlDir)
063                    throws Exception {
064    
065                    _databaseName = databaseName;
066                    _databaseTypes = databaseTypes;
067    
068                    if (!sqlDir.endsWith("/META-INF/sql") &&
069                            !sqlDir.endsWith("/WEB-INF/sql")) {
070    
071                            _buildSQLFile(sqlDir, "portal");
072                            _buildSQLFile(sqlDir, "portal-tables");
073                    }
074                    else {
075                            _buildSQLFile(sqlDir, "tables");
076                    }
077    
078                    _buildSQLFile(sqlDir, "indexes");
079                    _buildSQLFile(sqlDir, "sequences");
080                    _buildSQLFile(sqlDir, "update-5.0.1-5.1.0");
081                    _buildSQLFile(sqlDir, "update-5.1.1-5.1.2");
082                    _buildSQLFile(sqlDir, "update-5.1.2-5.2.0");
083                    _buildSQLFile(sqlDir, "update-5.2.0-5.2.1");
084                    _buildSQLFile(sqlDir, "update-5.2.2-5.2.3");
085                    _buildSQLFile(sqlDir, "update-5.2.3-6.0.0");
086                    _buildSQLFile(sqlDir, "update-5.2.5-6.0.0");
087                    _buildSQLFile(sqlDir, "update-5.2.7-6.0.0");
088                    _buildSQLFile(sqlDir, "update-5.2.8-6.0.5");
089                    _buildSQLFile(sqlDir, "update-6.0.0-6.0.1");
090                    _buildSQLFile(sqlDir, "update-6.0.1-6.0.2");
091                    _buildSQLFile(sqlDir, "update-6.0.2-6.0.3");
092                    _buildSQLFile(sqlDir, "update-6.0.4-6.0.5");
093                    _buildSQLFile(sqlDir, "update-6.0.5-6.0.6");
094                    _buildSQLFile(sqlDir, "update-6.0.6-6.1.0");
095                    _buildSQLFile(sqlDir, "update-6.0.12-6.1.0");
096                    _buildSQLFile(sqlDir, "update-6.1.0-6.1.1");
097                    _buildSQLFile(sqlDir, "update-6.1.1-6.2.0");
098    
099                    _buildCreateFile(sqlDir);
100            }
101    
102            private void _buildCreateFile(String sqlDir) throws IOException {
103                    for (String databaseType : _databaseTypes) {
104                            if (databaseType.equals(DB.TYPE_HYPERSONIC)) {
105                                    continue;
106                            }
107    
108                            DB db = DBFactoryUtil.getDB(databaseType, null);
109    
110                            if (db != null) {
111                                    if (!sqlDir.endsWith("/WEB-INF/sql")) {
112                                            db.buildCreateFile(sqlDir, _databaseName);
113                                    }
114                                    else {
115                                            db.buildCreateFile(sqlDir, _databaseName, DB.DEFAULT);
116                                    }
117                            }
118                    }
119            }
120    
121            private void _buildSQLFile(String sqlDir, String fileName)
122                    throws IOException {
123    
124                    if (!FileUtil.exists(sqlDir + "/" + fileName + ".sql")) {
125                            return;
126                    }
127    
128                    for (String _databaseType : _databaseTypes) {
129                            DB db = DBFactoryUtil.getDB(_databaseType, null);
130    
131                            if (db != null) {
132                                    db.buildSQLFile(sqlDir, fileName);
133                            }
134                    }
135            }
136    
137            private final String _databaseName;
138            private final String[] _databaseTypes;
139    
140    }