public String escapeTableName ( final String tableName ) { // step 1: to quote or no? how many single quotes? boolean toQuote = false ; int apostropheCount = 0 ; for ( int i = 0 ; i < tableName . length ( ) ; i ++ ) { final char c = tableName . charAt ( i ) ; if ( Character . isWhitespace ( c ) || c == ' ' ) { toQuote = true ; } else if ( c == ' ' ) { toQuote = true ; apostropheCount += 1 ; } } final StringBuilder sb = new StringBuilder ( tableName . length ( ) + ( toQuote ? 2 : 0 ) + apostropheCount ) ; // step 2: build the string if ( toQuote ) { sb . append ( SINGLE_QUOTE ) ; } for ( int i = 0 ; i < tableName . length ( ) ; i ++ ) { final char c = tableName . charAt ( i ) ; sb . append ( c ) ; if ( c == ' ' ) { sb . append ( SINGLE_QUOTE ) ; } } if ( toQuote ) { sb . append ( SINGLE_QUOTE ) ; } return sb . toString ( ) ; }
9 . 2 . 1 Referencing Table Cells