1 /*
2  *  Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License, version 2.0,
6  *  as published by the Free Software Foundation.
7  *
8  *  This program is also distributed with certain software (including
9  *  but not limited to OpenSSL) that is licensed under separate terms,
10  *  as designated in a particular file or component or in included license
11  *  documentation.  The authors of MySQL hereby grant you an additional
12  *  permission to link the program and your derivative works with the
13  *  separately licensed software that they have included with MySQL.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License, version 2.0, for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23  */
24 
25 package com.mysql.clusterj.jdbc;
26 
27 import java.sql.SQLException;
28 
29 import com.mysql.clusterj.core.util.I18NHelper;
30 import com.mysql.clusterj.core.util.Logger;
31 import com.mysql.clusterj.core.util.LoggerFactoryService;
32 import com.mysql.jdbc.ResultSetInternalMethods;
33 
34 /** This class is part of the statement interceptor contract with the MySQL JDBC connection.
35  * When a statement is intercepted and executed, an instance of this class is returned if there
36  * is only an insert/delete/update count. A sibling class, ResultSetInternalMethodsImpl, is
37  * returned if there is a real result to be iterated.
38  * This class in turn delegates to the clusterj ResultData to retrieve data from the cluster.
39  */
40 public class ResultSetInternalMethodsUpdateCount extends
41         AbstractResultSetInternalMethods {
42 
43     /** My message translator */
44     static final I18NHelper local = I18NHelper.getInstance(ResultSetInternalMethodsUpdateCount.class);
45 
46     /** My logger */
47     static final Logger logger = LoggerFactoryService.getFactory().getInstance(ResultSetInternalMethodsUpdateCount.class);
48 
49     /** Counts for multi-statement result */
50     private long[] counts;
51 
52     /** The current result set for multi-statement results */
53     private int current = 0;
54 
55     /** Construct an instance with the count to be returned.
56      *
57      * @param count the number of "rows affected"
58      */
ResultSetInternalMethodsUpdateCount(long count)59     public ResultSetInternalMethodsUpdateCount(long count) {
60         this.counts = new long[1];
61         this.counts[0] = count;
62     }
63 
64     /** Construct an instance with the count to be returned.
65      *
66      * @param counts the number of "rows affected" for each row of a multi-statement result
67      */
ResultSetInternalMethodsUpdateCount(long[] counts)68     public ResultSetInternalMethodsUpdateCount(long[] counts) {
69         this.counts = counts;
70     }
71 
72     /**
73      * Returns the next ResultSet in a multi-resultset "chain", if any,
74      * null if none exists.
75      * @return the next ResultSet
76      */
77     @Override
getNextResultSet()78     public ResultSetInternalMethods getNextResultSet() {
79         if (++current >= counts.length) {
80             return null;
81         } else {
82             return this;
83         }
84     }
85 
86     /**
87      * Clears the reference to the next result set in a multi-result set
88      * "chain".
89      */
90     @Override
clearNextResult()91     public void clearNextResult() {
92         // nothing to do
93     }
94 
95     @Override
getUpdateCount()96     public long getUpdateCount() {
97         return counts[current];
98     }
99 
100     @Override
getUpdateID()101     public long getUpdateID() {
102         return 0;
103     }
104 
105     @Override
reallyResult()106     public boolean reallyResult() {
107         return false;
108     }
109 
110     @Override
realClose(boolean arg0)111     public void realClose(boolean arg0) throws SQLException {
112     }
113 
114 }
115