public VarDTO_Light getVarLight(long id) { Var v = em.find(Var.class, id); if (v != null) { v.setEM(em); return v.toDTO_Light(); } else return null; }
/** For each node, update the block stack and reference collection as appropriate. */ @Override public void visit(NodeTraversal t, Node n, Node parent) { if (n.isName() || n.isRest() || (n.isStringKey() && parent.isObjectPattern() && !n.hasChildren())) { Var v; if (n.getString().equals("arguments")) { v = t.getScope().getArgumentsVar(); } else { v = t.getScope().getVar(n.getString()); } if (v != null) { if (varFilter.apply(v)) { addReference(v, new Reference(n, t, peek(blockStack))); } if (v.getParentNode() != null && NodeUtil.isHoistedFunctionDeclaration(v.getParentNode()) && // If we're only traversing a narrow scope, do not try to climb outside. (narrowScope == null || narrowScope.getDepth() <= v.getScope().getDepth())) { outOfBandTraversal(v); } } } if (isBlockBoundary(n, parent)) { pop(blockStack); } }
private boolean isStaticFunction(Node node, NodeTraversal traversal) { if (!node.isQualifiedName()) { return false; } String root = NodeUtil.getRootOfQualifiedName(node).getQualifiedName(); if (globals == null) { return false; } GlobalNamespace.Name fullName = globals.getOwnSlot(node.getQualifiedName()); GlobalNamespace.Name rootName = globals.getOwnSlot(root); if (fullName == null || rootName == null) { return false; } GlobalNamespace.Ref rootDecl = rootName.getDeclaration(); if (rootDecl == null) { return false; } Node globalDeclNode = rootDecl.getNode(); if (globalDeclNode == null) { return false; // don't know where the root came from so assume it could be anything } Var rootScope = traversal.getScope().getVar(root); if (rootScope == null) { return true; // root is not in the current scope, so it's a static function } Node scopeDeclNode = rootScope.getNode(); return scopeDeclNode == globalDeclNode; // is the global name currently in scope? }
private QueryIterator resultsToQueryIterator( Binding binding, Node s, Node score, Node literal, Collection<TextHit> results, ExecutionContext execCxt) { Var sVar = Var.isVar(s) ? Var.alloc(s) : null; Var scoreVar = (score == null) ? null : Var.alloc(score); Var literalVar = (literal == null) ? null : Var.alloc(literal); Function<TextHit, Binding> converter = (TextHit hit) -> { if (score == null && literal == null) return sVar != null ? BindingFactory.binding(binding, sVar, hit.getNode()) : BindingFactory.binding(binding); BindingMap bmap = BindingFactory.create(binding); if (sVar != null) bmap.add(sVar, hit.getNode()); if (scoreVar != null) bmap.add(scoreVar, NodeFactoryExtra.floatToNode(hit.getScore())); if (literalVar != null) bmap.add(literalVar, hit.getLiteral()); return bmap; }; Iterator<Binding> bIter = Iter.map(results.iterator(), converter); QueryIterator qIter = new QueryIterPlainWrapper(bIter, execCxt); return qIter; }
@Test public void testNot() { Var a = new Var("a"); Formula f = (a.not()); assertTrue("!a".equals(f.toString())); }
/** Comparison operator */ public void operatorCompare( HplsqlParser.Bool_expr_binaryContext ctx, HplsqlParser.Bool_expr_binary_operatorContext op) { Var v1 = evalPop(ctx.expr(0)); Var v2 = evalPop(ctx.expr(1)); int cmp = v1.compareTo(v2); boolean bool = false; if (op.T_GREATER() != null) { if (cmp > 0) { bool = true; } } else if (op.T_GREATEREQUAL() != null) { if (cmp >= 0) { bool = true; } } if (op.T_LESS() != null) { if (cmp < 0) { bool = true; } } else if (op.T_LESSEQUAL() != null) { if (cmp <= 0) { bool = true; } } exec.stackPush(bool); }
@Override public ReferenceCollection getReferences(Var var) { if (!var.isGlobal()) { return null; } return refMap.get(var.getName()); }
@Test public void testAnd() { Var a = new Var("a"); Var b = new Var("b"); Formula f = (a.and(b)); assertTrue("a and b".equals(f.toString())); }
@Test public void testCreateFromConjunctionOfLiterals() { Var a = new Var("a"); Var b = new Var("b"); Phrase phrase = new Phrase(a.and(b)); assertTrue("a and b".equals(phrase.toString())); }
@Test public void testGetOp() { Var a = new Var("a"); Var b = new Var("b"); Formula f = (a.and(b)); assertTrue(f.getOp().equals(Formula.BOP.AND)); }
@Test public void testOr() { Var a = new Var("a"); Var b = new Var("b"); Formula f = (a.or(b)); assertTrue("a or b".equals(f.toString())); }
@Test public void testGetRight() { Var a = new Var("a"); Var b = new Var("b"); Formula f = (a.and(b)); assertTrue(f.getRight().equals(b)); }
/** * Resets global var reference map with the new provide map. * * @param globalRefMap The reference map result of a {@link ReferenceCollectingCallback} pass * collected from the whole AST. */ private void resetGlobalVarReferences(Map<Var, ReferenceCollection> globalRefMap) { refMap = new HashMap<>(); for (Entry<Var, ReferenceCollection> entry : globalRefMap.entrySet()) { Var var = entry.getKey(); if (var.isGlobal()) { refMap.put(var.getName(), entry.getValue()); } } }
@Test public void testIsLeaf() { Var a = new Var("a"); Var b = new Var("b"); Formula f = (a.and(b)); assertFalse(f.isLeaf()); assertTrue(f.getLeft().isLeaf() == true && f.getRight().isLeaf() == true); }
public JoodoServlet() { try { final Var var = loadVar("joodo.kake.servlet", "initialize-joodo-servlet"); var.invoke(this); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } }
/** Equality operator */ public void operatorEqual(HplsqlParser.Bool_expr_binaryContext ctx, boolean equal) { Var v1 = evalPop(ctx.expr(0)); Var v2 = evalPop(ctx.expr(1)); boolean eq = v1.equals(v2); if (!equal) { eq = !eq; } exec.stackPush(eq); }
@Override public Iterator<String> varNames() { List<String> x = new ArrayList<String>(); for (Iterator<Var> iter = binding.vars(); iter.hasNext(); ) { Var v = iter.next(); x.add(v.getVarName()); } return x.iterator(); }
@Override public VarDTO_Detailed getVarDetailed(long id, UserAccountDTO dto, UserHistoryDTO hist_dto) { Var v = em.find(Var.class, id); if (v != null) { v.setEM(em); if (dto != null && dto.getId() != 0 && dto.getCurrent_research() == 0) { dto.setCurrent_research(v.getResearch_id()); } return v.toDTO_Detailed(dto, hist_dto, em); } else return null; }
/** * Binds the specified value to a variable. * * @param name variable name * @param args argument array * @param value value to be bound * @throws QueryException query exception */ private void bind(final QNm name, final Expr[] args, final Value value) throws QueryException { // skip nulled values if (value == null) return; for (int i = 0; i < function.args.length; i++) { final Var var = function.args[i]; if (!var.name.eq(name)) continue; // casts and binds the value args[i] = var.checkType(value, context, null); break; } }
@Test public void testIsAtomic() { Var a = new Var("a"); Var b = new Var("b"); assertTrue(a.isAtomic()); // a literal is atomic assertTrue(a.not().isAtomic()); // a literal and its negation is atomic Formula f = (a.and(b)); assertFalse(f.isAtomic()); assertTrue(f.getLeft().isAtomic() == true && f.getRight().isAtomic() == true); }
@Test public void testHashCode() { Var a = new Var("a"); Var b = new Var("b"); Formula f = a.and(b); Formula g = a.and(b); assertTrue(f.hashCode() == g.hashCode()); // two distinct instances Formula h = b.and(a); assertFalse(f.hashCode() == h.hashCode()); // logically equivalent but different hash codes }
@Test public void testCreateFromNegationOfLiteral() { Var a = new Var("a"); Phrase phrase = new Phrase(a.not()); assertTrue("!a".equals(phrase.toString())); a = new Var(1); phrase = new Phrase(a.not()); assertTrue("!1".equals(phrase.toString())); a = new Var(true); phrase = new Phrase(a.not()); assertTrue("!true".equals(phrase.toString())); }
/** FETCH cursor statement */ public Integer fetch(HplsqlParser.Fetch_stmtContext ctx) { trace(ctx, "FETCH"); String name = ctx.L_ID(0).toString(); Var cursor = exec.findVariable(name); if (cursor == null || cursor.type != Type.CURSOR) { trace(ctx, "Cursor not found: " + name); exec.setSqlCode(-1); exec.signal(Signal.Type.SQLEXCEPTION); return 1; } else if (exec.getOffline()) { exec.setSqlCode(100); exec.signal(Signal.Type.NOTFOUND); return 0; } // Assign values from the row to local variables try { Query query = (Query) cursor.value; ResultSet rs = query.getResultSet(); ResultSetMetaData rsm = null; if (rs != null) { rsm = rs.getMetaData(); } if (rs != null && rsm != null) { int cols = ctx.L_ID().size() - 1; if (rs.next()) { for (int i = 1; i <= cols; i++) { Var var = exec.findVariable(ctx.L_ID(i).getText()); if (var != null) { var.setValue(rs, rsm, i); if (trace) { trace(ctx, "COLUMN: " + rsm.getColumnName(i) + ", " + rsm.getColumnTypeName(i)); trace(ctx, "SET " + var.getName() + " = " + var.toString()); } } else if (trace) { trace(ctx, "Variable not found: " + ctx.L_ID(i).getText()); } } exec.incRowCount(); exec.setSqlSuccess(); } else { exec.setSqlCode(100); exec.signal(Signal.Type.NOTFOUND); } } } catch (SQLException e) { exec.setSqlCode(e); exec.signal(Signal.Type.SQLEXCEPTION, e.getMessage(), e); } return 0; }
@Test public void testEquals() { Var a = new Var("a"); Var b = new Var("b"); Formula f = a.and(b); Formula g = a.and(b); assertFalse(f == g); // two distinct instances assertTrue(f.equals(g)); // objects equal Formula h = b.and(a); assertFalse(f.equals(h)); // logically equivalent but objects not equal }
@Override public ArrayList<VarDTO_Research> getVarsResearchNames(ArrayList<Long> keys) { ArrayList<VarDTO_Research> map = new ArrayList<VarDTO_Research>(); for (Long k : keys) { Var v = em.find(Var.class, k); if (v != null) { v.setEM(em); VarDTO_Detailed dto = v.toDTO_Detailed(null, null, em); map.add( new VarDTO_Research( dto.getId(), dto.getResearch_id().intValue(), dto.getResearch_name())); } } return map; }
@Override public String toString() { final TokenBuilder tb = new TokenBuilder().add(var.toString()).add(' ').add(ASSIGN); tb.add(' ').add(expr.toString()); if (coll != null) tb.add(' ').add(COLLATION).add(" \"").add(coll.uri()).add('"'); return tb.toString(); }
@Override public void plan(final FElem plan) { final FElem e = planElem(); var.plan(e); expr.plan(e); plan.add(e); }
/** VALUES statement */ public Integer values(HplsqlParser.Values_into_stmtContext ctx) { trace(ctx, "VALUES statement"); int cnt = ctx.ident().size(); // Number of variables and assignment expressions int ecnt = ctx.expr().size(); for (int i = 0; i < cnt; i++) { String name = ctx.ident(i).getText(); if (i < ecnt) { visit(ctx.expr(i)); Var var = exec.setVariable(name); if (trace) { trace(ctx, "SET " + name + " = " + var.toString()); } } } return 0; }
@Override public ArrayList<VarDTO_Light> getResearchVarsSummaries(long research_id) { // Variant 1 // ArrayList<VarDTO_Light> list = new ArrayList<VarDTO_Light>(); // SocioResearch dsResearch,detached; // try { // dsResearch = em.find(SocioResearch.class, research_id); // ArrayList<Long> var_ids = dsResearch.getVar_ids(); // for(Long var_id:var_ids) // { // Var var = em.find(Var.class,var_id); // list.add(var.toDTO_Light()); // } // } finally // { // } // Variant 2 // TypedQuery<Var> q = em.createQuery("SELECT x FROM Var x WHERE x.research_id = :id // ORDER BY x.id", Var.class); // q.setParameter("id", research_id); // List<Var> l = q.getResultList(); // for(Var v:l) // { // list.add(v.toDTO_Light()); // } // Variant 3 return Var.getResearchVarsLight(em, research_id); // throw new UnsupportedOperationException("Not supported yet."); }
@Test public void testToString() { Formula emptyFormula = new Formula(); assertTrue(emptyFormula.toString().equals(StringUtils.EMPTY)); Var a = new Var("a"); Var b = new Var("b"); Var c = new Var("c"); Var d = new Var("d"); Formula f = (a.and(b)).or(c.and(d)); assertTrue("(a and b) or (c and d)".equals(f.toString())); f = (a.not().or(b)).and(c); assertTrue("(!a or b) and c".equals(f.toString())); }