OmniSciDB  b24e664e58
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
org.apache.calcite.prepare.SqlIdentifierCapturer Class Reference
+ Collaboration diagram for org.apache.calcite.prepare.SqlIdentifierCapturer:

Public Member Functions

void scan (SqlNode root)
 
String toString ()
 

Static Public Member Functions

static void main (String[] args) throws Exception
 

Public Attributes

final Set< String > selects = new HashSet<>()
 
final Set< String > inserts = new HashSet<>()
 
final Set< String > updates = new HashSet<>()
 
final Set< String > deletes = new HashSet<>()
 

Package Functions

 [instance initializer]
 
Set< Method > getRelevantGetters (Object obj)
 

Private Attributes

IdentityHashMap< SqlNode, SqlNode > visitedNodes = new IdentityHashMap<>()
 
Stack< Set< String > > currentList = new Stack<>()
 
final Set< String > ignore = new HashSet<>()
 

Static Private Attributes

static final Map< Class
<?>, Set< Method > > 
GETTERS_CACHE
 

Detailed Description

scans from a root SqlNode and retrieves all SqlIdentifiers used in a query.

Definition at line 34 of file SqlIdentifierCapturer.java.

Member Function Documentation

org.apache.calcite.prepare.SqlIdentifierCapturer.[instance initializer] ( )
inlinepackage
Set<Method> org.apache.calcite.prepare.SqlIdentifierCapturer.getRelevantGetters ( Object  obj)
inlinepackage

Definition at line 163 of file SqlIdentifierCapturer.java.

Referenced by org.apache.calcite.prepare.SqlIdentifierCapturer.scan().

163  {
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  }

+ Here is the caller graph for this function:

static void org.apache.calcite.prepare.SqlIdentifierCapturer.main ( String[]  args) throws Exception
inlinestatic

Definition at line 209 of file SqlIdentifierCapturer.java.

References run_benchmark_import.parser, and Experimental.String.

209  {
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 
216  SqlIdentifierCapturer capturer = new SqlIdentifierCapturer();
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  }
void org.apache.calcite.prepare.SqlIdentifierCapturer.scan ( SqlNode  root)
inline

Definition at line 51 of file SqlIdentifierCapturer.java.

References org.apache.calcite.prepare.SqlIdentifierCapturer.deletes, org.apache.calcite.prepare.SqlIdentifierCapturer.getRelevantGetters(), org.apache.calcite.prepare.SqlIdentifierCapturer.ignore, org.apache.calcite.prepare.SqlIdentifierCapturer.inserts, org.apache.calcite.prepare.SqlIdentifierCapturer.selects, org.apache.calcite.prepare.SqlIdentifierCapturer.updates, and org.apache.calcite.prepare.SqlIdentifierCapturer.visitedNodes.

51  {
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  }
IdentityHashMap< SqlNode, SqlNode > visitedNodes

+ Here is the call graph for this function:

String org.apache.calcite.prepare.SqlIdentifierCapturer.toString ( )
inline

Definition at line 198 of file SqlIdentifierCapturer.java.

References org.apache.calcite.prepare.SqlIdentifierCapturer.deletes, org.apache.calcite.prepare.SqlIdentifierCapturer.ignore, org.apache.calcite.prepare.SqlIdentifierCapturer.inserts, out, org.apache.calcite.prepare.SqlIdentifierCapturer.selects, Experimental.String, and org.apache.calcite.prepare.SqlIdentifierCapturer.updates.

198  {
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  }
const int8_t const int64_t const uint64_t const int32_t const int64_t int64_t ** out

Member Data Documentation

Stack<Set<String> > org.apache.calcite.prepare.SqlIdentifierCapturer.currentList = new Stack<>()
private

Definition at line 40 of file SqlIdentifierCapturer.java.

final Map<Class<?>, Set<Method> > org.apache.calcite.prepare.SqlIdentifierCapturer.GETTERS_CACHE
staticprivate
Initial value:
=
new ConcurrentHashMap<>()

Definition at line 35 of file SqlIdentifierCapturer.java.

final Set<String> org.apache.calcite.prepare.SqlIdentifierCapturer.ignore = new HashSet<>()
private
IdentityHashMap<SqlNode, SqlNode> org.apache.calcite.prepare.SqlIdentifierCapturer.visitedNodes = new IdentityHashMap<>()
private

The documentation for this class was generated from the following file: