1 23 24 package org.xquark.extractor.algebra; 25 26 import java.sql.Types ; 27 import java.util.*; 28 29 import org.xquark.extractor.common.Debug; 30 import org.xquark.jdbc.typing.DbType; 31 import org.xquark.schema.*; 32 import org.xquark.xquery.parser.QName; 33 import org.xquark.xquery.parser.XQueryException; 34 import org.xquark.xquery.parser.XQueryExpression; 35 import org.xquark.xquery.typing.*; 36 37 45 public final class TypeInterpreter extends DefaultQTypeVisitor 48 { 49 private static final String RCSRevision = "$Revision: 1.10 $"; 50 private static final String RCSName = "$Name: $"; 51 52 53 private static final int[] CODE_TABLE = { -1, Types.VARCHAR, Types.BIT, Types.DECIMAL, Types.DOUBLE, Types.FLOAT, -1, Types.TIMESTAMP, Types.TIME, Types.DATE, -1, -1, -1, -1, -1 - 1, -1, -1, -1, -1, -1 }; 54 55 private SqlType _type = null; 56 57 public TypeInterpreter() {} 58 59 private int sqlTypeCode(int schemaTypeCode) { 60 int retVal = -1; 61 if (schemaTypeCode < 1 || schemaTypeCode > 20) 62 retVal = -1; 63 else 64 retVal = CODE_TABLE[schemaTypeCode]; 65 66 return retVal; 67 } 68 69 public SqlType interprete(QType qtype) throws XQueryException { 70 Debug.assertTrue(null != qtype, "null != qtype"); 72 73 qtype.accept(this); 74 75 if (_type != null) { 76 if (qtype.isMultiple()) 77 _type.setMultiplicity(SqlType.MANY); 78 else 79 _type.setMultiplicity(SqlType.ONE); 80 } 81 82 return _type; 84 } 85 86 public void visit(QTypeAtom qtype) throws XQueryException { 87 89 _type = interprete(qtype.getSimpleType()); 90 91 } 93 94 public void visit(QTypeText qtype) throws XQueryException { 95 97 _type = interprete(qtype.getSimpleType()); 98 99 } 101 102 public void visit(QTypeElement qte) throws XQueryException { 103 Type type = qte.getType(); 105 if (type instanceof SimpleType) { SqlTypeAtom sqlAtom; 107 sqlAtom = (SqlTypeAtom) interprete((SimpleType) type); 108 if (null != sqlAtom) { 109 XQueryExpression name = qte.getName(); 111 Debug.assertTrue((name instanceof QName), "(name instanceof QName)"); 112 sqlAtom.setName(((QName) name).getName()); 113 _type = sqlAtom; 114 } 115 } 116 else _type = interprete((ComplexType) type); 118 119 } 121 122 public void visit(QTypeSequence qtype) throws XQueryException { 123 125 SqlType sqlType = interprete((QType) qtype.getList().get(0)); 126 if (sqlType.isRelation()) { 127 _type = VerifyRelation(qtype); 128 } else if (sqlType.isAtom()) { 129 _type = VerifyColumn(qtype); 130 } else { 131 Debug.assertTrue(false, "NYI"); 132 } 133 134 } 136 137 public void visit(QTypeDocument qtype) throws XQueryException { 138 140 if (qtype.isFromDocument()) 142 throw new XQueryException("Function doc is not supported"); 143 _type = interprete(qtype.getQType()); 144 145 } 147 148 private SqlType VerifyColumn(QTypeSequence qtype) throws XQueryException { 149 151 SqlTypeStructure retVal = new SqlTypeStructure(null); 152 153 SqlType sqlType = null; 154 Iterator iter = qtype.getList().iterator(); 155 while (iter.hasNext()) { 156 sqlType = interprete((QType) iter.next()); 158 if (null == sqlType) { 159 retVal = null; 160 break; 161 } 162 if (sqlType.isAtom()) { 163 retVal.addAttribute((SqlTypeAtom) sqlType); 164 } else if (sqlType.isTuple()) { 165 retVal.addAttributes(((SqlTypeStructure) sqlType).getAttributes()); 166 } else { 167 retVal = null; 168 break; 169 } 170 } 171 172 return retVal; 174 } 175 176 private SqlType VerifyRelation(QTypeSequence qtype) throws XQueryException { 177 SqlType retVal = null; 179 180 SqlType sqlType = null; 181 Iterator iter = qtype.getList().iterator(); 182 while (iter.hasNext()) { 183 sqlType = interprete((QType) iter.next()); 185 if (null == sqlType && !sqlType.isRelation()) { 186 retVal = null; 187 break; 188 } else if (null == retVal) { 189 retVal = sqlType; 190 } 191 192 } 193 194 return retVal; 196 } 197 198 private SqlType interprete(SimpleType stype) { 199 SqlTypeAtom retVal; 201 Debug.assertTrue(SimpleType.VARIETY_ATOMIC == stype.getVariety(), "SimpleType.VARIETY_ATOMIC==stype.getVariety()"); 202 203 DbType dt = new DbType(sqlTypeCode(stype.getPrimitive().getType())); 204 retVal = new SqlTypeAtom(dt, null, SqlType.ONE); 205 206 return retVal; 208 } 209 210 private SqlType interprete(ComplexType stype) { 211 213 SqlTypeStructure retVal = null; 214 retVal = new SqlTypeStructure(null, SqlType.ONE); 215 216 Collection elements = getElementDeclarations(stype); 217 ElementDeclaration element; 218 SqlType elementType; 219 Iterator iter = elements.iterator(); 220 Debug.assertTrue(iter.hasNext(), "iter.hasNext()"); 221 while (iter.hasNext()) { 222 element = (ElementDeclaration) iter.next(); 223 elementType = interprete(element); 224 if (elementType != null && elementType.isAtom()) { 225 retVal.addAttribute((SqlTypeAtom) elementType); 226 } else { 227 retVal = null; 228 break; 229 } 230 } 231 return retVal; 233 } 234 235 private SqlType interprete(ElementDeclaration stype) { 236 238 SqlType retVal = null; 239 240 Type schemaType = stype.getType(); 241 if (schemaType.isSimpleType()) { 242 retVal = interprete((SimpleType) schemaType); 243 ((SqlTypeAtom) retVal).setName(stype.getName()); 244 } else { 245 retVal = interprete((ComplexType) schemaType); 246 } 247 248 return retVal; 250 } 251 252 private List getElementDeclarations(ComplexType complextype) { 253 255 List retVal = new ArrayList(); 256 ContentModel contentmodel = complextype.getContentModel(); 257 Particle particle = (Particle) contentmodel.getModel(); 258 Object term = particle.getTerm(); 259 if (term instanceof ElementDeclaration) 260 retVal.add(term); 261 else if (term instanceof ModelGroup) { 262 ModelGroup modelgroup = (ModelGroup) term; 263 int compositor = modelgroup.getCompositor(); 264 Debug.assertTrue(compositor == ModelGroup.SEQUENCE, "compositor == ModelGroup.SEQUENCE"); 265 266 for (int i = 0; i < modelgroup.size(); i++) { 267 Particle part = (Particle) modelgroup.get(i); 268 ElementDeclaration elemDecl = (ElementDeclaration) part.getTerm(); 269 retVal.add(elemDecl); 270 } 271 } 272 273 return retVal; 275 } 276 } 277 | Popular Tags |