OmniSciDB  a987f07e93
 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 54 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 204 of file SqlIdentifierCapturer.java.

References setup.root.

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

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

References run_benchmark_import.parser.

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

Definition at line 74 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.

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

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

Member Data Documentation

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

Definition at line 63 of file SqlIdentifierCapturer.java.

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

Definition at line 58 of file SqlIdentifierCapturer.java.

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

Definition at line 55 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: