001
002package gudusoft.gsqlparser.pp.stmtformatter.type.comm;
003
004import gudusoft.gsqlparser.EDbVendor;
005import gudusoft.gsqlparser.TSourceToken;
006import gudusoft.gsqlparser.nodes.TColumnDefinition;
007import gudusoft.gsqlparser.nodes.TColumnDefinitionList;
008import gudusoft.gsqlparser.nodes.TConstraintList;
009import gudusoft.gsqlparser.pp.processor.type.comm.AbstractProcessor;
010import gudusoft.gsqlparser.pp.processor.type.comm.ExpressionProcessor;
011import gudusoft.gsqlparser.pp.stmtformatter.type.AbstractStmtFormatter;
012import gudusoft.gsqlparser.pp.utils.SourceTokenNameConstant;
013import gudusoft.gsqlparser.pp.utils.SourceTokenOperator;
014import gudusoft.gsqlparser.pp.utils.SourceTokenSearcher;
015import gudusoft.gsqlparser.stmt.TCreateTableSqlStatement;
016
017import java.util.ArrayList;
018import java.util.List;
019
020public class CreateTableStmtFormatter extends
021                AbstractStmtFormatter<TCreateTableSqlStatement>
022{
023
024        /**
025         * the processor for the table column items
026         */
027        private List<AbstractProcessor> itemListProcessors;
028
029        /**
030         * the processor for the table constraints
031         */
032        private List<AbstractProcessor> constraintListProcessors;
033
034        /**
035         * add the item processor
036         * 
037         * @param p
038         */
039        public void addItemListProcessor( AbstractProcessor p )
040        {
041                if ( this.itemListProcessors == null )
042                {
043                        this.itemListProcessors = new ArrayList<AbstractProcessor>( );
044                }
045                this.itemListProcessors.add( p );
046        }
047
048        /**
049         * add the constraint processor
050         * 
051         * @param p
052         */
053        public void addConstraintListProcessor( AbstractProcessor p )
054        {
055                if ( this.constraintListProcessors == null )
056                {
057                        this.constraintListProcessors = new ArrayList<AbstractProcessor>( );
058                }
059                this.constraintListProcessors.add( p );
060        }
061
062        @Override
063        protected void doFormat( TCreateTableSqlStatement sql )
064        {
065                this.runProcessor( this.getSpecialProcessors( ), sql );
066
067                TColumnDefinitionList items = sql.getColumnList( );
068                if ( items != null && items.size( ) > 0 )
069                {
070                        this.runProcessor( this.itemListProcessors, items );
071
072                        for ( int i = 0; i < items.size( ); i++ )
073                        {
074                                TColumnDefinition item = items.getColumn( i );
075                                if ( item != null && item.getDatatype( ) != null )
076                                {
077                                        ExpressionProcessor.processTypeName( this.getOption( ),
078                                                        item.getDatatype( ) );
079                                }
080                        }
081                }
082
083                TConstraintList constraints = sql.getTableConstraints( );
084                if ( constraints != null && constraints.size( ) > 0 )
085                {
086                        this.runProcessor( this.constraintListProcessors, constraints );
087                }
088
089                if ( sql.getSubQuery( ) != null )
090                {
091                        ExpressionProcessor.processParenthesesNodeInSubQuery( this.getOption( ),
092                                        sql.getSubQuery( ),
093                                        sql.getSubQuery( ).getStartToken( ),
094                                        sql.getSubQuery( ).getEndToken( ) );
095                }
096
097                if ( sql.getGsqlparser( ) != null
098                                && sql.getGsqlparser( ).getDbVendor( ) == EDbVendor.dbvhive )
099                {
100                        TSourceToken token = sql.getStartToken( );
101                        int curIndetLen = SourceTokenOperator.curIndentLenVT( token );
102
103                        List<TSourceToken> tokens = new ArrayList<TSourceToken>( );
104                        if ( sql.getTableComment( ) != null )
105                        {
106                                TSourceToken commentToken = SourceTokenSearcher.backforwardSearchNotWhitespaceAndReturnToken( sql.getTableComment( )
107                                                .getStartToken( ),
108                                                5,
109                                                SourceTokenNameConstant.COMMENT );
110                                tokens.add( commentToken );
111                        }
112                        if ( sql.getHiveRowFormat( ) != null )
113                        {
114                                tokens.add( sql.getHiveRowFormat( ).getStartToken( ) );
115                        }
116                        if ( sql.getHiveTablePartition( ) != null )
117                        {
118                                tokens.add( sql.getHiveTablePartition( ).getStartToken( ) );
119                        }
120                        if ( sql.getHiveTableBuckets( ) != null )
121                        {
122                                tokens.add( sql.getHiveTableBuckets( ).getStartToken( ) );
123                        }
124                        if ( sql.getHiveTableFileFormat( ) != null )
125                        {
126                                tokens.add( sql.getHiveTableFileFormat( ).getStartToken( ) );
127                        }
128                        if ( sql.getHiveTableProperties( ) != null )
129                        {
130                                tokens.add( sql.getHiveTableProperties( ).getStartToken( ) );
131                        }
132                        if ( sql.getHiveTableSkewed( ) != null )
133                        {
134                                tokens.add( sql.getHiveTableSkewed( ).getStartToken( ) );
135                        }
136
137                        for ( int i = 0; i < tokens.size( ); i++ )
138                        {
139                                TSourceToken currentToken = tokens.get( i );
140                                SourceTokenOperator.removeWhitespaceAndReturnFromEnd( this.getOption( ),
141                                                currentToken );
142                                SourceTokenOperator.addBefore( this.getOption( ),
143                                                currentToken,
144                                                SourceTokenOperator.createReturnSourceToken( ) );
145                                SourceTokenOperator.addBefore( this.getOption( ),
146                                                currentToken,
147                                                SourceTokenOperator.createWhitespaceSourceToken( curIndetLen ) );
148                        }
149                }
150        }
151}