[Mulgara-svn] r1958 - in trunk/src/jar/resolver-relational/java/org/mulgara/resolver/relational: . d2rq

pag at mulgara.org pag at mulgara.org
Thu Jun 10 18:33:13 UTC 2010


Author: pag
Date: 2010-06-10 18:33:12 +0000 (Thu, 10 Jun 2010)
New Revision: 1958

Added:
   trunk/src/jar/resolver-relational/java/org/mulgara/resolver/relational/d2rq/DB2XSD.java
Modified:
   trunk/src/jar/resolver-relational/java/org/mulgara/resolver/relational/ColumnDesc.java
   trunk/src/jar/resolver-relational/java/org/mulgara/resolver/relational/LiteralDesc.java
   trunk/src/jar/resolver-relational/java/org/mulgara/resolver/relational/PatternDesc.java
   trunk/src/jar/resolver-relational/java/org/mulgara/resolver/relational/RelationalResolution.java
   trunk/src/jar/resolver-relational/java/org/mulgara/resolver/relational/VariableDesc.java
   trunk/src/jar/resolver-relational/java/org/mulgara/resolver/relational/d2rq/DatabaseElem.java
Log:
Added information indicating if a database is Oracle or not, with some differing date extraction code if it is. Added a mapping for Literals to map SQL types to XSD types.

Modified: trunk/src/jar/resolver-relational/java/org/mulgara/resolver/relational/ColumnDesc.java
===================================================================
--- trunk/src/jar/resolver-relational/java/org/mulgara/resolver/relational/ColumnDesc.java	2010-06-10 18:24:58 UTC (rev 1957)
+++ trunk/src/jar/resolver-relational/java/org/mulgara/resolver/relational/ColumnDesc.java	2010-06-10 18:33:12 UTC (rev 1958)
@@ -61,6 +61,7 @@
 import org.mulgara.resolver.relational.d2rq.DatatypePropertyBridgeElem;
 import org.mulgara.resolver.relational.d2rq.ObjectPropertyBridgeElem;
 import org.mulgara.resolver.relational.d2rq.TranslationTableElem;
+import org.mulgara.resolver.relational.d2rq.DatabaseElem.DBType;;
 
 public class ColumnDesc extends VariableDesc {
   @SuppressWarnings("unused")
@@ -72,29 +73,33 @@
   private TranslationTableElem ttable;
 
   private Class<? extends Node> resourceType;
+  private final DBType dbType;
 
-  public ColumnDesc(ClassMapElem classMap) {
+  public ColumnDesc(ClassMapElem classMap, DBType dbType) {
     super(classMap);
     column = classMap.uriColumn;
     table = RelationalResolver.parseTableFromColumn(column);
     resourceType = URIReference.class;
     ttable = classMap.translateWith;
+    this.dbType = dbType;
   }
 
-  public ColumnDesc(ObjectPropertyBridgeElem bridge) {
+  public ColumnDesc(ObjectPropertyBridgeElem bridge, DBType dbType) {
     super(bridge);
     this.column = bridge.column;
     this.table = RelationalResolver.parseTableFromColumn(this.column);
     this.resourceType = URIReference.class;
     ttable = bridge.translateWith;
+    this.dbType = dbType;
   }
 
-  public ColumnDesc(DatatypePropertyBridgeElem bridge) {
+  public ColumnDesc(DatatypePropertyBridgeElem bridge, DBType dbType) {
     super(bridge);
     this.column = bridge.column;
     this.table = RelationalResolver.parseTableFromColumn(this.column);
     this.resourceType = Literal.class;
     ttable = bridge.translateWith;
+    this.dbType = dbType;
   }
 
   public Node getNode(ResultSet result) throws SQLException, TuplesException {
@@ -130,7 +135,7 @@
     }
 
     // Handle multiple datatypes here
-    return getColumn() + " = " + encode(getColumn(), value);
+    return getColumn() + " = " + encode(getColumn(), value, dbType);
   }
 
   public Set<String> getTables() {

Modified: trunk/src/jar/resolver-relational/java/org/mulgara/resolver/relational/LiteralDesc.java
===================================================================
--- trunk/src/jar/resolver-relational/java/org/mulgara/resolver/relational/LiteralDesc.java	2010-06-10 18:24:58 UTC (rev 1957)
+++ trunk/src/jar/resolver-relational/java/org/mulgara/resolver/relational/LiteralDesc.java	2010-06-10 18:33:12 UTC (rev 1958)
@@ -42,7 +42,6 @@
 package org.mulgara.resolver.relational;
 
 import java.net.URI;
-import java.net.URISyntaxException;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.Collections;
@@ -57,6 +56,7 @@
 import org.mulgara.query.rdf.LiteralImpl;
 import org.mulgara.query.TuplesException;
 import org.mulgara.resolver.relational.d2rq.D2RQDefn;
+import org.mulgara.resolver.relational.d2rq.DB2XSD;
 import org.mulgara.util.StackTrace;
 
 public class LiteralDesc extends VariableDesc {
@@ -80,10 +80,9 @@
 
     try {
       return new URIReferenceImpl(new URI(value));
-    } catch (URISyntaxException eu) {
-      return new LiteralImpl(value);
-    } catch (IllegalArgumentException ei) {
-      return new LiteralImpl(value);
+    } catch (Exception eu) {
+      URI dataType = DB2XSD.get(result.getMetaData().getColumnType(index + 1));
+      return (dataType == null) ? new LiteralImpl(value) :new LiteralImpl(value, dataType) ;
     }
   }
 

Modified: trunk/src/jar/resolver-relational/java/org/mulgara/resolver/relational/PatternDesc.java
===================================================================
--- trunk/src/jar/resolver-relational/java/org/mulgara/resolver/relational/PatternDesc.java	2010-06-10 18:24:58 UTC (rev 1957)
+++ trunk/src/jar/resolver-relational/java/org/mulgara/resolver/relational/PatternDesc.java	2010-06-10 18:33:12 UTC (rev 1958)
@@ -67,6 +67,7 @@
 import org.mulgara.resolver.relational.d2rq.DatatypePropertyBridgeElem;
 import org.mulgara.resolver.relational.d2rq.ObjectPropertyBridgeElem;
 import org.mulgara.resolver.relational.d2rq.TranslationTableElem;
+import org.mulgara.resolver.relational.d2rq.DatabaseElem.DBType;
 
 
 public class PatternDesc extends VariableDesc {
@@ -87,18 +88,21 @@
   private Map<String,Integer> columnIndices;
 
   private TranslationTableElem ttable;
+  private final DBType dbType;
 
-  public PatternDesc(ClassMapElem cmap) {
+  public PatternDesc(ClassMapElem cmap, DBType dbType) {
     super(cmap);
     init(cmap.uriPattern, cmap.translateWith, URIReference.class);
+    this.dbType = dbType;
   }
 
-  public PatternDesc(ObjectPropertyBridgeElem bridge) {
+  public PatternDesc(ObjectPropertyBridgeElem bridge, DBType dbType) {
     super(bridge);
     init(bridge.pattern, bridge.translateWith, URIReference.class);
+    this.dbType = dbType;
   }
 
-  public PatternDesc(DatatypePropertyBridgeElem bridge) {
+  public PatternDesc(DatatypePropertyBridgeElem bridge, DBType dbType) {
     super(bridge);
     init(bridge.pattern, bridge.translateWith, Literal.class);
 
@@ -111,6 +115,7 @@
     } else if (bridge.lang != null) {
       lang = bridge.lang;
     }
+    this.dbType = dbType;
   }
 
   private void init(String rawPattern, TranslationTableElem ttable, Class<? extends Node> anticipatedClass) {
@@ -265,7 +270,7 @@
       // If column != null then the final pattern term was a column, and should match the remainder
       // of the value.
       if (column != null) {
-        terms.add(column + " = " + encode(column, value));
+        terms.add(column + " = " + encode(column, value, dbType));
       }
 
       String result = RelationalQuery.toList(terms, " AND ");

Modified: trunk/src/jar/resolver-relational/java/org/mulgara/resolver/relational/RelationalResolution.java
===================================================================
--- trunk/src/jar/resolver-relational/java/org/mulgara/resolver/relational/RelationalResolution.java	2010-06-10 18:24:58 UTC (rev 1957)
+++ trunk/src/jar/resolver-relational/java/org/mulgara/resolver/relational/RelationalResolution.java	2010-06-10 18:33:12 UTC (rev 1958)
@@ -460,9 +460,9 @@
     if (instance instanceof Variable) {
       VariableDesc desc;
       if (classMap.uriColumn != null) {
-        desc = new ColumnDesc(classMap);
+        desc = new ColumnDesc(classMap, defn.databaseDefn.dbType);
       } else if (classMap.uriPattern != null) {
-        desc = new PatternDesc(classMap);
+        desc = new PatternDesc(classMap, defn.databaseDefn.dbType);
       } else if (classMap.bNodeIdColumns != null) {
         desc = new BNodeDesc(classMap);
       } else {
@@ -485,9 +485,9 @@
     } else { // subj !instanceof Variable
       VariableDesc desc;
       if (classMap.uriColumn != null) {
-        desc = new ColumnDesc(classMap);
+        desc = new ColumnDesc(classMap, defn.databaseDefn.dbType);
       } else if (classMap.uriPattern != null) {
-        desc = new PatternDesc(classMap);
+        desc = new PatternDesc(classMap, defn.databaseDefn.dbType);
       } else {
         throw new TuplesException("Unknown class map definition type (not column or uripattern)");
       }
@@ -565,13 +565,13 @@
     if (propB instanceof ObjectPropertyBridgeElem) {
       ObjectPropertyBridgeElem ob = (ObjectPropertyBridgeElem)propB;
       if (ob.column != null) {
-        return new ColumnDesc(ob);
+        return new ColumnDesc(ob, defn.databaseDefn.dbType);
       } else if (ob.refersToClassMap != null) {
         VariableDesc cdesc;
         if (ob.refersToClassMap.uriColumn != null) {
-          cdesc = new ColumnDesc(ob.refersToClassMap);
+          cdesc = new ColumnDesc(ob.refersToClassMap, defn.databaseDefn.dbType);
         } else if (ob.refersToClassMap.uriPattern != null) {
-          cdesc =new PatternDesc(ob.refersToClassMap);
+          cdesc = new PatternDesc(ob.refersToClassMap, defn.databaseDefn.dbType);
         } else if (ob.refersToClassMap.bNodeIdColumns != null) {
           return null;  // ignore blanknodes until we figure out their interraction with restrict.
         } else {
@@ -582,16 +582,16 @@
 
         return cdesc;
       } else if (ob.pattern != null) {
-        return new PatternDesc(ob);
+        return new PatternDesc(ob, defn.databaseDefn.dbType);
       } else {
         throw new TuplesException("Unknown object property bridge type: " + propB);
       }
     } else if (propB instanceof DatatypePropertyBridgeElem) {
       DatatypePropertyBridgeElem db = (DatatypePropertyBridgeElem)propB;
       if (db.column != null) {
-        return new ColumnDesc(db);
+        return new ColumnDesc(db, defn.databaseDefn.dbType);
       } else if (db.pattern != null) {
-        return new PatternDesc(db);
+        return new PatternDesc(db, defn.databaseDefn.dbType);
       } else {
         throw new TuplesException("Unknown datatype property bridge type: " + propB);
       }
@@ -604,11 +604,11 @@
       throws TuplesException {
     if (propB != null) {
       if (propB.column != null) {
-        includeColumnBasedProperty(query, new ColumnDesc(propB), obj);
+        includeColumnBasedProperty(query, new ColumnDesc(propB, defn.databaseDefn.dbType), obj);
       } else if (propB.refersToClassMap != null) {
         includeInstanceQuery(query, obj, propB.refersToClassMap);
       } else if (propB.pattern != null) {
-        includePatternBasedProperty(query, new PatternDesc(propB), obj);
+        includePatternBasedProperty(query, new PatternDesc(propB, defn.databaseDefn.dbType), obj);
       } else {
         throw new TuplesException("Unknown object property bridge type: " + propB);
       }
@@ -619,9 +619,9 @@
       throws TuplesException {
     if (propB != null) {
       if (propB.column != null) {
-        includeColumnBasedProperty(query, new ColumnDesc(propB), obj);
+        includeColumnBasedProperty(query, new ColumnDesc(propB, defn.databaseDefn.dbType), obj);
       } else if (propB.pattern != null) {
-        includePatternBasedProperty(query, new PatternDesc(propB), obj);
+        includePatternBasedProperty(query, new PatternDesc(propB, defn.databaseDefn.dbType), obj);
       } else {
         throw new TuplesException("Unknown datatype property bridge type: " + propB);
       }

Modified: trunk/src/jar/resolver-relational/java/org/mulgara/resolver/relational/VariableDesc.java
===================================================================
--- trunk/src/jar/resolver-relational/java/org/mulgara/resolver/relational/VariableDesc.java	2010-06-10 18:24:58 UTC (rev 1957)
+++ trunk/src/jar/resolver-relational/java/org/mulgara/resolver/relational/VariableDesc.java	2010-06-10 18:33:12 UTC (rev 1958)
@@ -57,6 +57,8 @@
 import org.mulgara.resolver.relational.d2rq.D2RQDefn;
 import org.mulgara.resolver.relational.d2rq.ClassMapElem;
 import org.mulgara.resolver.relational.d2rq.PropertyBridgeElem;
+import org.mulgara.resolver.relational.d2rq.DatabaseElem.DBType;
+import static org.mulgara.resolver.relational.d2rq.DatabaseElem.DBType.*;
 
 public abstract class VariableDesc {
   /** Logger.  */
@@ -116,7 +118,7 @@
   /**
    * Encodes a value for inclusion in an sql query based on type.
    */
-  public String encode(String column, String rdfValue) {
+  public String encode(String column, String rdfValue, DBType dbType) {
     Object type = columnTypeMap.get(column);
     if (type == Constants.numericColumn) {
       return rdfValue;
@@ -125,10 +127,14 @@
       return "'" + rdfValue + "'";
     }
     if (type == Constants.dateColumn) {
-      return "date('" + rdfValue + "')";
+      if (dbType == oracle) {
+        return "TO_CHAR(TO_DATE('" + rdfValue + "'),'YYYY-MM-DD')";
+      } else {
+        return "date('" + rdfValue + "')";
+      }
     }
 
-    logger.warn("Column: " + column + " untyped in definition, unable to guarentee encoding for " + rdfValue);
+    logger.warn("Column: " + column + " untyped in definition, unable to guarantee encoding for " + rdfValue);
 
     return rdfValue;
   }

Added: trunk/src/jar/resolver-relational/java/org/mulgara/resolver/relational/d2rq/DB2XSD.java
===================================================================
--- trunk/src/jar/resolver-relational/java/org/mulgara/resolver/relational/d2rq/DB2XSD.java	                        (rev 0)
+++ trunk/src/jar/resolver-relational/java/org/mulgara/resolver/relational/d2rq/DB2XSD.java	2010-06-10 18:33:12 UTC (rev 1958)
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2010 Paul Gearon.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.mulgara.resolver.relational.d2rq;
+
+import java.net.URI;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.mulgara.query.rdf.XSD;
+
+import static java.sql.Types.*;
+
+/**
+ * Provides a mapping function between JDBC data types and XSD types.
+ *
+ * @created Jun 9, 2010
+ * @author Paul Gearon
+ */
+public class DB2XSD {
+
+  /** The mapping structure keyed on the integers found in java.sql.Types */
+  private static final Map<Integer,URI> typeMap = new HashMap<Integer,URI>();
+
+  static {
+    typeMap.put(DECIMAL, XSD.DOUBLE_URI);
+    typeMap.put(DOUBLE, XSD.DOUBLE_URI);
+    typeMap.put(FLOAT, XSD.FLOAT_URI);
+    typeMap.put(INTEGER, XSD.INT_URI);
+    typeMap.put(SMALLINT, XSD.INT_URI);
+    typeMap.put(TINYINT, XSD.INT_URI);
+    typeMap.put(DATE, XSD.DATE_URI);
+    typeMap.put(TIME, XSD.DATE_URI);
+    typeMap.put(TIMESTAMP, XSD.DATE_URI);
+  }
+
+  /**
+   * Retrieves a URI from the XSD namespace that can hold the data represented
+   * in JDBC. The JDBC type is identified by an integer from {@link java.sql.Types}
+   * @param dbt The database type identifier
+   * @return The URI of the required XSD type or <code>null</code> if none is found. 
+   */
+  public static URI get(int dbt) {
+    return typeMap.get(dbt);
+  }
+}

Modified: trunk/src/jar/resolver-relational/java/org/mulgara/resolver/relational/d2rq/DatabaseElem.java
===================================================================
--- trunk/src/jar/resolver-relational/java/org/mulgara/resolver/relational/d2rq/DatabaseElem.java	2010-06-10 18:24:58 UTC (rev 1957)
+++ trunk/src/jar/resolver-relational/java/org/mulgara/resolver/relational/d2rq/DatabaseElem.java	2010-06-10 18:33:12 UTC (rev 1958)
@@ -63,7 +63,10 @@
   public final List<String> numericColumns;
   public final List<String> textColumns;
   public final List<String> dateColumns;
+  public final DBType dbType;
 
+  public enum DBType { oracle, other };
+
   public DatabaseElem(Resolver resolver, ResolverSession session, long rdftype, long defModel) throws LocalizeException, QueryException, TuplesException, GlobalizeException {
     super(resolver, session);
     long[] dbs = getSubjects(rdftype, session.lookup(Constants.database), defModel);
@@ -83,6 +86,7 @@
     numericColumns = getStringObjects(database, Constants.numericColumn, model);
     textColumns = getStringObjects(database, Constants.textColumn, model);
     dateColumns = getStringObjects(database, Constants.dateColumn, model);
+    dbType = jdbcDriver.toLowerCase().contains("oracle") ? DBType.oracle : DBType.other;
 
     Map<String,URIReference> typeMap = new HashMap<String,URIReference>();
     // "_" is a dummy anonymous column to describe the types of literals within a query (always text)



More information about the Mulgara-svn mailing list