01: /*
02: * Copyright Aduna (http://www.aduna-software.com/) (c) 2008.
03: *
04: * Licensed under the Aduna BSD-style license.
05: */
06: package org.openrdf.sail.rdbms.postgresql;
07:
08: import org.openrdf.query.BindingSet;
09: import org.openrdf.query.Dataset;
10: import org.openrdf.query.algebra.TupleExpr;
11: import org.openrdf.query.algebra.evaluation.QueryOptimizer;
12: import org.openrdf.sail.rdbms.algebra.SqlConcat;
13: import org.openrdf.sail.rdbms.algebra.SqlRegex;
14: import org.openrdf.sail.rdbms.algebra.SqlNull;
15: import org.openrdf.sail.rdbms.algebra.StringValue;
16: import org.openrdf.sail.rdbms.algebra.base.RdbmsQueryModelVisitorBase;
17: import org.openrdf.sail.rdbms.algebra.base.SqlExpr;
18:
19: /**
20: * Moves the regular expression flags into the pattern string as per the
21: * PostgreSQL syntax.
22: *
23: * @author James Leigh
24: *
25: */
26: public class PgSqlRegexFlagsInliner extends
27: RdbmsQueryModelVisitorBase<RuntimeException> implements
28: QueryOptimizer {
29:
30: public void optimize(TupleExpr tupleExpr, Dataset dataset,
31: BindingSet bindings) {
32: tupleExpr.visit(this );
33: }
34:
35: @Override
36: public void meet(SqlRegex node) throws RuntimeException {
37: super .meet(node);
38: SqlExpr flags = node.getFlagsArg();
39: if (!(flags instanceof SqlNull)) {
40: SqlExpr pattern = node.getPatternArg();
41: SqlExpr prefix = concat(str("(?"), flags, str(")"));
42: pattern.replaceWith(concat(prefix, pattern.clone()));
43: node.setFlagsArg(null);
44: }
45: }
46:
47: private SqlExpr str(String string) {
48: return new StringValue(string);
49: }
50:
51: private SqlExpr concat(SqlExpr... exprs) {
52: SqlExpr concat = null;
53: for (SqlExpr expr : exprs) {
54: if (concat == null) {
55: concat = expr;
56: } else {
57: concat = new SqlConcat(concat, expr);
58: }
59: }
60: return concat;
61: }
62: }
|