KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > util > CompiereStatement


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Smart Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2003 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.util;
15
16 import java.util.*;
17 import java.sql.ResultSet JavaDoc;
18 import java.sql.SQLException JavaDoc;
19 import java.math.BigDecimal JavaDoc;
20 import java.sql.Date JavaDoc;
21 import java.sql.Time JavaDoc;
22 import java.sql.Timestamp JavaDoc;
23 import java.sql.Ref JavaDoc;
24 import java.sql.Blob JavaDoc;
25 import java.sql.Clob JavaDoc;
26 import java.sql.Array JavaDoc;
27 import java.sql.ResultSetMetaData JavaDoc;
28 //import java.sql.Types;
29
import java.util.Calendar JavaDoc;
30 import java.net.URL JavaDoc;
31 import java.sql.ParameterMetaData JavaDoc;
32 import java.sql.SQLWarning JavaDoc;
33 import java.sql.Connection JavaDoc;
34 import java.sql.PreparedStatement JavaDoc;
35 //import java.sql.CallableStatement;
36
import java.io.*;
37 import javax.sql.RowSet JavaDoc;
38
39
40 import org.compiere.interfaces.Server;
41 import org.compiere.db.CConnection;
42 import org.compiere.db.CompiereDatabase;
43 import java.rmi.*;
44
45 /**
46  * Compiere Statement
47  *
48  * @author Jorg Janke
49  * @version $Id: CompiereStatement.java,v 1.7 2003/10/04 03:55:46 jjanke Exp $
50  */

51 public class CompiereStatement implements PreparedStatement JavaDoc
52 {
53     /**
54      * Constructor
55      *
56      * @param sql0 unconverted sql statement
57      * @param resultSetType - ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.TYPE_SCROLL_SENSITIVE
58      * @param resultSetConcurrency - ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE
59      */

60     public CompiereStatement (String JavaDoc sql0, int resultSetType, int resultSetConcurrency)
61     {
62         // Convert SQL
63
String JavaDoc sql = DB.getDatabase().convertStatement(sql0);
64
65         // Create Remote access
66
boolean remote = Ini.isClient() && Ini.getPropertyBool(Ini.P_OBJECTS);
67         if (remote) // Check if Apps Server is running
68
remote = CConnection.get().isAppsServerOK(false);
69     // log.debug("remote=" + remote);
70
if (remote)
71             m_vo = new CompiereStatementVO (sql, resultSetType, resultSetConcurrency);
72         else // local
73
{
74             try
75             {
76                 Connection JavaDoc conn = null;
77                 if (resultSetConcurrency == ResultSet.CONCUR_UPDATABLE)
78                     conn = DB.getConnectionRW();
79                 else
80                     conn = DB.getConnectionRO();
81                 m_pstmt = conn.prepareStatement (sql, resultSetType, resultSetConcurrency);
82                 return;
83             }
84             catch (SQLException JavaDoc e)
85             {
86                 log.error(sql, e);
87             }
88         }
89     } // CompiereStatement
90

91     /**
92      * Constructor
93      * @param vo value object
94      */

95     public CompiereStatement (CompiereStatementVO vo)
96     {
97     // log.debug("local");
98
m_vo = vo;
99     } // CompiereStatement
100

101     /** Logger */
102     private transient Logger log = Logger.getCLogger (getClass());
103     /** Used if local */
104     private transient PreparedStatement JavaDoc m_pstmt = null;
105     /** Value Object */
106     private CompiereStatementVO m_vo = null;
107
108     /**
109      * Execute Query
110      * @return ResultSet or RowSet
111      * @throws SQLException
112      */

113     public ResultSet JavaDoc executeQuery () throws SQLException JavaDoc
114     {
115         if (m_pstmt != null) // local
116
return m_pstmt.executeQuery();
117         //
118
// Client -> remote sever
119
Server server = CConnection.get().getServer();
120         if (Log.isTraceLevel(10))
121             log.debug("executeQuery - server => " + m_vo);
122         try
123         {
124             if (server != null)
125             {
126                 ResultSet JavaDoc rs = server.stmt_getRowSet (m_vo);
127                 if (rs == null)
128                     log.warn("executeQuery - ResultSet is null - " + m_vo);
129                 m_vo.clearParameters(); // re-use of result set
130
return rs;
131             }
132             log.error("executeQuery - AppsServer not found");
133         }
134         catch (RemoteException ex)
135         {
136             log.error("executeQuery - AppsServer error", ex);
137         }
138         // Try locally
139
CConnection.get().isAppsServerOK(true);
140         PreparedStatement JavaDoc pstmt = local_getPreparedStatement();
141         m_vo.clearParameters(); // re-use of result set
142
return pstmt.executeQuery();
143     } // executeQuery
144

145     /**
146      * Execute Query
147      * @param sql0 unconverted SQL to execute
148      * @return ResultSet or RowSet
149      * @throws SQLException
150      */

151     public ResultSet JavaDoc executeQuery (String JavaDoc sql0) throws SQLException JavaDoc
152     {
153         // Convert SQL
154
String JavaDoc sql = DB.getDatabase().convertStatement(sql0);
155         if (m_pstmt != null) // local
156
return m_pstmt.executeQuery(sql);
157         //
158
m_vo.setSql(sql);
159         return executeQuery();
160     } // executeQuery
161

162     /*************************************************************************/
163
164     /**
165      * Execute Update
166      * @return no of updated rows
167      * @throws SQLException
168      */

169     public int executeUpdate () throws SQLException JavaDoc
170     {
171         if (m_pstmt != null)
172             return m_pstmt.executeUpdate();
173         //
174
// Client -> remote sever
175
Server server = CConnection.get().getServer();
176         if (Log.isTraceLevel(10))
177             log.debug("executeUpdate - server => " + m_vo);
178         try
179         {
180             if (server != null)
181             {
182                 int result = server.stmt_executeUpdate (m_vo);
183                 m_vo.clearParameters(); // re-use of result set
184
return result;
185             }
186             log.error("executeUpdate - AppsServer not found");
187         }
188         catch (RemoteException ex)
189         {
190             log.error("executeUpdate - AppsServer error", ex);
191         }
192         // Try locally
193
CConnection.get().isAppsServerOK(true);
194         PreparedStatement JavaDoc pstmt = local_getPreparedStatement();
195         m_vo.clearParameters(); // re-use of result set
196
return pstmt.executeUpdate();
197     } // executeUpdate
198

199     /**
200      * Execute Update
201      * @param sql0 unconverted sql
202      * @return no of updated rows
203      * @throws SQLException
204      */

205     public int executeUpdate (String JavaDoc sql0) throws SQLException JavaDoc
206     {
207         // Convert SQL
208
String JavaDoc sql = DB.getDatabase().convertStatement(sql0);
209         if (m_pstmt != null)
210             return m_pstmt.executeUpdate(sql);
211         //
212
m_vo.setSql(sql);
213         return executeUpdate();
214     } // executeUpdate
215

216     public int executeUpdate (String JavaDoc sql, int autoGeneratedKeys) throws SQLException JavaDoc
217     {
218         if (m_pstmt != null)
219             return m_pstmt.executeUpdate(sql, autoGeneratedKeys);
220         throw new java.lang.UnsupportedOperationException JavaDoc ("Method executeUpdate() not yet implemented.");
221     }
222
223     public int executeUpdate (String JavaDoc sql, int[] columnIndexes) throws SQLException JavaDoc
224     {
225         if (m_pstmt != null)
226             return m_pstmt.executeUpdate(sql, columnIndexes);
227         throw new java.lang.UnsupportedOperationException JavaDoc ("Method executeUpdate() not yet implemented.");
228     }
229
230     public int executeUpdate (String JavaDoc sql, String JavaDoc[] columnNames) throws SQLException JavaDoc
231     {
232         if (m_pstmt != null)
233             return m_pstmt.executeUpdate(sql, columnNames);
234         throw new java.lang.UnsupportedOperationException JavaDoc ("Method executeUpdate() not yet implemented.");
235     }
236
237     public boolean execute () throws SQLException JavaDoc
238     {
239         if (m_pstmt != null)
240             return m_pstmt.execute();
241         throw new java.lang.UnsupportedOperationException JavaDoc ("Method execute() not yet implemented.");
242     }
243
244     public boolean execute (String JavaDoc sql, int autoGeneratedKeys) throws SQLException JavaDoc
245     {
246         if (m_pstmt != null)
247             return m_pstmt.execute(sql, autoGeneratedKeys);
248         throw new java.lang.UnsupportedOperationException JavaDoc ("Method execute() not yet implemented.");
249     }
250
251     public boolean execute (String JavaDoc sql, int[] columnIndexes) throws SQLException JavaDoc
252     {
253         if (m_pstmt != null)
254             return m_pstmt.execute(sql, columnIndexes);
255         throw new java.lang.UnsupportedOperationException JavaDoc ("Method execute() not yet implemented.");
256     }
257
258     public boolean execute (String JavaDoc sql, String JavaDoc[] columnNames) throws SQLException JavaDoc
259     {
260         if (m_pstmt != null)
261             return m_pstmt.execute(sql, columnNames);
262         throw new java.lang.UnsupportedOperationException JavaDoc ("Method execute() not yet implemented.");
263     }
264
265
266     public ResultSetMetaData JavaDoc getMetaData () throws SQLException JavaDoc
267     {
268         if (m_pstmt != null)
269             return m_pstmt.getMetaData ();
270         else
271             throw new java.lang.UnsupportedOperationException JavaDoc ("Method getMetaData() not yet implemented.");
272     }
273
274     public ParameterMetaData JavaDoc getParameterMetaData () throws SQLException JavaDoc
275     {
276         if (m_pstmt != null)
277             return m_pstmt.getParameterMetaData();
278         throw new java.lang.UnsupportedOperationException JavaDoc ("Method getParameterMetaData() not yet implemented.");
279     }
280
281     /**
282      * Close
283      * @throws SQLException
284      */

285     public void close () throws SQLException JavaDoc
286     {
287         if (m_pstmt != null)
288             m_pstmt.close();
289     } // close
290

291     /*************************************************************************/
292
293     /**
294      * Get Max Field Size
295      * @return field size
296      * @throws SQLException
297      */

298     public int getMaxFieldSize () throws SQLException JavaDoc
299     {
300         if (m_pstmt != null)
301             return m_pstmt.getMaxFieldSize();
302         throw new java.lang.UnsupportedOperationException JavaDoc ("Method getMaxFieldSize() not yet implemented.");
303     }
304
305     public void setMaxFieldSize (int max) throws SQLException JavaDoc
306     {
307         if (m_pstmt != null)
308             m_pstmt.setMaxFieldSize(max);
309         else
310             throw new java.lang.UnsupportedOperationException JavaDoc ("Method setMaxFieldSize() not yet implemented.");
311     }
312
313     public int getMaxRows () throws SQLException JavaDoc
314     {
315         if (m_pstmt != null)
316             return m_pstmt.getMaxRows();
317         throw new java.lang.UnsupportedOperationException JavaDoc ("Method getMaxRows() not yet implemented.");
318     }
319
320     public void setMaxRows (int max) throws SQLException JavaDoc
321     {
322         if (m_pstmt != null)
323             m_pstmt.setMaxRows(max);
324         else
325             throw new java.lang.UnsupportedOperationException JavaDoc ("Method setMaxRows() not yet implemented.");
326     }
327
328     /*************************************************************************/
329
330     /**
331      * Add Batch
332      * @param sql sql
333      * @throws SQLException
334      */

335     public void addBatch (String JavaDoc sql) throws SQLException JavaDoc
336     {
337         if (m_pstmt != null)
338             m_pstmt.addBatch(sql);
339         else
340             throw new java.lang.UnsupportedOperationException JavaDoc ("Method addBatch() not yet implemented.");
341     }
342
343     public void clearBatch () throws SQLException JavaDoc
344     {
345         if (m_pstmt != null)
346             m_pstmt.clearBatch();
347         else
348             throw new java.lang.UnsupportedOperationException JavaDoc ("Method clearBatch() not yet implemented.");
349     }
350
351     public int[] executeBatch () throws SQLException JavaDoc
352     {
353         if (m_pstmt != null)
354             return m_pstmt.executeBatch();
355         throw new java.lang.UnsupportedOperationException JavaDoc ("Method executeBatch() not yet implemented.");
356     }
357
358     public Connection JavaDoc getConnection () throws SQLException JavaDoc
359     {
360         if (m_pstmt != null)
361             return m_pstmt.getConnection();
362         throw new java.lang.UnsupportedOperationException JavaDoc ("Method getConnection() not yet implemented.");
363     }
364
365     public boolean getMoreResults (int current) throws SQLException JavaDoc
366     {
367         if (m_pstmt != null)
368             return m_pstmt.getMoreResults(current);
369         throw new java.lang.UnsupportedOperationException JavaDoc ("Method getMoreResults() not yet implemented.");
370     }
371
372     public ResultSet JavaDoc getGeneratedKeys () throws SQLException JavaDoc
373     {
374         if (m_pstmt != null)
375             return m_pstmt.getGeneratedKeys();
376         throw new java.lang.UnsupportedOperationException JavaDoc ("Method getGeneratedKeys() not yet implemented.");
377     }
378
379     public int getResultSetHoldability () throws SQLException JavaDoc
380     {
381         if (m_pstmt != null)
382             return m_pstmt.getResultSetHoldability();
383         throw new java.lang.UnsupportedOperationException JavaDoc ("Method getResultSetHoldability() not yet implemented.");
384     }
385
386     public void setEscapeProcessing (boolean enable) throws SQLException JavaDoc
387     {
388         if (m_pstmt != null)
389             m_pstmt.setEscapeProcessing(enable);
390         else
391             throw new java.lang.UnsupportedOperationException JavaDoc ("Method setEscapeProcessing() not yet implemented.");
392     }
393
394     public int getQueryTimeout () throws SQLException JavaDoc
395     {
396         if (m_pstmt != null)
397             return m_pstmt.getQueryTimeout();
398         throw new java.lang.UnsupportedOperationException JavaDoc ("Method getQueryTimeout() not yet implemented.");
399     }
400
401     public void setQueryTimeout (int seconds) throws SQLException JavaDoc
402     {
403         if (m_pstmt != null)
404             m_pstmt.setQueryTimeout (seconds);
405         else
406             throw new java.lang.UnsupportedOperationException JavaDoc ("Method setQueryTimeout() not yet implemented.");
407     }
408
409     public void cancel () throws SQLException JavaDoc
410     {
411         if (m_pstmt != null)
412             m_pstmt.cancel();
413         else
414             throw new java.lang.UnsupportedOperationException JavaDoc ("Method cancel() not yet implemented.");
415     }
416
417     public SQLWarning JavaDoc getWarnings () throws SQLException JavaDoc
418     {
419         if (m_pstmt != null)
420             return m_pstmt.getWarnings();
421         throw new java.lang.UnsupportedOperationException JavaDoc ("Method getWarnings() not yet implemented.");
422     }
423
424     public void clearWarnings () throws SQLException JavaDoc
425     {
426         if (m_pstmt != null)
427             m_pstmt.clearWarnings();
428         else
429             throw new java.lang.UnsupportedOperationException JavaDoc ("Method clearWarnings() not yet implemented.");
430     }
431
432     public void setCursorName (String JavaDoc name) throws SQLException JavaDoc
433     {
434         if (m_pstmt != null)
435             m_pstmt.setCursorName(name);
436         else
437             throw new java.lang.UnsupportedOperationException JavaDoc ("Method setCursorName() not yet implemented.");
438     }
439
440     public boolean execute (String JavaDoc sql) throws SQLException JavaDoc
441     {
442         if (m_pstmt != null)
443             return m_pstmt.execute(sql);
444         throw new java.lang.UnsupportedOperationException JavaDoc ("Method execute() not yet implemented.");
445     }
446
447     public ResultSet JavaDoc getResultSet () throws SQLException JavaDoc
448     {
449         if (m_pstmt != null)
450             return m_pstmt.getResultSet();
451         throw new java.lang.UnsupportedOperationException JavaDoc ("Method getResultSet() not yet implemented.");
452     }
453
454     public int getUpdateCount () throws SQLException JavaDoc
455     {
456         if (m_pstmt != null)
457             return m_pstmt.getUpdateCount();
458         throw new java.lang.UnsupportedOperationException JavaDoc ("Method getUpdateCount() not yet implemented.");
459     }
460
461     public boolean getMoreResults () throws SQLException JavaDoc
462     {
463         if (m_pstmt != null)
464             return m_pstmt.getMoreResults();
465         throw new java.lang.UnsupportedOperationException JavaDoc ("Method getMoreResults() not yet implemented.");
466     }
467
468     public void setFetchDirection (int direction) throws SQLException JavaDoc
469     {
470         if (m_pstmt != null)
471             m_pstmt.setFetchDirection(direction);
472         else
473             throw new java.lang.UnsupportedOperationException JavaDoc ("Method setFetchDirection() not yet implemented.");
474     }
475
476     public int getFetchDirection () throws SQLException JavaDoc
477     {
478         if (m_pstmt != null)
479             return m_pstmt.getFetchDirection();
480         throw new java.lang.UnsupportedOperationException JavaDoc ("Method getFetchDirection() not yet implemented.");
481     }
482
483     public void setFetchSize (int rows) throws SQLException JavaDoc
484     {
485         if (m_pstmt != null)
486             m_pstmt.setFetchSize(rows);
487         else
488             throw new java.lang.UnsupportedOperationException JavaDoc ("Method setFetchSize() not yet implemented.");
489     }
490
491     public int getFetchSize () throws SQLException JavaDoc
492     {
493         if (m_pstmt != null)
494             return m_pstmt.getFetchSize();
495         throw new java.lang.UnsupportedOperationException JavaDoc ("Method getFetchSize() not yet implemented.");
496     }
497
498     public int getResultSetConcurrency () throws SQLException JavaDoc
499     {
500         if (m_pstmt != null)
501             return m_pstmt.getResultSetConcurrency();
502         throw new java.lang.UnsupportedOperationException JavaDoc ("Method getResultSetConcurrency() not yet implemented.");
503     }
504
505     public int getResultSetType () throws SQLException JavaDoc
506     {
507         if (m_pstmt != null)
508             return m_pstmt.getResultSetType();
509         throw new java.lang.UnsupportedOperationException JavaDoc ("Method getResultSetType() not yet implemented.");
510     }
511
512     public void addBatch () throws SQLException JavaDoc
513     {
514         if (m_pstmt != null)
515             m_pstmt.addBatch ();
516         else
517             throw new java.lang.UnsupportedOperationException JavaDoc ("Method addBatch() not yet implemented.");
518     }
519
520     /*************************************************************************/
521
522     /**
523      * Set Null
524      * @param parameterIndex index
525      * @param sqlType type
526      * @throws SQLException
527      */

528     public void setNull (int parameterIndex, int sqlType) throws SQLException JavaDoc
529     {
530         if (m_pstmt != null)
531             m_pstmt.setNull (parameterIndex, sqlType);
532         else
533             m_vo.setParameter(parameterIndex, new NullParameter(sqlType));
534     } // setNull
535

536     public void setNull (int parameterIndex, int sqlType, String JavaDoc typeName) throws SQLException JavaDoc
537     {
538         if (m_pstmt != null)
539             m_pstmt.setNull (parameterIndex, sqlType);
540         else
541             m_vo.setParameter(parameterIndex, new NullParameter(sqlType));
542     }
543
544     public void setBoolean (int parameterIndex, boolean x) throws SQLException JavaDoc
545     {
546         if (m_pstmt != null)
547             m_pstmt.setBoolean (parameterIndex, x);
548         else
549             m_vo.setParameter(parameterIndex, new Boolean JavaDoc(x));
550     }
551
552     public void setByte (int parameterIndex, byte x) throws SQLException JavaDoc
553     {
554         if (m_pstmt != null)
555             m_pstmt.setByte (parameterIndex, x);
556         else
557             m_vo.setParameter(parameterIndex, new Byte JavaDoc(x));
558     }
559
560     public void setShort (int parameterIndex, short x) throws SQLException JavaDoc
561     {
562         if (m_pstmt != null)
563             m_pstmt.setShort (parameterIndex, x);
564         else
565             m_vo.setParameter(parameterIndex, new Short JavaDoc(x));
566     }
567
568     public void setInt (int parameterIndex, int x) throws SQLException JavaDoc
569     {
570         if (m_pstmt != null)
571             m_pstmt.setInt (parameterIndex, x);
572         else
573             m_vo.setParameter(parameterIndex, new Integer JavaDoc(x));
574     }
575
576     public void setLong (int parameterIndex, long x) throws SQLException JavaDoc
577     {
578         if (m_pstmt != null)
579             m_pstmt.setLong (parameterIndex, x);
580         else
581             m_vo.setParameter(parameterIndex, new Long JavaDoc(x));
582     }
583
584     public void setFloat (int parameterIndex, float x) throws SQLException JavaDoc
585     {
586         if (m_pstmt != null)
587             m_pstmt.setFloat (parameterIndex, x);
588         else
589             m_vo.setParameter(parameterIndex, new Float JavaDoc(x));
590     }
591
592     public void setDouble (int parameterIndex, double x) throws SQLException JavaDoc
593     {
594         if (m_pstmt != null)
595             m_pstmt.setDouble (parameterIndex, x);
596         else
597             m_vo.setParameter(parameterIndex, new Double JavaDoc(x));
598     }
599
600     public void setBigDecimal (int parameterIndex, BigDecimal JavaDoc x) throws SQLException JavaDoc
601     {
602         if (m_pstmt != null)
603             m_pstmt.setBigDecimal (parameterIndex, x);
604         else
605             m_vo.setParameter(parameterIndex, x);
606     }
607
608     public void setString (int parameterIndex, String JavaDoc x) throws SQLException JavaDoc
609     {
610         if (m_pstmt != null)
611             m_pstmt.setString (parameterIndex, x);
612         else
613             m_vo.setParameter(parameterIndex, x);
614     }
615
616     public void setBytes (int parameterIndex, byte[] x) throws SQLException JavaDoc
617     {
618         if (m_pstmt != null)
619             m_pstmt.setBytes (parameterIndex, x);
620         else
621             throw new java.lang.UnsupportedOperationException JavaDoc ("Method setBytes() not yet implemented.");
622     }
623
624     public void setDate (int parameterIndex, Date JavaDoc x) throws SQLException JavaDoc
625     {
626         if (m_pstmt != null)
627             m_pstmt.setDate (parameterIndex, x);
628         else
629             m_vo.setParameter(parameterIndex, x);
630     }
631
632     public void setTime (int parameterIndex, Time JavaDoc x) throws SQLException JavaDoc
633     {
634         if (m_pstmt != null)
635             m_pstmt.setTime (parameterIndex, x);
636         else
637             m_vo.setParameter(parameterIndex, x);
638     }
639
640     public void setTimestamp (int parameterIndex, Timestamp JavaDoc x) throws SQLException JavaDoc
641     {
642         if (m_pstmt != null)
643             m_pstmt.setTimestamp (parameterIndex, x);
644         else
645             m_vo.setParameter(parameterIndex, x);
646     }
647
648     public void setAsciiStream (int parameterIndex, InputStream x, int length) throws SQLException JavaDoc
649     {
650         if (m_pstmt != null)
651             m_pstmt.setAsciiStream (parameterIndex, x, length);
652         else
653             throw new java.lang.UnsupportedOperationException JavaDoc ("Method setAsciiStream() not yet implemented.");
654     }
655
656     /**
657      * @param parameterIndex the first parameter is 1, the second is 2, ...
658      * @param x a <code>java.io.InputStream</code> object that contains the
659      * Unicode parameter value as two-byte Unicode characters
660      * @param length the number of bytes in the stream
661      * @exception SQLException if a database access error occurs
662      * @deprecated
663      */

664     public void setUnicodeStream (int parameterIndex, InputStream x, int length) throws SQLException JavaDoc
665     {
666         throw new UnsupportedOperationException JavaDoc ("Method setUnicodeStream() not yet implemented.");
667     }
668
669     public void setBinaryStream (int parameterIndex, InputStream x, int length) throws SQLException JavaDoc
670     {
671         if (m_pstmt != null)
672             m_pstmt.setBinaryStream (parameterIndex, x, length);
673         else
674             throw new java.lang.UnsupportedOperationException JavaDoc ("Method setBinaryStream() not yet implemented.");
675     }
676
677     public void clearParameters () throws SQLException JavaDoc
678     {
679         if (m_pstmt != null)
680             m_pstmt.clearParameters ();
681         else
682             m_vo.clearParameters();
683     }
684
685     public void setObject (int parameterIndex, Object JavaDoc x, int targetSqlType, int scale) throws SQLException JavaDoc
686     {
687         if (m_pstmt != null)
688             m_pstmt.setObject (parameterIndex, x, targetSqlType, scale);
689         else
690             throw new java.lang.UnsupportedOperationException JavaDoc ("Method setObject() not yet implemented.");
691     }
692
693     public void setObject (int parameterIndex, Object JavaDoc x, int targetSqlType) throws SQLException JavaDoc
694     {
695         if (m_pstmt != null)
696             m_pstmt.setObject (parameterIndex, x);
697         else
698             throw new java.lang.UnsupportedOperationException JavaDoc ("Method setObject() not yet implemented.");
699     }
700
701     public void setObject (int parameterIndex, Object JavaDoc x) throws SQLException JavaDoc
702     {
703         if (m_pstmt != null)
704             m_pstmt.setObject (parameterIndex, x);
705         else
706             m_vo.setParameter(parameterIndex, x);
707     }
708
709     public void setCharacterStream (int parameterIndex, Reader reader, int length) throws SQLException JavaDoc
710     {
711         if (m_pstmt != null)
712             m_pstmt.setCharacterStream (parameterIndex, reader, length);
713         else
714             throw new java.lang.UnsupportedOperationException JavaDoc ("Method setCharacterStream() not yet implemented.");
715     }
716
717     public void setRef (int parameterIndex, Ref JavaDoc x) throws SQLException JavaDoc
718     {
719         if (m_pstmt != null)
720             m_pstmt.setRef (parameterIndex, x);
721         else
722             m_vo.setParameter(parameterIndex, x);
723     }
724
725     public void setBlob (int parameterIndex, Blob JavaDoc x) throws SQLException JavaDoc
726     {
727         if (m_pstmt != null)
728             m_pstmt.setObject (parameterIndex, x);
729         else
730             m_vo.setParameter(parameterIndex, x);
731     }
732
733     public void setClob (int parameterIndex, Clob JavaDoc x) throws SQLException JavaDoc
734     {
735         if (m_pstmt != null)
736             m_pstmt.setObject (parameterIndex, x);
737         else
738             m_vo.setParameter(parameterIndex, x);
739     }
740
741     public void setArray (int parameterIndex, Array JavaDoc x) throws SQLException JavaDoc
742     {
743         if (m_pstmt != null)
744             m_pstmt.setObject (parameterIndex, x);
745         else
746             m_vo.setParameter(parameterIndex, x);
747     }
748
749     public void setDate (int parameterIndex, Date JavaDoc x, Calendar JavaDoc cal) throws SQLException JavaDoc
750     {
751         if (m_pstmt != null)
752             m_pstmt.setDate (parameterIndex, x, cal);
753         else
754             throw new java.lang.UnsupportedOperationException JavaDoc ("Method setDate() not yet implemented.");
755     }
756
757     public void setTime (int parameterIndex, Time JavaDoc x, Calendar JavaDoc cal) throws SQLException JavaDoc
758     {
759         if (m_pstmt != null)
760             m_pstmt.setTime (parameterIndex, x, cal);
761         else
762             throw new java.lang.UnsupportedOperationException JavaDoc ("Method setTime() not yet implemented.");
763     }
764
765     public void setTimestamp (int parameterIndex, Timestamp JavaDoc x, Calendar JavaDoc cal) throws SQLException JavaDoc
766     {
767         if (m_pstmt != null)
768             m_pstmt.setTimestamp (parameterIndex, x, cal);
769         else
770             throw new java.lang.UnsupportedOperationException JavaDoc ("Method setTimestamp() not yet implemented.");
771     }
772
773     public void setURL (int parameterIndex, URL JavaDoc x) throws SQLException JavaDoc
774     {
775         if (m_pstmt != null)
776             m_pstmt.setObject (parameterIndex, x);
777         else
778             m_vo.setParameter(parameterIndex, x);
779     }
780
781     /**
782     private void writeObject (ObjectOutputStream oos) throws IOException
783     {
784         oos.defaultWriteObject ();
785     }
786
787     private void readObject (ObjectInputStream ois) throws ClassNotFoundException, IOException
788     {
789         ois.defaultReadObject ();
790     }
791     **/

792
793     /**
794      * String representation
795      * @return info
796      */

797     public String JavaDoc toString()
798     {
799         if (m_pstmt != null)
800             return "CompiereStatement[Local=" + m_pstmt + "]";
801         return "CompiereStatement[" + m_vo + "]";
802     } // toString
803

804     /*************************************************************************/
805
806     /**
807      * Get Prepared Statement to create RowSet.
808      * Method called on Ramote to execute locally
809      * @return Prepared Statement
810      */

811     private PreparedStatement JavaDoc local_getPreparedStatement()
812     {
813     // log.debug("local_getPreparedStatement");
814
Connection JavaDoc conn = local_getConnection();
815         PreparedStatement JavaDoc pstmt = null;
816         try
817         {
818             pstmt = conn.prepareStatement(m_vo.getSql(), m_vo.getResultSetType(), m_vo.getResultSetConcurrency());
819             // Set Parameters
820
ArrayList parameters = m_vo.getParameters();
821             for (int i = 0; i < parameters.size(); i++)
822             {
823                 Object JavaDoc o = parameters.get(i);
824                 if (o == null)
825                     throw new java.lang.NullPointerException JavaDoc ("local_getPreparedStatement Null Parameter #" + i);
826                 else if (o instanceof NullParameter)
827                 {
828                     int type = ((NullParameter)o).getType();
829                     pstmt.setNull(i+1, type);
830                 }
831                 else if (o instanceof Integer JavaDoc)
832                     pstmt.setInt(i+1, ((Integer JavaDoc)o).intValue());
833                 else if (o instanceof String JavaDoc)
834                     pstmt.setString(i+1, (String JavaDoc)o);
835                 else if (o instanceof Timestamp JavaDoc)
836                     pstmt.setTimestamp(i+1, (Timestamp JavaDoc)o);
837                 else if (o instanceof BigDecimal JavaDoc)
838                     pstmt.setBigDecimal(i+1, (BigDecimal JavaDoc)o);
839                 else
840                     throw new java.lang.UnsupportedOperationException JavaDoc ("local_getPreparedStatement unknown Parameter Class=" + o.getClass());
841             }
842         }
843         catch (SQLException JavaDoc ex)
844         {
845             log.error("local_getPreparedStatement", ex);
846             try
847             {
848                 if (pstmt != null)
849                     pstmt.close();
850                 pstmt = null;
851             }
852             catch (SQLException JavaDoc ex1)
853             {
854             }
855         }
856         return pstmt;
857     } // local_getPreparedStatement
858

859     /**
860      * Get Local Connection
861      * @return connection
862      */

863     private Connection JavaDoc local_getConnection()
864     {
865         Connection JavaDoc conn = null;
866         if (m_vo.getResultSetConcurrency () == ResultSet.CONCUR_UPDATABLE)
867             conn = DB.getConnectionRW ();
868         else
869             conn = DB.getConnectionRO ();
870         return conn;
871     } // local_getConnection
872

873     /*************************************************************************/
874
875     /**
876      * Get Result as RowSet.
877      * @return result as RowSet
878      */

879     public RowSet JavaDoc remote_getRowSet()
880     {
881     // log.debug("remote_getRowSet");
882
try
883         {
884             CompiereDatabase db = CConnection.get().getDatabase();
885             if (db == null)
886                 throw new NullPointerException JavaDoc("CompiereStatement.remote_getRowSet - no Database");
887             //
888
PreparedStatement JavaDoc pstmt = local_getPreparedStatement();
889             ResultSet JavaDoc rs = pstmt.executeQuery();
890             RowSet JavaDoc rowSet = db.getRowSet (rs);
891             rs.close();
892             pstmt.close();
893             //
894
if (rowSet == null)
895                 throw new NullPointerException JavaDoc("CompiereStatement.remore_getRowSet - no RowSet");
896             return rowSet;
897         }
898         catch (Exception JavaDoc ex)
899         {
900             log.error("remote_getRowSet - " + m_vo, ex);
901             throw new RuntimeException JavaDoc (ex);
902         }
903     } // remote_getRowSet
904

905     /**
906      * Get Result as RowSet.
907      * @return result as RowSet
908      */

909     public int remote_executeUpdate()
910     {
911         log.debug("remote_executeUpdate");
912         try
913         {
914             CompiereDatabase db = CConnection.get().getDatabase();
915             if (db == null)
916                 throw new NullPointerException JavaDoc("CompiereStatement.remote_executeUpdate - no Database");
917             //
918
PreparedStatement JavaDoc pstmt = local_getPreparedStatement();
919             int result = pstmt.executeUpdate();
920             pstmt.close();
921             //
922
return result;
923         }
924         catch (Exception JavaDoc ex)
925         {
926             log.error("remote_executeUpdate - " + m_vo, ex);
927             throw new RuntimeException JavaDoc (ex);
928         }
929     } // remote_executeUpdate
930

931 } // CompiereStatement
932

933
Popular Tags