001    /**
002     * Copyright (c) 2000-2010 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            public String buildSQL(String template) throws IOException {
037                    template = convertTimestamp(template);
038                    template = replaceTemplate(template, getTemplate());
039    
040                    template = reword(template);
041    
042                    return template;
043            }
044    
045            protected SAPDB() {
046                    super(TYPE_SAP);
047            }
048    
049            protected String buildCreateFileContent(
050                    String sqlDir, String databaseName, int population) {
051    
052                    return null;
053            }
054    
055            protected String getServerName() {
056                    return "sap";
057            }
058    
059            protected String[] getTemplate() {
060                    return _SAP;
061            }
062    
063            protected String reword(String data) throws IOException {
064                    UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
065                            new UnsyncStringReader(data));
066    
067                    StringBundler sb = new StringBundler();
068    
069                    String line = null;
070    
071                    while ((line = unsyncBufferedReader.readLine()) != null) {
072                            if (line.startsWith(ALTER_COLUMN_NAME)) {
073                                    String[] template = buildColumnNameTokens(line);
074    
075                                    line = StringUtil.replace(
076                                            "rename column @table@.@old-column@ to @new-column@;",
077                                            REWORD_TEMPLATE, template);
078                            }
079                            else if (line.startsWith(ALTER_COLUMN_TYPE)) {
080                                    String[] template = buildColumnTypeTokens(line);
081    
082                                    line = StringUtil.replace(
083                                            "alter table @table@ modify @old-column@ @type@;",
084                                            REWORD_TEMPLATE, template);
085                            }
086    
087                            sb.append(line);
088                            sb.append("\n");
089                    }
090    
091                    unsyncBufferedReader.close();
092    
093                    return sb.toString();
094            }
095    
096            private static String[] _SAP = {
097                    "##", "TRUE", "FALSE",
098                    "'1970-01-01 00:00:00.000000'", "timestamp",
099                    " long byte", " boolean", " timestamp",
100                    " float", " int", " bigint",
101                    " varchar", " varchar", " varchar",
102                    "", "commit"
103            };
104    
105            private static SAPDB _instance = new SAPDB();
106    
107    }