OmniSciDB  085a039ca4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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< ImmutableList
< String > > 
selects = new HashSet<>()
 
final Set< ImmutableList
< String > > 
inserts = new HashSet<>()
 
final Set< ImmutableList
< String > > 
updates = new HashSet<>()
 
final Set< ImmutableList
< String > > 
deletes = new HashSet<>()
 

Package Functions

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

Static Package Attributes

static final Logger HEAVYDBLOGGER
 

Private Attributes

IdentityHashMap< SqlNode, SqlNode > visitedNodes = new IdentityHashMap<>()
 
Stack< Set< ImmutableList
< String > > > 
currentList = new Stack<>()
 
final Set< ImmutableList
< 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 53 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 203 of file SqlIdentifierCapturer.java.

References setup.root.

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

203  {
204  Class<?> root = obj.getClass();
205 
206  Set<Method> methods = GETTERS_CACHE.get(root);
207  if (null != methods) {
208  return methods;
209  } else {
210  methods = new HashSet<>();
211  }
212 
213  while (root != null) {
214  if (root == SqlNode.class) break;
215 
216  for (Method m : root.getDeclaredMethods()) {
217  if (m.getParameterTypes().length > 0) continue;
218 
219  if (!Modifier.isPublic(m.getModifiers())) continue;
220 
221  Class<?> returnType = m.getReturnType();
222  if (!SqlNode.class.isAssignableFrom(returnType) && SqlNode[].class != returnType
223  && !Collection.class.isAssignableFrom(returnType)) {
224  continue;
225  }
226 
227  methods.add(m);
228  }
229 
230  root = root.getSuperclass();
231  }
232 
233  GETTERS_CACHE.put(obj.getClass(), methods);
234 
235  return methods;
236  }
tuple root
Definition: setup.in.py:14

+ Here is the caller graph for this function:

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

Definition at line 249 of file SqlIdentifierCapturer.java.

References run_benchmark_import.parser.

249  {
250  String sql = "UPDATE sales set f=(SELECT max(r.num) from report as r)";
251  sql = "INSER INTO sales (a, b, c ) VALUES(10, (SELECT max(foo) from bob), 0)";
252  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)";
253 
254  SqlParser parser = SqlParser.create(sql);
255 
256  SqlIdentifierCapturer capturer = new SqlIdentifierCapturer();
257  capturer.scan(parser.parseQuery());
258 
259  System.out.println(capturer.selects);
260  System.out.println(capturer.inserts);
261  System.out.println(capturer.updates);
262  System.out.println(capturer.deletes);
263  System.out.println(capturer.ignore);
264  }
void org.apache.calcite.prepare.SqlIdentifierCapturer.scan ( SqlNode  root)
inline

Definition at line 73 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, setup.root, org.apache.calcite.prepare.SqlIdentifierCapturer.selects, org.apache.calcite.prepare.SqlIdentifierCapturer.updates, and org.apache.calcite.prepare.SqlIdentifierCapturer.visitedNodes.

73  {
74  if (null == root) {
75  return;
76  }
77 
78  if (root instanceof SqlLiteral || root instanceof SqlDataTypeSpec) {
79  return;
80  }
81 
82  if (null != visitedNodes.put(root, root)) {
83  return;
84  }
85 
86  if (root instanceof SqlNodeList) {
87  SqlNodeList snl = (SqlNodeList) root;
88  for (SqlNode node : snl) {
89  scan(node);
90  }
91  return;
92  }
93 
94  if (root instanceof SqlIdentifier) {
95  // we need all the hierachy now to deal with multischema
96  currentList.peek().add(((SqlIdentifier) root).names.reverse());
97  return;
98  }
99 
100  if (root instanceof SqlBasicCall) {
101  SqlBasicCall call = (SqlBasicCall) root;
102  if (call.getOperator().getKind() == SqlKind.ARGUMENT_ASSIGNMENT) {
103  // We have a => named parameter operator
104  // We need to ignore it as otherwise we will pick up literal args
105  // as tables, EXCEPT if it points to a CURSOR operator, as there
106  // will be at least one table inside of the CURSOR.
107  if (call.operandCount() == 0) {
108  return;
109  }
110  if (call.getOperands()[0].getKind() == SqlKind.CURSOR) {
111  SqlBasicCall cursor_call = (SqlBasicCall) call.getOperands()[0];
112  if (cursor_call.operandCount() == 0) {
113  return;
114  }
115  scan(cursor_call.getOperands()[0]);
116  return;
117  } else {
118  return;
119  }
120  } else if (call.getOperator().getKind() == SqlKind.AS) {
121  // only really interested in the first operand
122  scan(call.getOperands()[0]);
123  return;
124  }
125  }
126 
127  if (root instanceof SqlOrderBy) {
128  scan(((SqlOrderBy) root).fetch);
129  scan(((SqlOrderBy) root).offset);
130  scan(((SqlOrderBy) root).query);
131  return;
132  }
133 
134  boolean needsPop = false;
135  if (root instanceof SqlSelect) {
136  currentList.push(selects);
137  scan(((SqlSelect) root).getFrom());
138  currentList.pop();
139  currentList.push(ignore);
140  needsPop = true;
141  } else if (root instanceof SqlInsert) {
142  currentList.push(inserts);
143  scan(((SqlInsert) root).getTargetTable());
144  currentList.pop();
145  currentList.push(ignore);
146  needsPop = true;
147  } else if (root instanceof SqlUpdate) {
148  currentList.push(updates);
149  scan(((SqlUpdate) root).getTargetTable());
150  currentList.pop();
151  currentList.push(ignore);
152  needsPop = true;
153  } else if (root instanceof SqlDelete) {
154  currentList.push(deletes);
155  scan(((SqlDelete) root).getTargetTable());
156  currentList.pop();
157  currentList.push(ignore);
158  needsPop = true;
159  } else if (root instanceof SqlJoin) {
160  currentList.push(ignore);
161  scan(((SqlJoin) root).getCondition());
162  currentList.pop();
163  }
164 
165  Set<Method> methods = getRelevantGetters(root);
166  for (Method m : methods) {
167  Object value = null;
168  try {
169  value = m.invoke(root);
170  } catch (Exception e) {
171  }
172 
173  if (value instanceof SqlNode[]) {
174  SqlNode[] nodes = (SqlNode[]) value;
175  for (SqlNode node : nodes) {
176  scan(node);
177  }
178  } else if (value instanceof SqlNode) {
179  scan((SqlNode) value);
180  } else if (value instanceof Collection) {
181  for (Object vobj : ((Collection<?>) value)) {
182  if (vobj instanceof SqlNode) {
183  scan((SqlNode) vobj);
184  }
185  }
186  }
187  }
188 
189  if (root instanceof SqlWith) {
190  SqlWith with = (SqlWith) root;
191 
192  for (SqlNode node : with.withList) {
193  SqlWithItem item = (SqlWithItem) node;
194  selects.remove(((SqlIdentifier) item.name).names.reverse());
195  }
196  }
197 
198  if (needsPop) {
199  currentList.pop();
200  }
201  }
tuple root
Definition: setup.in.py:14
IdentityHashMap< SqlNode, SqlNode > visitedNodes

+ Here is the call graph for this function:

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

Definition at line 238 of file SqlIdentifierCapturer.java.

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

238  {
239  String out = "";
240  out += " Selects: " + selects + "\n";
241  out += " Inserts: " + inserts + "\n";
242  out += " Updates: " + updates + "\n";
243  out += " Deletes: " + deletes + "\n";
244  out += " Ignore : " + ignore + "\n";
245 
246  return out;
247  }

Member Data Documentation

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

Definition at line 62 of file SqlIdentifierCapturer.java.

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

Definition at line 57 of file SqlIdentifierCapturer.java.

final Logger org.apache.calcite.prepare.SqlIdentifierCapturer.HEAVYDBLOGGER
staticpackage
Initial value:
=
LoggerFactory.getLogger(SqlIdentifierCapturer.class)

Definition at line 54 of file SqlIdentifierCapturer.java.

final Set<ImmutableList<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: