OmniSciDB  1dac507f6e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SqlIdentifierCapturer.java
Go to the documentation of this file.
1 package org.apache.calcite.prepare;
2 
3 import org.apache.calcite.sql.SqlBasicCall;
4 import org.apache.calcite.sql.SqlDataTypeSpec;
5 import org.apache.calcite.sql.SqlDelete;
6 import org.apache.calcite.sql.SqlIdentifier;
7 import org.apache.calcite.sql.SqlInsert;
8 import org.apache.calcite.sql.SqlJoin;
9 import org.apache.calcite.sql.SqlKind;
10 import org.apache.calcite.sql.SqlLiteral;
11 import org.apache.calcite.sql.SqlNode;
12 import org.apache.calcite.sql.SqlNodeList;
13 import org.apache.calcite.sql.SqlOrderBy;
14 import org.apache.calcite.sql.SqlSelect;
15 import org.apache.calcite.sql.SqlUpdate;
16 import org.apache.calcite.sql.SqlWith;
17 import org.apache.calcite.sql.SqlWithItem;
18 import org.apache.calcite.sql.parser.SqlParser;
19 
20 import java.lang.reflect.Method;
21 import java.lang.reflect.Modifier;
22 import java.util.Collection;
23 import java.util.HashSet;
24 import java.util.IdentityHashMap;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.Stack;
28 import java.util.concurrent.ConcurrentHashMap;
29 
34 public class SqlIdentifierCapturer {
35  private static final Map<Class<?>, Set<Method>> GETTERS_CACHE =
36  new ConcurrentHashMap<>();
37 
38  private IdentityHashMap<SqlNode, SqlNode> visitedNodes = new IdentityHashMap<>();
39 
40  private Stack<Set<String>> currentList = new Stack<>();
41 
42  public final Set<String> selects = new HashSet<>();
43  public final Set<String> inserts = new HashSet<>();
44  public final Set<String> updates = new HashSet<>();
45  public final Set<String> deletes = new HashSet<>();
46 
47  private final Set<String> ignore = new HashSet<>();
48 
49  { currentList.push(ignore); }
50 
51  public void scan(SqlNode root) {
52  if (null == root) {
53  return;
54  }
55 
56  if (root instanceof SqlLiteral || root instanceof SqlDataTypeSpec) {
57  return;
58  }
59 
60  if (null != visitedNodes.put(root, root)) {
61  return;
62  }
63 
64  if (root instanceof SqlNodeList) {
65  SqlNodeList snl = (SqlNodeList) root;
66  for (SqlNode node : snl) {
67  scan(node);
68  }
69  return;
70  }
71 
72  if (root instanceof SqlIdentifier) {
73  // only the last element!
74  currentList.peek().add(((SqlIdentifier) root).names.reverse().get(0));
75  return;
76  }
77 
78  if (root instanceof SqlBasicCall) {
79  SqlBasicCall call = (SqlBasicCall) root;
80  if (call.getOperator().getKind() == SqlKind.AS) {
81  // only really interested in the first operand
82  scan(call.getOperands()[0]);
83  return;
84  }
85  }
86 
87  if (root instanceof SqlOrderBy) {
88  scan(((SqlOrderBy) root).fetch);
89  scan(((SqlOrderBy) root).offset);
90  scan(((SqlOrderBy) root).query);
91  return;
92  }
93 
94  boolean needsPop = false;
95  if (root instanceof SqlSelect) {
96  currentList.push(selects);
97  scan(((SqlSelect) root).getFrom());
98  currentList.pop();
99  currentList.push(ignore);
100  needsPop = true;
101  } else if (root instanceof SqlInsert) {
102  currentList.push(inserts);
103  scan(((SqlInsert) root).getTargetTable());
104  currentList.pop();
105  currentList.push(ignore);
106  needsPop = true;
107  } else if (root instanceof SqlUpdate) {
108  currentList.push(updates);
109  scan(((SqlUpdate) root).getTargetTable());
110  currentList.pop();
111  currentList.push(ignore);
112  needsPop = true;
113  } else if (root instanceof SqlDelete) {
114  currentList.push(deletes);
115  scan(((SqlDelete) root).getTargetTable());
116  currentList.pop();
117  currentList.push(ignore);
118  needsPop = true;
119  } else if (root instanceof SqlJoin) {
120  currentList.push(ignore);
121  scan(((SqlJoin) root).getCondition());
122  currentList.pop();
123  }
124 
125  Set<Method> methods = getRelevantGetters(root);
126  for (Method m : methods) {
127  Object value = null;
128  try {
129  value = m.invoke(root);
130  } catch (Exception e) {
131  }
132 
133  if (value instanceof SqlNode[]) {
134  SqlNode[] nodes = (SqlNode[]) value;
135  for (SqlNode node : nodes) {
136  scan(node);
137  }
138  } else if (value instanceof SqlNode) {
139  scan((SqlNode) value);
140  } else if (value instanceof Collection) {
141  for (Object vobj : ((Collection<?>) value)) {
142  if (vobj instanceof SqlNode) {
143  scan((SqlNode) vobj);
144  }
145  }
146  }
147  }
148 
149  if (root instanceof SqlWith) {
150  SqlWith with = (SqlWith) root;
151 
152  for (SqlNode node : with.withList) {
153  SqlWithItem item = (SqlWithItem) node;
154  selects.remove(item.name.getSimple());
155  }
156  }
157 
158  if (needsPop) {
159  currentList.pop();
160  }
161  }
162 
163  Set<Method> getRelevantGetters(Object obj) {
164  Class<?> root = obj.getClass();
165 
166  Set<Method> methods = GETTERS_CACHE.get(root);
167  if (null != methods) {
168  return methods;
169  } else {
170  methods = new HashSet<>();
171  }
172 
173  while (root != null) {
174  if (root == SqlNode.class) break;
175 
176  for (Method m : root.getDeclaredMethods()) {
177  if (m.getParameterTypes().length > 0) continue;
178 
179  if (!Modifier.isPublic(m.getModifiers())) continue;
180 
181  Class<?> returnType = m.getReturnType();
182  if (!SqlNode.class.isAssignableFrom(returnType) && SqlNode[].class != returnType
183  && !Collection.class.isAssignableFrom(returnType)) {
184  continue;
185  }
186 
187  methods.add(m);
188  }
189 
190  root = root.getSuperclass();
191  }
192 
193  GETTERS_CACHE.put(obj.getClass(), methods);
194 
195  return methods;
196  }
197 
198  public String toString() {
199  String out = "";
200  out += " Selects: " + selects + "\n";
201  out += " Inserts: " + inserts + "\n";
202  out += " Updates: " + updates + "\n";
203  out += " Deletes: " + deletes + "\n";
204  out += " Ignore : " + ignore + "\n";
205 
206  return out;
207  }
208 
209  public static void main(String[] args) throws Exception {
210  String sql = "UPDATE sales set f=(SELECT max(r.num) from report as r)";
211  sql = "INSER INTO sales (a, b, c ) VALUES(10, (SELECT max(foo) from bob), 0)";
212  sql = "SELECT * FROM sales a left outer join (select (select max(id) from rupert) from report2) r on a.id=(select max(r.di) from test)";
213 
214  SqlParser parser = SqlParser.create(sql);
215 
217  capturer.scan(parser.parseQuery());
218 
219  System.out.println(capturer.selects);
220  System.out.println(capturer.inserts);
221  System.out.println(capturer.updates);
222  System.out.println(capturer.deletes);
223  System.out.println(capturer.ignore);
224  }
225 }
static final Map< Class<?>, Set< Method > > GETTERS_CACHE
IdentityHashMap< SqlNode, SqlNode > visitedNodes
const int8_t const int64_t const uint64_t const int32_t const int64_t int64_t ** out