001    /**
002     * Copyright (c) 2000-2013 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.dao.db;
016    
017    import com.liferay.portal.kernel.dao.db.DB;
018    import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
019    import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringUtil;
022    
023    import java.io.IOException;
024    
025    /**
026     * @author Alexander Chow
027     * @author Sandeep Soni
028     * @author Ganesh Ram
029     */
030    public class SAPDB extends BaseDB {
031    
032            public static DB getInstance() {
033                    return _instance;
034            }
035    
036            @Override
037            public String buildSQL(String template) throws IOException {
038                    template = convertTimestamp(template);
039                    template = replaceTemplate(template, getTemplate());
040    
041                    template = reword(template);
042    
043                    return template;
044            }
045    
046            protected SAPDB() {
047                    super(TYPE_SAP);
048            }
049    
050            @Override
051            protected String buildCreateFileContent(
052                    String sqlDir, String databaseName, int population) {
053    
054                    return null;
055            }
056    
057            @Override
058            protected String getServerName() {
059                    return "sap";
060            }
061    
062            @Override
063            protected String[] getTemplate() {
064                    return _SAP;
065            }
066    
067            @Override
068            protected String reword(String data) throws IOException {
069                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
070                            new UnsyncStringReader(data));
071    
072                    StringBundler sb = new StringBundler();
073    
074                    String line = null;
075    
076                    while ((line = unsyncBufferedReader.readLine()) != null) {
077                            if (line.startsWith(ALTER_COLUMN_NAME)) {
078                                    String[] template = buildColumnNameTokens(line);
079    
080                                    line = StringUtil.replace(
081                                            "rename column @table@.@old-column@ to @new-column@;",
082                                            REWORD_TEMPLATE, template);
083                            }
084                            else if (line.startsWith(ALTER_COLUMN_TYPE)) {
085                                    String[] template = buildColumnTypeTokens(line);
086    
087                                    line = StringUtil.replace(
088                                            "alter table @table@ modify @old-column@ @type@;",
089                                            REWORD_TEMPLATE, template);
090                            }
091                            else if (line.startsWith(ALTER_TABLE_NAME)) {
092                                    String[] template = buildTableNameTokens(line);
093    
094                                    line = StringUtil.replace(
095                                            "alter table @old-table@ to @new-table@;",
096                                            RENAME_TABLE_TEMPLATE, template);
097                            }
098    
099                            sb.append(line);
100                            sb.append("\n");
101                    }
102    
103                    unsyncBufferedReader.close();
104    
105                    return sb.toString();
106            }
107    
108            private static final String[] _SAP = {
109                    "##", "TRUE", "FALSE", "'1970-01-01 00:00:00.000000'", "timestamp",
110                    " blob", " blob", " boolean", " timestamp", " float", " int", " bigint",
111                    " varchar", " varchar", " varchar", "", "commit"
112            };
113    
114            private static SAPDB _instance = new SAPDB();
115    
116    }