-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathModifySelect.java
More file actions
107 lines (85 loc) · 2.7 KB
/
Copy pathModifySelect.java
File metadata and controls
107 lines (85 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package gudusoft.gsqlparser.demos.modifySelect;
import gudusoft.gsqlparser.EDbVendor;
import gudusoft.gsqlparser.TGSqlParser;
import gudusoft.gsqlparser.nodes.TExpression;
import gudusoft.gsqlparser.nodes.TTable;
import gudusoft.gsqlparser.nodes.TTableList;
import gudusoft.gsqlparser.stmt.TSelectSqlStatement;
import java.util.HashMap;
import java.util.Map;
public class ModifySelect
{
class Table
{
public String tableName;
public String tableAlias;
}
public ModifySelect( )
{
TGSqlParser sqlparser = new TGSqlParser( EDbVendor.dbvmssql );
sqlparser.sqltext = "SELECT A.COLUMN1, B.COLUMN2 from TABLE1 A, TABLE2 B where A.COLUMN1=B.COLUMN1";
System.out.println( "Original SQL:" );
System.out.println( sqlparser.sqltext );
String newColumn1 = "table1.newcolumn";
String newColumn2 = "table2.newcolumn";
sqlparser.parse( );
TSelectSqlStatement select = (TSelectSqlStatement) sqlparser.sqlstatements.get( 0 );
TTableList tables = select.tables;
Map<String, Table> tableMap = new HashMap<String, Table>( );
for ( int i = 0; i < tables.size( ); i++ )
{
TTable table = tables.getTable( i );
Table model = new Table( );
if ( table.getName( ) != null )
{
model.tableName = table.getName( );
tableMap.put( model.tableName.toLowerCase( ), model );
}
if ( table.getAliasClause( ) != null )
model.tableAlias = table.getAliasClause( )
.getAliasName( )
.toString( );
}
TExpression whereCondition = select.getWhereClause( ).getCondition( );
whereCondition.setString( whereCondition.toString( )
+ " AND "
+ getAliasColumn( newColumn1, tableMap )
+ "="
+ getAliasColumn( newColumn2, tableMap ) );
whereCondition.setString( whereCondition.toString( )
+ " AND "
+ getAliasColumn( newColumn1, tableMap )
+ "=?" );
whereCondition.setString( whereCondition.toString( )
+ " AND "
+ getAliasColumn( newColumn2, tableMap )
+ "=?" );
select.getResultColumnList( )
.addResultColumn( getAliasColumn( newColumn1, tableMap ) );
select.getResultColumnList( )
.addResultColumn( getAliasColumn( newColumn2, tableMap ) );
System.out.println( "Modified SQL:" );
System.out.println( select.toString( ) );
}
private String getAliasColumn( String tableColumn,
Map<String, Table> tableMap )
{
String[] splits = tableColumn.trim( ).split( "\\." );
if ( splits.length == 2 )
{
if ( tableMap.containsKey( splits[0].toLowerCase( ) ) )
{
Table table = tableMap.get( splits[0].toLowerCase( ) );
if ( table.tableAlias != null )
return table.tableAlias + "." + splits[1];
else
return tableColumn;
}
}
return tableColumn;
}
public static void main( String[] args )
{
new ModifySelect( );
}
}