001package gudusoft.gsqlparser.stmt;
002
003import gudusoft.gsqlparser.*;
004import gudusoft.gsqlparser.nodes.*;
005import gudusoft.gsqlparser.sqlenv.TSQLTable;
006
007public class TCopyStmt extends TCustomSqlStatement {
008
009    public enum ECopyType { unknown,to, from};
010
011    private ECopyType copyType = ECopyType.unknown;
012
013    public ECopyType getCopyType() {
014        return copyType;
015    }
016
017    private TSelectSqlStatement subQuery;
018
019    public TSelectSqlStatement getSubQuery() {
020        return subQuery;
021    }
022
023    public TObjectNameList getColumnList() {
024        return columnList;
025    }
026
027    public String getFilename() {
028        return filename;
029    }
030
031    private TObjectNameList columnList;
032    private String filename;
033
034
035    public TCopyStmt(EDbVendor dbvendor){
036        super(dbvendor);
037        sqlstatementtype = ESqlStatementType.sstCopy;
038    }
039
040    public int doParseStatement(TCustomSqlStatement psql) {
041        if (rootNode == null) return -1;
042        TCopySqlNode copySqlNode = (TCopySqlNode)rootNode;
043        super.doParseStatement(psql);
044        this.filename = copySqlNode.getFilename().toString();
045        this.copyType = copySqlNode.getCopyType();
046
047
048        if (copySqlNode.getTablename() != null){
049            TTable table = new TTable();
050            table.setTableName(copySqlNode.getTablename());
051            table.setTableType(ETableSource.objectname);
052            table.setEffectType(ETableEffectType.tetCopy);
053            this.tables.addTable(table);
054            this.setTargetTable(table);
055            this.columnList = copySqlNode.getColumnList();
056
057            TSQLTable sqlTable = null;
058            if (getSqlEnv() != null)  {
059                if (this.getTargetTable() != null){
060                    sqlTable = getSqlEnv().addTable(this.getTargetTable().getFullName(),true);
061                }
062            }
063
064            if (columnList != null){
065                for(TObjectName column : columnList){
066                    this.getTargetTable().getLinkedColumns().addObjectName(column);
067                    column.setSourceTable(this.getTargetTable());
068
069                    if (sqlTable != null){
070                        sqlTable.addColumn(column.toString());
071                    }
072                }
073            }
074        }else if (copySqlNode.getSubQueryNode() != null){
075            subQuery = new TSelectSqlStatement(this.dbvendor);
076            subQuery.rootNode = copySqlNode.getSubQueryNode();
077            subQuery.doParseStatement(this);
078        }
079
080
081        return 0;
082    }
083
084    public void accept(TParseTreeVisitor v){
085        v.preVisit(this);
086        v.postVisit(this);
087    }
088
089    public void acceptChildren(TParseTreeVisitor v){
090        v.preVisit(this);
091        v.postVisit(this);
092    }
093}