001package gudusoft.gsqlparser.sqlenv;
002
003import gudusoft.gsqlparser.nodes.TTypeName;
004import gudusoft.gsqlparser.util.SQLUtil;
005
006import java.util.*;
007
008/**
009 * SQL table, includes a list of columns.
010 */
011public class TSQLTable extends TSQLSchemaObject {
012
013    private boolean isView = false;
014
015    public void setView(boolean view) {
016        isView = view;
017    }
018
019    /**
020     * Used to check whether this is a view.
021     *
022     * @return true if this is a view.
023     */
024    public boolean isView() {
025        return isView;
026    }
027
028    private String definition;
029
030    public void setDefinition(String definition) {
031        this.definition = definition;
032    }
033
034    /**
035     * This is the script that used to create this view( {@link #isView()} returns true).
036     *
037     * @return sql script that used to create this view.
038     */
039    public String getDefinition() {
040        return definition;
041    }
042
043    /**
044     * create a new table belong to a schema
045     *
046     * @param sqlSchema schema
047     * @param tableName table name
048     */
049    public TSQLTable(TSQLSchema sqlSchema, String tableName){
050        super(sqlSchema,tableName,ESQLDataObjectType.dotTable);
051    }
052
053    /**
054     * column list
055     * @return a column list
056     */
057    public List<TSQLColumn> getColumnList() {
058        List<TSQLColumn> columnList = new LinkedList<>();
059        for(String column: columnMap.keySet()){
060            columnList.add(columnMap.get(column));
061        }
062        return columnList;
063    }
064
065    private Map<String, TSQLColumn> columnMap = Collections.synchronizedMap(new LinkedHashMap<String, TSQLColumn>( ));
066
067    /**
068     * add a new column to the table
069     *
070     * @param columnName column name
071     */
072    public void addColumn(String columnName){
073        if (!searchColumn(columnName)){
074            columnMap.put(SQLUtil.getIdentifierNormalColumnName(this.sqlEnv.getDBVendor(), columnName), new TSQLColumn(this,columnName));
075        }
076    }
077
078    public void addColumn(String columnName, TTypeName columnDataType){
079        if (!searchColumn(columnName)){
080            columnMap.put(SQLUtil.getIdentifierNormalColumnName(this.sqlEnv.getDBVendor(), columnName), new TSQLColumn(this,columnName,columnDataType));
081        }
082    }
083
084    public boolean searchColumn(String columnName){
085        return columnMap.containsKey(SQLUtil.getIdentifierNormalColumnName(this.sqlEnv.getDBVendor(), columnName));
086    }
087
088    public TSQLColumn getColumn(String columnName){
089        return columnMap.get(SQLUtil.getIdentifierNormalColumnName(this.sqlEnv.getDBVendor(), columnName));
090    }
091
092
093    public ArrayList<String> getColumns(boolean columnNameOnly ){
094        ArrayList<String> columns = new ArrayList<>();
095        for(String column: columnMap.keySet()){
096            TSQLColumn s = columnMap.get(column);
097            if (columnNameOnly){
098                columns.add(s.name);
099            }else{
100                columns.add(this.name+"."+ s.name);
101            }
102
103           // columns.add(s.name);
104        }
105
106        return columns;
107    }
108
109}