001: package org.mandarax.jdbc.server;
002:
003: import java.sql.Types;
004: import java.util.*;
005: import org.apache.commons.collections.iterators.FilterIterator;
006: import org.mandarax.kernel.*;
007: import org.mandarax.lib.LibObject;
008: import org.mandarax.util.logging.LogCategories;
009:
010: /**
011: * Some utilities related to the kb - jdbc mapping.
012: * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
013: * @version 3.3.2 <29 December 2004>
014: * @since 3.0
015: */
016:
017: public class JDBC2KBUtils implements LogCategories {
018:
019: static LogicFactory lfactory = LogicFactory.getDefaultFactory();
020:
021: /**
022: * Get the slot name for a predicate.
023: * @return a slot name
024: * @param predicate a predicate
025: * @param index an index
026: */
027: public static String getSlotName(Predicate predicate, int index) {
028: return predicate.getSlotNames()[index];
029: }
030:
031: /**
032: * Get the predicate with a certain name.
033: * @return a predicate or null if such a predicate does not exist
034: * @param kb a knowledge base
035: * @param name a predicate name
036: */
037: public static Predicate getPredicate(KnowledgeBase kb, String name) {
038: return kb.getPredicate(name);
039: }
040:
041: /**
042: * Get a list of predicates that can be used as tables in SQL queries.
043: * @return list of predicates
044: * @param kb a knowledge base
045: */
046: public static Iterator getExposedPredicates(KnowledgeBase kb) {
047: Iterator predicates = kb.predicates();
048: org.apache.commons.collections.Predicate condition = new org.apache.commons.collections.Predicate() {
049: public boolean evaluate(Object obj) {
050: if (obj instanceof Predicate) {
051: Predicate p = (Predicate) obj;
052: return !(LibObject.isLibPredicate(p));
053: }
054: return false;
055: }
056: };
057: Iterator filtered = new FilterIterator(predicates, condition);
058: return filtered;
059: }
060:
061: /**
062: * Get an iterator that lists all predicates found in a knowledge base.
063: * @param predicate
064: * @param name
065: * @return an iterator
066: */
067:
068: /**
069: * Get the slot number for a certain slot name.
070: * @return a slot index or -1 if no slot with this name exists
071: * @param predicate a predicate
072: * @param name a slot name
073: */
074: public static int getSlotNumber(Predicate predicate, String name) {
075: String[] slotNames = predicate.getSlotNames();
076: for (int i = 0; i < slotNames.length; i++) {
077: if (name.equals(slotNames[i]))
078: return i;
079: }
080: return -1;
081: }
082:
083: /**
084: * Get the list of query variables.
085: * @param query a query
086: * @return a list of variables
087: */
088: public static List getVariables(Query query) {
089: Fact[] facts = query.getFacts();
090: List vars = new ArrayList();
091: if (facts.length > 1)
092: LogCategories.LOG_JDBC
093: .warn("Try to get query variables for mandarax jdnc driver for queries with more than one fact");
094: for (int i = 0; i < facts.length; i++) {
095: Predicate p = facts[i].getPredicate();
096: Class[] structure = p.getStructure();
097: for (int j = 0; j < structure.length; j++) {
098: vars.add(lfactory.createVariableTerm(getSlotName(p, j),
099: structure[j]));
100: }
101: }
102: return vars;
103: }
104:
105: /**
106: * Specifies the SQL type associated with a java type.
107: * @see java.sql.Types
108: * @param jtype a java type
109: * @return SQL type from java.sql.Types
110: */
111: public static int getTypeMapping(Class jtype) {
112: if (jtype == String.class)
113: return Types.VARCHAR;
114: else if (jtype == Integer.class)
115: return Types.INTEGER;
116: else if (jtype == Double.class)
117: return Types.DOUBLE;
118: else if (jtype == Float.class)
119: return Types.FLOAT;
120: else if (jtype == Short.class)
121: return Types.SMALLINT;
122: else if (jtype == Long.class)
123: return Types.BIGINT;
124: else if (jtype == Boolean.class)
125: return Types.BOOLEAN;
126: else if (jtype == java.sql.Date.class)
127: return Types.DATE;
128: // warning: util.Date instance also handled as dates !!
129: // TODO more testing and investigation of side effects
130: else if (jtype == java.util.Date.class)
131: return Types.DATE;
132: else if (jtype == java.sql.Time.class)
133: return Types.TIME;
134: else if (jtype == java.sql.Timestamp.class)
135: return Types.TIMESTAMP;
136: else
137: return Types.JAVA_OBJECT;
138: }
139: }
|