1 17 package org.apache.ws.jaxme.pm.impl; 18 19 import java.sql.Types ; 20 import java.util.ArrayList ; 21 import java.util.Calendar ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 25 import javax.xml.bind.DatatypeConverter; 26 import javax.xml.bind.JAXBException; 27 28 import org.apache.ws.jaxme.JMManager; 29 import org.apache.ws.jaxme.Observer; 30 import org.apache.ws.jaxme.PM; 31 import org.apache.ws.jaxme.PMException; 32 import org.apache.ws.jaxme.PMParams; 33 34 38 public abstract class PMImpl implements PM { 39 private JMManager manager; 40 41 43 public PMImpl() { 44 } 45 46 public void init(JMManager pManager) throws JAXBException { 47 manager = pManager; 48 } 49 50 public JMManager getManager() { 51 return manager; 52 } 53 54 public void select(Observer pObserver, String pQuery) throws JAXBException { 55 select(pObserver, pQuery, null); 56 } 57 58 public Iterator select(String pQuery) throws JAXBException { 59 return select(pQuery, null); 60 } 61 62 66 public static class ListObserver implements Observer { 67 private List list; 68 72 public ListObserver(List pList) { 73 list = pList; 74 } 75 public void notify(Object pObservable) { 76 list.add(pObservable); 77 } 78 } 79 80 public Iterator select(String pQuery, PMParams pPlaceHolderArgs) throws JAXBException { 81 List result = new ArrayList (); 82 ListObserver observer = new ListObserver(result); 83 select(observer, pQuery, pPlaceHolderArgs); 84 return result.iterator(); 85 } 86 87 public Object create() throws JAXBException { 88 return manager.getElementJ(); 89 } 90 91 protected String parseQuery(String pQuery, PMParams pPlaceHolderArgs) throws PMException { 92 if (pPlaceHolderArgs == null) { 93 return pQuery; 94 } 95 Iterator iter = pPlaceHolderArgs.getParams(); 96 if (!iter.hasNext()) { 97 return pQuery; 98 } 99 StringBuffer sb = new StringBuffer (); 100 boolean inStr = false; 101 char delim = 0; 102 for (int i = 0; i < pQuery.length(); i++) { 103 char c = pQuery.charAt(i); 104 if (inStr) { 105 if (c == delim) { 106 inStr = false; 107 } 108 sb.append(c); 109 } else { 110 switch (c) { 111 case '\'': 112 inStr = true; 113 delim = c; 114 sb.append(c); 115 break; 116 case '?': 117 if (!iter.hasNext()) { 118 throw new PMException("Number of placeholder marks exceeds number of actual parameters"); 119 } 120 PMParams.Param param = (PMParams.Param) iter.next(); 121 switch (param.getType()) { 122 case Types.VARCHAR: 123 sb.append('\''); 124 sb.append(param.getValue()); 125 sb.append('\''); 126 break; 127 case Types.BIGINT: 128 case Types.INTEGER: 129 case Types.SMALLINT: 130 case Types.TINYINT: 131 sb.append(param.getValue()); 132 break; 133 case Types.TIMESTAMP: 134 sb.append('\''); 135 sb.append(DatatypeConverter.printDateTime((Calendar ) param.getValue())); 136 sb.append('\''); 137 break; 138 case Types.DATE: 139 sb.append('\''); 140 sb.append(DatatypeConverter.printDate((Calendar ) param.getValue())); 141 sb.append('\''); 142 break; 143 case Types.TIME: 144 sb.append('\''); 145 sb.append(DatatypeConverter.printTime((Calendar ) param.getValue())); 146 sb.append('\''); 147 break; 148 default: 149 throw new PMException("Invalid parameter type: " + param.getType()); 150 } 151 break; 152 default: 153 sb.append(c); 154 break; 155 } 156 } 157 } 158 if (inStr) { 159 throw new PMException("Failed to parse query, expected trailing " + delim + " character: " + pQuery); 160 } 161 if (iter.hasNext()) { 162 throw new PMException("Number of actual parameters exceeds number of placeholder marks."); 163 } 164 return sb.toString(); 165 } 166 } 167 | Popular Tags |