com.hangum.tadpole.rdb.erd.core.utils.TDBDataHandler.java Source code

Java tutorial

Introduction

Here is the source code for com.hangum.tadpole.rdb.erd.core.utils.TDBDataHandler.java

Source

/*******************************************************************************
 * Copyright (c) 2016 hangum.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     hangum - initial API and implementation
 ******************************************************************************/
package com.hangum.tadpole.rdb.erd.core.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.hangum.tadpole.commons.libs.core.define.PublicTadpoleDefine;
import com.hangum.tadpole.engine.define.DBDefine;
import com.hangum.tadpole.engine.manager.TadpoleSQLManager;
import com.hangum.tadpole.engine.query.dao.mysql.TableColumnDAO;
import com.hangum.tadpole.engine.query.dao.mysql.TableDAO;
import com.hangum.tadpole.engine.query.dao.sqlite.SQLiteForeignKeyListDAO;
import com.hangum.tadpole.engine.query.dao.system.UserDBDAO;
import com.hangum.tadpole.engine.security.DBAccessCtlManager;
import com.hangum.tadpole.engine.sql.util.SQLUtil;
import com.hangum.tadpole.tajo.core.connections.TajoConnectionManager;
import com.ibatis.sqlmap.client.SqlMapClient;

/**
 *   .
 * 
 * @author hangum
 *
 */
public class TDBDataHandler {
    private static final Logger logger = Logger.getLogger(TDBDataHandler.class);

    /**
     * ?? Table?   .
     * 
     * @param userDB
     * @param tableDao
     * @throws Exception
     */
    public static List<TableColumnDAO> getColumns(UserDBDAO userDB, TableDAO tableDao) throws Exception {
        List<TableColumnDAO> returnColumns = new ArrayList<TableColumnDAO>();

        Map<String, String> mapParam = new HashMap<String, String>();
        mapParam.put("db", userDB.getDb());
        String strTableName = "";
        if (userDB.getDBDefine() == DBDefine.SQLite_DEFAULT)
            strTableName = tableDao.getSysName();
        else
            strTableName = tableDao.getName();

        if (userDB.getDBDefine() == DBDefine.ALTIBASE_DEFAULT) {
            mapParam.put("user", StringUtils.substringBefore(strTableName, "."));
            mapParam.put("table", StringUtils.substringAfter(strTableName, "."));
        } else {
            mapParam.put("schema", tableDao.getSchema_name());
            mapParam.put("table", strTableName);
        }

        if (userDB.getDBDefine() == DBDefine.TAJO_DEFAULT) {
            returnColumns = new TajoConnectionManager().tableColumnList(userDB, mapParam);
        } else if (userDB.getDBDefine() == DBDefine.POSTGRE_DEFAULT) {
            if ("".equals(mapParam.get("schema")) || null == mapParam.get("schema")) {
                mapParam.put("schema", "public");
            }
            SqlMapClient sqlClient = TadpoleSQLManager.getInstance(userDB);
            returnColumns = sqlClient.queryForList("tableColumnList", mapParam); //$NON-NLS-1$
        } else {
            SqlMapClient sqlClient = TadpoleSQLManager.getInstance(userDB);
            returnColumns = sqlClient.queryForList("tableColumnList", mapParam); //$NON-NLS-1$
        }

        if (DBDefine.SQLite_DEFAULT == userDB.getDBDefine()) {
            try {
                SqlMapClient sqlClient = TadpoleSQLManager.getInstance(userDB);
                List<SQLiteForeignKeyListDAO> foreignKeyList = sqlClient.queryForList("tableForeignKeyList", //$NON-NLS-1$
                        mapParam);
                for (SQLiteForeignKeyListDAO fkeydao : foreignKeyList) {
                    for (TableColumnDAO dao : returnColumns) {
                        if (dao.getName().equals(fkeydao.getFrom())) {
                            if (PublicTadpoleDefine.isPK(dao.getKey())) {
                                dao.setKey("MUL");
                            } else {
                                dao.setKey("FK");
                            }
                        }
                    }
                }
            } catch (Exception e) {
                logger.error("not found foreignkey for " + tableDao.getName());
            }
        }

        // if find the keyword is add system quote.
        for (TableColumnDAO td : returnColumns) {
            td.setTableDao(tableDao);
            td.setSysName(SQLUtil.makeIdentifierName(userDB, td.getField()));
        }

        returnColumns = DBAccessCtlManager.getInstance().getColumnFilter(tableDao, returnColumns, userDB);

        return returnColumns;
    }
}