001    package com.mockrunner.mock.jdbc;
002    
003    import java.io.ByteArrayInputStream;
004    import java.io.InputStream;
005    import java.io.Reader;
006    import java.io.StringReader;
007    import java.io.UnsupportedEncodingException;
008    import java.math.BigDecimal;
009    import java.net.MalformedURLException;
010    import java.net.URL;
011    import java.sql.Array;
012    import java.sql.Blob;
013    import java.sql.CallableStatement;
014    import java.sql.Clob;
015    import java.sql.Connection;
016    import java.sql.Date;
017    import java.sql.NClob;
018    import java.sql.Ref;
019    import java.sql.ResultSet;
020    import java.sql.RowId;
021    import java.sql.SQLException;
022    import java.sql.SQLXML;
023    import java.sql.Time;
024    import java.sql.Timestamp;
025    import java.util.ArrayList;
026    import java.util.Calendar;
027    import java.util.Collections;
028    import java.util.HashMap;
029    import java.util.HashSet;
030    import java.util.Iterator;
031    import java.util.List;
032    import java.util.Map;
033    import java.util.Set;
034    
035    import com.mockrunner.base.NestedApplicationException;
036    import com.mockrunner.jdbc.AbstractOutParameterResultSetHandler;
037    import com.mockrunner.util.common.StreamUtil;
038    
039    /**
040     * Mock implementation of <code>CallableStatement</code>.
041     */
042    public class MockCallableStatement extends MockPreparedStatement implements CallableStatement
043    {
044        private AbstractOutParameterResultSetHandler resultSetHandler;
045        private Map paramObjects = new HashMap();
046        private Set registeredOutParameterSetIndexed = new HashSet();
047        private Set registeredOutParameterSetNamed = new HashSet();
048        private List batchParameters = new ArrayList();
049        private Map lastOutParameters = null;
050        private boolean wasNull = false;
051        
052        public MockCallableStatement(Connection connection, String sql)
053        {
054            super(connection, sql);
055        }
056    
057        public MockCallableStatement(Connection connection, String sql, int resultSetType, int resultSetConcurrency)
058        {
059            super(connection, sql, resultSetType, resultSetConcurrency);
060        }
061    
062        public MockCallableStatement(Connection connection, String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)
063        {
064            super(connection, sql, resultSetType, resultSetConcurrency, resultSetHoldability);
065        }
066        
067        public void setCallableStatementResultSetHandler(AbstractOutParameterResultSetHandler resultSetHandler)
068        {
069            super.setPreparedStatementResultSetHandler(resultSetHandler);
070            this.resultSetHandler = resultSetHandler;
071        }
072        
073        public Map getNamedParameterMap()
074        {
075            return Collections.unmodifiableMap(paramObjects);
076        }
077        
078        public Map getParameterMap()
079        {
080            Map parameterMap = new HashMap(getIndexedParameterMap());
081            parameterMap.putAll(getNamedParameterMap());
082            return Collections.unmodifiableMap(parameterMap);
083        }
084        
085        public Object getParameter(String name)
086        {
087            return paramObjects.get(name);
088        }
089        
090        public void clearParameters() throws SQLException
091        {
092            super.clearParameters();
093            paramObjects.clear();
094        }
095        
096        public Set getNamedRegisteredOutParameterSet()
097        {
098            return Collections.unmodifiableSet(registeredOutParameterSetNamed);
099        }
100        
101        public boolean isOutParameterRegistered(int index)
102        {
103            return registeredOutParameterSetIndexed.contains(new Integer(index));
104        }
105        
106        public Set getIndexedRegisteredOutParameterSet()
107        {
108            return Collections.unmodifiableSet(registeredOutParameterSetIndexed);
109        }
110        
111        public boolean isOutParameterRegistered(String parameterName)
112        {
113            return registeredOutParameterSetNamed.contains(parameterName);
114        }
115        
116        public void clearRegisteredOutParameter()
117        {
118            registeredOutParameterSetIndexed.clear();
119            registeredOutParameterSetNamed.clear();
120        }
121        
122        public ResultSet executeQuery() throws SQLException
123        {
124            ResultSet resultSet = executeQuery(getParameterMap());
125            lastOutParameters = getOutParameterMap();
126            return resultSet;
127        }
128        
129        public int executeUpdate() throws SQLException
130        {
131            int updateCount = executeUpdate(getParameterMap());
132            lastOutParameters = getOutParameterMap();
133            return updateCount;
134        }
135        
136        public void addBatch() throws SQLException
137        {
138            batchParameters.add(new HashMap(getParameterMap()));
139        }
140    
141        public int[] executeBatch() throws SQLException
142        {
143            return executeBatch(batchParameters);
144        }
145        
146        public void registerOutParameter(int parameterIndex, int sqlType) throws SQLException
147        {
148            registeredOutParameterSetIndexed.add(new Integer(parameterIndex));
149        }
150    
151        public void registerOutParameter(int parameterIndex, int sqlType, int scale) throws SQLException
152        {
153            registerOutParameter(parameterIndex, sqlType);
154        }
155    
156        public void registerOutParameter(int parameterIndex, int sqlType, String typeName) throws SQLException
157        {
158            registerOutParameter(parameterIndex, sqlType);
159        }
160        
161        public void registerOutParameter(String parameterName, int sqlType) throws SQLException
162        {
163            registeredOutParameterSetNamed.add(parameterName);
164        }
165        
166        public void registerOutParameter(String parameterName, int sqlType, int scale) throws SQLException
167        {
168            registerOutParameter(parameterName, sqlType);
169        }
170        
171        public void registerOutParameter(String parameterName, int sqlType, String typeName) throws SQLException
172        {
173            registerOutParameter(parameterName, sqlType);
174        }
175            
176        public boolean wasNull() throws SQLException
177        {
178            return wasNull;
179        }
180        
181        public Object getObject(int parameterIndex) throws SQLException
182        {
183            wasNull = false;
184            Object returnValue = null;
185            if(null != lastOutParameters)
186            {
187                returnValue = lastOutParameters.get(new Integer(parameterIndex));
188            }
189            if(null == returnValue) wasNull = true;
190            return returnValue;
191        }
192        
193        public Object getObject(int parameterIndex, Map map) throws SQLException
194        {
195            return getObject(parameterIndex);
196        }
197    
198        public byte getByte(int parameterIndex) throws SQLException
199        {
200            Object value = getObject(parameterIndex);
201            if(null != value)
202            {
203                if(value instanceof Number) return ((Number)value).byteValue();
204                return new Byte(value.toString()).byteValue();
205            }
206            return 0;
207        }
208    
209        public double getDouble(int parameterIndex) throws SQLException
210        {
211            Object value = getObject(parameterIndex);
212            if(null != value)
213            {
214                if(value instanceof Number) return ((Number)value).doubleValue();
215                return new Double(value.toString()).doubleValue();
216            }
217            return 0;
218        }
219    
220        public float getFloat(int parameterIndex) throws SQLException
221        {
222            Object value = getObject(parameterIndex);
223            if(null != value)
224            {
225                if(value instanceof Number) return ((Number)value).floatValue();
226                return new Float(value.toString()).floatValue();
227            }
228            return 0;
229        }
230    
231        public int getInt(int parameterIndex) throws SQLException
232        {
233            Object value = getObject(parameterIndex);
234            if(null != value)
235            {
236                if(value instanceof Number) return ((Number)value).intValue();
237                return new Integer(value.toString()).intValue();
238            }
239            return 0;
240        }
241    
242        public long getLong(int parameterIndex) throws SQLException
243        {
244            Object value = getObject(parameterIndex);
245            if(null != value)
246            {
247                if(value instanceof Number) return ((Number)value).longValue();
248                return new Long(value.toString()).longValue();
249            }
250            return 0;
251        }
252    
253        public short getShort(int parameterIndex) throws SQLException
254        {
255            Object value = getObject(parameterIndex);
256            if(null != value)
257            {
258                if(value instanceof Number) return ((Number)value).shortValue();
259                return new Short(value.toString()).shortValue();
260            }
261            return 0;
262        }
263    
264        public boolean getBoolean(int parameterIndex) throws SQLException
265        {
266            Object value = getObject(parameterIndex);
267            if(null != value)
268            {
269                if(value instanceof Boolean) return ((Boolean)value).booleanValue();
270                return new Boolean(value.toString()).booleanValue();
271            }
272            return false;
273        }
274    
275        public byte[] getBytes(int parameterIndex) throws SQLException
276        {
277            Object value = getObject(parameterIndex);
278            if(null != value)
279            {
280                if(value instanceof byte[]) return (byte[])value;
281                try
282                {
283                    return value.toString().getBytes("ISO-8859-1");
284                } 
285                catch(UnsupportedEncodingException exc)
286                {
287                    throw new NestedApplicationException(exc);
288                }
289            }
290            return null;
291        }
292    
293        public String getString(int parameterIndex) throws SQLException
294        {
295            Object value = getObject(parameterIndex);
296            if(null != value) return value.toString();
297            return null;
298        }
299        
300        public String getNString(int parameterIndex) throws SQLException
301        {
302            return getString(parameterIndex);
303        }
304    
305        public BigDecimal getBigDecimal(int parameterIndex) throws SQLException
306        {
307            Object value = getObject(parameterIndex);
308            if(null != value)
309            {
310                if(value instanceof Number) return new BigDecimal(((Number)value).doubleValue());
311                return new BigDecimal(value.toString());
312            }
313            return null;
314        }
315    
316        public BigDecimal getBigDecimal(int parameterIndex, int scale) throws SQLException
317        {
318            return getBigDecimal(parameterIndex);
319        }
320    
321        public URL getURL(int parameterIndex) throws SQLException
322        {
323            Object value = getObject(parameterIndex);
324            if(null != value)
325            {
326                if(value instanceof URL) return (URL)value;
327                try
328                {
329                    return new URL(value.toString());
330                }
331                catch(MalformedURLException exc)
332                {
333                
334                }
335            }
336            return null;
337        }
338    
339        public Array getArray(int parameterIndex) throws SQLException
340        {
341            Object value = getObject(parameterIndex);
342            if(null != value)
343            {
344                if(value instanceof Array) return (Array)value;
345                return new MockArray(value);
346            }
347            return null;
348        }
349    
350        public Blob getBlob(int parameterIndex) throws SQLException
351        {
352            Object value = getObject(parameterIndex);
353            if(null != value)
354            {
355                if(value instanceof Blob) return (Blob)value;
356                return new MockBlob(getBytes(parameterIndex));
357            }
358            return null;
359        }
360    
361        public Clob getClob(int parameterIndex) throws SQLException
362        {
363            Object value = getObject(parameterIndex);
364            if(null != value)
365            {
366                if(value instanceof Clob) return (Clob)value;
367                return new MockClob(getString(parameterIndex));
368            }
369            return null;
370        }
371    
372        public NClob getNClob(int parameterIndex) throws SQLException
373        {
374            Object value = getObject(parameterIndex);
375            if(null != value)
376            {
377                if(value instanceof NClob) return (NClob)value;
378                if(value instanceof Clob) return getNClobFromClob((Clob)value);
379                return new MockNClob(getString(parameterIndex));
380            }
381            return null;
382        }
383    
384        public SQLXML getSQLXML(int parameterIndex) throws SQLException
385        {
386            Object value = getObject(parameterIndex);
387            if(null != value)
388            {
389                if(value instanceof SQLXML) return (SQLXML)value;
390                return new MockSQLXML(getString(parameterIndex));
391            }
392            return null;
393        }
394    
395        public Reader getCharacterStream(int parameterIndex) throws SQLException
396        {
397            Object value = getObject(parameterIndex);
398            if(null != value)
399            {
400                if(value instanceof Reader) return (Reader)value;
401                return new StringReader(getString(parameterIndex));
402            }
403            return null;
404        }
405    
406        public Reader getNCharacterStream(int parameterIndex) throws SQLException
407        {
408            return getCharacterStream(parameterIndex);
409        }
410    
411        public Date getDate(int parameterIndex) throws SQLException
412        {
413            Object value = getObject(parameterIndex);
414            if(null != value)
415            {
416                if(value instanceof Date) return (Date)value;
417                return Date.valueOf(value.toString());
418            }
419            return null;
420        }
421        
422        public Date getDate(int parameterIndex, Calendar calendar) throws SQLException
423        {
424            return getDate(parameterIndex);
425        }
426    
427        public Ref getRef(int parameterIndex) throws SQLException
428        {
429            Object value = getObject(parameterIndex);
430            if(null != value)
431            {
432                if(value instanceof Ref) return (Ref)value;
433                return new MockRef(value);
434            }
435            return null;
436        }
437    
438        public Time getTime(int parameterIndex) throws SQLException
439        {
440            Object value = getObject(parameterIndex);
441            if(null != value)
442            {
443                if(value instanceof Time) return (Time)value;
444                return Time.valueOf(value.toString());
445            }
446            return null;
447        }
448        
449        public Time getTime(int parameterIndex, Calendar calendar) throws SQLException
450        {
451            return getTime(parameterIndex);
452        }
453    
454        public Timestamp getTimestamp(int parameterIndex) throws SQLException
455        {
456            Object value = getObject(parameterIndex);
457            if(null != value)
458            {
459                if(value instanceof Timestamp) return (Timestamp)value;
460                return Timestamp.valueOf(value.toString());
461            }
462            return null;
463        }
464        
465        public Timestamp getTimestamp(int parameterIndex, Calendar calendar) throws SQLException
466        {
467            return getTimestamp(parameterIndex);
468        }
469        
470        public RowId getRowId(int parameterIndex) throws SQLException
471        {
472            Object value = getObject(parameterIndex);
473            if(null != value)
474            {
475                if(value instanceof RowId) return (RowId)value;
476                return new MockRowId(getBytes(parameterIndex));
477            }
478            return null;
479        }
480        
481        public Object getObject(String parameterName) throws SQLException
482        {
483            wasNull = false;
484            Object returnValue = null;
485            if(null != lastOutParameters)
486            {
487                returnValue = lastOutParameters.get(parameterName);
488            }
489            if(null == returnValue) wasNull = true;
490            return returnValue;
491        }
492        
493        public Object getObject(String parameterName, Map map) throws SQLException
494        {
495            return getObject(parameterName);
496        }
497        
498        public byte getByte(String parameterName) throws SQLException
499        {
500            Object value = getObject(parameterName);
501            if(null != value)
502            {
503                if(value instanceof Number) return ((Number)value).byteValue();
504                return new Byte(value.toString()).byteValue();
505            }
506            return 0;
507        }
508    
509        public double getDouble(String parameterName) throws SQLException
510        {
511            Object value = getObject(parameterName);
512            if(null != value)
513            {
514                if(value instanceof Number) return ((Number)value).doubleValue();
515                return new Double(value.toString()).doubleValue();
516            }
517            return 0;
518        }
519    
520        public float getFloat(String parameterName) throws SQLException
521        {
522            Object value = getObject(parameterName);
523            if(null != value)
524            {
525                if(value instanceof Number) return ((Number)value).floatValue();
526                return new Float(value.toString()).floatValue();
527            }
528            return 0;
529        }
530    
531        public int getInt(String parameterName) throws SQLException
532        {
533            Object value = getObject(parameterName);
534            if(null != value)
535            {
536                if(value instanceof Number) return ((Number)value).intValue();
537                return new Integer(value.toString()).intValue();
538            }
539            return 0;
540        }
541    
542        public long getLong(String parameterName) throws SQLException
543        {
544            Object value = getObject(parameterName);
545            if(null != value)
546            {
547                if(value instanceof Number) return ((Number)value).longValue();
548                return new Long(value.toString()).longValue();
549            }
550            return 0;
551        }
552    
553        public short getShort(String parameterName) throws SQLException
554        {
555            Object value = getObject(parameterName);
556            if(null != value)
557            {
558                if(value instanceof Number) return ((Number)value).shortValue();
559                return new Short(value.toString()).shortValue();
560            }
561            return 0;
562        }
563    
564        public boolean getBoolean(String parameterName) throws SQLException
565        {
566            Object value = getObject(parameterName);
567            if(null != value)
568            {
569                if(value instanceof Boolean) return ((Boolean)value).booleanValue();
570                return new Boolean(value.toString()).booleanValue();
571            }
572            return false;
573        }
574    
575        public byte[] getBytes(String parameterName) throws SQLException
576        {
577            Object value = getObject(parameterName);
578            if(null != value)
579            {
580                if(value instanceof byte[]) return (byte[])value;
581                try
582                {
583                    return value.toString().getBytes("ISO-8859-1");
584                } 
585                catch(UnsupportedEncodingException exc)
586                {
587                    throw new NestedApplicationException(exc);
588                }
589            }
590            return null;
591        }
592        
593        public String getString(String parameterName) throws SQLException
594        {
595            Object value = getObject(parameterName);
596            if(null != value) return value.toString();
597            return null;
598        }
599    
600        public String getNString(String parameterName) throws SQLException
601        {
602            return getString(parameterName);
603        }
604    
605        public BigDecimal getBigDecimal(String parameterName) throws SQLException
606        {
607            Object value = getObject(parameterName);
608            if(null != value)
609            {
610                if(value instanceof Number) return new BigDecimal(((Number)value).doubleValue());
611                return new BigDecimal(value.toString());
612            }
613            return null;
614        }
615        
616        public URL getURL(String parameterName) throws SQLException
617        {
618            Object value = getObject(parameterName);
619            if(null != value)
620            {
621                if(value instanceof URL) return (URL)value;
622                try
623                {
624                    return new URL(value.toString());
625                }
626                catch(MalformedURLException exc)
627                {
628                
629                }
630            }
631            return null;
632        }
633        
634        public Array getArray(String parameterName) throws SQLException
635        {
636            Object value = getObject(parameterName);
637            if(null != value)
638            {
639                if(value instanceof Array) return (Array)value;
640                return new MockArray(value);
641            }
642            return null;
643        }
644    
645        public Blob getBlob(String parameterName) throws SQLException
646        {
647            Object value = getObject(parameterName);
648            if(null != value)
649            {
650                if(value instanceof Blob) return (Blob)value;
651                return new MockBlob(getBytes(parameterName));
652            }
653            return null;
654        }
655    
656        public Clob getClob(String parameterName) throws SQLException
657        {
658            Object value = getObject(parameterName);
659            if(null != value)
660            {
661                if(value instanceof Clob) return (Clob)value;
662                return new MockClob(getString(parameterName));
663            }
664            return null;
665        }
666    
667        public NClob getNClob(String parameterName) throws SQLException
668        {
669            Object value = getObject(parameterName);
670            if(null != value)
671            {
672                if(value instanceof NClob) return (NClob)value;
673                if(value instanceof Clob) return getNClobFromClob((Clob)value);
674                return new MockNClob(getString(parameterName));
675            }
676            return null;
677        }
678    
679        public SQLXML getSQLXML(String parameterName) throws SQLException
680        {
681            Object value = getObject(parameterName);
682            if(null != value)
683            {
684                if(value instanceof SQLXML) return (SQLXML)value;
685                return new MockSQLXML(getString(parameterName));
686            }
687            return null;
688        }
689    
690        public Reader getCharacterStream(String parameterName) throws SQLException
691        {
692            Object value = getObject(parameterName);
693            if(null != value)
694            {
695                if(value instanceof Reader) return (Reader)value;
696                return new StringReader(getString(parameterName));
697            }
698            return null;
699        }
700    
701        public Reader getNCharacterStream(String parameterName) throws SQLException
702        {
703            return getCharacterStream(parameterName);
704        }
705    
706        public Date getDate(String parameterName) throws SQLException
707        {
708            Object value = getObject(parameterName);
709            if(null != value)
710            {
711                if(value instanceof Date) return (Date)value;
712                return Date.valueOf(value.toString());
713            }
714            return null;
715        }
716        
717        public Date getDate(String parameterName, Calendar calendar) throws SQLException
718        {
719            return getDate(parameterName);
720        }
721        
722        public Ref getRef(String parameterName) throws SQLException
723        {
724            Object value = getObject(parameterName);
725            if(null != value)
726            {
727                if(value instanceof Ref) return (Ref)value;
728                return new MockRef(value);
729            }
730            return null;
731        }
732    
733        public Time getTime(String parameterName) throws SQLException
734        {
735            Object value = getObject(parameterName);
736            if(null != value)
737            {
738                if(value instanceof Time) return (Time)value;
739                return Time.valueOf(value.toString());
740            }
741            return null;
742        }
743    
744        public Time getTime(String parameterName, Calendar calendar) throws SQLException
745        {
746            return getTime(parameterName);
747        }
748        
749        public Timestamp getTimestamp(String parameterName) throws SQLException
750        {
751            Object value = getObject(parameterName);
752            if(null != value)
753            {
754                if(value instanceof Timestamp) return (Timestamp)value;
755                return Timestamp.valueOf(value.toString());
756            }
757            return null;
758        }
759    
760        public Timestamp getTimestamp(String parameterName, Calendar calendar) throws SQLException
761        {
762            return getTimestamp(parameterName);
763        }
764        
765        public RowId getRowId(String parameterName) throws SQLException
766        {
767            Object value = getObject(parameterName);
768            if(null != value)
769            {
770                if(value instanceof RowId) return (RowId)value;
771                return new MockRowId(getBytes(parameterName));
772            }
773            return null;
774        }
775    
776        public void setByte(String parameterName, byte byteValue) throws SQLException
777        {
778            setObject(parameterName, new Byte(byteValue));
779        }
780    
781        public void setDouble(String parameterName, double doubleValue) throws SQLException
782        {
783            setObject(parameterName, new Double(doubleValue));
784        }
785    
786        public void setFloat(String parameterName, float floatValue) throws SQLException
787        {
788            setObject(parameterName, new Float(floatValue));
789        }
790    
791        public void setInt(String parameterName, int intValue) throws SQLException
792        {
793            setObject(parameterName, new Integer(intValue));
794        }
795    
796        public void setNull(String parameterName, int sqlType) throws SQLException
797        {
798            setObject(parameterName, null);
799        }
800        
801        public void setNull(String parameterName, int sqlType, String typeName) throws SQLException
802        {
803            setNull(parameterName, sqlType);
804        }
805    
806        public void setLong(String parameterName, long longValue) throws SQLException
807        {
808            setObject(parameterName, new Long(longValue));
809        }
810    
811        public void setShort(String parameterName, short shortValue) throws SQLException
812        {
813            setObject(parameterName, new Short(shortValue));
814        }
815    
816        public void setBoolean(String parameterName, boolean booleanValue) throws SQLException
817        {
818            setObject(parameterName, new Boolean(booleanValue));
819        }
820    
821        public void setBytes(String parameterName, byte[] byteArray) throws SQLException
822        {
823            setObject(parameterName, byteArray);
824        }
825        
826        public void setAsciiStream(String parameterName, InputStream stream) throws SQLException
827        {
828            setBinaryStream(parameterName, stream);
829        }
830    
831        public void setAsciiStream(String parameterName, InputStream stream, int length) throws SQLException
832        {
833            setBinaryStream(parameterName, stream, length);
834        }
835        
836        public void setAsciiStream(String parameterName, InputStream stream, long length) throws SQLException
837        {
838            setBinaryStream(parameterName, stream, length);
839        }
840    
841        public void setBinaryStream(String parameterName, InputStream stream) throws SQLException
842        {
843            byte[] data = StreamUtil.getStreamAsByteArray(stream);
844            setObject(parameterName, new ByteArrayInputStream(data));
845        }
846    
847        public void setBinaryStream(String parameterName, InputStream stream, int length) throws SQLException
848        {
849            byte[] data = StreamUtil.getStreamAsByteArray(stream, length);
850            setObject(parameterName, new ByteArrayInputStream(data));
851        }
852    
853        public void setBinaryStream(String parameterName, InputStream stream, long length) throws SQLException
854        {
855            setBinaryStream(parameterName, stream, (int)length);
856        }
857        
858        public void setCharacterStream(String parameterName, Reader reader) throws SQLException
859        {
860            String data = StreamUtil.getReaderAsString(reader);
861            setObject(parameterName, new StringReader(data));
862        }
863    
864        public void setCharacterStream(String parameterName, Reader reader, int length) throws SQLException
865        {
866            String data = StreamUtil.getReaderAsString(reader, length);
867            setObject(parameterName, new StringReader(data));
868        }
869    
870        public void setCharacterStream(String parameterName, Reader reader, long length) throws SQLException
871        {
872            setCharacterStream(parameterName, reader, (int)length);
873        }
874        
875        public void setNCharacterStream(String parameterName, Reader reader) throws SQLException
876        {
877            setCharacterStream(parameterName, reader);
878        }
879    
880        public void setNCharacterStream(String parameterName, Reader reader, long length) throws SQLException
881        {
882            setCharacterStream(parameterName, reader, length);
883        }
884    
885        public void setBlob(String parameterName, Blob blob) throws SQLException
886        {
887            setObject(parameterName, blob);
888        }
889        
890        public void setBlob(String parameterName, InputStream inputStream) throws SQLException
891        {
892            byte[] data = StreamUtil.getStreamAsByteArray(inputStream);
893            setBlob(parameterName, new MockBlob(data));
894        }
895        
896        public void setBlob(String parameterName, InputStream inputStream, long length) throws SQLException
897        {
898            byte[] data = StreamUtil.getStreamAsByteArray(inputStream, (int)length);
899            setBlob(parameterName, new MockBlob(data));
900        }
901    
902        public void setClob(String parameterName, Clob clob) throws SQLException
903        {
904            setObject(parameterName, clob);
905        }
906        
907        public void setClob(String parameterName, Reader reader) throws SQLException
908        {
909            String data = StreamUtil.getReaderAsString(reader);
910            setClob(parameterName, new MockClob(data));
911        }
912        
913        public void setClob(String parameterName, Reader reader, long length) throws SQLException
914        {
915            String data = StreamUtil.getReaderAsString(reader, (int)length);
916            setClob(parameterName, new MockClob(data));
917        }
918    
919        public void setNClob(String parameterName, NClob nClob) throws SQLException
920        {
921            setObject(parameterName, nClob);
922        }
923    
924        public void setNClob(String parameterName, Reader reader) throws SQLException
925        {
926            String data = StreamUtil.getReaderAsString(reader);
927            setNClob(parameterName, new MockNClob(data));
928        }
929        
930        public void setNClob(String parameterName, Reader reader, long length) throws SQLException
931        {
932            String data = StreamUtil.getReaderAsString(reader, (int)length);
933            setNClob(parameterName, new MockNClob(data));
934        }
935    
936        public void setSQLXML(String parameterName, SQLXML xmlObject) throws SQLException
937        {
938            setObject(parameterName, xmlObject);
939        }
940    
941        public void setString(String parameterName, String string) throws SQLException
942        {
943            setObject(parameterName, string);
944        }
945    
946        public void setNString(String parameterName, String string) throws SQLException
947        {
948            setObject(parameterName, string);
949        }
950    
951        public void setBigDecimal(String parameterName, BigDecimal bigDecimal) throws SQLException
952        {
953            setObject(parameterName, bigDecimal);
954        }
955    
956        public void setURL(String parameterName, URL url) throws SQLException
957        {
958            setObject(parameterName, url);
959        }
960    
961        public void setDate(String parameterName, Date date) throws SQLException
962        {
963            setObject(parameterName, date);
964        }
965    
966        public void setTime(String parameterName, Time time) throws SQLException
967        {
968            setObject(parameterName, time);
969        }
970        
971        public void setTimestamp(String parameterName, Timestamp timestamp) throws SQLException
972        {
973            setObject(parameterName, timestamp);
974        }
975    
976        public void setDate(String parameterName, Date date, Calendar calendar) throws SQLException
977        {
978            setDate(parameterName, date);
979        }
980    
981        public void setTime(String parameterName, Time time, Calendar calendar) throws SQLException
982        {
983            setTime(parameterName, time);
984        }
985    
986        public void setTimestamp(String parameterName, Timestamp timestamp, Calendar calendar) throws SQLException
987        {
988            setTimestamp(parameterName, timestamp);
989        }
990        
991        public void setRowId(String parameterName, RowId rowId) throws SQLException
992        {
993            setObject(parameterName, rowId);
994        }
995    
996        public void setObject(String parameterName, Object object) throws SQLException
997        {
998            paramObjects.put(parameterName, object);
999        }
1000    
1001        public void setObject(String parameterName, Object object, int targetSqlType) throws SQLException
1002        {
1003            setObject(parameterName, object);
1004        }
1005        
1006        public void setObject(String parameterName, Object object, int targetSqlType, int scale) throws SQLException
1007        {
1008            setObject(parameterName, object);
1009        }
1010        
1011        private Map getOutParameterMap()
1012        {
1013            Map outParameter = resultSetHandler.getOutParameter(getSQL(), getParameterMap());
1014            if(null == outParameter)
1015            {
1016                outParameter = resultSetHandler.getOutParameter(getSQL());
1017            }
1018            if(null == outParameter)
1019            {
1020                outParameter = resultSetHandler.getGlobalOutParameter();
1021            }
1022            if(resultSetHandler.getMustRegisterOutParameters())
1023            {
1024                return filterNotRegisteredParameters(outParameter);
1025            }
1026            return outParameter;
1027        }
1028        
1029        private Map filterNotRegisteredParameters(Map outParameter)
1030        {
1031            Map filteredMap = new HashMap();
1032            Iterator keys = outParameter.keySet().iterator();
1033            while(keys.hasNext())
1034            {
1035                Object nextKey = keys.next();
1036                if(registeredOutParameterSetIndexed.contains(nextKey) || registeredOutParameterSetNamed.contains(nextKey))
1037                {
1038                    filteredMap.put(nextKey, outParameter.get(nextKey));
1039                }
1040            }
1041            return Collections.unmodifiableMap(filteredMap);
1042        }
1043        
1044        private NClob getNClobFromClob(Clob clobValue) throws SQLException
1045        {
1046            return new MockNClob(clobValue.getSubString(1, (int)clobValue.length()));
1047        }
1048    }