OmniSciDB  d2f719934e
 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 MAPDLOGGER = LoggerFactory.getLogger(SqlIdentifierCapturer.class)
 

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 184 of file SqlIdentifierCapturer.java.

References setup.root.

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

184  {
185  Class<?> root = obj.getClass();
186 
187  Set<Method> methods = GETTERS_CACHE.get(root);
188  if (null != methods) {
189  return methods;
190  } else {
191  methods = new HashSet<>();
192  }
193 
194  while (root != null) {
195  if (root == SqlNode.class) break;
196 
197  for (Method m : root.getDeclaredMethods()) {
198  if (m.getParameterTypes().length > 0) continue;
199 
200  if (!Modifier.isPublic(m.getModifiers())) continue;
201 
202  Class<?> returnType = m.getReturnType();
203  if (!SqlNode.class.isAssignableFrom(returnType) && SqlNode[].class != returnType
204  && !Collection.class.isAssignableFrom(returnType)) {
205  continue;
206  }
207 
208  methods.add(m);
209  }
210 
211  root = root.getSuperclass();
212  }
213 
214  GETTERS_CACHE.put(obj.getClass(), methods);
215 
216  return methods;
217  }
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 230 of file SqlIdentifierCapturer.java.

References run_benchmark_import.parser.

230  {
231  String sql = "UPDATE sales set f=(SELECT max(r.num) from report as r)";
232  sql = "INSER INTO sales (a, b, c ) VALUES(10, (SELECT max(foo) from bob), 0)";
233  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)";
234 
235  SqlParser parser = SqlParser.create(sql);
236 
237  SqlIdentifierCapturer capturer = new SqlIdentifierCapturer();
238  capturer.scan(parser.parseQuery());
239 
240  System.out.println(capturer.selects);
241  System.out.println(capturer.inserts);
242  System.out.println(capturer.updates);
243  System.out.println(capturer.deletes);
244  System.out.println(capturer.ignore);
245  }
void org.apache.calcite.prepare.SqlIdentifierCapturer.scan ( SqlNode  root)
inline

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

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

219  {
220  String out = "";
221  out += " Selects: " + selects + "\n";
222  out += " Inserts: " + inserts + "\n";
223  out += " Updates: " + updates + "\n";
224  out += " Deletes: " + deletes + "\n";
225  out += " Ignore : " + ignore + "\n";
226 
227  return out;
228  }

Member Data Documentation

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

Definition at line 61 of file SqlIdentifierCapturer.java.

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

Definition at line 56 of file SqlIdentifierCapturer.java.

final Set<ImmutableList<String> > org.apache.calcite.prepare.SqlIdentifierCapturer.ignore = new HashSet<>()
private
final Logger org.apache.calcite.prepare.SqlIdentifierCapturer.MAPDLOGGER = LoggerFactory.getLogger(SqlIdentifierCapturer.class)
staticpackage

Definition at line 54 of file SqlIdentifierCapturer.java.

IdentityHashMap<SqlNode, SqlNode> org.apache.calcite.prepare.SqlIdentifierCapturer.visitedNodes = new IdentityHashMap<>()
private

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